blob: da5e9105b9f4c1e9c0a5b5c9c39cbc9a0b6efad2 [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>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000029#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/ioport.h>
31#include <linux/pci.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000036#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070038#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070039#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_vlan.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070044#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020045#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080046#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030049#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <asm/system.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000052#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000054#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David S. Miller49b6e95f2007-03-29 01:38:42 -070056#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070058#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
Matt Carlson63532392008-11-03 16:49:57 -080061#define BAR_0 0
62#define BAR_2 2
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include "tg3.h"
65
Joe Perches63c3a662011-04-26 08:12:10 +000066/* Functions & macros to verify TG3_FLAGS types */
67
68static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
69{
70 return test_bit(flag, bits);
71}
72
73static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
74{
75 set_bit(flag, bits);
76}
77
78static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
79{
80 clear_bit(flag, bits);
81}
82
83#define tg3_flag(tp, flag) \
84 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
85#define tg3_flag_set(tp, flag) \
86 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
87#define tg3_flag_clear(tp, flag) \
88 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000091#define TG3_MAJ_NUM 3
Matt Carlson43a5f002011-05-19 12:12:56 +000092#define TG3_MIN_NUM 119
Matt Carlson6867c842010-07-11 09:31:44 +000093#define DRV_MODULE_VERSION \
94 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlson43a5f002011-05-19 12:12:56 +000095#define DRV_MODULE_RELDATE "May 18, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#define TG3_DEF_MAC_MODE 0
98#define TG3_DEF_RX_MODE 0
99#define TG3_DEF_TX_MODE 0
100#define TG3_DEF_MSG_ENABLE \
101 (NETIF_MSG_DRV | \
102 NETIF_MSG_PROBE | \
103 NETIF_MSG_LINK | \
104 NETIF_MSG_TIMER | \
105 NETIF_MSG_IFDOWN | \
106 NETIF_MSG_IFUP | \
107 NETIF_MSG_RX_ERR | \
108 NETIF_MSG_TX_ERR)
109
110/* length of time before we decide the hardware is borked,
111 * and dev->tx_timeout() should be called to fix the problem
112 */
Joe Perches63c3a662011-04-26 08:12:10 +0000113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#define TG3_TX_TIMEOUT (5 * HZ)
115
116/* hardware minimum and maximum for a single frame's data payload */
117#define TG3_MIN_MTU 60
118#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000119 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121/* These numbers seem to be hard coded in the NIC firmware somehow.
122 * You can't change the ring sizes, but you can change where you place
123 * them in the NIC onboard memory.
124 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000125#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000126 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000127 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000129#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000130 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000131 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000133#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/* Do not place this n-ring entries value into the tp struct itself,
136 * we really want to expose these constants to GCC so that modulo et
137 * al. operations are done with shifts and masks instead of with
138 * hw multiply/modulo instructions. Another solution would be to
139 * replace things like '% foo' with '& (foo - 1)'.
140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142#define TG3_TX_RING_SIZE 512
143#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
144
Matt Carlson2c49a442010-09-30 10:34:35 +0000145#define TG3_RX_STD_RING_BYTES(tp) \
146 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
147#define TG3_RX_JMB_RING_BYTES(tp) \
148 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
149#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000150 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
152 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
154
Matt Carlson287be122009-08-28 13:58:46 +0000155#define TG3_DMA_BYTE_ENAB 64
156
157#define TG3_RX_STD_DMA_SZ 1536
158#define TG3_RX_JMB_DMA_SZ 9046
159
160#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
161
162#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
163#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Matt Carlson2c49a442010-09-30 10:34:35 +0000165#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
166 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000167
Matt Carlson2c49a442010-09-30 10:34:35 +0000168#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
169 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000170
Matt Carlsond2757fc2010-04-12 06:58:27 +0000171/* Due to a hardware bug, the 5701 can only DMA to memory addresses
172 * that are at least dword aligned when used in PCIX mode. The driver
173 * works around this bug by double copying the packet. This workaround
174 * is built into the normal double copy length check for efficiency.
175 *
176 * However, the double copy is only necessary on those architectures
177 * where unaligned memory accesses are inefficient. For those architectures
178 * where unaligned memory accesses incur little penalty, we can reintegrate
179 * the 5701 in the normal rx path. Doing so saves a device structure
180 * dereference by hardcoding the double copy threshold in place.
181 */
182#define TG3_RX_COPY_THRESHOLD 256
183#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
184 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
185#else
186 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
187#endif
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000190#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Matt Carlsonad829262008-11-21 17:16:16 -0800192#define TG3_RAW_IP_ALIGN 2
193
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000194#define TG3_FW_UPDATE_TIMEOUT_SEC 5
195
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800196#define FIRMWARE_TG3 "tigon/tg3.bin"
197#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
198#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000201 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
204MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
205MODULE_LICENSE("GPL");
206MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800207MODULE_FIRMWARE(FIRMWARE_TG3);
208MODULE_FIRMWARE(FIRMWARE_TG3TSO);
209MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
212module_param(tg3_debug, int, 0);
213MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
214
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000215static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700289 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
290 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
291 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
292 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
293 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
294 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
295 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000296 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700297 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298};
299
300MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
301
Andreas Mohr50da8592006-08-14 23:54:30 -0700302static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000304} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 { "rx_octets" },
306 { "rx_fragments" },
307 { "rx_ucast_packets" },
308 { "rx_mcast_packets" },
309 { "rx_bcast_packets" },
310 { "rx_fcs_errors" },
311 { "rx_align_errors" },
312 { "rx_xon_pause_rcvd" },
313 { "rx_xoff_pause_rcvd" },
314 { "rx_mac_ctrl_rcvd" },
315 { "rx_xoff_entered" },
316 { "rx_frame_too_long_errors" },
317 { "rx_jabbers" },
318 { "rx_undersize_packets" },
319 { "rx_in_length_errors" },
320 { "rx_out_length_errors" },
321 { "rx_64_or_less_octet_packets" },
322 { "rx_65_to_127_octet_packets" },
323 { "rx_128_to_255_octet_packets" },
324 { "rx_256_to_511_octet_packets" },
325 { "rx_512_to_1023_octet_packets" },
326 { "rx_1024_to_1522_octet_packets" },
327 { "rx_1523_to_2047_octet_packets" },
328 { "rx_2048_to_4095_octet_packets" },
329 { "rx_4096_to_8191_octet_packets" },
330 { "rx_8192_to_9022_octet_packets" },
331
332 { "tx_octets" },
333 { "tx_collisions" },
334
335 { "tx_xon_sent" },
336 { "tx_xoff_sent" },
337 { "tx_flow_control" },
338 { "tx_mac_errors" },
339 { "tx_single_collisions" },
340 { "tx_mult_collisions" },
341 { "tx_deferred" },
342 { "tx_excessive_collisions" },
343 { "tx_late_collisions" },
344 { "tx_collide_2times" },
345 { "tx_collide_3times" },
346 { "tx_collide_4times" },
347 { "tx_collide_5times" },
348 { "tx_collide_6times" },
349 { "tx_collide_7times" },
350 { "tx_collide_8times" },
351 { "tx_collide_9times" },
352 { "tx_collide_10times" },
353 { "tx_collide_11times" },
354 { "tx_collide_12times" },
355 { "tx_collide_13times" },
356 { "tx_collide_14times" },
357 { "tx_collide_15times" },
358 { "tx_ucast_packets" },
359 { "tx_mcast_packets" },
360 { "tx_bcast_packets" },
361 { "tx_carrier_sense_errors" },
362 { "tx_discards" },
363 { "tx_errors" },
364
365 { "dma_writeq_full" },
366 { "dma_write_prioq_full" },
367 { "rxbds_empty" },
368 { "rx_discards" },
369 { "rx_errors" },
370 { "rx_threshold_hit" },
371
372 { "dma_readq_full" },
373 { "dma_read_prioq_full" },
374 { "tx_comp_queue_full" },
375
376 { "ring_set_send_prod_index" },
377 { "ring_status_update" },
378 { "nic_irqs" },
379 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000380 { "nic_tx_threshold_hit" },
381
382 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383};
384
Matt Carlson48fa55a2011-04-13 11:05:06 +0000385#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
386
387
Andreas Mohr50da8592006-08-14 23:54:30 -0700388static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700389 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000390} ethtool_test_keys[] = {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700391 { "nvram test (online) " },
392 { "link test (online) " },
393 { "register test (offline)" },
394 { "memory test (offline)" },
395 { "loopback test (offline)" },
396 { "interrupt test (offline)" },
397};
398
Matt Carlson48fa55a2011-04-13 11:05:06 +0000399#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
400
401
Michael Chanb401e9e2005-12-19 16:27:04 -0800402static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
403{
404 writel(val, tp->regs + off);
405}
406
407static u32 tg3_read32(struct tg3 *tp, u32 off)
408{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000409 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800410}
411
Matt Carlson0d3031d2007-10-10 18:02:43 -0700412static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
413{
414 writel(val, tp->aperegs + off);
415}
416
417static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
418{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000419 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
423{
Michael Chan68929142005-08-09 20:17:14 -0700424 unsigned long flags;
425
426 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700427 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
428 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700429 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700430}
431
432static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
433{
434 writel(val, tp->regs + off);
435 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Michael Chan68929142005-08-09 20:17:14 -0700438static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
439{
440 unsigned long flags;
441 u32 val;
442
443 spin_lock_irqsave(&tp->indirect_lock, flags);
444 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
445 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
446 spin_unlock_irqrestore(&tp->indirect_lock, flags);
447 return val;
448}
449
450static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
451{
452 unsigned long flags;
453
454 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
455 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
456 TG3_64BIT_REG_LOW, val);
457 return;
458 }
Matt Carlson66711e62009-11-13 13:03:49 +0000459 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700460 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
461 TG3_64BIT_REG_LOW, val);
462 return;
463 }
464
465 spin_lock_irqsave(&tp->indirect_lock, flags);
466 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
467 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
468 spin_unlock_irqrestore(&tp->indirect_lock, flags);
469
470 /* In indirect mode when disabling interrupts, we also need
471 * to clear the interrupt bit in the GRC local ctrl register.
472 */
473 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
474 (val == 0x1)) {
475 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
476 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
477 }
478}
479
480static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
481{
482 unsigned long flags;
483 u32 val;
484
485 spin_lock_irqsave(&tp->indirect_lock, flags);
486 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
487 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
488 spin_unlock_irqrestore(&tp->indirect_lock, flags);
489 return val;
490}
491
Michael Chanb401e9e2005-12-19 16:27:04 -0800492/* usec_wait specifies the wait time in usec when writing to certain registers
493 * where it is unsafe to read back the register without some delay.
494 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
495 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
496 */
497static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Joe Perches63c3a662011-04-26 08:12:10 +0000499 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800500 /* Non-posted methods */
501 tp->write32(tp, off, val);
502 else {
503 /* Posted method */
504 tg3_write32(tp, off, val);
505 if (usec_wait)
506 udelay(usec_wait);
507 tp->read32(tp, off);
508 }
509 /* Wait again after the read for the posted method to guarantee that
510 * the wait time is met.
511 */
512 if (usec_wait)
513 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Michael Chan09ee9292005-08-09 20:17:00 -0700516static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
517{
518 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000519 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700520 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700521}
522
Michael Chan20094932005-08-09 20:16:32 -0700523static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 void __iomem *mbox = tp->regs + off;
526 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000527 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000529 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 readl(mbox);
531}
532
Michael Chanb5d37722006-09-27 16:06:21 -0700533static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
534{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000535 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700536}
537
538static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
539{
540 writel(val, tp->regs + off + GRCMBOX_BASE);
541}
542
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000543#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700544#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000545#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
546#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
547#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700548
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000549#define tw32(reg, val) tp->write32(tp, reg, val)
550#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
551#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
552#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
555{
Michael Chan68929142005-08-09 20:17:14 -0700556 unsigned long flags;
557
Matt Carlson6ff6f812011-05-19 12:12:54 +0000558 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700559 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
560 return;
561
Michael Chan68929142005-08-09 20:17:14 -0700562 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000563 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700564 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
565 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Michael Chanbbadf502006-04-06 21:46:34 -0700567 /* Always leave this as zero. */
568 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
569 } else {
570 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
571 tw32_f(TG3PCI_MEM_WIN_DATA, val);
572
573 /* Always leave this as zero. */
574 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
575 }
Michael Chan68929142005-08-09 20:17:14 -0700576 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
580{
Michael Chan68929142005-08-09 20:17:14 -0700581 unsigned long flags;
582
Matt Carlson6ff6f812011-05-19 12:12:54 +0000583 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700584 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
585 *val = 0;
586 return;
587 }
588
Michael Chan68929142005-08-09 20:17:14 -0700589 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000590 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700591 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
592 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Michael Chanbbadf502006-04-06 21:46:34 -0700594 /* Always leave this as zero. */
595 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
596 } else {
597 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
598 *val = tr32(TG3PCI_MEM_WIN_DATA);
599
600 /* Always leave this as zero. */
601 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
602 }
Michael Chan68929142005-08-09 20:17:14 -0700603 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
Matt Carlson0d3031d2007-10-10 18:02:43 -0700606static void tg3_ape_lock_init(struct tg3 *tp)
607{
608 int i;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000609 u32 regbase;
610
611 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
612 regbase = TG3_APE_LOCK_GRANT;
613 else
614 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700615
616 /* Make sure the driver hasn't any stale locks. */
617 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000618 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700619}
620
621static int tg3_ape_lock(struct tg3 *tp, int locknum)
622{
623 int i, off;
624 int ret = 0;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000625 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700626
Joe Perches63c3a662011-04-26 08:12:10 +0000627 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700628 return 0;
629
630 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000631 case TG3_APE_LOCK_GRC:
632 case TG3_APE_LOCK_MEM:
633 break;
634 default:
635 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700636 }
637
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000638 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
639 req = TG3_APE_LOCK_REQ;
640 gnt = TG3_APE_LOCK_GRANT;
641 } else {
642 req = TG3_APE_PER_LOCK_REQ;
643 gnt = TG3_APE_PER_LOCK_GRANT;
644 }
645
Matt Carlson0d3031d2007-10-10 18:02:43 -0700646 off = 4 * locknum;
647
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000648 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700649
650 /* Wait for up to 1 millisecond to acquire lock. */
651 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000652 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700653 if (status == APE_LOCK_GRANT_DRIVER)
654 break;
655 udelay(10);
656 }
657
658 if (status != APE_LOCK_GRANT_DRIVER) {
659 /* Revoke the lock request. */
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000660 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700661 APE_LOCK_GRANT_DRIVER);
662
663 ret = -EBUSY;
664 }
665
666 return ret;
667}
668
669static void tg3_ape_unlock(struct tg3 *tp, int locknum)
670{
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000671 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700672
Joe Perches63c3a662011-04-26 08:12:10 +0000673 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700674 return;
675
676 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000677 case TG3_APE_LOCK_GRC:
678 case TG3_APE_LOCK_MEM:
679 break;
680 default:
681 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700682 }
683
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000684 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
685 gnt = TG3_APE_LOCK_GRANT;
686 else
687 gnt = TG3_APE_PER_LOCK_GRANT;
688
689 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700690}
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692static void tg3_disable_ints(struct tg3 *tp)
693{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000694 int i;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 tw32(TG3PCI_MISC_HOST_CTRL,
697 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000698 for (i = 0; i < tp->irq_max; i++)
699 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702static void tg3_enable_ints(struct tg3 *tp)
703{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000704 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000705
Michael Chanbbe832c2005-06-24 20:20:04 -0700706 tp->irq_sync = 0;
707 wmb();
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 tw32(TG3PCI_MISC_HOST_CTRL,
710 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000711
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000712 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000713 for (i = 0; i < tp->irq_cnt; i++) {
714 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000715
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000716 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000717 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000718 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
719
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000720 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000721 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000722
723 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000724 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000725 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
726 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
727 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000728 tw32(HOSTCC_MODE, tp->coal_now);
729
730 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Matt Carlson17375d22009-08-28 14:02:18 +0000733static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700734{
Matt Carlson17375d22009-08-28 14:02:18 +0000735 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000736 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700737 unsigned int work_exists = 0;
738
739 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000740 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700741 if (sblk->status & SD_STATUS_LINK_CHG)
742 work_exists = 1;
743 }
744 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000745 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000746 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700747 work_exists = 1;
748
749 return work_exists;
750}
751
Matt Carlson17375d22009-08-28 14:02:18 +0000752/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700753 * similar to tg3_enable_ints, but it accurately determines whether there
754 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400755 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 */
Matt Carlson17375d22009-08-28 14:02:18 +0000757static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Matt Carlson17375d22009-08-28 14:02:18 +0000759 struct tg3 *tp = tnapi->tp;
760
Matt Carlson898a56f2009-08-28 14:02:40 +0000761 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 mmiowb();
763
David S. Millerfac9b832005-05-18 22:46:34 -0700764 /* When doing tagged status, this work check is unnecessary.
765 * The last_tag we write above tells the chip which piece of
766 * work we've completed.
767 */
Joe Perches63c3a662011-04-26 08:12:10 +0000768 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700769 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000770 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773static void tg3_switch_clocks(struct tg3 *tp)
774{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000775 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 u32 orig_clock_ctrl;
777
Joe Perches63c3a662011-04-26 08:12:10 +0000778 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700779 return;
780
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000781 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 orig_clock_ctrl = clock_ctrl;
784 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
785 CLOCK_CTRL_CLKRUN_OENABLE |
786 0x1f);
787 tp->pci_clock_ctrl = clock_ctrl;
788
Joe Perches63c3a662011-04-26 08:12:10 +0000789 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800791 tw32_wait_f(TG3PCI_CLOCK_CTRL,
792 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800795 tw32_wait_f(TG3PCI_CLOCK_CTRL,
796 clock_ctrl |
797 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
798 40);
799 tw32_wait_f(TG3PCI_CLOCK_CTRL,
800 clock_ctrl | (CLOCK_CTRL_ALTCLK),
801 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800803 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
806#define PHY_BUSY_LOOPS 5000
807
808static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
809{
810 u32 frame_val;
811 unsigned int loops;
812 int ret;
813
814 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
815 tw32_f(MAC_MI_MODE,
816 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
817 udelay(80);
818 }
819
820 *val = 0x0;
821
Matt Carlson882e9792009-09-01 13:21:36 +0000822 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 MI_COM_PHY_ADDR_MASK);
824 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
825 MI_COM_REG_ADDR_MASK);
826 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 tw32_f(MAC_MI_COM, frame_val);
829
830 loops = PHY_BUSY_LOOPS;
831 while (loops != 0) {
832 udelay(10);
833 frame_val = tr32(MAC_MI_COM);
834
835 if ((frame_val & MI_COM_BUSY) == 0) {
836 udelay(5);
837 frame_val = tr32(MAC_MI_COM);
838 break;
839 }
840 loops -= 1;
841 }
842
843 ret = -EBUSY;
844 if (loops != 0) {
845 *val = frame_val & MI_COM_DATA_MASK;
846 ret = 0;
847 }
848
849 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
850 tw32_f(MAC_MI_MODE, tp->mi_mode);
851 udelay(80);
852 }
853
854 return ret;
855}
856
857static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
858{
859 u32 frame_val;
860 unsigned int loops;
861 int ret;
862
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000863 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +0000864 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -0700865 return 0;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
868 tw32_f(MAC_MI_MODE,
869 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
870 udelay(80);
871 }
872
Matt Carlson882e9792009-09-01 13:21:36 +0000873 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 MI_COM_PHY_ADDR_MASK);
875 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
876 MI_COM_REG_ADDR_MASK);
877 frame_val |= (val & MI_COM_DATA_MASK);
878 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 tw32_f(MAC_MI_COM, frame_val);
881
882 loops = PHY_BUSY_LOOPS;
883 while (loops != 0) {
884 udelay(10);
885 frame_val = tr32(MAC_MI_COM);
886 if ((frame_val & MI_COM_BUSY) == 0) {
887 udelay(5);
888 frame_val = tr32(MAC_MI_COM);
889 break;
890 }
891 loops -= 1;
892 }
893
894 ret = -EBUSY;
895 if (loops != 0)
896 ret = 0;
897
898 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
899 tw32_f(MAC_MI_MODE, tp->mi_mode);
900 udelay(80);
901 }
902
903 return ret;
904}
905
Matt Carlsonb0988c12011-04-20 07:57:39 +0000906static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
907{
908 int err;
909
910 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
911 if (err)
912 goto done;
913
914 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
915 if (err)
916 goto done;
917
918 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
919 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
920 if (err)
921 goto done;
922
923 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
924
925done:
926 return err;
927}
928
929static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
930{
931 int err;
932
933 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
934 if (err)
935 goto done;
936
937 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
938 if (err)
939 goto done;
940
941 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
942 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
943 if (err)
944 goto done;
945
946 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
947
948done:
949 return err;
950}
951
952static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
953{
954 int err;
955
956 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
957 if (!err)
958 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
959
960 return err;
961}
962
963static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
964{
965 int err;
966
967 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
968 if (!err)
969 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
970
971 return err;
972}
973
Matt Carlson15ee95c2011-04-20 07:57:40 +0000974static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
975{
976 int err;
977
978 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
979 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
980 MII_TG3_AUXCTL_SHDWSEL_MISC);
981 if (!err)
982 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
983
984 return err;
985}
986
Matt Carlsonb4bd2922011-04-20 07:57:41 +0000987static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
988{
989 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
990 set |= MII_TG3_AUXCTL_MISC_WREN;
991
992 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
993}
994
Matt Carlson1d36ba42011-04-20 07:57:42 +0000995#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
996 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
997 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
998 MII_TG3_AUXCTL_ACTL_TX_6DB)
999
1000#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1001 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1002 MII_TG3_AUXCTL_ACTL_TX_6DB);
1003
Matt Carlson95e28692008-05-25 23:44:14 -07001004static int tg3_bmcr_reset(struct tg3 *tp)
1005{
1006 u32 phy_control;
1007 int limit, err;
1008
1009 /* OK, reset it, and poll the BMCR_RESET bit until it
1010 * clears or we time out.
1011 */
1012 phy_control = BMCR_RESET;
1013 err = tg3_writephy(tp, MII_BMCR, phy_control);
1014 if (err != 0)
1015 return -EBUSY;
1016
1017 limit = 5000;
1018 while (limit--) {
1019 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1020 if (err != 0)
1021 return -EBUSY;
1022
1023 if ((phy_control & BMCR_RESET) == 0) {
1024 udelay(40);
1025 break;
1026 }
1027 udelay(10);
1028 }
Roel Kluind4675b52009-02-12 16:33:27 -08001029 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001030 return -EBUSY;
1031
1032 return 0;
1033}
1034
Matt Carlson158d7ab2008-05-29 01:37:54 -07001035static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1036{
Francois Romieu3d165432009-01-19 16:56:50 -08001037 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001038 u32 val;
1039
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001040 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001041
1042 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001043 val = -EIO;
1044
1045 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001046
1047 return val;
1048}
1049
1050static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1051{
Francois Romieu3d165432009-01-19 16:56:50 -08001052 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001053 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001054
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001055 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001056
1057 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001058 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001059
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001060 spin_unlock_bh(&tp->lock);
1061
1062 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001063}
1064
1065static int tg3_mdio_reset(struct mii_bus *bp)
1066{
1067 return 0;
1068}
1069
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001070static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001071{
1072 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001073 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001074
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001075 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001076 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001077 case PHY_ID_BCM50610:
1078 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001079 val = MAC_PHYCFG2_50610_LED_MODES;
1080 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001081 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001082 val = MAC_PHYCFG2_AC131_LED_MODES;
1083 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001084 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001085 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1086 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001087 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001088 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1089 break;
1090 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001091 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001092 }
1093
1094 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1095 tw32(MAC_PHYCFG2, val);
1096
1097 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001098 val &= ~(MAC_PHYCFG1_RGMII_INT |
1099 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1100 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001101 tw32(MAC_PHYCFG1, val);
1102
1103 return;
1104 }
1105
Joe Perches63c3a662011-04-26 08:12:10 +00001106 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001107 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1108 MAC_PHYCFG2_FMODE_MASK_MASK |
1109 MAC_PHYCFG2_GMODE_MASK_MASK |
1110 MAC_PHYCFG2_ACT_MASK_MASK |
1111 MAC_PHYCFG2_QUAL_MASK_MASK |
1112 MAC_PHYCFG2_INBAND_ENABLE;
1113
1114 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001115
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001116 val = tr32(MAC_PHYCFG1);
1117 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1118 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001119 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1120 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001121 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001122 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001123 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1124 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001125 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1126 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1127 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001128
Matt Carlsona9daf362008-05-25 23:49:44 -07001129 val = tr32(MAC_EXT_RGMII_MODE);
1130 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1131 MAC_RGMII_MODE_RX_QUALITY |
1132 MAC_RGMII_MODE_RX_ACTIVITY |
1133 MAC_RGMII_MODE_RX_ENG_DET |
1134 MAC_RGMII_MODE_TX_ENABLE |
1135 MAC_RGMII_MODE_TX_LOWPWR |
1136 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001137 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1138 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001139 val |= MAC_RGMII_MODE_RX_INT_B |
1140 MAC_RGMII_MODE_RX_QUALITY |
1141 MAC_RGMII_MODE_RX_ACTIVITY |
1142 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001143 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001144 val |= MAC_RGMII_MODE_TX_ENABLE |
1145 MAC_RGMII_MODE_TX_LOWPWR |
1146 MAC_RGMII_MODE_TX_RESET;
1147 }
1148 tw32(MAC_EXT_RGMII_MODE, val);
1149}
1150
Matt Carlson158d7ab2008-05-29 01:37:54 -07001151static void tg3_mdio_start(struct tg3 *tp)
1152{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001153 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1154 tw32_f(MAC_MI_MODE, tp->mi_mode);
1155 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001156
Joe Perches63c3a662011-04-26 08:12:10 +00001157 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001158 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1159 tg3_mdio_config_5785(tp);
1160}
1161
1162static int tg3_mdio_init(struct tg3 *tp)
1163{
1164 int i;
1165 u32 reg;
1166 struct phy_device *phydev;
1167
Joe Perches63c3a662011-04-26 08:12:10 +00001168 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001169 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001170
Matt Carlson9c7df912010-06-05 17:24:36 +00001171 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001172
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001173 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1174 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1175 else
1176 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1177 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001178 if (is_serdes)
1179 tp->phy_addr += 7;
1180 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001181 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001182
Matt Carlson158d7ab2008-05-29 01:37:54 -07001183 tg3_mdio_start(tp);
1184
Joe Perches63c3a662011-04-26 08:12:10 +00001185 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001186 return 0;
1187
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001188 tp->mdio_bus = mdiobus_alloc();
1189 if (tp->mdio_bus == NULL)
1190 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001191
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001192 tp->mdio_bus->name = "tg3 mdio bus";
1193 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001194 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001195 tp->mdio_bus->priv = tp;
1196 tp->mdio_bus->parent = &tp->pdev->dev;
1197 tp->mdio_bus->read = &tg3_mdio_read;
1198 tp->mdio_bus->write = &tg3_mdio_write;
1199 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001200 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001201 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001202
1203 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001204 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001205
1206 /* The bus registration will look for all the PHYs on the mdio bus.
1207 * Unfortunately, it does not ensure the PHY is powered up before
1208 * accessing the PHY ID registers. A chip reset is the
1209 * quickest way to bring the device back to an operational state..
1210 */
1211 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1212 tg3_bmcr_reset(tp);
1213
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001214 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001215 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001216 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001217 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001218 return i;
1219 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001220
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001221 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001222
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001223 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001224 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001225 mdiobus_unregister(tp->mdio_bus);
1226 mdiobus_free(tp->mdio_bus);
1227 return -ENODEV;
1228 }
1229
1230 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001231 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001232 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001233 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001234 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001235 case PHY_ID_BCM50610:
1236 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001237 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001238 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001239 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001240 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001241 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001242 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001243 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001244 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001245 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001246 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001247 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001248 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001249 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001250 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001251 case PHY_ID_RTL8201E:
1252 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001253 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001254 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001255 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001256 break;
1257 }
1258
Joe Perches63c3a662011-04-26 08:12:10 +00001259 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001260
1261 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1262 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001263
1264 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001265}
1266
1267static void tg3_mdio_fini(struct tg3 *tp)
1268{
Joe Perches63c3a662011-04-26 08:12:10 +00001269 if (tg3_flag(tp, MDIOBUS_INITED)) {
1270 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001271 mdiobus_unregister(tp->mdio_bus);
1272 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001273 }
1274}
1275
Matt Carlson95e28692008-05-25 23:44:14 -07001276/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001277static inline void tg3_generate_fw_event(struct tg3 *tp)
1278{
1279 u32 val;
1280
1281 val = tr32(GRC_RX_CPU_EVENT);
1282 val |= GRC_RX_CPU_DRIVER_EVENT;
1283 tw32_f(GRC_RX_CPU_EVENT, val);
1284
1285 tp->last_event_jiffies = jiffies;
1286}
1287
1288#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1289
1290/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001291static void tg3_wait_for_event_ack(struct tg3 *tp)
1292{
1293 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001294 unsigned int delay_cnt;
1295 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001296
Matt Carlson4ba526c2008-08-15 14:10:04 -07001297 /* If enough time has passed, no wait is necessary. */
1298 time_remain = (long)(tp->last_event_jiffies + 1 +
1299 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1300 (long)jiffies;
1301 if (time_remain < 0)
1302 return;
1303
1304 /* Check if we can shorten the wait time. */
1305 delay_cnt = jiffies_to_usecs(time_remain);
1306 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1307 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1308 delay_cnt = (delay_cnt >> 3) + 1;
1309
1310 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001311 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1312 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001313 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001314 }
1315}
1316
1317/* tp->lock is held. */
1318static void tg3_ump_link_report(struct tg3 *tp)
1319{
1320 u32 reg;
1321 u32 val;
1322
Joe Perches63c3a662011-04-26 08:12:10 +00001323 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001324 return;
1325
1326 tg3_wait_for_event_ack(tp);
1327
1328 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1329
1330 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1331
1332 val = 0;
1333 if (!tg3_readphy(tp, MII_BMCR, &reg))
1334 val = reg << 16;
1335 if (!tg3_readphy(tp, MII_BMSR, &reg))
1336 val |= (reg & 0xffff);
1337 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1338
1339 val = 0;
1340 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1341 val = reg << 16;
1342 if (!tg3_readphy(tp, MII_LPA, &reg))
1343 val |= (reg & 0xffff);
1344 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1345
1346 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001347 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001348 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1349 val = reg << 16;
1350 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1351 val |= (reg & 0xffff);
1352 }
1353 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1354
1355 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1356 val = reg << 16;
1357 else
1358 val = 0;
1359 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1360
Matt Carlson4ba526c2008-08-15 14:10:04 -07001361 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001362}
1363
1364static void tg3_link_report(struct tg3 *tp)
1365{
1366 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001367 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001368 tg3_ump_link_report(tp);
1369 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001370 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1371 (tp->link_config.active_speed == SPEED_1000 ?
1372 1000 :
1373 (tp->link_config.active_speed == SPEED_100 ?
1374 100 : 10)),
1375 (tp->link_config.active_duplex == DUPLEX_FULL ?
1376 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001377
Joe Perches05dbe002010-02-17 19:44:19 +00001378 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1379 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1380 "on" : "off",
1381 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1382 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001383
1384 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1385 netdev_info(tp->dev, "EEE is %s\n",
1386 tp->setlpicnt ? "enabled" : "disabled");
1387
Matt Carlson95e28692008-05-25 23:44:14 -07001388 tg3_ump_link_report(tp);
1389 }
1390}
1391
1392static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1393{
1394 u16 miireg;
1395
Steve Glendinninge18ce342008-12-16 02:00:00 -08001396 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001397 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001398 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001399 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001400 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001401 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1402 else
1403 miireg = 0;
1404
1405 return miireg;
1406}
1407
1408static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1409{
1410 u16 miireg;
1411
Steve Glendinninge18ce342008-12-16 02:00:00 -08001412 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001413 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001414 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001415 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001416 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001417 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1418 else
1419 miireg = 0;
1420
1421 return miireg;
1422}
1423
Matt Carlson95e28692008-05-25 23:44:14 -07001424static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1425{
1426 u8 cap = 0;
1427
1428 if (lcladv & ADVERTISE_1000XPAUSE) {
1429 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1430 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001431 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001432 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001433 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001434 } else {
1435 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001436 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001437 }
1438 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1439 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001440 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001441 }
1442
1443 return cap;
1444}
1445
Matt Carlsonf51f3562008-05-25 23:45:08 -07001446static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001447{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001448 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001449 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001450 u32 old_rx_mode = tp->rx_mode;
1451 u32 old_tx_mode = tp->tx_mode;
1452
Joe Perches63c3a662011-04-26 08:12:10 +00001453 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001454 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001455 else
1456 autoneg = tp->link_config.autoneg;
1457
Joe Perches63c3a662011-04-26 08:12:10 +00001458 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001459 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001460 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001461 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001462 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001463 } else
1464 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001465
Matt Carlsonf51f3562008-05-25 23:45:08 -07001466 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001467
Steve Glendinninge18ce342008-12-16 02:00:00 -08001468 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001469 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1470 else
1471 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1472
Matt Carlsonf51f3562008-05-25 23:45:08 -07001473 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001474 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001475
Steve Glendinninge18ce342008-12-16 02:00:00 -08001476 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001477 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1478 else
1479 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1480
Matt Carlsonf51f3562008-05-25 23:45:08 -07001481 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001482 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001483}
1484
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001485static void tg3_adjust_link(struct net_device *dev)
1486{
1487 u8 oldflowctrl, linkmesg = 0;
1488 u32 mac_mode, lcl_adv, rmt_adv;
1489 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001490 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001491
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001492 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001493
1494 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1495 MAC_MODE_HALF_DUPLEX);
1496
1497 oldflowctrl = tp->link_config.active_flowctrl;
1498
1499 if (phydev->link) {
1500 lcl_adv = 0;
1501 rmt_adv = 0;
1502
1503 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1504 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001505 else if (phydev->speed == SPEED_1000 ||
1506 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001507 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001508 else
1509 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001510
1511 if (phydev->duplex == DUPLEX_HALF)
1512 mac_mode |= MAC_MODE_HALF_DUPLEX;
1513 else {
1514 lcl_adv = tg3_advert_flowctrl_1000T(
1515 tp->link_config.flowctrl);
1516
1517 if (phydev->pause)
1518 rmt_adv = LPA_PAUSE_CAP;
1519 if (phydev->asym_pause)
1520 rmt_adv |= LPA_PAUSE_ASYM;
1521 }
1522
1523 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1524 } else
1525 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1526
1527 if (mac_mode != tp->mac_mode) {
1528 tp->mac_mode = mac_mode;
1529 tw32_f(MAC_MODE, tp->mac_mode);
1530 udelay(40);
1531 }
1532
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001533 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1534 if (phydev->speed == SPEED_10)
1535 tw32(MAC_MI_STAT,
1536 MAC_MI_STAT_10MBPS_MODE |
1537 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1538 else
1539 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1540 }
1541
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001542 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1543 tw32(MAC_TX_LENGTHS,
1544 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1545 (6 << TX_LENGTHS_IPG_SHIFT) |
1546 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1547 else
1548 tw32(MAC_TX_LENGTHS,
1549 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1550 (6 << TX_LENGTHS_IPG_SHIFT) |
1551 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1552
1553 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1554 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1555 phydev->speed != tp->link_config.active_speed ||
1556 phydev->duplex != tp->link_config.active_duplex ||
1557 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001558 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001559
1560 tp->link_config.active_speed = phydev->speed;
1561 tp->link_config.active_duplex = phydev->duplex;
1562
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001563 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001564
1565 if (linkmesg)
1566 tg3_link_report(tp);
1567}
1568
1569static int tg3_phy_init(struct tg3 *tp)
1570{
1571 struct phy_device *phydev;
1572
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001573 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001574 return 0;
1575
1576 /* Bring the PHY back to a known state. */
1577 tg3_bmcr_reset(tp);
1578
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001579 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001580
1581 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad352008-11-10 13:55:14 -08001582 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001583 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001584 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001585 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001586 return PTR_ERR(phydev);
1587 }
1588
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001589 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001590 switch (phydev->interface) {
1591 case PHY_INTERFACE_MODE_GMII:
1592 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001593 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001594 phydev->supported &= (PHY_GBIT_FEATURES |
1595 SUPPORTED_Pause |
1596 SUPPORTED_Asym_Pause);
1597 break;
1598 }
1599 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001600 case PHY_INTERFACE_MODE_MII:
1601 phydev->supported &= (PHY_BASIC_FEATURES |
1602 SUPPORTED_Pause |
1603 SUPPORTED_Asym_Pause);
1604 break;
1605 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001606 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001607 return -EINVAL;
1608 }
1609
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001610 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001611
1612 phydev->advertising = phydev->supported;
1613
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001614 return 0;
1615}
1616
1617static void tg3_phy_start(struct tg3 *tp)
1618{
1619 struct phy_device *phydev;
1620
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001621 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001622 return;
1623
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001624 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001625
Matt Carlson80096062010-08-02 11:26:06 +00001626 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1627 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001628 phydev->speed = tp->link_config.orig_speed;
1629 phydev->duplex = tp->link_config.orig_duplex;
1630 phydev->autoneg = tp->link_config.orig_autoneg;
1631 phydev->advertising = tp->link_config.orig_advertising;
1632 }
1633
1634 phy_start(phydev);
1635
1636 phy_start_aneg(phydev);
1637}
1638
1639static void tg3_phy_stop(struct tg3 *tp)
1640{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001641 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001642 return;
1643
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001644 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001645}
1646
1647static void tg3_phy_fini(struct tg3 *tp)
1648{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001649 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001650 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001651 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001652 }
1653}
1654
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001655static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1656{
1657 u32 phytest;
1658
1659 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1660 u32 phy;
1661
1662 tg3_writephy(tp, MII_TG3_FET_TEST,
1663 phytest | MII_TG3_FET_SHADOW_EN);
1664 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1665 if (enable)
1666 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1667 else
1668 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1669 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1670 }
1671 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1672 }
1673}
1674
Matt Carlson6833c042008-11-21 17:18:59 -08001675static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1676{
1677 u32 reg;
1678
Joe Perches63c3a662011-04-26 08:12:10 +00001679 if (!tg3_flag(tp, 5705_PLUS) ||
1680 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001681 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001682 return;
1683
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001684 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001685 tg3_phy_fet_toggle_apd(tp, enable);
1686 return;
1687 }
1688
Matt Carlson6833c042008-11-21 17:18:59 -08001689 reg = MII_TG3_MISC_SHDW_WREN |
1690 MII_TG3_MISC_SHDW_SCR5_SEL |
1691 MII_TG3_MISC_SHDW_SCR5_LPED |
1692 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1693 MII_TG3_MISC_SHDW_SCR5_SDTL |
1694 MII_TG3_MISC_SHDW_SCR5_C125OE;
1695 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1696 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1697
1698 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1699
1700
1701 reg = MII_TG3_MISC_SHDW_WREN |
1702 MII_TG3_MISC_SHDW_APD_SEL |
1703 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1704 if (enable)
1705 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1706
1707 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1708}
1709
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001710static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1711{
1712 u32 phy;
1713
Joe Perches63c3a662011-04-26 08:12:10 +00001714 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001715 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001716 return;
1717
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001718 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001719 u32 ephy;
1720
Matt Carlson535ef6e2009-08-25 10:09:36 +00001721 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1722 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1723
1724 tg3_writephy(tp, MII_TG3_FET_TEST,
1725 ephy | MII_TG3_FET_SHADOW_EN);
1726 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001727 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001728 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001729 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001730 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1731 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001732 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001733 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001734 }
1735 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00001736 int ret;
1737
1738 ret = tg3_phy_auxctl_read(tp,
1739 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
1740 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001741 if (enable)
1742 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1743 else
1744 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001745 tg3_phy_auxctl_write(tp,
1746 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001747 }
1748 }
1749}
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751static void tg3_phy_set_wirespeed(struct tg3 *tp)
1752{
Matt Carlson15ee95c2011-04-20 07:57:40 +00001753 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 u32 val;
1755
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001756 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return;
1758
Matt Carlson15ee95c2011-04-20 07:57:40 +00001759 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
1760 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001761 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
1762 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001765static void tg3_phy_apply_otp(struct tg3 *tp)
1766{
1767 u32 otp, phy;
1768
1769 if (!tp->phy_otp)
1770 return;
1771
1772 otp = tp->phy_otp;
1773
Matt Carlson1d36ba42011-04-20 07:57:42 +00001774 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
1775 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001776
1777 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1778 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1779 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1780
1781 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1782 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1783 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1784
1785 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1786 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1787 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1788
1789 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1790 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1791
1792 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1793 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1794
1795 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1796 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1797 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1798
Matt Carlson1d36ba42011-04-20 07:57:42 +00001799 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001800}
1801
Matt Carlson52b02d02010-10-14 10:37:41 +00001802static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1803{
1804 u32 val;
1805
1806 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1807 return;
1808
1809 tp->setlpicnt = 0;
1810
1811 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1812 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001813 tp->link_config.active_duplex == DUPLEX_FULL &&
1814 (tp->link_config.active_speed == SPEED_100 ||
1815 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001816 u32 eeectl;
1817
1818 if (tp->link_config.active_speed == SPEED_1000)
1819 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1820 else
1821 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1822
1823 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1824
Matt Carlson3110f5f52010-12-06 08:28:50 +00001825 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1826 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001827
Matt Carlsonb0c59432011-05-19 12:12:48 +00001828 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
1829 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00001830 tp->setlpicnt = 2;
1831 }
1832
1833 if (!tp->setlpicnt) {
1834 val = tr32(TG3_CPMU_EEE_MODE);
1835 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1836 }
1837}
1838
Matt Carlsonb0c59432011-05-19 12:12:48 +00001839static void tg3_phy_eee_enable(struct tg3 *tp)
1840{
1841 u32 val;
1842
1843 if (tp->link_config.active_speed == SPEED_1000 &&
1844 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1845 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
1846 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
1847 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
1848 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0003);
1849 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1850 }
1851
1852 val = tr32(TG3_CPMU_EEE_MODE);
1853 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
1854}
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856static int tg3_wait_macro_done(struct tg3 *tp)
1857{
1858 int limit = 100;
1859
1860 while (limit--) {
1861 u32 tmp32;
1862
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001863 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if ((tmp32 & 0x1000) == 0)
1865 break;
1866 }
1867 }
Roel Kluind4675b52009-02-12 16:33:27 -08001868 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return -EBUSY;
1870
1871 return 0;
1872}
1873
1874static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1875{
1876 static const u32 test_pat[4][6] = {
1877 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1878 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1879 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1880 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1881 };
1882 int chan;
1883
1884 for (chan = 0; chan < 4; chan++) {
1885 int i;
1886
1887 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1888 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001889 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 for (i = 0; i < 6; i++)
1892 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1893 test_pat[chan][i]);
1894
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001895 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (tg3_wait_macro_done(tp)) {
1897 *resetp = 1;
1898 return -EBUSY;
1899 }
1900
1901 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1902 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001903 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (tg3_wait_macro_done(tp)) {
1905 *resetp = 1;
1906 return -EBUSY;
1907 }
1908
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001909 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (tg3_wait_macro_done(tp)) {
1911 *resetp = 1;
1912 return -EBUSY;
1913 }
1914
1915 for (i = 0; i < 6; i += 2) {
1916 u32 low, high;
1917
1918 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1919 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1920 tg3_wait_macro_done(tp)) {
1921 *resetp = 1;
1922 return -EBUSY;
1923 }
1924 low &= 0x7fff;
1925 high &= 0x000f;
1926 if (low != test_pat[chan][i] ||
1927 high != test_pat[chan][i+1]) {
1928 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1929 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1930 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1931
1932 return -EBUSY;
1933 }
1934 }
1935 }
1936
1937 return 0;
1938}
1939
1940static int tg3_phy_reset_chanpat(struct tg3 *tp)
1941{
1942 int chan;
1943
1944 for (chan = 0; chan < 4; chan++) {
1945 int i;
1946
1947 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1948 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001949 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 for (i = 0; i < 6; i++)
1951 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001952 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (tg3_wait_macro_done(tp))
1954 return -EBUSY;
1955 }
1956
1957 return 0;
1958}
1959
1960static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1961{
1962 u32 reg32, phy9_orig;
1963 int retries, do_phy_reset, err;
1964
1965 retries = 10;
1966 do_phy_reset = 1;
1967 do {
1968 if (do_phy_reset) {
1969 err = tg3_bmcr_reset(tp);
1970 if (err)
1971 return err;
1972 do_phy_reset = 0;
1973 }
1974
1975 /* Disable transmitter and interrupt. */
1976 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1977 continue;
1978
1979 reg32 |= 0x3000;
1980 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1981
1982 /* Set full-duplex, 1000 mbps. */
1983 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00001984 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00001987 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 continue;
1989
Matt Carlson221c5632011-06-13 13:39:01 +00001990 tg3_writephy(tp, MII_CTRL1000,
1991 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
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
Matt Carlson221c5632011-06-13 13:39:01 +00002016 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
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)
Matt Carlson221c5632011-06-13 13:39:01 +00002960 new_adv |= ADVERTISE_1000HALF;
Matt Carlson42b64a42011-05-19 12:12:49 +00002961 if (advertise & ADVERTISED_1000baseT_Full)
Matt Carlson221c5632011-06-13 13:39:01 +00002962 new_adv |= ADVERTISE_1000FULL;
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)
Matt Carlson221c5632011-06-13 13:39:01 +00002966 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Matt Carlson221c5632011-06-13 13:39:01 +00002968 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
Matt Carlson42b64a42011-05-19 12:12:49 +00002969 if (err)
2970 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002971
Matt Carlson42b64a42011-05-19 12:12:49 +00002972 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
2973 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Matt Carlson42b64a42011-05-19 12:12:49 +00002975 tw32(TG3_CPMU_EEE_MODE,
2976 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002977
Matt Carlson42b64a42011-05-19 12:12:49 +00002978 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2979 if (!err) {
2980 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00002981
Matt Carlson21a00ab2011-01-25 15:58:55 +00002982 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
2983 case ASIC_REV_5717:
2984 case ASIC_REV_57765:
2985 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2986 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
2987 MII_TG3_DSP_CH34TP2_HIBW01);
2988 /* Fall through */
2989 case ASIC_REV_5719:
2990 val = MII_TG3_DSP_TAP26_ALNOKO |
2991 MII_TG3_DSP_TAP26_RMRXSTO |
2992 MII_TG3_DSP_TAP26_OPCSINPT;
2993 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
2994 }
Matt Carlson52b02d02010-10-14 10:37:41 +00002995
Matt Carlsona6b68da2010-12-06 08:28:52 +00002996 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00002997 /* Advertise 100-BaseTX EEE ability */
2998 if (advertise & ADVERTISED_100baseT_Full)
2999 val |= MDIO_AN_EEE_ADV_100TX;
3000 /* Advertise 1000-BaseT EEE ability */
3001 if (advertise & ADVERTISED_1000baseT_Full)
3002 val |= MDIO_AN_EEE_ADV_1000T;
3003 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003004
Matt Carlson42b64a42011-05-19 12:12:49 +00003005 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3006 if (!err)
3007 err = err2;
3008 }
3009
3010done:
3011 return err;
3012}
3013
3014static void tg3_phy_copper_begin(struct tg3 *tp)
3015{
3016 u32 new_adv;
3017 int i;
3018
3019 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3020 new_adv = ADVERTISED_10baseT_Half |
3021 ADVERTISED_10baseT_Full;
3022 if (tg3_flag(tp, WOL_SPEED_100MB))
3023 new_adv |= ADVERTISED_100baseT_Half |
3024 ADVERTISED_100baseT_Full;
3025
3026 tg3_phy_autoneg_cfg(tp, new_adv,
3027 FLOW_CTRL_TX | FLOW_CTRL_RX);
3028 } else if (tp->link_config.speed == SPEED_INVALID) {
3029 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3030 tp->link_config.advertising &=
3031 ~(ADVERTISED_1000baseT_Half |
3032 ADVERTISED_1000baseT_Full);
3033
3034 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3035 tp->link_config.flowctrl);
3036 } else {
3037 /* Asking for a specific link mode. */
3038 if (tp->link_config.speed == SPEED_1000) {
3039 if (tp->link_config.duplex == DUPLEX_FULL)
3040 new_adv = ADVERTISED_1000baseT_Full;
3041 else
3042 new_adv = ADVERTISED_1000baseT_Half;
3043 } else if (tp->link_config.speed == SPEED_100) {
3044 if (tp->link_config.duplex == DUPLEX_FULL)
3045 new_adv = ADVERTISED_100baseT_Full;
3046 else
3047 new_adv = ADVERTISED_100baseT_Half;
3048 } else {
3049 if (tp->link_config.duplex == DUPLEX_FULL)
3050 new_adv = ADVERTISED_10baseT_Full;
3051 else
3052 new_adv = ADVERTISED_10baseT_Half;
3053 }
3054
3055 tg3_phy_autoneg_cfg(tp, new_adv,
3056 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003057 }
3058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3060 tp->link_config.speed != SPEED_INVALID) {
3061 u32 bmcr, orig_bmcr;
3062
3063 tp->link_config.active_speed = tp->link_config.speed;
3064 tp->link_config.active_duplex = tp->link_config.duplex;
3065
3066 bmcr = 0;
3067 switch (tp->link_config.speed) {
3068 default:
3069 case SPEED_10:
3070 break;
3071
3072 case SPEED_100:
3073 bmcr |= BMCR_SPEED100;
3074 break;
3075
3076 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003077 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
3081 if (tp->link_config.duplex == DUPLEX_FULL)
3082 bmcr |= BMCR_FULLDPLX;
3083
3084 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3085 (bmcr != orig_bmcr)) {
3086 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3087 for (i = 0; i < 1500; i++) {
3088 u32 tmp;
3089
3090 udelay(10);
3091 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3092 tg3_readphy(tp, MII_BMSR, &tmp))
3093 continue;
3094 if (!(tmp & BMSR_LSTATUS)) {
3095 udelay(40);
3096 break;
3097 }
3098 }
3099 tg3_writephy(tp, MII_BMCR, bmcr);
3100 udelay(40);
3101 }
3102 } else {
3103 tg3_writephy(tp, MII_BMCR,
3104 BMCR_ANENABLE | BMCR_ANRESTART);
3105 }
3106}
3107
3108static int tg3_init_5401phy_dsp(struct tg3 *tp)
3109{
3110 int err;
3111
3112 /* Turn off tap power management. */
3113 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003114 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003116 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3117 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3118 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3119 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3120 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
3122 udelay(40);
3123
3124 return err;
3125}
3126
Michael Chan3600d912006-12-07 00:21:48 -08003127static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
Michael Chan3600d912006-12-07 00:21:48 -08003129 u32 adv_reg, all_mask = 0;
3130
3131 if (mask & ADVERTISED_10baseT_Half)
3132 all_mask |= ADVERTISE_10HALF;
3133 if (mask & ADVERTISED_10baseT_Full)
3134 all_mask |= ADVERTISE_10FULL;
3135 if (mask & ADVERTISED_100baseT_Half)
3136 all_mask |= ADVERTISE_100HALF;
3137 if (mask & ADVERTISED_100baseT_Full)
3138 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
3140 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3141 return 0;
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 if ((adv_reg & all_mask) != all_mask)
3144 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003145 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 u32 tg3_ctrl;
3147
Michael Chan3600d912006-12-07 00:21:48 -08003148 all_mask = 0;
3149 if (mask & ADVERTISED_1000baseT_Half)
3150 all_mask |= ADVERTISE_1000HALF;
3151 if (mask & ADVERTISED_1000baseT_Full)
3152 all_mask |= ADVERTISE_1000FULL;
3153
Matt Carlson221c5632011-06-13 13:39:01 +00003154 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return 0;
3156
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 if ((tg3_ctrl & all_mask) != all_mask)
3158 return 0;
3159 }
3160 return 1;
3161}
3162
Matt Carlsonef167e22007-12-20 20:10:01 -08003163static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3164{
3165 u32 curadv, reqadv;
3166
3167 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3168 return 1;
3169
3170 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3171 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3172
3173 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3174 if (curadv != reqadv)
3175 return 0;
3176
Joe Perches63c3a662011-04-26 08:12:10 +00003177 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003178 tg3_readphy(tp, MII_LPA, rmtadv);
3179 } else {
3180 /* Reprogram the advertisement register, even if it
3181 * does not affect the current link. If the link
3182 * gets renegotiated in the future, we can save an
3183 * additional renegotiation cycle by advertising
3184 * it correctly in the first place.
3185 */
3186 if (curadv != reqadv) {
3187 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3188 ADVERTISE_PAUSE_ASYM);
3189 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3190 }
3191 }
3192
3193 return 1;
3194}
3195
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3197{
3198 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003199 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003200 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 u16 current_speed;
3202 u8 current_duplex;
3203 int i, err;
3204
3205 tw32(MAC_EVENT, 0);
3206
3207 tw32_f(MAC_STATUS,
3208 (MAC_STATUS_SYNC_CHANGED |
3209 MAC_STATUS_CFG_CHANGED |
3210 MAC_STATUS_MI_COMPLETION |
3211 MAC_STATUS_LNKSTATE_CHANGED));
3212 udelay(40);
3213
Matt Carlson8ef21422008-05-02 16:47:53 -07003214 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3215 tw32_f(MAC_MI_MODE,
3216 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3217 udelay(80);
3218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003220 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 /* Some third-party PHYs need to be reset on link going
3223 * down.
3224 */
3225 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3226 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3227 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3228 netif_carrier_ok(tp->dev)) {
3229 tg3_readphy(tp, MII_BMSR, &bmsr);
3230 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3231 !(bmsr & BMSR_LSTATUS))
3232 force_reset = 1;
3233 }
3234 if (force_reset)
3235 tg3_phy_reset(tp);
3236
Matt Carlson79eb6902010-02-17 15:17:03 +00003237 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 tg3_readphy(tp, MII_BMSR, &bmsr);
3239 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003240 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 bmsr = 0;
3242
3243 if (!(bmsr & BMSR_LSTATUS)) {
3244 err = tg3_init_5401phy_dsp(tp);
3245 if (err)
3246 return err;
3247
3248 tg3_readphy(tp, MII_BMSR, &bmsr);
3249 for (i = 0; i < 1000; i++) {
3250 udelay(10);
3251 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3252 (bmsr & BMSR_LSTATUS)) {
3253 udelay(40);
3254 break;
3255 }
3256 }
3257
Matt Carlson79eb6902010-02-17 15:17:03 +00003258 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3259 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 !(bmsr & BMSR_LSTATUS) &&
3261 tp->link_config.active_speed == SPEED_1000) {
3262 err = tg3_phy_reset(tp);
3263 if (!err)
3264 err = tg3_init_5401phy_dsp(tp);
3265 if (err)
3266 return err;
3267 }
3268 }
3269 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3270 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3271 /* 5701 {A0,B0} CRC bug workaround */
3272 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003273 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3274 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3275 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 }
3277
3278 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003279 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3280 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003282 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003284 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3286
3287 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3288 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3289 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3290 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3291 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3292 else
3293 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3294 }
3295
3296 current_link_up = 0;
3297 current_speed = SPEED_INVALID;
3298 current_duplex = DUPLEX_INVALID;
3299
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003300 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003301 err = tg3_phy_auxctl_read(tp,
3302 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3303 &val);
3304 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003305 tg3_phy_auxctl_write(tp,
3306 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3307 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 goto relink;
3309 }
3310 }
3311
3312 bmsr = 0;
3313 for (i = 0; i < 100; i++) {
3314 tg3_readphy(tp, MII_BMSR, &bmsr);
3315 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3316 (bmsr & BMSR_LSTATUS))
3317 break;
3318 udelay(40);
3319 }
3320
3321 if (bmsr & BMSR_LSTATUS) {
3322 u32 aux_stat, bmcr;
3323
3324 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3325 for (i = 0; i < 2000; i++) {
3326 udelay(10);
3327 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3328 aux_stat)
3329 break;
3330 }
3331
3332 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3333 &current_speed,
3334 &current_duplex);
3335
3336 bmcr = 0;
3337 for (i = 0; i < 200; i++) {
3338 tg3_readphy(tp, MII_BMCR, &bmcr);
3339 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3340 continue;
3341 if (bmcr && bmcr != 0x7fff)
3342 break;
3343 udelay(10);
3344 }
3345
Matt Carlsonef167e22007-12-20 20:10:01 -08003346 lcl_adv = 0;
3347 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Matt Carlsonef167e22007-12-20 20:10:01 -08003349 tp->link_config.active_speed = current_speed;
3350 tp->link_config.active_duplex = current_duplex;
3351
3352 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3353 if ((bmcr & BMCR_ANENABLE) &&
3354 tg3_copper_is_advertising_all(tp,
3355 tp->link_config.advertising)) {
3356 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3357 &rmt_adv))
3358 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 }
3360 } else {
3361 if (!(bmcr & BMCR_ANENABLE) &&
3362 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003363 tp->link_config.duplex == current_duplex &&
3364 tp->link_config.flowctrl ==
3365 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 }
3368 }
3369
Matt Carlsonef167e22007-12-20 20:10:01 -08003370 if (current_link_up == 1 &&
3371 tp->link_config.active_duplex == DUPLEX_FULL)
3372 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 }
3374
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375relink:
Matt Carlson80096062010-08-02 11:26:06 +00003376 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 tg3_phy_copper_begin(tp);
3378
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003379 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00003380 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
3381 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 current_link_up = 1;
3383 }
3384
3385 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3386 if (current_link_up == 1) {
3387 if (tp->link_config.active_speed == SPEED_100 ||
3388 tp->link_config.active_speed == SPEED_10)
3389 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3390 else
3391 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003392 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003393 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3394 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3396
3397 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3398 if (tp->link_config.active_duplex == DUPLEX_HALF)
3399 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3400
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003402 if (current_link_up == 1 &&
3403 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003405 else
3406 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 }
3408
3409 /* ??? Without this setting Netgear GA302T PHY does not
3410 * ??? send/receive packets...
3411 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003412 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3414 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3415 tw32_f(MAC_MI_MODE, tp->mi_mode);
3416 udelay(80);
3417 }
3418
3419 tw32_f(MAC_MODE, tp->mac_mode);
3420 udelay(40);
3421
Matt Carlson52b02d02010-10-14 10:37:41 +00003422 tg3_phy_eee_adjust(tp, current_link_up);
3423
Joe Perches63c3a662011-04-26 08:12:10 +00003424 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 /* Polled via timer. */
3426 tw32_f(MAC_EVENT, 0);
3427 } else {
3428 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3429 }
3430 udelay(40);
3431
3432 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3433 current_link_up == 1 &&
3434 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00003435 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 udelay(120);
3437 tw32_f(MAC_STATUS,
3438 (MAC_STATUS_SYNC_CHANGED |
3439 MAC_STATUS_CFG_CHANGED));
3440 udelay(40);
3441 tg3_write_mem(tp,
3442 NIC_SRAM_FIRMWARE_MBOX,
3443 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3444 }
3445
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003446 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00003447 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003448 u16 oldlnkctl, newlnkctl;
3449
3450 pci_read_config_word(tp->pdev,
3451 tp->pcie_cap + PCI_EXP_LNKCTL,
3452 &oldlnkctl);
3453 if (tp->link_config.active_speed == SPEED_100 ||
3454 tp->link_config.active_speed == SPEED_10)
3455 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3456 else
3457 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3458 if (newlnkctl != oldlnkctl)
3459 pci_write_config_word(tp->pdev,
3460 tp->pcie_cap + PCI_EXP_LNKCTL,
3461 newlnkctl);
3462 }
3463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 if (current_link_up != netif_carrier_ok(tp->dev)) {
3465 if (current_link_up)
3466 netif_carrier_on(tp->dev);
3467 else
3468 netif_carrier_off(tp->dev);
3469 tg3_link_report(tp);
3470 }
3471
3472 return 0;
3473}
3474
3475struct tg3_fiber_aneginfo {
3476 int state;
3477#define ANEG_STATE_UNKNOWN 0
3478#define ANEG_STATE_AN_ENABLE 1
3479#define ANEG_STATE_RESTART_INIT 2
3480#define ANEG_STATE_RESTART 3
3481#define ANEG_STATE_DISABLE_LINK_OK 4
3482#define ANEG_STATE_ABILITY_DETECT_INIT 5
3483#define ANEG_STATE_ABILITY_DETECT 6
3484#define ANEG_STATE_ACK_DETECT_INIT 7
3485#define ANEG_STATE_ACK_DETECT 8
3486#define ANEG_STATE_COMPLETE_ACK_INIT 9
3487#define ANEG_STATE_COMPLETE_ACK 10
3488#define ANEG_STATE_IDLE_DETECT_INIT 11
3489#define ANEG_STATE_IDLE_DETECT 12
3490#define ANEG_STATE_LINK_OK 13
3491#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3492#define ANEG_STATE_NEXT_PAGE_WAIT 15
3493
3494 u32 flags;
3495#define MR_AN_ENABLE 0x00000001
3496#define MR_RESTART_AN 0x00000002
3497#define MR_AN_COMPLETE 0x00000004
3498#define MR_PAGE_RX 0x00000008
3499#define MR_NP_LOADED 0x00000010
3500#define MR_TOGGLE_TX 0x00000020
3501#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3502#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3503#define MR_LP_ADV_SYM_PAUSE 0x00000100
3504#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3505#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3506#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3507#define MR_LP_ADV_NEXT_PAGE 0x00001000
3508#define MR_TOGGLE_RX 0x00002000
3509#define MR_NP_RX 0x00004000
3510
3511#define MR_LINK_OK 0x80000000
3512
3513 unsigned long link_time, cur_time;
3514
3515 u32 ability_match_cfg;
3516 int ability_match_count;
3517
3518 char ability_match, idle_match, ack_match;
3519
3520 u32 txconfig, rxconfig;
3521#define ANEG_CFG_NP 0x00000080
3522#define ANEG_CFG_ACK 0x00000040
3523#define ANEG_CFG_RF2 0x00000020
3524#define ANEG_CFG_RF1 0x00000010
3525#define ANEG_CFG_PS2 0x00000001
3526#define ANEG_CFG_PS1 0x00008000
3527#define ANEG_CFG_HD 0x00004000
3528#define ANEG_CFG_FD 0x00002000
3529#define ANEG_CFG_INVAL 0x00001f06
3530
3531};
3532#define ANEG_OK 0
3533#define ANEG_DONE 1
3534#define ANEG_TIMER_ENAB 2
3535#define ANEG_FAILED -1
3536
3537#define ANEG_STATE_SETTLE_TIME 10000
3538
3539static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3540 struct tg3_fiber_aneginfo *ap)
3541{
Matt Carlson5be73b42007-12-20 20:09:29 -08003542 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 unsigned long delta;
3544 u32 rx_cfg_reg;
3545 int ret;
3546
3547 if (ap->state == ANEG_STATE_UNKNOWN) {
3548 ap->rxconfig = 0;
3549 ap->link_time = 0;
3550 ap->cur_time = 0;
3551 ap->ability_match_cfg = 0;
3552 ap->ability_match_count = 0;
3553 ap->ability_match = 0;
3554 ap->idle_match = 0;
3555 ap->ack_match = 0;
3556 }
3557 ap->cur_time++;
3558
3559 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3560 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3561
3562 if (rx_cfg_reg != ap->ability_match_cfg) {
3563 ap->ability_match_cfg = rx_cfg_reg;
3564 ap->ability_match = 0;
3565 ap->ability_match_count = 0;
3566 } else {
3567 if (++ap->ability_match_count > 1) {
3568 ap->ability_match = 1;
3569 ap->ability_match_cfg = rx_cfg_reg;
3570 }
3571 }
3572 if (rx_cfg_reg & ANEG_CFG_ACK)
3573 ap->ack_match = 1;
3574 else
3575 ap->ack_match = 0;
3576
3577 ap->idle_match = 0;
3578 } else {
3579 ap->idle_match = 1;
3580 ap->ability_match_cfg = 0;
3581 ap->ability_match_count = 0;
3582 ap->ability_match = 0;
3583 ap->ack_match = 0;
3584
3585 rx_cfg_reg = 0;
3586 }
3587
3588 ap->rxconfig = rx_cfg_reg;
3589 ret = ANEG_OK;
3590
Matt Carlson33f401a2010-04-05 10:19:27 +00003591 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 case ANEG_STATE_UNKNOWN:
3593 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3594 ap->state = ANEG_STATE_AN_ENABLE;
3595
3596 /* fallthru */
3597 case ANEG_STATE_AN_ENABLE:
3598 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3599 if (ap->flags & MR_AN_ENABLE) {
3600 ap->link_time = 0;
3601 ap->cur_time = 0;
3602 ap->ability_match_cfg = 0;
3603 ap->ability_match_count = 0;
3604 ap->ability_match = 0;
3605 ap->idle_match = 0;
3606 ap->ack_match = 0;
3607
3608 ap->state = ANEG_STATE_RESTART_INIT;
3609 } else {
3610 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3611 }
3612 break;
3613
3614 case ANEG_STATE_RESTART_INIT:
3615 ap->link_time = ap->cur_time;
3616 ap->flags &= ~(MR_NP_LOADED);
3617 ap->txconfig = 0;
3618 tw32(MAC_TX_AUTO_NEG, 0);
3619 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3620 tw32_f(MAC_MODE, tp->mac_mode);
3621 udelay(40);
3622
3623 ret = ANEG_TIMER_ENAB;
3624 ap->state = ANEG_STATE_RESTART;
3625
3626 /* fallthru */
3627 case ANEG_STATE_RESTART:
3628 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003629 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003631 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 break;
3634
3635 case ANEG_STATE_DISABLE_LINK_OK:
3636 ret = ANEG_DONE;
3637 break;
3638
3639 case ANEG_STATE_ABILITY_DETECT_INIT:
3640 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003641 ap->txconfig = ANEG_CFG_FD;
3642 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3643 if (flowctrl & ADVERTISE_1000XPAUSE)
3644 ap->txconfig |= ANEG_CFG_PS1;
3645 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3646 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3648 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3649 tw32_f(MAC_MODE, tp->mac_mode);
3650 udelay(40);
3651
3652 ap->state = ANEG_STATE_ABILITY_DETECT;
3653 break;
3654
3655 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003656 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 break;
3659
3660 case ANEG_STATE_ACK_DETECT_INIT:
3661 ap->txconfig |= ANEG_CFG_ACK;
3662 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3663 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3664 tw32_f(MAC_MODE, tp->mac_mode);
3665 udelay(40);
3666
3667 ap->state = ANEG_STATE_ACK_DETECT;
3668
3669 /* fallthru */
3670 case ANEG_STATE_ACK_DETECT:
3671 if (ap->ack_match != 0) {
3672 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3673 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3674 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3675 } else {
3676 ap->state = ANEG_STATE_AN_ENABLE;
3677 }
3678 } else if (ap->ability_match != 0 &&
3679 ap->rxconfig == 0) {
3680 ap->state = ANEG_STATE_AN_ENABLE;
3681 }
3682 break;
3683
3684 case ANEG_STATE_COMPLETE_ACK_INIT:
3685 if (ap->rxconfig & ANEG_CFG_INVAL) {
3686 ret = ANEG_FAILED;
3687 break;
3688 }
3689 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3690 MR_LP_ADV_HALF_DUPLEX |
3691 MR_LP_ADV_SYM_PAUSE |
3692 MR_LP_ADV_ASYM_PAUSE |
3693 MR_LP_ADV_REMOTE_FAULT1 |
3694 MR_LP_ADV_REMOTE_FAULT2 |
3695 MR_LP_ADV_NEXT_PAGE |
3696 MR_TOGGLE_RX |
3697 MR_NP_RX);
3698 if (ap->rxconfig & ANEG_CFG_FD)
3699 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3700 if (ap->rxconfig & ANEG_CFG_HD)
3701 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3702 if (ap->rxconfig & ANEG_CFG_PS1)
3703 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3704 if (ap->rxconfig & ANEG_CFG_PS2)
3705 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3706 if (ap->rxconfig & ANEG_CFG_RF1)
3707 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3708 if (ap->rxconfig & ANEG_CFG_RF2)
3709 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3710 if (ap->rxconfig & ANEG_CFG_NP)
3711 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3712
3713 ap->link_time = ap->cur_time;
3714
3715 ap->flags ^= (MR_TOGGLE_TX);
3716 if (ap->rxconfig & 0x0008)
3717 ap->flags |= MR_TOGGLE_RX;
3718 if (ap->rxconfig & ANEG_CFG_NP)
3719 ap->flags |= MR_NP_RX;
3720 ap->flags |= MR_PAGE_RX;
3721
3722 ap->state = ANEG_STATE_COMPLETE_ACK;
3723 ret = ANEG_TIMER_ENAB;
3724 break;
3725
3726 case ANEG_STATE_COMPLETE_ACK:
3727 if (ap->ability_match != 0 &&
3728 ap->rxconfig == 0) {
3729 ap->state = ANEG_STATE_AN_ENABLE;
3730 break;
3731 }
3732 delta = ap->cur_time - ap->link_time;
3733 if (delta > ANEG_STATE_SETTLE_TIME) {
3734 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3735 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3736 } else {
3737 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3738 !(ap->flags & MR_NP_RX)) {
3739 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3740 } else {
3741 ret = ANEG_FAILED;
3742 }
3743 }
3744 }
3745 break;
3746
3747 case ANEG_STATE_IDLE_DETECT_INIT:
3748 ap->link_time = ap->cur_time;
3749 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3750 tw32_f(MAC_MODE, tp->mac_mode);
3751 udelay(40);
3752
3753 ap->state = ANEG_STATE_IDLE_DETECT;
3754 ret = ANEG_TIMER_ENAB;
3755 break;
3756
3757 case ANEG_STATE_IDLE_DETECT:
3758 if (ap->ability_match != 0 &&
3759 ap->rxconfig == 0) {
3760 ap->state = ANEG_STATE_AN_ENABLE;
3761 break;
3762 }
3763 delta = ap->cur_time - ap->link_time;
3764 if (delta > ANEG_STATE_SETTLE_TIME) {
3765 /* XXX another gem from the Broadcom driver :( */
3766 ap->state = ANEG_STATE_LINK_OK;
3767 }
3768 break;
3769
3770 case ANEG_STATE_LINK_OK:
3771 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3772 ret = ANEG_DONE;
3773 break;
3774
3775 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3776 /* ??? unimplemented */
3777 break;
3778
3779 case ANEG_STATE_NEXT_PAGE_WAIT:
3780 /* ??? unimplemented */
3781 break;
3782
3783 default:
3784 ret = ANEG_FAILED;
3785 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
3788 return ret;
3789}
3790
Matt Carlson5be73b42007-12-20 20:09:29 -08003791static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792{
3793 int res = 0;
3794 struct tg3_fiber_aneginfo aninfo;
3795 int status = ANEG_FAILED;
3796 unsigned int tick;
3797 u32 tmp;
3798
3799 tw32_f(MAC_TX_AUTO_NEG, 0);
3800
3801 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3802 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3803 udelay(40);
3804
3805 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3806 udelay(40);
3807
3808 memset(&aninfo, 0, sizeof(aninfo));
3809 aninfo.flags |= MR_AN_ENABLE;
3810 aninfo.state = ANEG_STATE_UNKNOWN;
3811 aninfo.cur_time = 0;
3812 tick = 0;
3813 while (++tick < 195000) {
3814 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3815 if (status == ANEG_DONE || status == ANEG_FAILED)
3816 break;
3817
3818 udelay(1);
3819 }
3820
3821 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3822 tw32_f(MAC_MODE, tp->mac_mode);
3823 udelay(40);
3824
Matt Carlson5be73b42007-12-20 20:09:29 -08003825 *txflags = aninfo.txconfig;
3826 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
3828 if (status == ANEG_DONE &&
3829 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3830 MR_LP_ADV_FULL_DUPLEX)))
3831 res = 1;
3832
3833 return res;
3834}
3835
3836static void tg3_init_bcm8002(struct tg3 *tp)
3837{
3838 u32 mac_status = tr32(MAC_STATUS);
3839 int i;
3840
3841 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00003842 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 !(mac_status & MAC_STATUS_PCS_SYNCED))
3844 return;
3845
3846 /* Set PLL lock range. */
3847 tg3_writephy(tp, 0x16, 0x8007);
3848
3849 /* SW reset */
3850 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3851
3852 /* Wait for reset to complete. */
3853 /* XXX schedule_timeout() ... */
3854 for (i = 0; i < 500; i++)
3855 udelay(10);
3856
3857 /* Config mode; select PMA/Ch 1 regs. */
3858 tg3_writephy(tp, 0x10, 0x8411);
3859
3860 /* Enable auto-lock and comdet, select txclk for tx. */
3861 tg3_writephy(tp, 0x11, 0x0a10);
3862
3863 tg3_writephy(tp, 0x18, 0x00a0);
3864 tg3_writephy(tp, 0x16, 0x41ff);
3865
3866 /* Assert and deassert POR. */
3867 tg3_writephy(tp, 0x13, 0x0400);
3868 udelay(40);
3869 tg3_writephy(tp, 0x13, 0x0000);
3870
3871 tg3_writephy(tp, 0x11, 0x0a50);
3872 udelay(40);
3873 tg3_writephy(tp, 0x11, 0x0a10);
3874
3875 /* Wait for signal to stabilize */
3876 /* XXX schedule_timeout() ... */
3877 for (i = 0; i < 15000; i++)
3878 udelay(10);
3879
3880 /* Deselect the channel register so we can read the PHYID
3881 * later.
3882 */
3883 tg3_writephy(tp, 0x10, 0x8011);
3884}
3885
3886static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3887{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003888 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 u32 sg_dig_ctrl, sg_dig_status;
3890 u32 serdes_cfg, expected_sg_dig_ctrl;
3891 int workaround, port_a;
3892 int current_link_up;
3893
3894 serdes_cfg = 0;
3895 expected_sg_dig_ctrl = 0;
3896 workaround = 0;
3897 port_a = 1;
3898 current_link_up = 0;
3899
3900 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3901 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3902 workaround = 1;
3903 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3904 port_a = 0;
3905
3906 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3907 /* preserve bits 20-23 for voltage regulator */
3908 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3909 }
3910
3911 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3912
3913 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003914 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 if (workaround) {
3916 u32 val = serdes_cfg;
3917
3918 if (port_a)
3919 val |= 0xc010000;
3920 else
3921 val |= 0x4010000;
3922 tw32_f(MAC_SERDES_CFG, val);
3923 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003924
3925 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 }
3927 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3928 tg3_setup_flow_control(tp, 0, 0);
3929 current_link_up = 1;
3930 }
3931 goto out;
3932 }
3933
3934 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003935 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Matt Carlson82cd3d12007-12-20 20:09:00 -08003937 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3938 if (flowctrl & ADVERTISE_1000XPAUSE)
3939 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3940 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3941 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
3943 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003944 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003945 tp->serdes_counter &&
3946 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3947 MAC_STATUS_RCVD_CFG)) ==
3948 MAC_STATUS_PCS_SYNCED)) {
3949 tp->serdes_counter--;
3950 current_link_up = 1;
3951 goto out;
3952 }
3953restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 if (workaround)
3955 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003956 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 udelay(5);
3958 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3959
Michael Chan3d3ebe72006-09-27 15:59:15 -07003960 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003961 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3963 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003964 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 mac_status = tr32(MAC_STATUS);
3966
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003967 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003969 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
Matt Carlson82cd3d12007-12-20 20:09:00 -08003971 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3972 local_adv |= ADVERTISE_1000XPAUSE;
3973 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3974 local_adv |= ADVERTISE_1000XPSE_ASYM;
3975
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003976 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003977 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003978 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003979 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
3981 tg3_setup_flow_control(tp, local_adv, remote_adv);
3982 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003983 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003984 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003985 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003986 if (tp->serdes_counter)
3987 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 else {
3989 if (workaround) {
3990 u32 val = serdes_cfg;
3991
3992 if (port_a)
3993 val |= 0xc010000;
3994 else
3995 val |= 0x4010000;
3996
3997 tw32_f(MAC_SERDES_CFG, val);
3998 }
3999
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004000 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 udelay(40);
4002
4003 /* Link parallel detection - link is up */
4004 /* only if we have PCS_SYNC and not */
4005 /* receiving config code words */
4006 mac_status = tr32(MAC_STATUS);
4007 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4008 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4009 tg3_setup_flow_control(tp, 0, 0);
4010 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004011 tp->phy_flags |=
4012 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004013 tp->serdes_counter =
4014 SERDES_PARALLEL_DET_TIMEOUT;
4015 } else
4016 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 }
4018 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004019 } else {
4020 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004021 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 }
4023
4024out:
4025 return current_link_up;
4026}
4027
4028static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4029{
4030 int current_link_up = 0;
4031
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004032 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
4035 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004036 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004038
Matt Carlson5be73b42007-12-20 20:09:29 -08004039 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4040 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Matt Carlson5be73b42007-12-20 20:09:29 -08004042 if (txflags & ANEG_CFG_PS1)
4043 local_adv |= ADVERTISE_1000XPAUSE;
4044 if (txflags & ANEG_CFG_PS2)
4045 local_adv |= ADVERTISE_1000XPSE_ASYM;
4046
4047 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4048 remote_adv |= LPA_1000XPAUSE;
4049 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4050 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
4052 tg3_setup_flow_control(tp, local_adv, remote_adv);
4053
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 current_link_up = 1;
4055 }
4056 for (i = 0; i < 30; i++) {
4057 udelay(20);
4058 tw32_f(MAC_STATUS,
4059 (MAC_STATUS_SYNC_CHANGED |
4060 MAC_STATUS_CFG_CHANGED));
4061 udelay(40);
4062 if ((tr32(MAC_STATUS) &
4063 (MAC_STATUS_SYNC_CHANGED |
4064 MAC_STATUS_CFG_CHANGED)) == 0)
4065 break;
4066 }
4067
4068 mac_status = tr32(MAC_STATUS);
4069 if (current_link_up == 0 &&
4070 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4071 !(mac_status & MAC_STATUS_RCVD_CFG))
4072 current_link_up = 1;
4073 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004074 tg3_setup_flow_control(tp, 0, 0);
4075
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 /* Forcing 1000FD link up. */
4077 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
4079 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4080 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004081
4082 tw32_f(MAC_MODE, tp->mac_mode);
4083 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 }
4085
4086out:
4087 return current_link_up;
4088}
4089
4090static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4091{
4092 u32 orig_pause_cfg;
4093 u16 orig_active_speed;
4094 u8 orig_active_duplex;
4095 u32 mac_status;
4096 int current_link_up;
4097 int i;
4098
Matt Carlson8d018622007-12-20 20:05:44 -08004099 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 orig_active_speed = tp->link_config.active_speed;
4101 orig_active_duplex = tp->link_config.active_duplex;
4102
Joe Perches63c3a662011-04-26 08:12:10 +00004103 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004105 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 mac_status = tr32(MAC_STATUS);
4107 mac_status &= (MAC_STATUS_PCS_SYNCED |
4108 MAC_STATUS_SIGNAL_DET |
4109 MAC_STATUS_CFG_CHANGED |
4110 MAC_STATUS_RCVD_CFG);
4111 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4112 MAC_STATUS_SIGNAL_DET)) {
4113 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4114 MAC_STATUS_CFG_CHANGED));
4115 return 0;
4116 }
4117 }
4118
4119 tw32_f(MAC_TX_AUTO_NEG, 0);
4120
4121 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4122 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4123 tw32_f(MAC_MODE, tp->mac_mode);
4124 udelay(40);
4125
Matt Carlson79eb6902010-02-17 15:17:03 +00004126 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 tg3_init_bcm8002(tp);
4128
4129 /* Enable link change event even when serdes polling. */
4130 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4131 udelay(40);
4132
4133 current_link_up = 0;
4134 mac_status = tr32(MAC_STATUS);
4135
Joe Perches63c3a662011-04-26 08:12:10 +00004136 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4138 else
4139 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4140
Matt Carlson898a56f2009-08-28 14:02:40 +00004141 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004143 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
4145 for (i = 0; i < 100; i++) {
4146 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4147 MAC_STATUS_CFG_CHANGED));
4148 udelay(5);
4149 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004150 MAC_STATUS_CFG_CHANGED |
4151 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 break;
4153 }
4154
4155 mac_status = tr32(MAC_STATUS);
4156 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4157 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004158 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4159 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 tw32_f(MAC_MODE, (tp->mac_mode |
4161 MAC_MODE_SEND_CONFIGS));
4162 udelay(1);
4163 tw32_f(MAC_MODE, tp->mac_mode);
4164 }
4165 }
4166
4167 if (current_link_up == 1) {
4168 tp->link_config.active_speed = SPEED_1000;
4169 tp->link_config.active_duplex = DUPLEX_FULL;
4170 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4171 LED_CTRL_LNKLED_OVERRIDE |
4172 LED_CTRL_1000MBPS_ON));
4173 } else {
4174 tp->link_config.active_speed = SPEED_INVALID;
4175 tp->link_config.active_duplex = DUPLEX_INVALID;
4176 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4177 LED_CTRL_LNKLED_OVERRIDE |
4178 LED_CTRL_TRAFFIC_OVERRIDE));
4179 }
4180
4181 if (current_link_up != netif_carrier_ok(tp->dev)) {
4182 if (current_link_up)
4183 netif_carrier_on(tp->dev);
4184 else
4185 netif_carrier_off(tp->dev);
4186 tg3_link_report(tp);
4187 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004188 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 if (orig_pause_cfg != now_pause_cfg ||
4190 orig_active_speed != tp->link_config.active_speed ||
4191 orig_active_duplex != tp->link_config.active_duplex)
4192 tg3_link_report(tp);
4193 }
4194
4195 return 0;
4196}
4197
Michael Chan747e8f82005-07-25 12:33:22 -07004198static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4199{
4200 int current_link_up, err = 0;
4201 u32 bmsr, bmcr;
4202 u16 current_speed;
4203 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004204 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004205
4206 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4207 tw32_f(MAC_MODE, tp->mac_mode);
4208 udelay(40);
4209
4210 tw32(MAC_EVENT, 0);
4211
4212 tw32_f(MAC_STATUS,
4213 (MAC_STATUS_SYNC_CHANGED |
4214 MAC_STATUS_CFG_CHANGED |
4215 MAC_STATUS_MI_COMPLETION |
4216 MAC_STATUS_LNKSTATE_CHANGED));
4217 udelay(40);
4218
4219 if (force_reset)
4220 tg3_phy_reset(tp);
4221
4222 current_link_up = 0;
4223 current_speed = SPEED_INVALID;
4224 current_duplex = DUPLEX_INVALID;
4225
4226 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4227 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004228 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4229 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4230 bmsr |= BMSR_LSTATUS;
4231 else
4232 bmsr &= ~BMSR_LSTATUS;
4233 }
Michael Chan747e8f82005-07-25 12:33:22 -07004234
4235 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4236
4237 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004238 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004239 /* do nothing, just check for link up at the end */
4240 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4241 u32 adv, new_adv;
4242
4243 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4244 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4245 ADVERTISE_1000XPAUSE |
4246 ADVERTISE_1000XPSE_ASYM |
4247 ADVERTISE_SLCT);
4248
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004249 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004250
4251 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4252 new_adv |= ADVERTISE_1000XHALF;
4253 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4254 new_adv |= ADVERTISE_1000XFULL;
4255
4256 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4257 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4258 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4259 tg3_writephy(tp, MII_BMCR, bmcr);
4260
4261 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004262 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004263 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004264
4265 return err;
4266 }
4267 } else {
4268 u32 new_bmcr;
4269
4270 bmcr &= ~BMCR_SPEED1000;
4271 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4272
4273 if (tp->link_config.duplex == DUPLEX_FULL)
4274 new_bmcr |= BMCR_FULLDPLX;
4275
4276 if (new_bmcr != bmcr) {
4277 /* BMCR_SPEED1000 is a reserved bit that needs
4278 * to be set on write.
4279 */
4280 new_bmcr |= BMCR_SPEED1000;
4281
4282 /* Force a linkdown */
4283 if (netif_carrier_ok(tp->dev)) {
4284 u32 adv;
4285
4286 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4287 adv &= ~(ADVERTISE_1000XFULL |
4288 ADVERTISE_1000XHALF |
4289 ADVERTISE_SLCT);
4290 tg3_writephy(tp, MII_ADVERTISE, adv);
4291 tg3_writephy(tp, MII_BMCR, bmcr |
4292 BMCR_ANRESTART |
4293 BMCR_ANENABLE);
4294 udelay(10);
4295 netif_carrier_off(tp->dev);
4296 }
4297 tg3_writephy(tp, MII_BMCR, new_bmcr);
4298 bmcr = new_bmcr;
4299 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4300 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004301 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4302 ASIC_REV_5714) {
4303 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4304 bmsr |= BMSR_LSTATUS;
4305 else
4306 bmsr &= ~BMSR_LSTATUS;
4307 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004308 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004309 }
4310 }
4311
4312 if (bmsr & BMSR_LSTATUS) {
4313 current_speed = SPEED_1000;
4314 current_link_up = 1;
4315 if (bmcr & BMCR_FULLDPLX)
4316 current_duplex = DUPLEX_FULL;
4317 else
4318 current_duplex = DUPLEX_HALF;
4319
Matt Carlsonef167e22007-12-20 20:10:01 -08004320 local_adv = 0;
4321 remote_adv = 0;
4322
Michael Chan747e8f82005-07-25 12:33:22 -07004323 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004324 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004325
4326 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4327 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4328 common = local_adv & remote_adv;
4329 if (common & (ADVERTISE_1000XHALF |
4330 ADVERTISE_1000XFULL)) {
4331 if (common & ADVERTISE_1000XFULL)
4332 current_duplex = DUPLEX_FULL;
4333 else
4334 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004335 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004336 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004337 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004338 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004339 }
Michael Chan747e8f82005-07-25 12:33:22 -07004340 }
4341 }
4342
Matt Carlsonef167e22007-12-20 20:10:01 -08004343 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4344 tg3_setup_flow_control(tp, local_adv, remote_adv);
4345
Michael Chan747e8f82005-07-25 12:33:22 -07004346 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4347 if (tp->link_config.active_duplex == DUPLEX_HALF)
4348 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4349
4350 tw32_f(MAC_MODE, tp->mac_mode);
4351 udelay(40);
4352
4353 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4354
4355 tp->link_config.active_speed = current_speed;
4356 tp->link_config.active_duplex = current_duplex;
4357
4358 if (current_link_up != netif_carrier_ok(tp->dev)) {
4359 if (current_link_up)
4360 netif_carrier_on(tp->dev);
4361 else {
4362 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004363 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004364 }
4365 tg3_link_report(tp);
4366 }
4367 return err;
4368}
4369
4370static void tg3_serdes_parallel_detect(struct tg3 *tp)
4371{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004372 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004373 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004374 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004375 return;
4376 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004377
Michael Chan747e8f82005-07-25 12:33:22 -07004378 if (!netif_carrier_ok(tp->dev) &&
4379 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4380 u32 bmcr;
4381
4382 tg3_readphy(tp, MII_BMCR, &bmcr);
4383 if (bmcr & BMCR_ANENABLE) {
4384 u32 phy1, phy2;
4385
4386 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004387 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4388 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004389
4390 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004391 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4392 MII_TG3_DSP_EXP1_INT_STAT);
4393 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4394 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004395
4396 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4397 /* We have signal detect and not receiving
4398 * config code words, link is up by parallel
4399 * detection.
4400 */
4401
4402 bmcr &= ~BMCR_ANENABLE;
4403 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4404 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004405 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004406 }
4407 }
Matt Carlson859a588792010-04-05 10:19:28 +00004408 } else if (netif_carrier_ok(tp->dev) &&
4409 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004410 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004411 u32 phy2;
4412
4413 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004414 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4415 MII_TG3_DSP_EXP1_INT_STAT);
4416 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004417 if (phy2 & 0x20) {
4418 u32 bmcr;
4419
4420 /* Config code words received, turn on autoneg. */
4421 tg3_readphy(tp, MII_BMCR, &bmcr);
4422 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4423
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004424 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004425
4426 }
4427 }
4428}
4429
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4431{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004432 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 int err;
4434
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004435 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004437 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004438 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004439 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004442 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004443 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004444
4445 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4446 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4447 scale = 65;
4448 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4449 scale = 6;
4450 else
4451 scale = 12;
4452
4453 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4454 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4455 tw32(GRC_MISC_CFG, val);
4456 }
4457
Matt Carlsonf2096f92011-04-05 14:22:48 +00004458 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4459 (6 << TX_LENGTHS_IPG_SHIFT);
4460 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4461 val |= tr32(MAC_TX_LENGTHS) &
4462 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4463 TX_LENGTHS_CNT_DWN_VAL_MSK);
4464
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 if (tp->link_config.active_speed == SPEED_1000 &&
4466 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004467 tw32(MAC_TX_LENGTHS, val |
4468 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004470 tw32(MAC_TX_LENGTHS, val |
4471 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Joe Perches63c3a662011-04-26 08:12:10 +00004473 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 if (netif_carrier_ok(tp->dev)) {
4475 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004476 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 } else {
4478 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4479 }
4480 }
4481
Joe Perches63c3a662011-04-26 08:12:10 +00004482 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004483 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004484 if (!netif_carrier_ok(tp->dev))
4485 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4486 tp->pwrmgmt_thresh;
4487 else
4488 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4489 tw32(PCIE_PWR_MGMT_THRESH, val);
4490 }
4491
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 return err;
4493}
4494
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004495static inline int tg3_irq_sync(struct tg3 *tp)
4496{
4497 return tp->irq_sync;
4498}
4499
Matt Carlson97bd8e42011-04-13 11:05:04 +00004500static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4501{
4502 int i;
4503
4504 dst = (u32 *)((u8 *)dst + off);
4505 for (i = 0; i < len; i += sizeof(u32))
4506 *dst++ = tr32(off + i);
4507}
4508
4509static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4510{
4511 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4512 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4513 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4514 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4515 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4516 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4517 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4518 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4519 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4520 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4521 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4522 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4523 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4524 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4525 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4526 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4527 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4528 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4529 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4530
Joe Perches63c3a662011-04-26 08:12:10 +00004531 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004532 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4533
4534 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4535 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4536 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4537 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4538 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4539 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4540 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4541 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4542
Joe Perches63c3a662011-04-26 08:12:10 +00004543 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004544 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4545 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4546 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4547 }
4548
4549 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4550 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4551 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4552 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4553 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4554
Joe Perches63c3a662011-04-26 08:12:10 +00004555 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004556 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4557}
4558
4559static void tg3_dump_state(struct tg3 *tp)
4560{
4561 int i;
4562 u32 *regs;
4563
4564 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4565 if (!regs) {
4566 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4567 return;
4568 }
4569
Joe Perches63c3a662011-04-26 08:12:10 +00004570 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004571 /* Read up to but not including private PCI registers */
4572 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4573 regs[i / sizeof(u32)] = tr32(i);
4574 } else
4575 tg3_dump_legacy_regs(tp, regs);
4576
4577 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4578 if (!regs[i + 0] && !regs[i + 1] &&
4579 !regs[i + 2] && !regs[i + 3])
4580 continue;
4581
4582 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4583 i * 4,
4584 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4585 }
4586
4587 kfree(regs);
4588
4589 for (i = 0; i < tp->irq_cnt; i++) {
4590 struct tg3_napi *tnapi = &tp->napi[i];
4591
4592 /* SW status block */
4593 netdev_err(tp->dev,
4594 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4595 i,
4596 tnapi->hw_status->status,
4597 tnapi->hw_status->status_tag,
4598 tnapi->hw_status->rx_jumbo_consumer,
4599 tnapi->hw_status->rx_consumer,
4600 tnapi->hw_status->rx_mini_consumer,
4601 tnapi->hw_status->idx[0].rx_producer,
4602 tnapi->hw_status->idx[0].tx_consumer);
4603
4604 netdev_err(tp->dev,
4605 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4606 i,
4607 tnapi->last_tag, tnapi->last_irq_tag,
4608 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4609 tnapi->rx_rcb_ptr,
4610 tnapi->prodring.rx_std_prod_idx,
4611 tnapi->prodring.rx_std_cons_idx,
4612 tnapi->prodring.rx_jmb_prod_idx,
4613 tnapi->prodring.rx_jmb_cons_idx);
4614 }
4615}
4616
Michael Chandf3e6542006-05-26 17:48:07 -07004617/* This is called whenever we suspect that the system chipset is re-
4618 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4619 * is bogus tx completions. We try to recover by setting the
4620 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4621 * in the workqueue.
4622 */
4623static void tg3_tx_recover(struct tg3 *tp)
4624{
Joe Perches63c3a662011-04-26 08:12:10 +00004625 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07004626 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4627
Matt Carlson5129c3a2010-04-05 10:19:23 +00004628 netdev_warn(tp->dev,
4629 "The system may be re-ordering memory-mapped I/O "
4630 "cycles to the network device, attempting to recover. "
4631 "Please report the problem to the driver maintainer "
4632 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004633
4634 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00004635 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07004636 spin_unlock(&tp->lock);
4637}
4638
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004639static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004640{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004641 /* Tell compiler to fetch tx indices from memory. */
4642 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004643 return tnapi->tx_pending -
4644 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004645}
4646
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647/* Tigon3 never reports partial packet sends. So we do not
4648 * need special logic to handle SKBs that have not had all
4649 * of their frags sent yet, like SunGEM does.
4650 */
Matt Carlson17375d22009-08-28 14:02:18 +00004651static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652{
Matt Carlson17375d22009-08-28 14:02:18 +00004653 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004654 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004655 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004656 struct netdev_queue *txq;
4657 int index = tnapi - tp->napi;
4658
Joe Perches63c3a662011-04-26 08:12:10 +00004659 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004660 index--;
4661
4662 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
4664 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004665 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004667 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
Michael Chandf3e6542006-05-26 17:48:07 -07004669 if (unlikely(skb == NULL)) {
4670 tg3_tx_recover(tp);
4671 return;
4672 }
4673
Alexander Duyckf4188d82009-12-02 16:48:38 +00004674 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004675 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004676 skb_headlen(skb),
4677 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
4679 ri->skb = NULL;
4680
4681 sw_idx = NEXT_TX(sw_idx);
4682
4683 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004684 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004685 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4686 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004687
4688 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004689 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004690 skb_shinfo(skb)->frags[i].size,
4691 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 sw_idx = NEXT_TX(sw_idx);
4693 }
4694
David S. Millerf47c11e2005-06-24 20:18:35 -07004695 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004696
4697 if (unlikely(tx_bug)) {
4698 tg3_tx_recover(tp);
4699 return;
4700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 }
4702
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004703 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
Michael Chan1b2a7202006-08-07 21:46:02 -07004705 /* Need to make the tx_cons update visible to tg3_start_xmit()
4706 * before checking for netif_queue_stopped(). Without the
4707 * memory barrier, there is a small possibility that tg3_start_xmit()
4708 * will miss it and cause the queue to be stopped forever.
4709 */
4710 smp_mb();
4711
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004712 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004713 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004714 __netif_tx_lock(txq, smp_processor_id());
4715 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004716 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004717 netif_tx_wake_queue(txq);
4718 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720}
4721
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004722static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4723{
4724 if (!ri->skb)
4725 return;
4726
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004727 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004728 map_sz, PCI_DMA_FROMDEVICE);
4729 dev_kfree_skb_any(ri->skb);
4730 ri->skb = NULL;
4731}
4732
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733/* Returns size of skb allocated or < 0 on error.
4734 *
4735 * We only need to fill in the address because the other members
4736 * of the RX descriptor are invariant, see tg3_init_rings.
4737 *
4738 * Note the purposeful assymetry of cpu vs. chip accesses. For
4739 * posting buffers we only dirty the first cache line of the RX
4740 * descriptor (containing the address). Whereas for the RX status
4741 * buffers the cpu only reads the last cacheline of the RX descriptor
4742 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4743 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004744static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004745 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746{
4747 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004748 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 struct sk_buff *skb;
4750 dma_addr_t mapping;
4751 int skb_size, dest_idx;
4752
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 switch (opaque_key) {
4754 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004755 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004756 desc = &tpr->rx_std[dest_idx];
4757 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004758 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 break;
4760
4761 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004762 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004763 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004764 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004765 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 break;
4767
4768 default:
4769 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
4772 /* Do not overwrite any of the map or rp information
4773 * until we are sure we can commit to a new buffer.
4774 *
4775 * Callers depend upon this behavior and assume that
4776 * we leave everything unchanged if we fail.
4777 */
Matt Carlson287be122009-08-28 13:58:46 +00004778 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 if (skb == NULL)
4780 return -ENOMEM;
4781
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 skb_reserve(skb, tp->rx_offset);
4783
Matt Carlson287be122009-08-28 13:58:46 +00004784 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004786 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4787 dev_kfree_skb(skb);
4788 return -EIO;
4789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
4791 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004792 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 desc->addr_hi = ((u64)mapping >> 32);
4795 desc->addr_lo = ((u64)mapping & 0xffffffff);
4796
4797 return skb_size;
4798}
4799
4800/* We only need to move over in the address because the other
4801 * members of the RX descriptor are invariant. See notes above
4802 * tg3_alloc_rx_skb for full details.
4803 */
Matt Carlsona3896162009-11-13 13:03:44 +00004804static void tg3_recycle_rx(struct tg3_napi *tnapi,
4805 struct tg3_rx_prodring_set *dpr,
4806 u32 opaque_key, int src_idx,
4807 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808{
Matt Carlson17375d22009-08-28 14:02:18 +00004809 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4811 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004812 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004813 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
4815 switch (opaque_key) {
4816 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004817 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004818 dest_desc = &dpr->rx_std[dest_idx];
4819 dest_map = &dpr->rx_std_buffers[dest_idx];
4820 src_desc = &spr->rx_std[src_idx];
4821 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 break;
4823
4824 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004825 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004826 dest_desc = &dpr->rx_jmb[dest_idx].std;
4827 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4828 src_desc = &spr->rx_jmb[src_idx].std;
4829 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 break;
4831
4832 default:
4833 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
4836 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004837 dma_unmap_addr_set(dest_map, mapping,
4838 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 dest_desc->addr_hi = src_desc->addr_hi;
4840 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004841
4842 /* Ensure that the update to the skb happens after the physical
4843 * addresses have been transferred to the new BD location.
4844 */
4845 smp_wmb();
4846
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 src_map->skb = NULL;
4848}
4849
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850/* The RX ring scheme is composed of multiple rings which post fresh
4851 * buffers to the chip, and one special ring the chip uses to report
4852 * status back to the host.
4853 *
4854 * The special ring reports the status of received packets to the
4855 * host. The chip does not write into the original descriptor the
4856 * RX buffer was obtained from. The chip simply takes the original
4857 * descriptor as provided by the host, updates the status and length
4858 * field, then writes this into the next status ring entry.
4859 *
4860 * Each ring the host uses to post buffers to the chip is described
4861 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4862 * it is first placed into the on-chip ram. When the packet's length
4863 * is known, it walks down the TG3_BDINFO entries to select the ring.
4864 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4865 * which is within the range of the new packet's length is chosen.
4866 *
4867 * The "separate ring for rx status" scheme may sound queer, but it makes
4868 * sense from a cache coherency perspective. If only the host writes
4869 * to the buffer post rings, and only the chip writes to the rx status
4870 * rings, then cache lines never move beyond shared-modified state.
4871 * If both the host and chip were to write into the same ring, cache line
4872 * eviction could occur since both entities want it in an exclusive state.
4873 */
Matt Carlson17375d22009-08-28 14:02:18 +00004874static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875{
Matt Carlson17375d22009-08-28 14:02:18 +00004876 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004877 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004878 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004879 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004880 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004882 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004884 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 /*
4886 * We need to order the read of hw_idx and the read of
4887 * the opaque cookie.
4888 */
4889 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 work_mask = 0;
4891 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004892 std_prod_idx = tpr->rx_std_prod_idx;
4893 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004895 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004896 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 unsigned int len;
4898 struct sk_buff *skb;
4899 dma_addr_t dma_addr;
4900 u32 opaque_key, desc_idx, *post_ptr;
4901
4902 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4903 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4904 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004905 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004906 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004907 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004908 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004909 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004911 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004912 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004913 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004914 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004915 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917
4918 work_mask |= opaque_key;
4919
4920 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4921 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4922 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004923 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 desc_idx, *post_ptr);
4925 drop_it_no_recycle:
4926 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004927 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 goto next_pkt;
4929 }
4930
Matt Carlsonad829262008-11-21 17:16:16 -08004931 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4932 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Matt Carlsond2757fc2010-04-12 06:58:27 +00004934 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 int skb_size;
4936
Matt Carlson86b21e52009-11-13 13:03:45 +00004937 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004938 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 if (skb_size < 0)
4940 goto drop_it;
4941
Matt Carlson287be122009-08-28 13:58:46 +00004942 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 PCI_DMA_FROMDEVICE);
4944
Matt Carlson61e800c2010-02-17 15:16:54 +00004945 /* Ensure that the update to the skb happens
4946 * after the usage of the old DMA mapping.
4947 */
4948 smp_wmb();
4949
4950 ri->skb = NULL;
4951
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 skb_put(skb, len);
4953 } else {
4954 struct sk_buff *copy_skb;
4955
Matt Carlsona3896162009-11-13 13:03:44 +00004956 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 desc_idx, *post_ptr);
4958
Matt Carlsonbf933c82011-01-25 15:58:49 +00004959 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00004960 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 if (copy_skb == NULL)
4962 goto drop_it_no_recycle;
4963
Matt Carlsonbf933c82011-01-25 15:58:49 +00004964 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 skb_put(copy_skb, len);
4966 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004967 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4969
4970 /* We'll reuse the original ring buffer. */
4971 skb = copy_skb;
4972 }
4973
Michał Mirosławdc668912011-04-07 03:35:07 +00004974 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4976 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4977 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4978 skb->ip_summed = CHECKSUM_UNNECESSARY;
4979 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004980 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
4982 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004983
4984 if (len > (tp->dev->mtu + ETH_HLEN) &&
4985 skb->protocol != htons(ETH_P_8021Q)) {
4986 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00004987 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004988 }
4989
Matt Carlson9dc7a112010-04-12 06:58:28 +00004990 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00004991 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4992 __vlan_hwaccel_put_tag(skb,
4993 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00004994
Matt Carlsonbf933c82011-01-25 15:58:49 +00004995 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 received++;
4998 budget--;
4999
5000next_pkt:
5001 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005002
5003 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005004 tpr->rx_std_prod_idx = std_prod_idx &
5005 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005006 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5007 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005008 work_mask &= ~RXD_OPAQUE_RING_STD;
5009 rx_std_posted = 0;
5010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005012 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005013 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005014
5015 /* Refresh hw_idx to see if there is new work */
5016 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005017 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005018 rmb();
5019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 }
5021
5022 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005023 tnapi->rx_rcb_ptr = sw_idx;
5024 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
5026 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005027 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005028 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005029 tpr->rx_std_prod_idx = std_prod_idx &
5030 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005031 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5032 tpr->rx_std_prod_idx);
5033 }
5034 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005035 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5036 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005037 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5038 tpr->rx_jmb_prod_idx);
5039 }
5040 mmiowb();
5041 } else if (work_mask) {
5042 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5043 * updated before the producer indices can be updated.
5044 */
5045 smp_wmb();
5046
Matt Carlson2c49a442010-09-30 10:34:35 +00005047 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5048 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005049
Matt Carlsone4af1af2010-02-12 14:47:05 +00005050 if (tnapi != &tp->napi[1])
5051 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
5054 return received;
5055}
5056
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005057static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005060 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005061 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5062
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 if (sblk->status & SD_STATUS_LINK_CHG) {
5064 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005065 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005066 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005067 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005068 tw32_f(MAC_STATUS,
5069 (MAC_STATUS_SYNC_CHANGED |
5070 MAC_STATUS_CFG_CHANGED |
5071 MAC_STATUS_MI_COMPLETION |
5072 MAC_STATUS_LNKSTATE_CHANGED));
5073 udelay(40);
5074 } else
5075 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005076 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 }
5078 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005079}
5080
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005081static int tg3_rx_prodring_xfer(struct tg3 *tp,
5082 struct tg3_rx_prodring_set *dpr,
5083 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005084{
5085 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005086 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005087
5088 while (1) {
5089 src_prod_idx = spr->rx_std_prod_idx;
5090
5091 /* Make sure updates to the rx_std_buffers[] entries and the
5092 * standard producer index are seen in the correct order.
5093 */
5094 smp_rmb();
5095
5096 if (spr->rx_std_cons_idx == src_prod_idx)
5097 break;
5098
5099 if (spr->rx_std_cons_idx < src_prod_idx)
5100 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5101 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005102 cpycnt = tp->rx_std_ring_mask + 1 -
5103 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005104
Matt Carlson2c49a442010-09-30 10:34:35 +00005105 cpycnt = min(cpycnt,
5106 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005107
5108 si = spr->rx_std_cons_idx;
5109 di = dpr->rx_std_prod_idx;
5110
Matt Carlsone92967b2010-02-12 14:47:06 +00005111 for (i = di; i < di + cpycnt; i++) {
5112 if (dpr->rx_std_buffers[i].skb) {
5113 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005114 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005115 break;
5116 }
5117 }
5118
5119 if (!cpycnt)
5120 break;
5121
5122 /* Ensure that updates to the rx_std_buffers ring and the
5123 * shadowed hardware producer ring from tg3_recycle_skb() are
5124 * ordered correctly WRT the skb check above.
5125 */
5126 smp_rmb();
5127
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005128 memcpy(&dpr->rx_std_buffers[di],
5129 &spr->rx_std_buffers[si],
5130 cpycnt * sizeof(struct ring_info));
5131
5132 for (i = 0; i < cpycnt; i++, di++, si++) {
5133 struct tg3_rx_buffer_desc *sbd, *dbd;
5134 sbd = &spr->rx_std[si];
5135 dbd = &dpr->rx_std[di];
5136 dbd->addr_hi = sbd->addr_hi;
5137 dbd->addr_lo = sbd->addr_lo;
5138 }
5139
Matt Carlson2c49a442010-09-30 10:34:35 +00005140 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5141 tp->rx_std_ring_mask;
5142 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5143 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005144 }
5145
5146 while (1) {
5147 src_prod_idx = spr->rx_jmb_prod_idx;
5148
5149 /* Make sure updates to the rx_jmb_buffers[] entries and
5150 * the jumbo producer index are seen in the correct order.
5151 */
5152 smp_rmb();
5153
5154 if (spr->rx_jmb_cons_idx == src_prod_idx)
5155 break;
5156
5157 if (spr->rx_jmb_cons_idx < src_prod_idx)
5158 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5159 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005160 cpycnt = tp->rx_jmb_ring_mask + 1 -
5161 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005162
5163 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005164 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005165
5166 si = spr->rx_jmb_cons_idx;
5167 di = dpr->rx_jmb_prod_idx;
5168
Matt Carlsone92967b2010-02-12 14:47:06 +00005169 for (i = di; i < di + cpycnt; i++) {
5170 if (dpr->rx_jmb_buffers[i].skb) {
5171 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005172 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005173 break;
5174 }
5175 }
5176
5177 if (!cpycnt)
5178 break;
5179
5180 /* Ensure that updates to the rx_jmb_buffers ring and the
5181 * shadowed hardware producer ring from tg3_recycle_skb() are
5182 * ordered correctly WRT the skb check above.
5183 */
5184 smp_rmb();
5185
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005186 memcpy(&dpr->rx_jmb_buffers[di],
5187 &spr->rx_jmb_buffers[si],
5188 cpycnt * sizeof(struct ring_info));
5189
5190 for (i = 0; i < cpycnt; i++, di++, si++) {
5191 struct tg3_rx_buffer_desc *sbd, *dbd;
5192 sbd = &spr->rx_jmb[si].std;
5193 dbd = &dpr->rx_jmb[di].std;
5194 dbd->addr_hi = sbd->addr_hi;
5195 dbd->addr_lo = sbd->addr_lo;
5196 }
5197
Matt Carlson2c49a442010-09-30 10:34:35 +00005198 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5199 tp->rx_jmb_ring_mask;
5200 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5201 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005202 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005203
5204 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005205}
5206
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005207static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5208{
5209 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210
5211 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005212 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005213 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005214 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005215 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 }
5217
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 /* run RX thread, within the bounds set by NAPI.
5219 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005220 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005222 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005223 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
Joe Perches63c3a662011-04-26 08:12:10 +00005225 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005226 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005227 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005228 u32 std_prod_idx = dpr->rx_std_prod_idx;
5229 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005230
Matt Carlsone4af1af2010-02-12 14:47:05 +00005231 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005232 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005233 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005234
5235 wmb();
5236
Matt Carlsone4af1af2010-02-12 14:47:05 +00005237 if (std_prod_idx != dpr->rx_std_prod_idx)
5238 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5239 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005240
Matt Carlsone4af1af2010-02-12 14:47:05 +00005241 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5242 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5243 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005244
5245 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005246
5247 if (err)
5248 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005249 }
5250
David S. Miller6f535762007-10-11 18:08:29 -07005251 return work_done;
5252}
David S. Millerf7383c22005-05-18 22:50:53 -07005253
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005254static int tg3_poll_msix(struct napi_struct *napi, int budget)
5255{
5256 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5257 struct tg3 *tp = tnapi->tp;
5258 int work_done = 0;
5259 struct tg3_hw_status *sblk = tnapi->hw_status;
5260
5261 while (1) {
5262 work_done = tg3_poll_work(tnapi, work_done, budget);
5263
Joe Perches63c3a662011-04-26 08:12:10 +00005264 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005265 goto tx_recovery;
5266
5267 if (unlikely(work_done >= budget))
5268 break;
5269
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005270 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005271 * to tell the hw how much work has been processed,
5272 * so we must read it before checking for more work.
5273 */
5274 tnapi->last_tag = sblk->status_tag;
5275 tnapi->last_irq_tag = tnapi->last_tag;
5276 rmb();
5277
5278 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005279 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5280 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005281 napi_complete(napi);
5282 /* Reenable interrupts. */
5283 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5284 mmiowb();
5285 break;
5286 }
5287 }
5288
5289 return work_done;
5290
5291tx_recovery:
5292 /* work_done is guaranteed to be less than budget. */
5293 napi_complete(napi);
5294 schedule_work(&tp->reset_task);
5295 return work_done;
5296}
5297
Matt Carlsone64de4e2011-04-13 11:05:05 +00005298static void tg3_process_error(struct tg3 *tp)
5299{
5300 u32 val;
5301 bool real_error = false;
5302
Joe Perches63c3a662011-04-26 08:12:10 +00005303 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005304 return;
5305
5306 /* Check Flow Attention register */
5307 val = tr32(HOSTCC_FLOW_ATTN);
5308 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5309 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5310 real_error = true;
5311 }
5312
5313 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5314 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5315 real_error = true;
5316 }
5317
5318 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5319 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5320 real_error = true;
5321 }
5322
5323 if (!real_error)
5324 return;
5325
5326 tg3_dump_state(tp);
5327
Joe Perches63c3a662011-04-26 08:12:10 +00005328 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005329 schedule_work(&tp->reset_task);
5330}
5331
David S. Miller6f535762007-10-11 18:08:29 -07005332static int tg3_poll(struct napi_struct *napi, int budget)
5333{
Matt Carlson8ef04422009-08-28 14:01:37 +00005334 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5335 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005336 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005337 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005338
5339 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005340 if (sblk->status & SD_STATUS_ERROR)
5341 tg3_process_error(tp);
5342
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005343 tg3_poll_link(tp);
5344
Matt Carlson17375d22009-08-28 14:02:18 +00005345 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005346
Joe Perches63c3a662011-04-26 08:12:10 +00005347 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07005348 goto tx_recovery;
5349
5350 if (unlikely(work_done >= budget))
5351 break;
5352
Joe Perches63c3a662011-04-26 08:12:10 +00005353 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00005354 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005355 * to tell the hw how much work has been processed,
5356 * so we must read it before checking for more work.
5357 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005358 tnapi->last_tag = sblk->status_tag;
5359 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005360 rmb();
5361 } else
5362 sblk->status &= ~SD_STATUS_UPDATED;
5363
Matt Carlson17375d22009-08-28 14:02:18 +00005364 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005365 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005366 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005367 break;
5368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 }
5370
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005371 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005372
5373tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005374 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005375 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005376 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005377 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378}
5379
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005380static void tg3_napi_disable(struct tg3 *tp)
5381{
5382 int i;
5383
5384 for (i = tp->irq_cnt - 1; i >= 0; i--)
5385 napi_disable(&tp->napi[i].napi);
5386}
5387
5388static void tg3_napi_enable(struct tg3 *tp)
5389{
5390 int i;
5391
5392 for (i = 0; i < tp->irq_cnt; i++)
5393 napi_enable(&tp->napi[i].napi);
5394}
5395
5396static void tg3_napi_init(struct tg3 *tp)
5397{
5398 int i;
5399
5400 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5401 for (i = 1; i < tp->irq_cnt; i++)
5402 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5403}
5404
5405static void tg3_napi_fini(struct tg3 *tp)
5406{
5407 int i;
5408
5409 for (i = 0; i < tp->irq_cnt; i++)
5410 netif_napi_del(&tp->napi[i].napi);
5411}
5412
5413static inline void tg3_netif_stop(struct tg3 *tp)
5414{
5415 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5416 tg3_napi_disable(tp);
5417 netif_tx_disable(tp->dev);
5418}
5419
5420static inline void tg3_netif_start(struct tg3 *tp)
5421{
5422 /* NOTE: unconditional netif_tx_wake_all_queues is only
5423 * appropriate so long as all callers are assured to
5424 * have free tx slots (such as after tg3_init_hw)
5425 */
5426 netif_tx_wake_all_queues(tp->dev);
5427
5428 tg3_napi_enable(tp);
5429 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5430 tg3_enable_ints(tp);
5431}
5432
David S. Millerf47c11e2005-06-24 20:18:35 -07005433static void tg3_irq_quiesce(struct tg3 *tp)
5434{
Matt Carlson4f125f42009-09-01 12:55:02 +00005435 int i;
5436
David S. Millerf47c11e2005-06-24 20:18:35 -07005437 BUG_ON(tp->irq_sync);
5438
5439 tp->irq_sync = 1;
5440 smp_mb();
5441
Matt Carlson4f125f42009-09-01 12:55:02 +00005442 for (i = 0; i < tp->irq_cnt; i++)
5443 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005444}
5445
David S. Millerf47c11e2005-06-24 20:18:35 -07005446/* Fully shutdown all tg3 driver activity elsewhere in the system.
5447 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5448 * with as well. Most of the time, this is not necessary except when
5449 * shutting down the device.
5450 */
5451static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5452{
Michael Chan46966542007-07-11 19:47:19 -07005453 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005454 if (irq_sync)
5455 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005456}
5457
5458static inline void tg3_full_unlock(struct tg3 *tp)
5459{
David S. Millerf47c11e2005-06-24 20:18:35 -07005460 spin_unlock_bh(&tp->lock);
5461}
5462
Michael Chanfcfa0a32006-03-20 22:28:41 -08005463/* One-shot MSI handler - Chip automatically disables interrupt
5464 * after sending MSI so driver doesn't have to do it.
5465 */
David Howells7d12e782006-10-05 14:55:46 +01005466static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005467{
Matt Carlson09943a12009-08-28 14:01:57 +00005468 struct tg3_napi *tnapi = dev_id;
5469 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005470
Matt Carlson898a56f2009-08-28 14:02:40 +00005471 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005472 if (tnapi->rx_rcb)
5473 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005474
5475 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005476 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005477
5478 return IRQ_HANDLED;
5479}
5480
Michael Chan88b06bc22005-04-21 17:13:25 -07005481/* MSI ISR - No need to check for interrupt sharing and no need to
5482 * flush status block and interrupt mailbox. PCI ordering rules
5483 * guarantee that MSI will arrive after the status block.
5484 */
David Howells7d12e782006-10-05 14:55:46 +01005485static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005486{
Matt Carlson09943a12009-08-28 14:01:57 +00005487 struct tg3_napi *tnapi = dev_id;
5488 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005489
Matt Carlson898a56f2009-08-28 14:02:40 +00005490 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005491 if (tnapi->rx_rcb)
5492 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005493 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005494 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005495 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005496 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005497 * NIC to stop sending us irqs, engaging "in-intr-handler"
5498 * event coalescing.
5499 */
5500 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005501 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005502 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005503
Michael Chan88b06bc22005-04-21 17:13:25 -07005504 return IRQ_RETVAL(1);
5505}
5506
David Howells7d12e782006-10-05 14:55:46 +01005507static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508{
Matt Carlson09943a12009-08-28 14:01:57 +00005509 struct tg3_napi *tnapi = dev_id;
5510 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005511 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 unsigned int handled = 1;
5513
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 /* In INTx mode, it is possible for the interrupt to arrive at
5515 * the CPU before the status block posted prior to the interrupt.
5516 * Reading the PCI State register will confirm whether the
5517 * interrupt is ours and will flush the status block.
5518 */
Michael Chand18edcb2007-03-24 20:57:11 -07005519 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00005520 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005521 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5522 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005523 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005524 }
Michael Chand18edcb2007-03-24 20:57:11 -07005525 }
5526
5527 /*
5528 * Writing any value to intr-mbox-0 clears PCI INTA# and
5529 * chip-internal interrupt pending events.
5530 * Writing non-zero to intr-mbox-0 additional tells the
5531 * NIC to stop sending us irqs, engaging "in-intr-handler"
5532 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005533 *
5534 * Flush the mailbox to de-assert the IRQ immediately to prevent
5535 * spurious interrupts. The flush impacts performance but
5536 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005537 */
Michael Chanc04cb342007-05-07 00:26:15 -07005538 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005539 if (tg3_irq_sync(tp))
5540 goto out;
5541 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005542 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005543 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005544 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005545 } else {
5546 /* No work, shared interrupt perhaps? re-enable
5547 * interrupts, and flush that PCI write
5548 */
5549 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5550 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005551 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005552out:
David S. Millerfac9b832005-05-18 22:46:34 -07005553 return IRQ_RETVAL(handled);
5554}
5555
David Howells7d12e782006-10-05 14:55:46 +01005556static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005557{
Matt Carlson09943a12009-08-28 14:01:57 +00005558 struct tg3_napi *tnapi = dev_id;
5559 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005560 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005561 unsigned int handled = 1;
5562
David S. Millerfac9b832005-05-18 22:46:34 -07005563 /* In INTx mode, it is possible for the interrupt to arrive at
5564 * the CPU before the status block posted prior to the interrupt.
5565 * Reading the PCI State register will confirm whether the
5566 * interrupt is ours and will flush the status block.
5567 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005568 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00005569 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005570 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5571 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005572 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 }
Michael Chand18edcb2007-03-24 20:57:11 -07005574 }
5575
5576 /*
5577 * writing any value to intr-mbox-0 clears PCI INTA# and
5578 * chip-internal interrupt pending events.
5579 * writing non-zero to intr-mbox-0 additional tells the
5580 * NIC to stop sending us irqs, engaging "in-intr-handler"
5581 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005582 *
5583 * Flush the mailbox to de-assert the IRQ immediately to prevent
5584 * spurious interrupts. The flush impacts performance but
5585 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005586 */
Michael Chanc04cb342007-05-07 00:26:15 -07005587 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005588
5589 /*
5590 * In a shared interrupt configuration, sometimes other devices'
5591 * interrupts will scream. We record the current status tag here
5592 * so that the above check can report that the screaming interrupts
5593 * are unhandled. Eventually they will be silenced.
5594 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005595 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005596
Michael Chand18edcb2007-03-24 20:57:11 -07005597 if (tg3_irq_sync(tp))
5598 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005599
Matt Carlson72334482009-08-28 14:03:01 +00005600 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005601
Matt Carlson09943a12009-08-28 14:01:57 +00005602 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005603
David S. Millerf47c11e2005-06-24 20:18:35 -07005604out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 return IRQ_RETVAL(handled);
5606}
5607
Michael Chan79381092005-04-21 17:13:59 -07005608/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005609static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005610{
Matt Carlson09943a12009-08-28 14:01:57 +00005611 struct tg3_napi *tnapi = dev_id;
5612 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005613 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005614
Michael Chanf9804dd2005-09-27 12:13:10 -07005615 if ((sblk->status & SD_STATUS_UPDATED) ||
5616 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005617 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005618 return IRQ_RETVAL(1);
5619 }
5620 return IRQ_RETVAL(0);
5621}
5622
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005623static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005624static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
Michael Chanb9ec6c12006-07-25 16:37:27 -07005626/* Restart hardware after configuration changes, self-test, etc.
5627 * Invoked with tp->lock held.
5628 */
5629static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005630 __releases(tp->lock)
5631 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005632{
5633 int err;
5634
5635 err = tg3_init_hw(tp, reset_phy);
5636 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005637 netdev_err(tp->dev,
5638 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005639 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5640 tg3_full_unlock(tp);
5641 del_timer_sync(&tp->timer);
5642 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005643 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005644 dev_close(tp->dev);
5645 tg3_full_lock(tp, 0);
5646 }
5647 return err;
5648}
5649
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650#ifdef CONFIG_NET_POLL_CONTROLLER
5651static void tg3_poll_controller(struct net_device *dev)
5652{
Matt Carlson4f125f42009-09-01 12:55:02 +00005653 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005654 struct tg3 *tp = netdev_priv(dev);
5655
Matt Carlson4f125f42009-09-01 12:55:02 +00005656 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005657 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658}
5659#endif
5660
David Howellsc4028952006-11-22 14:57:56 +00005661static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662{
David Howellsc4028952006-11-22 14:57:56 +00005663 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005664 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 unsigned int restart_timer;
5666
Michael Chan7faa0062006-02-02 17:29:28 -08005667 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005668
5669 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005670 tg3_full_unlock(tp);
5671 return;
5672 }
5673
5674 tg3_full_unlock(tp);
5675
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005676 tg3_phy_stop(tp);
5677
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 tg3_netif_stop(tp);
5679
David S. Millerf47c11e2005-06-24 20:18:35 -07005680 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
Joe Perches63c3a662011-04-26 08:12:10 +00005682 restart_timer = tg3_flag(tp, RESTART_TIMER);
5683 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684
Joe Perches63c3a662011-04-26 08:12:10 +00005685 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07005686 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5687 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00005688 tg3_flag_set(tp, MBOX_WRITE_REORDER);
5689 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005690 }
5691
Michael Chan944d9802005-05-29 14:57:48 -07005692 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005693 err = tg3_init_hw(tp, 1);
5694 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005695 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
5697 tg3_netif_start(tp);
5698
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 if (restart_timer)
5700 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005701
Michael Chanb9ec6c12006-07-25 16:37:27 -07005702out:
Michael Chan7faa0062006-02-02 17:29:28 -08005703 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005704
5705 if (!err)
5706 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707}
5708
5709static void tg3_tx_timeout(struct net_device *dev)
5710{
5711 struct tg3 *tp = netdev_priv(dev);
5712
Michael Chanb0408752007-02-13 12:18:30 -08005713 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005714 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005715 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717
5718 schedule_work(&tp->reset_task);
5719}
5720
Michael Chanc58ec932005-09-17 00:46:27 -07005721/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5722static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5723{
5724 u32 base = (u32) mapping & 0xffffffff;
5725
Eric Dumazet807540b2010-09-23 05:40:09 +00005726 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005727}
5728
Michael Chan72f2afb2006-03-06 19:28:35 -08005729/* Test for DMA addresses > 40-bit */
5730static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5731 int len)
5732{
5733#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00005734 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00005735 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005736 return 0;
5737#else
5738 return 0;
5739#endif
5740}
5741
Matt Carlson2ffcc982011-05-19 12:12:44 +00005742static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
5743 dma_addr_t mapping, int len, u32 flags,
5744 u32 mss_and_is_end)
5745{
5746 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
5747 int is_end = (mss_and_is_end & 0x1);
5748 u32 mss = (mss_and_is_end >> 1);
5749 u32 vlan_tag = 0;
5750
5751 if (is_end)
5752 flags |= TXD_FLAG_END;
5753 if (flags & TXD_FLAG_VLAN) {
5754 vlan_tag = flags >> 16;
5755 flags &= 0xffff;
5756 }
5757 vlan_tag |= (mss << TXD_MSS_SHIFT);
5758
5759 txd->addr_hi = ((u64) mapping >> 32);
5760 txd->addr_lo = ((u64) mapping & 0xffffffff);
5761 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5762 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5763}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764
Matt Carlson432aa7e2011-05-19 12:12:45 +00005765static void tg3_skb_error_unmap(struct tg3_napi *tnapi,
5766 struct sk_buff *skb, int last)
5767{
5768 int i;
5769 u32 entry = tnapi->tx_prod;
5770 struct ring_info *txb = &tnapi->tx_buffers[entry];
5771
5772 pci_unmap_single(tnapi->tp->pdev,
5773 dma_unmap_addr(txb, mapping),
5774 skb_headlen(skb),
5775 PCI_DMA_TODEVICE);
Matt Carlson9a2e0fb2011-06-02 13:01:39 +00005776 for (i = 0; i < last; i++) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005777 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5778
5779 entry = NEXT_TX(entry);
5780 txb = &tnapi->tx_buffers[entry];
5781
5782 pci_unmap_page(tnapi->tp->pdev,
5783 dma_unmap_addr(txb, mapping),
5784 frag->size, PCI_DMA_TODEVICE);
5785 }
5786}
5787
Michael Chan72f2afb2006-03-06 19:28:35 -08005788/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005789static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
Matt Carlson432aa7e2011-05-19 12:12:45 +00005790 struct sk_buff *skb,
5791 u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005793 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005794 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005795 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005796 u32 entry = tnapi->tx_prod;
5797 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
Matt Carlson41588ba2008-04-19 18:12:33 -07005799 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5800 new_skb = skb_copy(skb, GFP_ATOMIC);
5801 else {
5802 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5803
5804 new_skb = skb_copy_expand(skb,
5805 skb_headroom(skb) + more_headroom,
5806 skb_tailroom(skb), GFP_ATOMIC);
5807 }
5808
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005810 ret = -1;
5811 } else {
5812 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005813 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5814 PCI_DMA_TODEVICE);
5815 /* Make sure the mapping succeeded */
5816 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5817 ret = -1;
5818 dev_kfree_skb(new_skb);
David S. Miller90079ce2008-09-11 04:52:51 -07005819
Michael Chanc58ec932005-09-17 00:46:27 -07005820 /* Make sure new skb does not cross any 4G boundaries.
5821 * Drop the packet if it does.
5822 */
Matt Carlsoneb69d562011-06-13 13:38:57 +00005823 } else if (tg3_4g_overflow_test(new_addr, new_skb->len)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005824 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5825 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005826 ret = -1;
5827 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07005828 } else {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005829 tnapi->tx_buffers[entry].skb = new_skb;
5830 dma_unmap_addr_set(&tnapi->tx_buffers[entry],
5831 mapping, new_addr);
5832
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005833 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005834 base_flags, 1 | (mss << 1));
Michael Chanc58ec932005-09-17 00:46:27 -07005835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 }
5837
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 dev_kfree_skb(skb);
5839
Michael Chanc58ec932005-09-17 00:46:27 -07005840 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841}
5842
Matt Carlson2ffcc982011-05-19 12:12:44 +00005843static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005844
5845/* Use GSO to workaround a rare TSO bug that may be triggered when the
5846 * TSO header is greater than 80 bytes.
5847 */
5848static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5849{
5850 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005851 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005852
5853 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005854 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005855 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005856
5857 /* netif_tx_stop_queue() must be done before checking
5858 * checking tx index in tg3_tx_avail() below, because in
5859 * tg3_tx(), we update tx index before checking for
5860 * netif_tx_queue_stopped().
5861 */
5862 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005863 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005864 return NETDEV_TX_BUSY;
5865
5866 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005867 }
5868
5869 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005870 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005871 goto tg3_tso_bug_end;
5872
5873 do {
5874 nskb = segs;
5875 segs = segs->next;
5876 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00005877 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005878 } while (segs);
5879
5880tg3_tso_bug_end:
5881 dev_kfree_skb(skb);
5882
5883 return NETDEV_TX_OK;
5884}
Michael Chan52c0fd82006-06-29 20:15:54 -07005885
Michael Chan5a6f3072006-03-20 22:28:05 -08005886/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00005887 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005888 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00005889static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005890{
5891 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005892 u32 len, entry, base_flags, mss;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005893 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005894 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005895 struct tg3_napi *tnapi;
5896 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005897 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005898
Matt Carlson24f4efd2009-11-13 13:03:35 +00005899 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5900 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00005901 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005902 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
Michael Chan00b70502006-06-17 21:58:45 -07005904 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005905 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005906 * interrupt. Furthermore, IRQ processing runs lockless so we have
5907 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005909 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005910 if (!netif_tx_queue_stopped(txq)) {
5911 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005912
5913 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005914 netdev_err(dev,
5915 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 return NETDEV_TX_BUSY;
5918 }
5919
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005920 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005922 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005924
Matt Carlsonbe98da62010-07-11 09:31:46 +00005925 mss = skb_shinfo(skb)->gso_size;
5926 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005927 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005928 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929
5930 if (skb_header_cloned(skb) &&
5931 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5932 dev_kfree_skb(skb);
5933 goto out_unlock;
5934 }
5935
Matt Carlson34195c32010-07-11 09:31:42 +00005936 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005937 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938
Matt Carlson02e96082010-09-15 08:59:59 +00005939 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00005940 hdr_len = skb_headlen(skb) - ETH_HLEN;
5941 } else {
5942 u32 ip_tcp_len;
5943
5944 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5945 hdr_len = ip_tcp_len + tcp_opt_len;
5946
5947 iph->check = 0;
5948 iph->tot_len = htons(mss + hdr_len);
5949 }
5950
Michael Chan52c0fd82006-06-29 20:15:54 -07005951 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00005952 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005953 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005954
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5956 TXD_FLAG_CPU_POST_DMA);
5957
Joe Perches63c3a662011-04-26 08:12:10 +00005958 if (tg3_flag(tp, HW_TSO_1) ||
5959 tg3_flag(tp, HW_TSO_2) ||
5960 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005961 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005963 } else
5964 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
5965 iph->daddr, 0,
5966 IPPROTO_TCP,
5967 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968
Joe Perches63c3a662011-04-26 08:12:10 +00005969 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00005970 mss |= (hdr_len & 0xc) << 12;
5971 if (hdr_len & 0x10)
5972 base_flags |= 0x00000010;
5973 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00005974 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005975 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00005976 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00005977 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005978 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 int tsflags;
5980
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005981 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 mss |= (tsflags << 11);
5983 }
5984 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005985 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 int tsflags;
5987
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005988 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 base_flags |= tsflags << 12;
5990 }
5991 }
5992 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00005993
Jesse Grosseab6d182010-10-20 13:56:03 +00005994 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 base_flags |= (TXD_FLAG_VLAN |
5996 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997
Joe Perches63c3a662011-04-26 08:12:10 +00005998 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00005999 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006000 base_flags |= TXD_FLAG_JMB_PKT;
6001
Alexander Duyckf4188d82009-12-02 16:48:38 +00006002 len = skb_headlen(skb);
6003
6004 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6005 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006006 dev_kfree_skb(skb);
6007 goto out_unlock;
6008 }
6009
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006010 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006011 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
6013 would_hit_hwbug = 0;
6014
Joe Perches63c3a662011-04-26 08:12:10 +00006015 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006016 would_hit_hwbug = 1;
6017
Matt Carlsoneb69d562011-06-13 13:38:57 +00006018 if (tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006019 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006020
Matt Carlsondaf9a552011-06-13 13:38:56 +00006021 if (tg3_40bit_overflow_test(tp, mapping, len))
Matt Carlson0e1406d2009-11-02 12:33:33 +00006022 would_hit_hwbug = 1;
6023
Joe Perches63c3a662011-04-26 08:12:10 +00006024 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006025 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006027 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6029
6030 entry = NEXT_TX(entry);
6031
6032 /* Now loop through additional data fragments, and queue them. */
6033 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 last = skb_shinfo(skb)->nr_frags - 1;
6035 for (i = 0; i <= last; i++) {
6036 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6037
6038 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006039 mapping = pci_map_page(tp->pdev,
6040 frag->page,
6041 frag->page_offset,
6042 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006044 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006045 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006046 mapping);
6047 if (pci_dma_mapping_error(tp->pdev, mapping))
6048 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049
Joe Perches63c3a662011-04-26 08:12:10 +00006050 if (tg3_flag(tp, SHORT_DMA_BUG) &&
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006051 len <= 8)
6052 would_hit_hwbug = 1;
6053
Matt Carlsoneb69d562011-06-13 13:38:57 +00006054 if (tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006055 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056
Matt Carlsondaf9a552011-06-13 13:38:56 +00006057 if (tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006058 would_hit_hwbug = 1;
6059
Joe Perches63c3a662011-04-26 08:12:10 +00006060 if (tg3_flag(tp, HW_TSO_1) ||
6061 tg3_flag(tp, HW_TSO_2) ||
6062 tg3_flag(tp, HW_TSO_3))
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006063 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 base_flags, (i == last)|(mss << 1));
6065 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006066 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 base_flags, (i == last));
6068
6069 entry = NEXT_TX(entry);
6070 }
6071 }
6072
6073 if (would_hit_hwbug) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00006074 tg3_skb_error_unmap(tnapi, skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
6076 /* If the workaround fails due to memory/mapping
6077 * failure, silently drop this packet.
6078 */
Matt Carlson432aa7e2011-05-19 12:12:45 +00006079 if (tigon3_dma_hwbug_workaround(tnapi, skb, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 goto out_unlock;
6081
Matt Carlson432aa7e2011-05-19 12:12:45 +00006082 entry = NEXT_TX(tnapi->tx_prod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 }
6084
6085 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006086 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087
Richard Cochran26690692011-06-12 02:19:02 +00006088 skb_tx_timestamp(skb);
6089
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006090 tnapi->tx_prod = entry;
6091 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006092 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006093
6094 /* netif_tx_stop_queue() must be done before checking
6095 * checking tx index in tg3_tx_avail() below, because in
6096 * tg3_tx(), we update tx index before checking for
6097 * netif_tx_queue_stopped().
6098 */
6099 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006100 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006101 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103
6104out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006105 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106
6107 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006108
6109dma_error:
Matt Carlson432aa7e2011-05-19 12:12:45 +00006110 tg3_skb_error_unmap(tnapi, skb, i);
Alexander Duyckf4188d82009-12-02 16:48:38 +00006111 dev_kfree_skb(skb);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006112 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006113 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114}
6115
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006116static void tg3_set_loopback(struct net_device *dev, u32 features)
6117{
6118 struct tg3 *tp = netdev_priv(dev);
6119
6120 if (features & NETIF_F_LOOPBACK) {
6121 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6122 return;
6123
6124 /*
6125 * Clear MAC_MODE_HALF_DUPLEX or you won't get packets back in
6126 * loopback mode if Half-Duplex mode was negotiated earlier.
6127 */
6128 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
6129
6130 /* Enable internal MAC loopback mode */
6131 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6132 spin_lock_bh(&tp->lock);
6133 tw32(MAC_MODE, tp->mac_mode);
6134 netif_carrier_on(tp->dev);
6135 spin_unlock_bh(&tp->lock);
6136 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6137 } else {
6138 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6139 return;
6140
6141 /* Disable internal MAC loopback mode */
6142 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6143 spin_lock_bh(&tp->lock);
6144 tw32(MAC_MODE, tp->mac_mode);
6145 /* Force link status check */
6146 tg3_setup_phy(tp, 1);
6147 spin_unlock_bh(&tp->lock);
6148 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6149 }
6150}
6151
Michał Mirosławdc668912011-04-07 03:35:07 +00006152static u32 tg3_fix_features(struct net_device *dev, u32 features)
6153{
6154 struct tg3 *tp = netdev_priv(dev);
6155
Joe Perches63c3a662011-04-26 08:12:10 +00006156 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006157 features &= ~NETIF_F_ALL_TSO;
6158
6159 return features;
6160}
6161
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006162static int tg3_set_features(struct net_device *dev, u32 features)
6163{
6164 u32 changed = dev->features ^ features;
6165
6166 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6167 tg3_set_loopback(dev, features);
6168
6169 return 0;
6170}
6171
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6173 int new_mtu)
6174{
6175 dev->mtu = new_mtu;
6176
Michael Chanef7f5ec2005-07-25 12:32:25 -07006177 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006178 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006179 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006180 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00006181 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006182 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00006183 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006184 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006185 if (tg3_flag(tp, 5780_CLASS)) {
6186 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00006187 netdev_update_features(dev);
6188 }
Joe Perches63c3a662011-04-26 08:12:10 +00006189 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191}
6192
6193static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6194{
6195 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006196 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197
6198 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6199 return -EINVAL;
6200
6201 if (!netif_running(dev)) {
6202 /* We'll just catch it later when the
6203 * device is up'd.
6204 */
6205 tg3_set_mtu(dev, tp, new_mtu);
6206 return 0;
6207 }
6208
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006209 tg3_phy_stop(tp);
6210
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006212
6213 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214
Michael Chan944d9802005-05-29 14:57:48 -07006215 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216
6217 tg3_set_mtu(dev, tp, new_mtu);
6218
Michael Chanb9ec6c12006-07-25 16:37:27 -07006219 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
Michael Chanb9ec6c12006-07-25 16:37:27 -07006221 if (!err)
6222 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
David S. Millerf47c11e2005-06-24 20:18:35 -07006224 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006226 if (!err)
6227 tg3_phy_start(tp);
6228
Michael Chanb9ec6c12006-07-25 16:37:27 -07006229 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230}
6231
Matt Carlson21f581a2009-08-28 14:00:25 +00006232static void tg3_rx_prodring_free(struct tg3 *tp,
6233 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 int i;
6236
Matt Carlson8fea32b2010-09-15 08:59:58 +00006237 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006238 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006239 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006240 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6241 tp->rx_pkt_map_sz);
6242
Joe Perches63c3a662011-04-26 08:12:10 +00006243 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006244 for (i = tpr->rx_jmb_cons_idx;
6245 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006246 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006247 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6248 TG3_RX_JMB_MAP_SZ);
6249 }
6250 }
6251
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006252 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254
Matt Carlson2c49a442010-09-30 10:34:35 +00006255 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006256 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6257 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258
Joe Perches63c3a662011-04-26 08:12:10 +00006259 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006260 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006261 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6262 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 }
6264}
6265
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006266/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 *
6268 * The chip has been shut down and the driver detached from
6269 * the networking, so no interrupts or new tx packets will
6270 * end up in the driver. tp->{tx,}lock are held and thus
6271 * we may not sleep.
6272 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006273static int tg3_rx_prodring_alloc(struct tg3 *tp,
6274 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275{
Matt Carlson287be122009-08-28 13:58:46 +00006276 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006278 tpr->rx_std_cons_idx = 0;
6279 tpr->rx_std_prod_idx = 0;
6280 tpr->rx_jmb_cons_idx = 0;
6281 tpr->rx_jmb_prod_idx = 0;
6282
Matt Carlson8fea32b2010-09-15 08:59:58 +00006283 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006284 memset(&tpr->rx_std_buffers[0], 0,
6285 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006286 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006287 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006288 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006289 goto done;
6290 }
6291
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006293 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
Matt Carlson287be122009-08-28 13:58:46 +00006295 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00006296 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006297 tp->dev->mtu > ETH_DATA_LEN)
6298 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6299 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006300
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 /* Initialize invariants of the rings, we only set this
6302 * stuff once. This works because the card does not
6303 * write into the rx buffer posting rings.
6304 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006305 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 struct tg3_rx_buffer_desc *rxd;
6307
Matt Carlson21f581a2009-08-28 14:00:25 +00006308 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006309 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6311 rxd->opaque = (RXD_OPAQUE_RING_STD |
6312 (i << RXD_OPAQUE_INDEX_SHIFT));
6313 }
6314
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006315 /* Now allocate fresh SKBs for each rx ring. */
6316 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006317 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006318 netdev_warn(tp->dev,
6319 "Using a smaller RX standard ring. Only "
6320 "%d out of %d buffers were allocated "
6321 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006322 if (i == 0)
6323 goto initfail;
6324 tp->rx_pending = i;
6325 break;
6326 }
6327 }
6328
Joe Perches63c3a662011-04-26 08:12:10 +00006329 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006330 goto done;
6331
Matt Carlson2c49a442010-09-30 10:34:35 +00006332 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006333
Joe Perches63c3a662011-04-26 08:12:10 +00006334 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00006335 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336
Matt Carlson2c49a442010-09-30 10:34:35 +00006337 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006338 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339
Matt Carlson0d86df82010-02-17 15:17:00 +00006340 rxd = &tpr->rx_jmb[i].std;
6341 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6342 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6343 RXD_FLAG_JUMBO;
6344 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6345 (i << RXD_OPAQUE_INDEX_SHIFT));
6346 }
6347
6348 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6349 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006350 netdev_warn(tp->dev,
6351 "Using a smaller RX jumbo ring. Only %d "
6352 "out of %d buffers were allocated "
6353 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006354 if (i == 0)
6355 goto initfail;
6356 tp->rx_jumbo_pending = i;
6357 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 }
6359 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006360
6361done:
Michael Chan32d8c572006-07-25 16:38:29 -07006362 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006363
6364initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006365 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006366 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367}
6368
Matt Carlson21f581a2009-08-28 14:00:25 +00006369static void tg3_rx_prodring_fini(struct tg3 *tp,
6370 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371{
Matt Carlson21f581a2009-08-28 14:00:25 +00006372 kfree(tpr->rx_std_buffers);
6373 tpr->rx_std_buffers = NULL;
6374 kfree(tpr->rx_jmb_buffers);
6375 tpr->rx_jmb_buffers = NULL;
6376 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006377 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6378 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006379 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006381 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006382 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6383 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006384 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006386}
6387
Matt Carlson21f581a2009-08-28 14:00:25 +00006388static int tg3_rx_prodring_init(struct tg3 *tp,
6389 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006390{
Matt Carlson2c49a442010-09-30 10:34:35 +00006391 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6392 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006393 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006394 return -ENOMEM;
6395
Matt Carlson4bae65c2010-11-24 08:31:52 +00006396 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6397 TG3_RX_STD_RING_BYTES(tp),
6398 &tpr->rx_std_mapping,
6399 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006400 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006401 goto err_out;
6402
Joe Perches63c3a662011-04-26 08:12:10 +00006403 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006404 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006405 GFP_KERNEL);
6406 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006407 goto err_out;
6408
Matt Carlson4bae65c2010-11-24 08:31:52 +00006409 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6410 TG3_RX_JMB_RING_BYTES(tp),
6411 &tpr->rx_jmb_mapping,
6412 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006413 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006414 goto err_out;
6415 }
6416
6417 return 0;
6418
6419err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006420 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006421 return -ENOMEM;
6422}
6423
6424/* Free up pending packets in all rx/tx rings.
6425 *
6426 * The chip has been shut down and the driver detached from
6427 * the networking, so no interrupts or new tx packets will
6428 * end up in the driver. tp->{tx,}lock is not held and we are not
6429 * in an interrupt context and thus may sleep.
6430 */
6431static void tg3_free_rings(struct tg3 *tp)
6432{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006433 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006434
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006435 for (j = 0; j < tp->irq_cnt; j++) {
6436 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006437
Matt Carlson8fea32b2010-09-15 08:59:58 +00006438 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006439
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006440 if (!tnapi->tx_buffers)
6441 continue;
6442
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006443 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006444 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006445 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006446 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006447
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006448 txp = &tnapi->tx_buffers[i];
6449 skb = txp->skb;
6450
6451 if (skb == NULL) {
6452 i++;
6453 continue;
6454 }
6455
Alexander Duyckf4188d82009-12-02 16:48:38 +00006456 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006457 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006458 skb_headlen(skb),
6459 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006460 txp->skb = NULL;
6461
Alexander Duyckf4188d82009-12-02 16:48:38 +00006462 i++;
6463
6464 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6465 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6466 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006467 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006468 skb_shinfo(skb)->frags[k].size,
6469 PCI_DMA_TODEVICE);
6470 i++;
6471 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006472
6473 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006474 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006475 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006476}
6477
6478/* Initialize tx/rx rings for packet processing.
6479 *
6480 * The chip has been shut down and the driver detached from
6481 * the networking, so no interrupts or new tx packets will
6482 * end up in the driver. tp->{tx,}lock are held and thus
6483 * we may not sleep.
6484 */
6485static int tg3_init_rings(struct tg3 *tp)
6486{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006487 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006488
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006489 /* Free up all the SKBs. */
6490 tg3_free_rings(tp);
6491
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006492 for (i = 0; i < tp->irq_cnt; i++) {
6493 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006494
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006495 tnapi->last_tag = 0;
6496 tnapi->last_irq_tag = 0;
6497 tnapi->hw_status->status = 0;
6498 tnapi->hw_status->status_tag = 0;
6499 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6500
6501 tnapi->tx_prod = 0;
6502 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006503 if (tnapi->tx_ring)
6504 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006505
6506 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006507 if (tnapi->rx_rcb)
6508 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006509
Matt Carlson8fea32b2010-09-15 08:59:58 +00006510 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006511 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006512 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006513 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006514 }
Matt Carlson72334482009-08-28 14:03:01 +00006515
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006516 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006517}
6518
6519/*
6520 * Must not be invoked with interrupt sources disabled and
6521 * the hardware shutdown down.
6522 */
6523static void tg3_free_consistent(struct tg3 *tp)
6524{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006525 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006526
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006527 for (i = 0; i < tp->irq_cnt; i++) {
6528 struct tg3_napi *tnapi = &tp->napi[i];
6529
6530 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006531 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006532 tnapi->tx_ring, tnapi->tx_desc_mapping);
6533 tnapi->tx_ring = NULL;
6534 }
6535
6536 kfree(tnapi->tx_buffers);
6537 tnapi->tx_buffers = NULL;
6538
6539 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006540 dma_free_coherent(&tp->pdev->dev,
6541 TG3_RX_RCB_RING_BYTES(tp),
6542 tnapi->rx_rcb,
6543 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006544 tnapi->rx_rcb = NULL;
6545 }
6546
Matt Carlson8fea32b2010-09-15 08:59:58 +00006547 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6548
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006549 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006550 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6551 tnapi->hw_status,
6552 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006553 tnapi->hw_status = NULL;
6554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006556
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006558 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6559 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 tp->hw_stats = NULL;
6561 }
6562}
6563
6564/*
6565 * Must not be invoked with interrupt sources disabled and
6566 * the hardware shutdown down. Can sleep.
6567 */
6568static int tg3_alloc_consistent(struct tg3 *tp)
6569{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006570 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006571
Matt Carlson4bae65c2010-11-24 08:31:52 +00006572 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6573 sizeof(struct tg3_hw_stats),
6574 &tp->stats_mapping,
6575 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 if (!tp->hw_stats)
6577 goto err_out;
6578
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6580
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006581 for (i = 0; i < tp->irq_cnt; i++) {
6582 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006583 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006584
Matt Carlson4bae65c2010-11-24 08:31:52 +00006585 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6586 TG3_HW_STATUS_SIZE,
6587 &tnapi->status_mapping,
6588 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006589 if (!tnapi->hw_status)
6590 goto err_out;
6591
6592 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006593 sblk = tnapi->hw_status;
6594
Matt Carlson8fea32b2010-09-15 08:59:58 +00006595 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6596 goto err_out;
6597
Matt Carlson19cfaec2009-12-03 08:36:20 +00006598 /* If multivector TSS is enabled, vector 0 does not handle
6599 * tx interrupts. Don't allocate any resources for it.
6600 */
Joe Perches63c3a662011-04-26 08:12:10 +00006601 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
6602 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00006603 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6604 TG3_TX_RING_SIZE,
6605 GFP_KERNEL);
6606 if (!tnapi->tx_buffers)
6607 goto err_out;
6608
Matt Carlson4bae65c2010-11-24 08:31:52 +00006609 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6610 TG3_TX_RING_BYTES,
6611 &tnapi->tx_desc_mapping,
6612 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006613 if (!tnapi->tx_ring)
6614 goto err_out;
6615 }
6616
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006617 /*
6618 * When RSS is enabled, the status block format changes
6619 * slightly. The "rx_jumbo_consumer", "reserved",
6620 * and "rx_mini_consumer" members get mapped to the
6621 * other three rx return ring producer indexes.
6622 */
6623 switch (i) {
6624 default:
6625 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6626 break;
6627 case 2:
6628 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6629 break;
6630 case 3:
6631 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6632 break;
6633 case 4:
6634 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6635 break;
6636 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006637
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006638 /*
6639 * If multivector RSS is enabled, vector 0 does not handle
6640 * rx or tx interrupts. Don't allocate any resources for it.
6641 */
Joe Perches63c3a662011-04-26 08:12:10 +00006642 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006643 continue;
6644
Matt Carlson4bae65c2010-11-24 08:31:52 +00006645 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6646 TG3_RX_RCB_RING_BYTES(tp),
6647 &tnapi->rx_rcb_mapping,
6648 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006649 if (!tnapi->rx_rcb)
6650 goto err_out;
6651
6652 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006653 }
6654
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655 return 0;
6656
6657err_out:
6658 tg3_free_consistent(tp);
6659 return -ENOMEM;
6660}
6661
6662#define MAX_WAIT_CNT 1000
6663
6664/* To stop a block, clear the enable bit and poll till it
6665 * clears. tp->lock is held.
6666 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006667static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668{
6669 unsigned int i;
6670 u32 val;
6671
Joe Perches63c3a662011-04-26 08:12:10 +00006672 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673 switch (ofs) {
6674 case RCVLSC_MODE:
6675 case DMAC_MODE:
6676 case MBFREE_MODE:
6677 case BUFMGR_MODE:
6678 case MEMARB_MODE:
6679 /* We can't enable/disable these bits of the
6680 * 5705/5750, just say success.
6681 */
6682 return 0;
6683
6684 default:
6685 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687 }
6688
6689 val = tr32(ofs);
6690 val &= ~enable_bit;
6691 tw32_f(ofs, val);
6692
6693 for (i = 0; i < MAX_WAIT_CNT; i++) {
6694 udelay(100);
6695 val = tr32(ofs);
6696 if ((val & enable_bit) == 0)
6697 break;
6698 }
6699
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006700 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006701 dev_err(&tp->pdev->dev,
6702 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6703 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 return -ENODEV;
6705 }
6706
6707 return 0;
6708}
6709
6710/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006711static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712{
6713 int i, err;
6714
6715 tg3_disable_ints(tp);
6716
6717 tp->rx_mode &= ~RX_MODE_ENABLE;
6718 tw32_f(MAC_RX_MODE, tp->rx_mode);
6719 udelay(10);
6720
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006721 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6722 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6723 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6724 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6725 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6726 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006728 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6729 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6730 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6731 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6732 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6733 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6734 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735
6736 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6737 tw32_f(MAC_MODE, tp->mac_mode);
6738 udelay(40);
6739
6740 tp->tx_mode &= ~TX_MODE_ENABLE;
6741 tw32_f(MAC_TX_MODE, tp->tx_mode);
6742
6743 for (i = 0; i < MAX_WAIT_CNT; i++) {
6744 udelay(100);
6745 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6746 break;
6747 }
6748 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006749 dev_err(&tp->pdev->dev,
6750 "%s timed out, TX_MODE_ENABLE will not clear "
6751 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006752 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 }
6754
Michael Chane6de8ad2005-05-05 14:42:41 -07006755 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006756 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6757 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758
6759 tw32(FTQ_RESET, 0xffffffff);
6760 tw32(FTQ_RESET, 0x00000000);
6761
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006762 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6763 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006765 for (i = 0; i < tp->irq_cnt; i++) {
6766 struct tg3_napi *tnapi = &tp->napi[i];
6767 if (tnapi->hw_status)
6768 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 if (tp->hw_stats)
6771 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6772
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 return err;
6774}
6775
Matt Carlson0d3031d2007-10-10 18:02:43 -07006776static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6777{
6778 int i;
6779 u32 apedata;
6780
Matt Carlsondc6d0742010-09-15 08:59:55 +00006781 /* NCSI does not support APE events */
Joe Perches63c3a662011-04-26 08:12:10 +00006782 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsondc6d0742010-09-15 08:59:55 +00006783 return;
6784
Matt Carlson0d3031d2007-10-10 18:02:43 -07006785 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6786 if (apedata != APE_SEG_SIG_MAGIC)
6787 return;
6788
6789 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006790 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006791 return;
6792
6793 /* Wait for up to 1 millisecond for APE to service previous event. */
6794 for (i = 0; i < 10; i++) {
6795 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6796 return;
6797
6798 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6799
6800 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6801 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6802 event | APE_EVENT_STATUS_EVENT_PENDING);
6803
6804 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6805
6806 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6807 break;
6808
6809 udelay(100);
6810 }
6811
6812 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6813 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6814}
6815
6816static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6817{
6818 u32 event;
6819 u32 apedata;
6820
Joe Perches63c3a662011-04-26 08:12:10 +00006821 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006822 return;
6823
6824 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006825 case RESET_KIND_INIT:
6826 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6827 APE_HOST_SEG_SIG_MAGIC);
6828 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6829 APE_HOST_SEG_LEN_MAGIC);
6830 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6831 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6832 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006833 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006834 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6835 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006836 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6837 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006838
Matt Carlson33f401a2010-04-05 10:19:27 +00006839 event = APE_EVENT_STATUS_STATE_START;
6840 break;
6841 case RESET_KIND_SHUTDOWN:
6842 /* With the interface we are currently using,
6843 * APE does not track driver state. Wiping
6844 * out the HOST SEGMENT SIGNATURE forces
6845 * the APE to assume OS absent status.
6846 */
6847 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006848
Matt Carlsondc6d0742010-09-15 08:59:55 +00006849 if (device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006850 tg3_flag(tp, WOL_ENABLE)) {
Matt Carlsondc6d0742010-09-15 08:59:55 +00006851 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6852 TG3_APE_HOST_WOL_SPEED_AUTO);
6853 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6854 } else
6855 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6856
6857 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6858
Matt Carlson33f401a2010-04-05 10:19:27 +00006859 event = APE_EVENT_STATUS_STATE_UNLOAD;
6860 break;
6861 case RESET_KIND_SUSPEND:
6862 event = APE_EVENT_STATUS_STATE_SUSPEND;
6863 break;
6864 default:
6865 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006866 }
6867
6868 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6869
6870 tg3_ape_send_event(tp, event);
6871}
6872
Michael Chane6af3012005-04-21 17:12:05 -07006873/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6875{
David S. Millerf49639e2006-06-09 11:58:36 -07006876 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6877 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878
Joe Perches63c3a662011-04-26 08:12:10 +00006879 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880 switch (kind) {
6881 case RESET_KIND_INIT:
6882 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6883 DRV_STATE_START);
6884 break;
6885
6886 case RESET_KIND_SHUTDOWN:
6887 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6888 DRV_STATE_UNLOAD);
6889 break;
6890
6891 case RESET_KIND_SUSPEND:
6892 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6893 DRV_STATE_SUSPEND);
6894 break;
6895
6896 default:
6897 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006900
6901 if (kind == RESET_KIND_INIT ||
6902 kind == RESET_KIND_SUSPEND)
6903 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904}
6905
6906/* tp->lock is held. */
6907static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6908{
Joe Perches63c3a662011-04-26 08:12:10 +00006909 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 switch (kind) {
6911 case RESET_KIND_INIT:
6912 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6913 DRV_STATE_START_DONE);
6914 break;
6915
6916 case RESET_KIND_SHUTDOWN:
6917 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6918 DRV_STATE_UNLOAD_DONE);
6919 break;
6920
6921 default:
6922 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006925
6926 if (kind == RESET_KIND_SHUTDOWN)
6927 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928}
6929
6930/* tp->lock is held. */
6931static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6932{
Joe Perches63c3a662011-04-26 08:12:10 +00006933 if (tg3_flag(tp, ENABLE_ASF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934 switch (kind) {
6935 case RESET_KIND_INIT:
6936 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6937 DRV_STATE_START);
6938 break;
6939
6940 case RESET_KIND_SHUTDOWN:
6941 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6942 DRV_STATE_UNLOAD);
6943 break;
6944
6945 case RESET_KIND_SUSPEND:
6946 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6947 DRV_STATE_SUSPEND);
6948 break;
6949
6950 default:
6951 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 }
6954}
6955
Michael Chan7a6f4362006-09-27 16:03:31 -07006956static int tg3_poll_fw(struct tg3 *tp)
6957{
6958 int i;
6959 u32 val;
6960
Michael Chanb5d37722006-09-27 16:06:21 -07006961 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006962 /* Wait up to 20ms for init done. */
6963 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07006964 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
6965 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08006966 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07006967 }
6968 return -ENODEV;
6969 }
6970
Michael Chan7a6f4362006-09-27 16:03:31 -07006971 /* Wait for firmware initialization to complete. */
6972 for (i = 0; i < 100000; i++) {
6973 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
6974 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
6975 break;
6976 udelay(10);
6977 }
6978
6979 /* Chip might not be fitted with firmware. Some Sun onboard
6980 * parts are configured like that. So don't signal the timeout
6981 * of the above loop as an error, but do report the lack of
6982 * running firmware once.
6983 */
Joe Perches63c3a662011-04-26 08:12:10 +00006984 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
6985 tg3_flag_set(tp, NO_FWARE_REPORTED);
Michael Chan7a6f4362006-09-27 16:03:31 -07006986
Joe Perches05dbe002010-02-17 19:44:19 +00006987 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07006988 }
6989
Matt Carlson6b10c162010-02-12 14:47:08 +00006990 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
6991 /* The 57765 A0 needs a little more
6992 * time to do some important work.
6993 */
6994 mdelay(10);
6995 }
6996
Michael Chan7a6f4362006-09-27 16:03:31 -07006997 return 0;
6998}
6999
Michael Chanee6a99b2007-07-18 21:49:10 -07007000/* Save PCI command register before chip reset */
7001static void tg3_save_pci_state(struct tg3 *tp)
7002{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007003 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007004}
7005
7006/* Restore PCI state after chip reset */
7007static void tg3_restore_pci_state(struct tg3 *tp)
7008{
7009 u32 val;
7010
7011 /* Re-enable indirect register accesses. */
7012 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7013 tp->misc_host_ctrl);
7014
7015 /* Set MAX PCI retry to zero. */
7016 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7017 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007018 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007019 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007020 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007021 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007022 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007023 PCISTATE_ALLOW_APE_SHMEM_WR |
7024 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007025 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7026
Matt Carlson8a6eac92007-10-21 16:17:55 -07007027 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007028
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007029 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007030 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007031 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007032 else {
7033 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7034 tp->pci_cacheline_sz);
7035 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7036 tp->pci_lat_timer);
7037 }
Michael Chan114342f2007-10-15 02:12:26 -07007038 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007039
Michael Chanee6a99b2007-07-18 21:49:10 -07007040 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007041 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007042 u16 pcix_cmd;
7043
7044 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7045 &pcix_cmd);
7046 pcix_cmd &= ~PCI_X_CMD_ERO;
7047 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7048 pcix_cmd);
7049 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007050
Joe Perches63c3a662011-04-26 08:12:10 +00007051 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007052
7053 /* Chip reset on 5780 will reset MSI enable bit,
7054 * so need to restore it.
7055 */
Joe Perches63c3a662011-04-26 08:12:10 +00007056 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007057 u16 ctrl;
7058
7059 pci_read_config_word(tp->pdev,
7060 tp->msi_cap + PCI_MSI_FLAGS,
7061 &ctrl);
7062 pci_write_config_word(tp->pdev,
7063 tp->msi_cap + PCI_MSI_FLAGS,
7064 ctrl | PCI_MSI_FLAGS_ENABLE);
7065 val = tr32(MSGINT_MODE);
7066 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7067 }
7068 }
7069}
7070
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071static void tg3_stop_fw(struct tg3 *);
7072
7073/* tp->lock is held. */
7074static int tg3_chip_reset(struct tg3 *tp)
7075{
7076 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007077 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007078 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079
David S. Millerf49639e2006-06-09 11:58:36 -07007080 tg3_nvram_lock(tp);
7081
Matt Carlson77b483f2008-08-15 14:07:24 -07007082 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7083
David S. Millerf49639e2006-06-09 11:58:36 -07007084 /* No matching tg3_nvram_unlock() after this because
7085 * chip reset below will undo the nvram lock.
7086 */
7087 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088
Michael Chanee6a99b2007-07-18 21:49:10 -07007089 /* GRC_MISC_CFG core clock reset will clear the memory
7090 * enable bit in PCI register 4 and the MSI enable bit
7091 * on some chips, so we save relevant registers here.
7092 */
7093 tg3_save_pci_state(tp);
7094
Michael Chand9ab5ad12006-03-20 22:27:35 -08007095 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007096 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007097 tw32(GRC_FASTBOOT_PC, 0);
7098
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099 /*
7100 * We must avoid the readl() that normally takes place.
7101 * It locks machines, causes machine checks, and other
7102 * fun things. So, temporarily disable the 5701
7103 * hardware workaround, while we do the reset.
7104 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007105 write_op = tp->write32;
7106 if (write_op == tg3_write_flush_reg32)
7107 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108
Michael Chand18edcb2007-03-24 20:57:11 -07007109 /* Prevent the irq handler from reading or writing PCI registers
7110 * during chip reset when the memory enable bit in the PCI command
7111 * register may be cleared. The chip does not generate interrupt
7112 * at this time, but the irq handler may still be called due to irq
7113 * sharing or irqpoll.
7114 */
Joe Perches63c3a662011-04-26 08:12:10 +00007115 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007116 for (i = 0; i < tp->irq_cnt; i++) {
7117 struct tg3_napi *tnapi = &tp->napi[i];
7118 if (tnapi->hw_status) {
7119 tnapi->hw_status->status = 0;
7120 tnapi->hw_status->status_tag = 0;
7121 }
7122 tnapi->last_tag = 0;
7123 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007124 }
Michael Chand18edcb2007-03-24 20:57:11 -07007125 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007126
7127 for (i = 0; i < tp->irq_cnt; i++)
7128 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007129
Matt Carlson255ca312009-08-25 10:07:27 +00007130 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7131 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7132 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7133 }
7134
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 /* do the reset */
7136 val = GRC_MISC_CFG_CORECLK_RESET;
7137
Joe Perches63c3a662011-04-26 08:12:10 +00007138 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007139 /* Force PCIe 1.0a mode */
7140 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007141 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007142 tr32(TG3_PCIE_PHY_TSTCTL) ==
7143 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7144 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7145
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7147 tw32(GRC_MISC_CFG, (1 << 29));
7148 val |= (1 << 29);
7149 }
7150 }
7151
Michael Chanb5d37722006-09-27 16:06:21 -07007152 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7153 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7154 tw32(GRC_VCPU_EXT_CTRL,
7155 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7156 }
7157
Matt Carlsonf37500d2010-08-02 11:25:59 +00007158 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007159 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007161
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 tw32(GRC_MISC_CFG, val);
7163
Michael Chan1ee582d2005-08-09 20:16:46 -07007164 /* restore 5701 hardware bug workaround write method */
7165 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166
7167 /* Unfortunately, we have to delay before the PCI read back.
7168 * Some 575X chips even will not respond to a PCI cfg access
7169 * when the reset command is given to the chip.
7170 *
7171 * How do these hardware designers expect things to work
7172 * properly if the PCI write is posted for a long period
7173 * of time? It is always necessary to have some method by
7174 * which a register read back can occur to push the write
7175 * out which does the reset.
7176 *
7177 * For most tg3 variants the trick below was working.
7178 * Ho hum...
7179 */
7180 udelay(120);
7181
7182 /* Flush PCI posted writes. The normal MMIO registers
7183 * are inaccessible at this time so this is the only
7184 * way to make this reliably (actually, this is no longer
7185 * the case, see above). I tried to use indirect
7186 * register read/write but this upset some 5701 variants.
7187 */
7188 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7189
7190 udelay(120);
7191
Joe Perches63c3a662011-04-26 08:12:10 +00007192 if (tg3_flag(tp, PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007193 u16 val16;
7194
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7196 int i;
7197 u32 cfg_val;
7198
7199 /* Wait for link training to complete. */
7200 for (i = 0; i < 5000; i++)
7201 udelay(100);
7202
7203 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7204 pci_write_config_dword(tp->pdev, 0xc4,
7205 cfg_val | (1 << 15));
7206 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007207
Matt Carlsone7126992009-08-25 10:08:16 +00007208 /* Clear the "no snoop" and "relaxed ordering" bits. */
7209 pci_read_config_word(tp->pdev,
7210 tp->pcie_cap + PCI_EXP_DEVCTL,
7211 &val16);
7212 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7213 PCI_EXP_DEVCTL_NOSNOOP_EN);
7214 /*
7215 * Older PCIe devices only support the 128 byte
7216 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007217 */
Joe Perches63c3a662011-04-26 08:12:10 +00007218 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007219 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007220 pci_write_config_word(tp->pdev,
7221 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007222 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007223
Matt Carlsoncf790032010-11-24 08:31:48 +00007224 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007225
7226 /* Clear error status */
7227 pci_write_config_word(tp->pdev,
7228 tp->pcie_cap + PCI_EXP_DEVSTA,
7229 PCI_EXP_DEVSTA_CED |
7230 PCI_EXP_DEVSTA_NFED |
7231 PCI_EXP_DEVSTA_FED |
7232 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 }
7234
Michael Chanee6a99b2007-07-18 21:49:10 -07007235 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236
Joe Perches63c3a662011-04-26 08:12:10 +00007237 tg3_flag_clear(tp, CHIP_RESETTING);
7238 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007239
Michael Chanee6a99b2007-07-18 21:49:10 -07007240 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007241 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007242 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007243 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244
7245 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7246 tg3_stop_fw(tp);
7247 tw32(0x5000, 0x400);
7248 }
7249
7250 tw32(GRC_MODE, tp->grc_mode);
7251
7252 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007253 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254
7255 tw32(0xc4, val | (1 << 15));
7256 }
7257
7258 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7259 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7260 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7261 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7262 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7263 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7264 }
7265
Joe Perches63c3a662011-04-26 08:12:10 +00007266 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007267 tp->mac_mode = MAC_MODE_APE_TX_EN |
7268 MAC_MODE_APE_RX_EN |
7269 MAC_MODE_TDE_ENABLE;
7270
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007271 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007272 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7273 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007274 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007275 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7276 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007277 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007278 val = 0;
7279
7280 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 udelay(40);
7282
Matt Carlson77b483f2008-08-15 14:07:24 -07007283 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7284
Michael Chan7a6f4362006-09-27 16:03:31 -07007285 err = tg3_poll_fw(tp);
7286 if (err)
7287 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288
Matt Carlson0a9140c2009-08-28 12:27:50 +00007289 tg3_mdio_start(tp);
7290
Joe Perches63c3a662011-04-26 08:12:10 +00007291 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007292 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7293 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007294 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007295 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296
7297 tw32(0x7c00, val | (1 << 25));
7298 }
7299
Matt Carlsond78b59f2011-04-05 14:22:46 +00007300 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7301 val = tr32(TG3_CPMU_CLCK_ORIDE);
7302 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7303 }
7304
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007306 tg3_flag_clear(tp, ENABLE_ASF);
7307 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7309 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7310 u32 nic_cfg;
7311
7312 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7313 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007314 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007315 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007316 if (tg3_flag(tp, 5750_PLUS))
7317 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 }
7319 }
7320
7321 return 0;
7322}
7323
7324/* tp->lock is held. */
7325static void tg3_stop_fw(struct tg3 *tp)
7326{
Joe Perches63c3a662011-04-26 08:12:10 +00007327 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007328 /* Wait for RX cpu to ACK the previous event. */
7329 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330
7331 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007332
7333 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334
Matt Carlson7c5026a2008-05-02 16:49:29 -07007335 /* Wait for RX cpu to ACK this event. */
7336 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337 }
7338}
7339
7340/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007341static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342{
7343 int err;
7344
7345 tg3_stop_fw(tp);
7346
Michael Chan944d9802005-05-29 14:57:48 -07007347 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007349 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 err = tg3_chip_reset(tp);
7351
Matt Carlsondaba2a62009-04-20 06:58:52 +00007352 __tg3_set_mac_addr(tp, 0);
7353
Michael Chan944d9802005-05-29 14:57:48 -07007354 tg3_write_sig_legacy(tp, kind);
7355 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356
7357 if (err)
7358 return err;
7359
7360 return 0;
7361}
7362
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363#define RX_CPU_SCRATCH_BASE 0x30000
7364#define RX_CPU_SCRATCH_SIZE 0x04000
7365#define TX_CPU_SCRATCH_BASE 0x34000
7366#define TX_CPU_SCRATCH_SIZE 0x04000
7367
7368/* tp->lock is held. */
7369static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7370{
7371 int i;
7372
Joe Perches63c3a662011-04-26 08:12:10 +00007373 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374
Michael Chanb5d37722006-09-27 16:06:21 -07007375 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7376 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7377
7378 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7379 return 0;
7380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 if (offset == RX_CPU_BASE) {
7382 for (i = 0; i < 10000; i++) {
7383 tw32(offset + CPU_STATE, 0xffffffff);
7384 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7385 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7386 break;
7387 }
7388
7389 tw32(offset + CPU_STATE, 0xffffffff);
7390 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7391 udelay(10);
7392 } else {
7393 for (i = 0; i < 10000; i++) {
7394 tw32(offset + CPU_STATE, 0xffffffff);
7395 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7396 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7397 break;
7398 }
7399 }
7400
7401 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007402 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7403 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 return -ENODEV;
7405 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007406
7407 /* Clear firmware's nvram arbitration. */
Joe Perches63c3a662011-04-26 08:12:10 +00007408 if (tg3_flag(tp, NVRAM))
Michael Chanec41c7d2006-01-17 02:40:55 -08007409 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007410 return 0;
7411}
7412
7413struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007414 unsigned int fw_base;
7415 unsigned int fw_len;
7416 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417};
7418
7419/* tp->lock is held. */
7420static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7421 int cpu_scratch_size, struct fw_info *info)
7422{
Michael Chanec41c7d2006-01-17 02:40:55 -08007423 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007424 void (*write_op)(struct tg3 *, u32, u32);
7425
Joe Perches63c3a662011-04-26 08:12:10 +00007426 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007427 netdev_err(tp->dev,
7428 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007429 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430 return -EINVAL;
7431 }
7432
Joe Perches63c3a662011-04-26 08:12:10 +00007433 if (tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434 write_op = tg3_write_mem;
7435 else
7436 write_op = tg3_write_indirect_reg32;
7437
Michael Chan1b628152005-05-29 14:59:49 -07007438 /* It is possible that bootcode is still loading at this point.
7439 * Get the nvram lock first before halting the cpu.
7440 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007441 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007442 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007443 if (!lock_err)
7444 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445 if (err)
7446 goto out;
7447
7448 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7449 write_op(tp, cpu_scratch_base + i, 0);
7450 tw32(cpu_base + CPU_STATE, 0xffffffff);
7451 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007452 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007454 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007456 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457
7458 err = 0;
7459
7460out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 return err;
7462}
7463
7464/* tp->lock is held. */
7465static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7466{
7467 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007468 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 int err, i;
7470
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007471 fw_data = (void *)tp->fw->data;
7472
7473 /* Firmware blob starts with version numbers, followed by
7474 start address and length. We are setting complete length.
7475 length = end_address_of_bss - start_address_of_text.
7476 Remainder is the blob to be loaded contiguously
7477 from start address. */
7478
7479 info.fw_base = be32_to_cpu(fw_data[1]);
7480 info.fw_len = tp->fw->size - 12;
7481 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482
7483 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7484 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7485 &info);
7486 if (err)
7487 return err;
7488
7489 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7490 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7491 &info);
7492 if (err)
7493 return err;
7494
7495 /* Now startup only the RX cpu. */
7496 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007497 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498
7499 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007500 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007501 break;
7502 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7503 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007504 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505 udelay(1000);
7506 }
7507 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007508 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7509 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007510 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511 return -ENODEV;
7512 }
7513 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7514 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7515
7516 return 0;
7517}
7518
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519/* tp->lock is held. */
7520static int tg3_load_tso_firmware(struct tg3 *tp)
7521{
7522 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007523 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7525 int err, i;
7526
Joe Perches63c3a662011-04-26 08:12:10 +00007527 if (tg3_flag(tp, HW_TSO_1) ||
7528 tg3_flag(tp, HW_TSO_2) ||
7529 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 return 0;
7531
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007532 fw_data = (void *)tp->fw->data;
7533
7534 /* Firmware blob starts with version numbers, followed by
7535 start address and length. We are setting complete length.
7536 length = end_address_of_bss - start_address_of_text.
7537 Remainder is the blob to be loaded contiguously
7538 from start address. */
7539
7540 info.fw_base = be32_to_cpu(fw_data[1]);
7541 cpu_scratch_size = tp->fw_len;
7542 info.fw_len = tp->fw->size - 12;
7543 info.fw_data = &fw_data[3];
7544
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 cpu_base = RX_CPU_BASE;
7547 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549 cpu_base = TX_CPU_BASE;
7550 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7551 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7552 }
7553
7554 err = tg3_load_firmware_cpu(tp, cpu_base,
7555 cpu_scratch_base, cpu_scratch_size,
7556 &info);
7557 if (err)
7558 return err;
7559
7560 /* Now startup the cpu. */
7561 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007562 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007563
7564 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007565 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566 break;
7567 tw32(cpu_base + CPU_STATE, 0xffffffff);
7568 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007569 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 udelay(1000);
7571 }
7572 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007573 netdev_err(tp->dev,
7574 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007575 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576 return -ENODEV;
7577 }
7578 tw32(cpu_base + CPU_STATE, 0xffffffff);
7579 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7580 return 0;
7581}
7582
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584static int tg3_set_mac_addr(struct net_device *dev, void *p)
7585{
7586 struct tg3 *tp = netdev_priv(dev);
7587 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007588 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589
Michael Chanf9804dd2005-09-27 12:13:10 -07007590 if (!is_valid_ether_addr(addr->sa_data))
7591 return -EINVAL;
7592
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7594
Michael Chane75f7c92006-03-20 21:33:26 -08007595 if (!netif_running(dev))
7596 return 0;
7597
Joe Perches63c3a662011-04-26 08:12:10 +00007598 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007599 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007600
Michael Chan986e0ae2007-05-05 12:10:20 -07007601 addr0_high = tr32(MAC_ADDR_0_HIGH);
7602 addr0_low = tr32(MAC_ADDR_0_LOW);
7603 addr1_high = tr32(MAC_ADDR_1_HIGH);
7604 addr1_low = tr32(MAC_ADDR_1_LOW);
7605
7606 /* Skip MAC addr 1 if ASF is using it. */
7607 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7608 !(addr1_high == 0 && addr1_low == 0))
7609 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007610 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007611 spin_lock_bh(&tp->lock);
7612 __tg3_set_mac_addr(tp, skip_mac_1);
7613 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614
Michael Chanb9ec6c12006-07-25 16:37:27 -07007615 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007616}
7617
7618/* tp->lock is held. */
7619static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7620 dma_addr_t mapping, u32 maxlen_flags,
7621 u32 nic_addr)
7622{
7623 tg3_write_mem(tp,
7624 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7625 ((u64) mapping >> 32));
7626 tg3_write_mem(tp,
7627 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7628 ((u64) mapping & 0xffffffff));
7629 tg3_write_mem(tp,
7630 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7631 maxlen_flags);
7632
Joe Perches63c3a662011-04-26 08:12:10 +00007633 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007634 tg3_write_mem(tp,
7635 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7636 nic_addr);
7637}
7638
7639static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007640static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007641{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007642 int i;
7643
Joe Perches63c3a662011-04-26 08:12:10 +00007644 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007645 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7646 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7647 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007648 } else {
7649 tw32(HOSTCC_TXCOL_TICKS, 0);
7650 tw32(HOSTCC_TXMAX_FRAMES, 0);
7651 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007652 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007653
Joe Perches63c3a662011-04-26 08:12:10 +00007654 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007655 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7656 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7657 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7658 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007659 tw32(HOSTCC_RXCOL_TICKS, 0);
7660 tw32(HOSTCC_RXMAX_FRAMES, 0);
7661 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007662 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007663
Joe Perches63c3a662011-04-26 08:12:10 +00007664 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07007665 u32 val = ec->stats_block_coalesce_usecs;
7666
Matt Carlsonb6080e12009-09-01 13:12:00 +00007667 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7668 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7669
David S. Miller15f98502005-05-18 22:49:26 -07007670 if (!netif_carrier_ok(tp->dev))
7671 val = 0;
7672
7673 tw32(HOSTCC_STAT_COAL_TICKS, val);
7674 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007675
7676 for (i = 0; i < tp->irq_cnt - 1; i++) {
7677 u32 reg;
7678
7679 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7680 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007681 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7682 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007683 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7684 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007685
Joe Perches63c3a662011-04-26 08:12:10 +00007686 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007687 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7688 tw32(reg, ec->tx_coalesce_usecs);
7689 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7690 tw32(reg, ec->tx_max_coalesced_frames);
7691 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7692 tw32(reg, ec->tx_max_coalesced_frames_irq);
7693 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007694 }
7695
7696 for (; i < tp->irq_max - 1; i++) {
7697 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007698 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007699 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007700
Joe Perches63c3a662011-04-26 08:12:10 +00007701 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007702 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7703 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7704 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7705 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007706 }
David S. Miller15f98502005-05-18 22:49:26 -07007707}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708
7709/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007710static void tg3_rings_reset(struct tg3 *tp)
7711{
7712 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007713 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007714 struct tg3_napi *tnapi = &tp->napi[0];
7715
7716 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007717 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007718 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00007719 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00007720 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007721 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7722 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007723 else
7724 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7725
7726 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7727 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7728 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7729 BDINFO_FLAGS_DISABLED);
7730
7731
7732 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007733 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007734 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00007735 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007736 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007737 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7738 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007739 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7740 else
7741 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7742
7743 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7744 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7745 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7746 BDINFO_FLAGS_DISABLED);
7747
7748 /* Disable interrupts */
7749 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00007750 tp->napi[0].chk_msi_cnt = 0;
7751 tp->napi[0].last_rx_cons = 0;
7752 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007753
7754 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00007755 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007756 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007757 tp->napi[i].tx_prod = 0;
7758 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007759 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007760 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007761 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7762 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00007763 tp->napi[0].chk_msi_cnt = 0;
7764 tp->napi[i].last_rx_cons = 0;
7765 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007766 }
Joe Perches63c3a662011-04-26 08:12:10 +00007767 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007768 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007769 } else {
7770 tp->napi[0].tx_prod = 0;
7771 tp->napi[0].tx_cons = 0;
7772 tw32_mailbox(tp->napi[0].prodmbox, 0);
7773 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7774 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007775
7776 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00007777 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00007778 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7779 for (i = 0; i < 16; i++)
7780 tw32_tx_mbox(mbox + i * 8, 0);
7781 }
7782
7783 txrcb = NIC_SRAM_SEND_RCB;
7784 rxrcb = NIC_SRAM_RCV_RET_RCB;
7785
7786 /* Clear status block in ram. */
7787 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7788
7789 /* Set status block DMA address */
7790 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7791 ((u64) tnapi->status_mapping >> 32));
7792 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7793 ((u64) tnapi->status_mapping & 0xffffffff));
7794
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007795 if (tnapi->tx_ring) {
7796 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7797 (TG3_TX_RING_SIZE <<
7798 BDINFO_FLAGS_MAXLEN_SHIFT),
7799 NIC_SRAM_TX_BUFFER_DESC);
7800 txrcb += TG3_BDINFO_SIZE;
7801 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007802
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007803 if (tnapi->rx_rcb) {
7804 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007805 (tp->rx_ret_ring_mask + 1) <<
7806 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007807 rxrcb += TG3_BDINFO_SIZE;
7808 }
7809
7810 stblk = HOSTCC_STATBLCK_RING1;
7811
7812 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7813 u64 mapping = (u64)tnapi->status_mapping;
7814 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7815 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7816
7817 /* Clear status block in ram. */
7818 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7819
Matt Carlson19cfaec2009-12-03 08:36:20 +00007820 if (tnapi->tx_ring) {
7821 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7822 (TG3_TX_RING_SIZE <<
7823 BDINFO_FLAGS_MAXLEN_SHIFT),
7824 NIC_SRAM_TX_BUFFER_DESC);
7825 txrcb += TG3_BDINFO_SIZE;
7826 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007827
7828 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007829 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007830 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7831
7832 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007833 rxrcb += TG3_BDINFO_SIZE;
7834 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007835}
7836
Matt Carlsoneb07a942011-04-20 07:57:36 +00007837static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
7838{
7839 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
7840
Joe Perches63c3a662011-04-26 08:12:10 +00007841 if (!tg3_flag(tp, 5750_PLUS) ||
7842 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00007843 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
7844 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7845 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
7846 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7847 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
7848 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
7849 else
7850 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
7851
7852 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
7853 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
7854
7855 val = min(nic_rep_thresh, host_rep_thresh);
7856 tw32(RCVBDI_STD_THRESH, val);
7857
Joe Perches63c3a662011-04-26 08:12:10 +00007858 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007859 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
7860
Joe Perches63c3a662011-04-26 08:12:10 +00007861 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007862 return;
7863
Joe Perches63c3a662011-04-26 08:12:10 +00007864 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007865 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
7866 else
7867 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
7868
7869 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
7870
7871 val = min(bdcache_maxcnt / 2, host_rep_thresh);
7872 tw32(RCVBDI_JUMBO_THRESH, val);
7873
Joe Perches63c3a662011-04-26 08:12:10 +00007874 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007875 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
7876}
7877
Matt Carlson2d31eca2009-09-01 12:53:31 +00007878/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007879static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007880{
7881 u32 val, rdmac_mode;
7882 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007883 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007884
7885 tg3_disable_ints(tp);
7886
7887 tg3_stop_fw(tp);
7888
7889 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7890
Joe Perches63c3a662011-04-26 08:12:10 +00007891 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07007892 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007893
Matt Carlson699c0192010-12-06 08:28:51 +00007894 /* Enable MAC control of LPI */
7895 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7896 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7897 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7898 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7899
7900 tw32_f(TG3_CPMU_EEE_CTRL,
7901 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7902
Matt Carlsona386b902010-12-06 08:28:53 +00007903 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7904 TG3_CPMU_EEEMD_LPI_IN_TX |
7905 TG3_CPMU_EEEMD_LPI_IN_RX |
7906 TG3_CPMU_EEEMD_EEE_ENABLE;
7907
7908 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
7909 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
7910
Joe Perches63c3a662011-04-26 08:12:10 +00007911 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00007912 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
7913
7914 tw32_f(TG3_CPMU_EEE_MODE, val);
7915
7916 tw32_f(TG3_CPMU_EEE_DBTMR1,
7917 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
7918 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
7919
7920 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00007921 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00007922 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00007923 }
7924
Matt Carlson603f1172010-02-12 14:47:10 +00007925 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007926 tg3_phy_reset(tp);
7927
Linus Torvalds1da177e2005-04-16 15:20:36 -07007928 err = tg3_chip_reset(tp);
7929 if (err)
7930 return err;
7931
7932 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7933
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007934 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007935 val = tr32(TG3_CPMU_CTRL);
7936 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7937 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007938
7939 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7940 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7941 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7942 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7943
7944 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7945 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7946 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7947 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7948
7949 val = tr32(TG3_CPMU_HST_ACC);
7950 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7951 val |= CPMU_HST_ACC_MACCLK_6_25;
7952 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007953 }
7954
Matt Carlson33466d92009-04-20 06:57:41 +00007955 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7956 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7957 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7958 PCIE_PWR_MGMT_L1_THRESH_4MS;
7959 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007960
7961 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7962 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
7963
7964 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00007965
Matt Carlsonf40386c2009-11-02 14:24:02 +00007966 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7967 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00007968 }
7969
Joe Perches63c3a662011-04-26 08:12:10 +00007970 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00007971 u32 grc_mode = tr32(GRC_MODE);
7972
7973 /* Access the lower 1K of PL PCIE block registers. */
7974 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7975 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
7976
7977 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
7978 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
7979 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
7980
7981 tw32(GRC_MODE, grc_mode);
7982 }
7983
Matt Carlson5093eed2010-11-24 08:31:45 +00007984 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
7985 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7986 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00007987
Matt Carlson5093eed2010-11-24 08:31:45 +00007988 /* Access the lower 1K of PL PCIE block registers. */
7989 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
7990 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00007991
Matt Carlson5093eed2010-11-24 08:31:45 +00007992 val = tr32(TG3_PCIE_TLDLPL_PORT +
7993 TG3_PCIE_PL_LO_PHYCTL5);
7994 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
7995 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00007996
Matt Carlson5093eed2010-11-24 08:31:45 +00007997 tw32(GRC_MODE, grc_mode);
7998 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00007999
Matt Carlson1ff30a52011-05-19 12:12:46 +00008000 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8001 u32 grc_mode = tr32(GRC_MODE);
8002
8003 /* Access the lower 1K of DL PCIE block registers. */
8004 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8005 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8006
8007 val = tr32(TG3_PCIE_TLDLPL_PORT +
8008 TG3_PCIE_DL_LO_FTSMAX);
8009 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8010 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8011 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8012
8013 tw32(GRC_MODE, grc_mode);
8014 }
8015
Matt Carlsona977dbe2010-04-12 06:58:26 +00008016 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8017 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8018 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8019 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008020 }
8021
Linus Torvalds1da177e2005-04-16 15:20:36 -07008022 /* This works around an issue with Athlon chipsets on
8023 * B3 tigon3 silicon. This bit has no effect on any
8024 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008025 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008026 */
Joe Perches63c3a662011-04-26 08:12:10 +00008027 if (!tg3_flag(tp, CPMU_PRESENT)) {
8028 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008029 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8030 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008032
8033 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008034 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008035 val = tr32(TG3PCI_PCISTATE);
8036 val |= PCISTATE_RETRY_SAME_DMA;
8037 tw32(TG3PCI_PCISTATE, val);
8038 }
8039
Joe Perches63c3a662011-04-26 08:12:10 +00008040 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008041 /* Allow reads and writes to the
8042 * APE register and memory space.
8043 */
8044 val = tr32(TG3PCI_PCISTATE);
8045 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008046 PCISTATE_ALLOW_APE_SHMEM_WR |
8047 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008048 tw32(TG3PCI_PCISTATE, val);
8049 }
8050
Linus Torvalds1da177e2005-04-16 15:20:36 -07008051 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8052 /* Enable some hw fixes. */
8053 val = tr32(TG3PCI_MSI_DATA);
8054 val |= (1 << 26) | (1 << 28) | (1 << 29);
8055 tw32(TG3PCI_MSI_DATA, val);
8056 }
8057
8058 /* Descriptor ring init may make accesses to the
8059 * NIC SRAM area to setup the TX descriptors, so we
8060 * can only do this after the hardware has been
8061 * successfully reset.
8062 */
Michael Chan32d8c572006-07-25 16:38:29 -07008063 err = tg3_init_rings(tp);
8064 if (err)
8065 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008066
Joe Perches63c3a662011-04-26 08:12:10 +00008067 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008068 val = tr32(TG3PCI_DMA_RW_CTRL) &
8069 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008070 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8071 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008072 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8073 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8074 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008075 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8076 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8077 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008078 /* This value is determined during the probe time DMA
8079 * engine test, tg3_test_dma.
8080 */
8081 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083
8084 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8085 GRC_MODE_4X_NIC_SEND_RINGS |
8086 GRC_MODE_NO_TX_PHDR_CSUM |
8087 GRC_MODE_NO_RX_PHDR_CSUM);
8088 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008089
8090 /* Pseudo-header checksum is done by hardware logic and not
8091 * the offload processers, so make the chip do the pseudo-
8092 * header checksums on receive. For transmit it is more
8093 * convenient to do the pseudo-header checksum in software
8094 * as Linux does that on transmit for us in all cases.
8095 */
8096 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008097
8098 tw32(GRC_MODE,
8099 tp->grc_mode |
8100 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8101
8102 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8103 val = tr32(GRC_MISC_CFG);
8104 val &= ~0xff;
8105 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8106 tw32(GRC_MISC_CFG, val);
8107
8108 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008109 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008110 /* Do nothing. */
8111 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8112 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8113 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8114 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8115 else
8116 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8117 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8118 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008119 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008120 int fw_len;
8121
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008122 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008123 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8124 tw32(BUFMGR_MB_POOL_ADDR,
8125 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8126 tw32(BUFMGR_MB_POOL_SIZE,
8127 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008129
Michael Chan0f893dc2005-07-25 12:30:38 -07008130 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008131 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8132 tp->bufmgr_config.mbuf_read_dma_low_water);
8133 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8134 tp->bufmgr_config.mbuf_mac_rx_low_water);
8135 tw32(BUFMGR_MB_HIGH_WATER,
8136 tp->bufmgr_config.mbuf_high_water);
8137 } else {
8138 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8139 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8140 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8141 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8142 tw32(BUFMGR_MB_HIGH_WATER,
8143 tp->bufmgr_config.mbuf_high_water_jumbo);
8144 }
8145 tw32(BUFMGR_DMA_LOW_WATER,
8146 tp->bufmgr_config.dma_low_water);
8147 tw32(BUFMGR_DMA_HIGH_WATER,
8148 tp->bufmgr_config.dma_high_water);
8149
Matt Carlsond309a462010-09-30 10:34:31 +00008150 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8151 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8152 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008153 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8154 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8155 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8156 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008157 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008158 for (i = 0; i < 2000; i++) {
8159 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8160 break;
8161 udelay(10);
8162 }
8163 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008164 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165 return -ENODEV;
8166 }
8167
Matt Carlsoneb07a942011-04-20 07:57:36 +00008168 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8169 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008170
Matt Carlsoneb07a942011-04-20 07:57:36 +00008171 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008172
8173 /* Initialize TG3_BDINFO's at:
8174 * RCVDBDI_STD_BD: standard eth size rx ring
8175 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8176 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8177 *
8178 * like so:
8179 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8180 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8181 * ring attribute flags
8182 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8183 *
8184 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8185 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8186 *
8187 * The size of each ring is fixed in the firmware, but the location is
8188 * configurable.
8189 */
8190 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008191 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008193 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008194 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008195 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8196 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008197
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008198 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008199 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008200 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8201 BDINFO_FLAGS_DISABLED);
8202
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008203 /* Program the jumbo buffer descriptor ring control
8204 * blocks on those devices that have them.
8205 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008206 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008207 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008208
Joe Perches63c3a662011-04-26 08:12:10 +00008209 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008210 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008211 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008212 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008213 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008214 val = TG3_RX_JMB_RING_SIZE(tp) <<
8215 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008217 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008218 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008219 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008220 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8221 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008222 } else {
8223 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8224 BDINFO_FLAGS_DISABLED);
8225 }
8226
Joe Perches63c3a662011-04-26 08:12:10 +00008227 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008228 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008229 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008230 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008231 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008232 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8233 val |= (TG3_RX_STD_DMA_SZ << 2);
8234 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008235 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008236 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008237 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008238
8239 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240
Matt Carlson411da642009-11-13 13:03:46 +00008241 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008242 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243
Joe Perches63c3a662011-04-26 08:12:10 +00008244 tpr->rx_jmb_prod_idx =
8245 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008246 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008247
Matt Carlson2d31eca2009-09-01 12:53:31 +00008248 tg3_rings_reset(tp);
8249
Linus Torvalds1da177e2005-04-16 15:20:36 -07008250 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008251 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008252
8253 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008254 tw32(MAC_RX_MTU_SIZE,
8255 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008256
8257 /* The slot time is changed by tg3_setup_phy if we
8258 * run at gigabit with half duplex.
8259 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008260 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8261 (6 << TX_LENGTHS_IPG_SHIFT) |
8262 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8263
8264 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8265 val |= tr32(MAC_TX_LENGTHS) &
8266 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8267 TX_LENGTHS_CNT_DWN_VAL_MSK);
8268
8269 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008270
8271 /* Receive rules. */
8272 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8273 tw32(RCVLPC_CONFIG, 0x0181);
8274
8275 /* Calculate RDMAC_MODE setting early, we need it to determine
8276 * the RCVLPC_STATE_ENABLE mask.
8277 */
8278 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8279 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8280 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8281 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8282 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008283
Matt Carlsondeabaac2010-11-24 08:31:50 +00008284 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008285 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8286
Matt Carlson57e69832008-05-25 23:48:31 -07008287 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008288 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8289 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008290 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8291 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8292 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8293
Matt Carlsonc5908932011-03-09 16:58:25 +00008294 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8295 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008296 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008297 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008298 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8299 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008300 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8302 }
8303 }
8304
Joe Perches63c3a662011-04-26 08:12:10 +00008305 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008306 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8307
Joe Perches63c3a662011-04-26 08:12:10 +00008308 if (tg3_flag(tp, HW_TSO_1) ||
8309 tg3_flag(tp, HW_TSO_2) ||
8310 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008311 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8312
Matt Carlson108a6c12011-05-19 12:12:47 +00008313 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008314 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008315 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8316 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008317
Matt Carlsonf2096f92011-04-05 14:22:48 +00008318 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8319 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8320
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008321 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8322 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8323 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8324 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008325 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008326 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008327 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8328 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008329 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8330 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8331 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8332 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8333 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8334 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008335 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008336 tw32(TG3_RDMA_RSRVCTRL_REG,
8337 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8338 }
8339
Matt Carlsond78b59f2011-04-05 14:22:46 +00008340 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8341 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008342 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8343 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8344 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8345 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8346 }
8347
Linus Torvalds1da177e2005-04-16 15:20:36 -07008348 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008349 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008350 val = tr32(RCVLPC_STATS_ENABLE);
8351 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8352 tw32(RCVLPC_STATS_ENABLE, val);
8353 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008354 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008355 val = tr32(RCVLPC_STATS_ENABLE);
8356 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8357 tw32(RCVLPC_STATS_ENABLE, val);
8358 } else {
8359 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8360 }
8361 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8362 tw32(SNDDATAI_STATSENAB, 0xffffff);
8363 tw32(SNDDATAI_STATSCTRL,
8364 (SNDDATAI_SCTRL_ENABLE |
8365 SNDDATAI_SCTRL_FASTUPD));
8366
8367 /* Setup host coalescing engine. */
8368 tw32(HOSTCC_MODE, 0);
8369 for (i = 0; i < 2000; i++) {
8370 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8371 break;
8372 udelay(10);
8373 }
8374
Michael Chand244c892005-07-05 14:42:33 -07008375 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008376
Joe Perches63c3a662011-04-26 08:12:10 +00008377 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378 /* Status/statistics block address. See tg3_timer,
8379 * the tg3_periodic_fetch_stats call there, and
8380 * tg3_get_stats to see how this works for 5705/5750 chips.
8381 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8383 ((u64) tp->stats_mapping >> 32));
8384 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8385 ((u64) tp->stats_mapping & 0xffffffff));
8386 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008387
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008389
8390 /* Clear statistics and status block memory areas */
8391 for (i = NIC_SRAM_STATS_BLK;
8392 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8393 i += sizeof(u32)) {
8394 tg3_write_mem(tp, i, 0);
8395 udelay(40);
8396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008397 }
8398
8399 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8400
8401 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8402 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008403 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008404 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8405
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008406 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8407 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008408 /* reset to prevent losing 1st rx packet intermittently */
8409 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8410 udelay(10);
8411 }
8412
Joe Perches63c3a662011-04-26 08:12:10 +00008413 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008414 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008415 else
8416 tp->mac_mode = 0;
8417 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008418 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008419 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008420 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008421 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8422 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008423 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8424 udelay(40);
8425
Michael Chan314fba32005-04-21 17:07:04 -07008426 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008427 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008428 * register to preserve the GPIO settings for LOMs. The GPIOs,
8429 * whether used as inputs or outputs, are set by boot code after
8430 * reset.
8431 */
Joe Perches63c3a662011-04-26 08:12:10 +00008432 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008433 u32 gpio_mask;
8434
Michael Chan9d26e212006-12-07 00:21:14 -08008435 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8436 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8437 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008438
8439 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8440 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8441 GRC_LCLCTRL_GPIO_OUTPUT3;
8442
Michael Chanaf36e6b2006-03-23 01:28:06 -08008443 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8444 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8445
Gary Zambranoaaf84462007-05-05 11:51:45 -07008446 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008447 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8448
8449 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008450 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008451 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8452 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8455 udelay(100);
8456
Joe Perches63c3a662011-04-26 08:12:10 +00008457 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008458 val = tr32(MSGINT_MODE);
8459 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8460 tw32(MSGINT_MODE, val);
8461 }
8462
Joe Perches63c3a662011-04-26 08:12:10 +00008463 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008464 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8465 udelay(40);
8466 }
8467
8468 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8469 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8470 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8471 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8472 WDMAC_MODE_LNGREAD_ENAB);
8473
Matt Carlsonc5908932011-03-09 16:58:25 +00008474 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8475 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008476 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008477 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8478 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8479 /* nothing */
8480 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008481 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008482 val |= WDMAC_MODE_RX_ACCEL;
8483 }
8484 }
8485
Michael Chand9ab5ad12006-03-20 22:27:35 -08008486 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008487 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008488 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08008489
Matt Carlson788a0352009-11-02 14:26:03 +00008490 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8491 val |= WDMAC_MODE_BURST_ALL_DATA;
8492
Linus Torvalds1da177e2005-04-16 15:20:36 -07008493 tw32_f(WDMAC_MODE, val);
8494 udelay(40);
8495
Joe Perches63c3a662011-04-26 08:12:10 +00008496 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008497 u16 pcix_cmd;
8498
8499 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8500 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008502 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8503 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008504 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008505 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8506 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507 }
Matt Carlson9974a352007-10-07 23:27:28 -07008508 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8509 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008510 }
8511
8512 tw32_f(RDMAC_MODE, rdmac_mode);
8513 udelay(40);
8514
8515 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008516 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008518
8519 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8520 tw32(SNDDATAC_MODE,
8521 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8522 else
8523 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8524
Linus Torvalds1da177e2005-04-16 15:20:36 -07008525 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8526 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008527 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008528 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008529 val |= RCVDBDI_MODE_LRG_RING_SZ;
8530 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008532 if (tg3_flag(tp, HW_TSO_1) ||
8533 tg3_flag(tp, HW_TSO_2) ||
8534 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008535 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008536 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008537 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008538 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8539 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8541
8542 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8543 err = tg3_load_5701_a0_firmware_fix(tp);
8544 if (err)
8545 return err;
8546 }
8547
Joe Perches63c3a662011-04-26 08:12:10 +00008548 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008549 err = tg3_load_tso_firmware(tp);
8550 if (err)
8551 return err;
8552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008553
8554 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008555
Joe Perches63c3a662011-04-26 08:12:10 +00008556 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008557 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8558 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008559
8560 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8561 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8562 tp->tx_mode &= ~val;
8563 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8564 }
8565
Linus Torvalds1da177e2005-04-16 15:20:36 -07008566 tw32_f(MAC_TX_MODE, tp->tx_mode);
8567 udelay(100);
8568
Joe Perches63c3a662011-04-26 08:12:10 +00008569 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008570 u32 reg = MAC_RSS_INDIR_TBL_0;
8571 u8 *ent = (u8 *)&val;
8572
8573 /* Setup the indirection table */
8574 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8575 int idx = i % sizeof(val);
8576
Matt Carlson5efeeea2010-07-11 09:31:40 +00008577 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008578 if (idx == sizeof(val) - 1) {
8579 tw32(reg, val);
8580 reg += 4;
8581 }
8582 }
8583
8584 /* Setup the "secret" hash key. */
8585 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8586 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8587 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8588 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8589 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8590 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8591 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8592 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8593 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8594 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8595 }
8596
Linus Torvalds1da177e2005-04-16 15:20:36 -07008597 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008598 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008599 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8600
Joe Perches63c3a662011-04-26 08:12:10 +00008601 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008602 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8603 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8604 RX_MODE_RSS_IPV6_HASH_EN |
8605 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8606 RX_MODE_RSS_IPV4_HASH_EN |
8607 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8608
Linus Torvalds1da177e2005-04-16 15:20:36 -07008609 tw32_f(MAC_RX_MODE, tp->rx_mode);
8610 udelay(10);
8611
Linus Torvalds1da177e2005-04-16 15:20:36 -07008612 tw32(MAC_LED_CTRL, tp->led_ctrl);
8613
8614 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008615 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008616 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8617 udelay(10);
8618 }
8619 tw32_f(MAC_RX_MODE, tp->rx_mode);
8620 udelay(10);
8621
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008622 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008623 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008624 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008625 /* Set drive transmission level to 1.2V */
8626 /* only if the signal pre-emphasis bit is not set */
8627 val = tr32(MAC_SERDES_CFG);
8628 val &= 0xfffff000;
8629 val |= 0x880;
8630 tw32(MAC_SERDES_CFG, val);
8631 }
8632 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8633 tw32(MAC_SERDES_CFG, 0x616000);
8634 }
8635
8636 /* Prevent chip from dropping frames when flow control
8637 * is enabled.
8638 */
Matt Carlson666bc832010-01-20 16:58:03 +00008639 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8640 val = 1;
8641 else
8642 val = 2;
8643 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644
8645 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008646 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00008648 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649 }
8650
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008651 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00008652 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08008653 u32 tmp;
8654
8655 tmp = tr32(SERDES_RX_CTRL);
8656 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8657 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8658 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8659 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8660 }
8661
Joe Perches63c3a662011-04-26 08:12:10 +00008662 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008663 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8664 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008665 tp->link_config.speed = tp->link_config.orig_speed;
8666 tp->link_config.duplex = tp->link_config.orig_duplex;
8667 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008669
Matt Carlsondd477002008-05-25 23:45:58 -07008670 err = tg3_setup_phy(tp, 0);
8671 if (err)
8672 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008673
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008674 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8675 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008676 u32 tmp;
8677
8678 /* Clear CRC stats. */
8679 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8680 tg3_writephy(tp, MII_TG3_TEST1,
8681 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008682 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008684 }
8685 }
8686
8687 __tg3_set_rx_mode(tp->dev);
8688
8689 /* Initialize receive rules. */
8690 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8691 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8692 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8693 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8694
Joe Perches63c3a662011-04-26 08:12:10 +00008695 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008696 limit = 8;
8697 else
8698 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008699 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008700 limit -= 4;
8701 switch (limit) {
8702 case 16:
8703 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8704 case 15:
8705 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8706 case 14:
8707 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8708 case 13:
8709 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8710 case 12:
8711 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8712 case 11:
8713 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8714 case 10:
8715 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8716 case 9:
8717 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8718 case 8:
8719 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8720 case 7:
8721 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8722 case 6:
8723 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8724 case 5:
8725 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8726 case 4:
8727 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8728 case 3:
8729 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8730 case 2:
8731 case 1:
8732
8733 default:
8734 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008736
Joe Perches63c3a662011-04-26 08:12:10 +00008737 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07008738 /* Write our heartbeat update interval to APE. */
8739 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8740 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008741
Linus Torvalds1da177e2005-04-16 15:20:36 -07008742 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8743
Linus Torvalds1da177e2005-04-16 15:20:36 -07008744 return 0;
8745}
8746
8747/* Called at device open time to get the chip ready for
8748 * packet processing. Invoked with tp->lock held.
8749 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008750static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008751{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752 tg3_switch_clocks(tp);
8753
8754 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8755
Matt Carlson2f751b62008-08-04 23:17:34 -07008756 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008757}
8758
8759#define TG3_STAT_ADD32(PSTAT, REG) \
8760do { u32 __val = tr32(REG); \
8761 (PSTAT)->low += __val; \
8762 if ((PSTAT)->low < __val) \
8763 (PSTAT)->high += 1; \
8764} while (0)
8765
8766static void tg3_periodic_fetch_stats(struct tg3 *tp)
8767{
8768 struct tg3_hw_stats *sp = tp->hw_stats;
8769
8770 if (!netif_carrier_ok(tp->dev))
8771 return;
8772
8773 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8774 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8775 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8776 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8777 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8778 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8779 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8780 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8781 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8782 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8783 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8784 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8785 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8786
8787 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8788 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8789 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8790 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8791 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8792 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8793 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8794 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8795 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8796 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8797 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8798 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8799 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8800 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008801
8802 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00008803 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8804 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
8805 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00008806 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8807 } else {
8808 u32 val = tr32(HOSTCC_FLOW_ATTN);
8809 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
8810 if (val) {
8811 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
8812 sp->rx_discards.low += val;
8813 if (sp->rx_discards.low < val)
8814 sp->rx_discards.high += 1;
8815 }
8816 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
8817 }
Michael Chan463d3052006-05-22 16:36:27 -07008818 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008819}
8820
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008821static void tg3_chk_missed_msi(struct tg3 *tp)
8822{
8823 u32 i;
8824
8825 for (i = 0; i < tp->irq_cnt; i++) {
8826 struct tg3_napi *tnapi = &tp->napi[i];
8827
8828 if (tg3_has_work(tnapi)) {
8829 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
8830 tnapi->last_tx_cons == tnapi->tx_cons) {
8831 if (tnapi->chk_msi_cnt < 1) {
8832 tnapi->chk_msi_cnt++;
8833 return;
8834 }
8835 tw32_mailbox(tnapi->int_mbox,
8836 tnapi->last_tag << 24);
8837 }
8838 }
8839 tnapi->chk_msi_cnt = 0;
8840 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
8841 tnapi->last_tx_cons = tnapi->tx_cons;
8842 }
8843}
8844
Linus Torvalds1da177e2005-04-16 15:20:36 -07008845static void tg3_timer(unsigned long __opaque)
8846{
8847 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008848
Michael Chanf475f162006-03-27 23:20:14 -08008849 if (tp->irq_sync)
8850 goto restart_timer;
8851
David S. Millerf47c11e2005-06-24 20:18:35 -07008852 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008853
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008854 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8855 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8856 tg3_chk_missed_msi(tp);
8857
Joe Perches63c3a662011-04-26 08:12:10 +00008858 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07008859 /* All of this garbage is because when using non-tagged
8860 * IRQ status the mailbox/status_block protocol the chip
8861 * uses with the cpu is race prone.
8862 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008863 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008864 tw32(GRC_LOCAL_CTRL,
8865 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8866 } else {
8867 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008868 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008870
David S. Millerfac9b832005-05-18 22:46:34 -07008871 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +00008872 tg3_flag_set(tp, RESTART_TIMER);
David S. Millerf47c11e2005-06-24 20:18:35 -07008873 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008874 schedule_work(&tp->reset_task);
8875 return;
8876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008877 }
8878
Linus Torvalds1da177e2005-04-16 15:20:36 -07008879 /* This part only runs once per second. */
8880 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008881 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07008882 tg3_periodic_fetch_stats(tp);
8883
Matt Carlsonb0c59432011-05-19 12:12:48 +00008884 if (tp->setlpicnt && !--tp->setlpicnt)
8885 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00008886
Joe Perches63c3a662011-04-26 08:12:10 +00008887 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888 u32 mac_stat;
8889 int phy_event;
8890
8891 mac_stat = tr32(MAC_STATUS);
8892
8893 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008894 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008895 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8896 phy_event = 1;
8897 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8898 phy_event = 1;
8899
8900 if (phy_event)
8901 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00008902 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008903 u32 mac_stat = tr32(MAC_STATUS);
8904 int need_setup = 0;
8905
8906 if (netif_carrier_ok(tp->dev) &&
8907 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8908 need_setup = 1;
8909 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008910 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008911 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8912 MAC_STATUS_SIGNAL_DET))) {
8913 need_setup = 1;
8914 }
8915 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008916 if (!tp->serdes_counter) {
8917 tw32_f(MAC_MODE,
8918 (tp->mac_mode &
8919 ~MAC_MODE_PORT_MODE_MASK));
8920 udelay(40);
8921 tw32_f(MAC_MODE, tp->mac_mode);
8922 udelay(40);
8923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008924 tg3_setup_phy(tp, 0);
8925 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008926 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008927 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008928 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008930
8931 tp->timer_counter = tp->timer_multiplier;
8932 }
8933
Michael Chan130b8e42006-09-27 16:00:40 -07008934 /* Heartbeat is only sent once every 2 seconds.
8935 *
8936 * The heartbeat is to tell the ASF firmware that the host
8937 * driver is still alive. In the event that the OS crashes,
8938 * ASF needs to reset the hardware to free up the FIFO space
8939 * that may be filled with rx packets destined for the host.
8940 * If the FIFO is full, ASF will no longer function properly.
8941 *
8942 * Unintended resets have been reported on real time kernels
8943 * where the timer doesn't run on time. Netpoll will also have
8944 * same problem.
8945 *
8946 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8947 * to check the ring condition when the heartbeat is expiring
8948 * before doing the reset. This will prevent most unintended
8949 * resets.
8950 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008951 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008952 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008953 tg3_wait_for_event_ack(tp);
8954
Michael Chanbbadf502006-04-06 21:46:34 -07008955 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008956 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008957 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008958 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8959 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008960
8961 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008962 }
8963 tp->asf_counter = tp->asf_multiplier;
8964 }
8965
David S. Millerf47c11e2005-06-24 20:18:35 -07008966 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008967
Michael Chanf475f162006-03-27 23:20:14 -08008968restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008969 tp->timer.expires = jiffies + tp->timer_offset;
8970 add_timer(&tp->timer);
8971}
8972
Matt Carlson4f125f42009-09-01 12:55:02 +00008973static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08008974{
David Howells7d12e782006-10-05 14:55:46 +01008975 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008976 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00008977 char *name;
8978 struct tg3_napi *tnapi = &tp->napi[irq_num];
8979
8980 if (tp->irq_cnt == 1)
8981 name = tp->dev->name;
8982 else {
8983 name = &tnapi->irq_lbl[0];
8984 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
8985 name[IFNAMSIZ-1] = 0;
8986 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08008987
Joe Perches63c3a662011-04-26 08:12:10 +00008988 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08008989 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00008990 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08008991 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008992 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008993 } else {
8994 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00008995 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08008996 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00008997 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08008998 }
Matt Carlson4f125f42009-09-01 12:55:02 +00008999
9000 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009001}
9002
Michael Chan79381092005-04-21 17:13:59 -07009003static int tg3_test_interrupt(struct tg3 *tp)
9004{
Matt Carlson09943a12009-08-28 14:01:57 +00009005 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009006 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009007 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009008 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009009
Michael Chand4bc3922005-05-29 14:59:20 -07009010 if (!netif_running(dev))
9011 return -ENODEV;
9012
Michael Chan79381092005-04-21 17:13:59 -07009013 tg3_disable_ints(tp);
9014
Matt Carlson4f125f42009-09-01 12:55:02 +00009015 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009016
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009017 /*
9018 * Turn off MSI one shot mode. Otherwise this test has no
9019 * observable way to know whether the interrupt was delivered.
9020 */
Joe Perches63c3a662011-04-26 08:12:10 +00009021 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009022 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9023 tw32(MSGINT_MODE, val);
9024 }
9025
Matt Carlson4f125f42009-09-01 12:55:02 +00009026 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009027 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009028 if (err)
9029 return err;
9030
Matt Carlson898a56f2009-08-28 14:02:40 +00009031 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009032 tg3_enable_ints(tp);
9033
9034 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009035 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009036
9037 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009038 u32 int_mbox, misc_host_ctrl;
9039
Matt Carlson898a56f2009-08-28 14:02:40 +00009040 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009041 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9042
9043 if ((int_mbox != 0) ||
9044 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9045 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009046 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009047 }
9048
Michael Chan79381092005-04-21 17:13:59 -07009049 msleep(10);
9050 }
9051
9052 tg3_disable_ints(tp);
9053
Matt Carlson4f125f42009-09-01 12:55:02 +00009054 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009055
Matt Carlson4f125f42009-09-01 12:55:02 +00009056 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009057
9058 if (err)
9059 return err;
9060
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009061 if (intr_ok) {
9062 /* Reenable MSI one shot mode. */
Joe Perches63c3a662011-04-26 08:12:10 +00009063 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009064 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9065 tw32(MSGINT_MODE, val);
9066 }
Michael Chan79381092005-04-21 17:13:59 -07009067 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009068 }
Michael Chan79381092005-04-21 17:13:59 -07009069
9070 return -EIO;
9071}
9072
9073/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9074 * successfully restored
9075 */
9076static int tg3_test_msi(struct tg3 *tp)
9077{
Michael Chan79381092005-04-21 17:13:59 -07009078 int err;
9079 u16 pci_cmd;
9080
Joe Perches63c3a662011-04-26 08:12:10 +00009081 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009082 return 0;
9083
9084 /* Turn off SERR reporting in case MSI terminates with Master
9085 * Abort.
9086 */
9087 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9088 pci_write_config_word(tp->pdev, PCI_COMMAND,
9089 pci_cmd & ~PCI_COMMAND_SERR);
9090
9091 err = tg3_test_interrupt(tp);
9092
9093 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9094
9095 if (!err)
9096 return 0;
9097
9098 /* other failures */
9099 if (err != -EIO)
9100 return err;
9101
9102 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009103 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9104 "to INTx mode. Please report this failure to the PCI "
9105 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009106
Matt Carlson4f125f42009-09-01 12:55:02 +00009107 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009108
Michael Chan79381092005-04-21 17:13:59 -07009109 pci_disable_msi(tp->pdev);
9110
Joe Perches63c3a662011-04-26 08:12:10 +00009111 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009112 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009113
Matt Carlson4f125f42009-09-01 12:55:02 +00009114 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009115 if (err)
9116 return err;
9117
9118 /* Need to reset the chip because the MSI cycle may have terminated
9119 * with Master Abort.
9120 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009121 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009122
Michael Chan944d9802005-05-29 14:57:48 -07009123 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009124 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009125
David S. Millerf47c11e2005-06-24 20:18:35 -07009126 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009127
9128 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009129 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009130
9131 return err;
9132}
9133
Matt Carlson9e9fd122009-01-19 16:57:45 -08009134static int tg3_request_firmware(struct tg3 *tp)
9135{
9136 const __be32 *fw_data;
9137
9138 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009139 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9140 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009141 return -ENOENT;
9142 }
9143
9144 fw_data = (void *)tp->fw->data;
9145
9146 /* Firmware blob starts with version numbers, followed by
9147 * start address and _full_ length including BSS sections
9148 * (which must be longer than the actual data, of course
9149 */
9150
9151 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9152 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009153 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9154 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009155 release_firmware(tp->fw);
9156 tp->fw = NULL;
9157 return -EINVAL;
9158 }
9159
9160 /* We no longer need firmware; we have it. */
9161 tp->fw_needed = NULL;
9162 return 0;
9163}
9164
Matt Carlson679563f2009-09-01 12:55:46 +00009165static bool tg3_enable_msix(struct tg3 *tp)
9166{
9167 int i, rc, cpus = num_online_cpus();
9168 struct msix_entry msix_ent[tp->irq_max];
9169
9170 if (cpus == 1)
9171 /* Just fallback to the simpler MSI mode. */
9172 return false;
9173
9174 /*
9175 * We want as many rx rings enabled as there are cpus.
9176 * The first MSIX vector only deals with link interrupts, etc,
9177 * so we add one to the number of vectors we are requesting.
9178 */
9179 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9180
9181 for (i = 0; i < tp->irq_max; i++) {
9182 msix_ent[i].entry = i;
9183 msix_ent[i].vector = 0;
9184 }
9185
9186 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009187 if (rc < 0) {
9188 return false;
9189 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009190 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9191 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009192 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9193 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009194 tp->irq_cnt = rc;
9195 }
9196
9197 for (i = 0; i < tp->irq_max; i++)
9198 tp->napi[i].irq_vec = msix_ent[i].vector;
9199
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009200 netif_set_real_num_tx_queues(tp->dev, 1);
9201 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9202 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9203 pci_disable_msix(tp->pdev);
9204 return false;
9205 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009206
9207 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009208 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009209
9210 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9211 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009212 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009213 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9214 }
9215 }
Matt Carlson2430b032010-06-05 17:24:34 +00009216
Matt Carlson679563f2009-09-01 12:55:46 +00009217 return true;
9218}
9219
Matt Carlson07b01732009-08-28 14:01:15 +00009220static void tg3_ints_init(struct tg3 *tp)
9221{
Joe Perches63c3a662011-04-26 08:12:10 +00009222 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9223 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009224 /* All MSI supporting chips should support tagged
9225 * status. Assert that this is the case.
9226 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009227 netdev_warn(tp->dev,
9228 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009229 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009230 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009231
Joe Perches63c3a662011-04-26 08:12:10 +00009232 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9233 tg3_flag_set(tp, USING_MSIX);
9234 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9235 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009236
Joe Perches63c3a662011-04-26 08:12:10 +00009237 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009238 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009239 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009240 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009241 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9242 }
9243defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009244 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009245 tp->irq_cnt = 1;
9246 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009247 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009248 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009249 }
Matt Carlson07b01732009-08-28 14:01:15 +00009250}
9251
9252static void tg3_ints_fini(struct tg3 *tp)
9253{
Joe Perches63c3a662011-04-26 08:12:10 +00009254 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009255 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009256 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009257 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009258 tg3_flag_clear(tp, USING_MSI);
9259 tg3_flag_clear(tp, USING_MSIX);
9260 tg3_flag_clear(tp, ENABLE_RSS);
9261 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009262}
9263
Linus Torvalds1da177e2005-04-16 15:20:36 -07009264static int tg3_open(struct net_device *dev)
9265{
9266 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009267 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009268
Matt Carlson9e9fd122009-01-19 16:57:45 -08009269 if (tp->fw_needed) {
9270 err = tg3_request_firmware(tp);
9271 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9272 if (err)
9273 return err;
9274 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009275 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009276 tg3_flag_clear(tp, TSO_CAPABLE);
9277 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009278 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009279 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009280 }
9281 }
9282
Michael Chanc49a1562006-12-17 17:07:29 -08009283 netif_carrier_off(tp->dev);
9284
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009285 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009286 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009287 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009288
9289 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009290
Linus Torvalds1da177e2005-04-16 15:20:36 -07009291 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009292 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009293
David S. Millerf47c11e2005-06-24 20:18:35 -07009294 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009295
Matt Carlson679563f2009-09-01 12:55:46 +00009296 /*
9297 * Setup interrupts first so we know how
9298 * many NAPI resources to allocate
9299 */
9300 tg3_ints_init(tp);
9301
Linus Torvalds1da177e2005-04-16 15:20:36 -07009302 /* The placement of this call is tied
9303 * to the setup and use of Host TX descriptors.
9304 */
9305 err = tg3_alloc_consistent(tp);
9306 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009307 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009308
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009309 tg3_napi_init(tp);
9310
Matt Carlsonfed97812009-09-01 13:10:19 +00009311 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009312
Matt Carlson4f125f42009-09-01 12:55:02 +00009313 for (i = 0; i < tp->irq_cnt; i++) {
9314 struct tg3_napi *tnapi = &tp->napi[i];
9315 err = tg3_request_irq(tp, i);
9316 if (err) {
9317 for (i--; i >= 0; i--)
9318 free_irq(tnapi->irq_vec, tnapi);
9319 break;
9320 }
9321 }
Matt Carlson07b01732009-08-28 14:01:15 +00009322
9323 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009324 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009325
David S. Millerf47c11e2005-06-24 20:18:35 -07009326 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009327
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009328 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009330 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009331 tg3_free_rings(tp);
9332 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009333 if (tg3_flag(tp, TAGGED_STATUS) &&
9334 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9335 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765)
David S. Millerfac9b832005-05-18 22:46:34 -07009336 tp->timer_offset = HZ;
9337 else
9338 tp->timer_offset = HZ / 10;
9339
9340 BUG_ON(tp->timer_offset > HZ);
9341 tp->timer_counter = tp->timer_multiplier =
9342 (HZ / tp->timer_offset);
9343 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009344 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009345
9346 init_timer(&tp->timer);
9347 tp->timer.expires = jiffies + tp->timer_offset;
9348 tp->timer.data = (unsigned long) tp;
9349 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009350 }
9351
David S. Millerf47c11e2005-06-24 20:18:35 -07009352 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009353
Matt Carlson07b01732009-08-28 14:01:15 +00009354 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009355 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009356
Joe Perches63c3a662011-04-26 08:12:10 +00009357 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009358 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009359
Michael Chan79381092005-04-21 17:13:59 -07009360 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009361 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009362 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009363 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009364 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009365
Matt Carlson679563f2009-09-01 12:55:46 +00009366 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009367 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009368
Joe Perches63c3a662011-04-26 08:12:10 +00009369 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009370 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009371
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009372 tw32(PCIE_TRANSACTION_CFG,
9373 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009374 }
Michael Chan79381092005-04-21 17:13:59 -07009375 }
9376
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009377 tg3_phy_start(tp);
9378
David S. Millerf47c11e2005-06-24 20:18:35 -07009379 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009380
Michael Chan79381092005-04-21 17:13:59 -07009381 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009382 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009383 tg3_enable_ints(tp);
9384
David S. Millerf47c11e2005-06-24 20:18:35 -07009385 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009386
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009387 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009388
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009389 /*
9390 * Reset loopback feature if it was turned on while the device was down
9391 * make sure that it's installed properly now.
9392 */
9393 if (dev->features & NETIF_F_LOOPBACK)
9394 tg3_set_loopback(dev, dev->features);
9395
Linus Torvalds1da177e2005-04-16 15:20:36 -07009396 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009397
Matt Carlson679563f2009-09-01 12:55:46 +00009398err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009399 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9400 struct tg3_napi *tnapi = &tp->napi[i];
9401 free_irq(tnapi->irq_vec, tnapi);
9402 }
Matt Carlson07b01732009-08-28 14:01:15 +00009403
Matt Carlson679563f2009-09-01 12:55:46 +00009404err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009405 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009406 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009407 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009408
9409err_out1:
9410 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009411 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009412}
9413
Eric Dumazet511d2222010-07-07 20:44:24 +00009414static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9415 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009416static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9417
9418static int tg3_close(struct net_device *dev)
9419{
Matt Carlson4f125f42009-09-01 12:55:02 +00009420 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009421 struct tg3 *tp = netdev_priv(dev);
9422
Matt Carlsonfed97812009-09-01 13:10:19 +00009423 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009424 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009425
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009426 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009427
9428 del_timer_sync(&tp->timer);
9429
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009430 tg3_phy_stop(tp);
9431
David S. Millerf47c11e2005-06-24 20:18:35 -07009432 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433
9434 tg3_disable_ints(tp);
9435
Michael Chan944d9802005-05-29 14:57:48 -07009436 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009438 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439
David S. Millerf47c11e2005-06-24 20:18:35 -07009440 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009441
Matt Carlson4f125f42009-09-01 12:55:02 +00009442 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9443 struct tg3_napi *tnapi = &tp->napi[i];
9444 free_irq(tnapi->irq_vec, tnapi);
9445 }
Matt Carlson07b01732009-08-28 14:01:15 +00009446
9447 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009448
Eric Dumazet511d2222010-07-07 20:44:24 +00009449 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9450
Linus Torvalds1da177e2005-04-16 15:20:36 -07009451 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9452 sizeof(tp->estats_prev));
9453
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009454 tg3_napi_fini(tp);
9455
Linus Torvalds1da177e2005-04-16 15:20:36 -07009456 tg3_free_consistent(tp);
9457
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009458 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009459
9460 netif_carrier_off(tp->dev);
9461
Linus Torvalds1da177e2005-04-16 15:20:36 -07009462 return 0;
9463}
9464
Eric Dumazet511d2222010-07-07 20:44:24 +00009465static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009466{
9467 return ((u64)val->high << 32) | ((u64)val->low);
9468}
9469
Eric Dumazet511d2222010-07-07 20:44:24 +00009470static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009471{
9472 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9473
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009474 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009475 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9476 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009477 u32 val;
9478
David S. Millerf47c11e2005-06-24 20:18:35 -07009479 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009480 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9481 tg3_writephy(tp, MII_TG3_TEST1,
9482 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009483 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009484 } else
9485 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009486 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009487
9488 tp->phy_crc_errors += val;
9489
9490 return tp->phy_crc_errors;
9491 }
9492
9493 return get_stat64(&hw_stats->rx_fcs_errors);
9494}
9495
9496#define ESTAT_ADD(member) \
9497 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009498 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009499
9500static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9501{
9502 struct tg3_ethtool_stats *estats = &tp->estats;
9503 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9504 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9505
9506 if (!hw_stats)
9507 return old_estats;
9508
9509 ESTAT_ADD(rx_octets);
9510 ESTAT_ADD(rx_fragments);
9511 ESTAT_ADD(rx_ucast_packets);
9512 ESTAT_ADD(rx_mcast_packets);
9513 ESTAT_ADD(rx_bcast_packets);
9514 ESTAT_ADD(rx_fcs_errors);
9515 ESTAT_ADD(rx_align_errors);
9516 ESTAT_ADD(rx_xon_pause_rcvd);
9517 ESTAT_ADD(rx_xoff_pause_rcvd);
9518 ESTAT_ADD(rx_mac_ctrl_rcvd);
9519 ESTAT_ADD(rx_xoff_entered);
9520 ESTAT_ADD(rx_frame_too_long_errors);
9521 ESTAT_ADD(rx_jabbers);
9522 ESTAT_ADD(rx_undersize_packets);
9523 ESTAT_ADD(rx_in_length_errors);
9524 ESTAT_ADD(rx_out_length_errors);
9525 ESTAT_ADD(rx_64_or_less_octet_packets);
9526 ESTAT_ADD(rx_65_to_127_octet_packets);
9527 ESTAT_ADD(rx_128_to_255_octet_packets);
9528 ESTAT_ADD(rx_256_to_511_octet_packets);
9529 ESTAT_ADD(rx_512_to_1023_octet_packets);
9530 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9531 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9532 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9533 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9534 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9535
9536 ESTAT_ADD(tx_octets);
9537 ESTAT_ADD(tx_collisions);
9538 ESTAT_ADD(tx_xon_sent);
9539 ESTAT_ADD(tx_xoff_sent);
9540 ESTAT_ADD(tx_flow_control);
9541 ESTAT_ADD(tx_mac_errors);
9542 ESTAT_ADD(tx_single_collisions);
9543 ESTAT_ADD(tx_mult_collisions);
9544 ESTAT_ADD(tx_deferred);
9545 ESTAT_ADD(tx_excessive_collisions);
9546 ESTAT_ADD(tx_late_collisions);
9547 ESTAT_ADD(tx_collide_2times);
9548 ESTAT_ADD(tx_collide_3times);
9549 ESTAT_ADD(tx_collide_4times);
9550 ESTAT_ADD(tx_collide_5times);
9551 ESTAT_ADD(tx_collide_6times);
9552 ESTAT_ADD(tx_collide_7times);
9553 ESTAT_ADD(tx_collide_8times);
9554 ESTAT_ADD(tx_collide_9times);
9555 ESTAT_ADD(tx_collide_10times);
9556 ESTAT_ADD(tx_collide_11times);
9557 ESTAT_ADD(tx_collide_12times);
9558 ESTAT_ADD(tx_collide_13times);
9559 ESTAT_ADD(tx_collide_14times);
9560 ESTAT_ADD(tx_collide_15times);
9561 ESTAT_ADD(tx_ucast_packets);
9562 ESTAT_ADD(tx_mcast_packets);
9563 ESTAT_ADD(tx_bcast_packets);
9564 ESTAT_ADD(tx_carrier_sense_errors);
9565 ESTAT_ADD(tx_discards);
9566 ESTAT_ADD(tx_errors);
9567
9568 ESTAT_ADD(dma_writeq_full);
9569 ESTAT_ADD(dma_write_prioq_full);
9570 ESTAT_ADD(rxbds_empty);
9571 ESTAT_ADD(rx_discards);
9572 ESTAT_ADD(rx_errors);
9573 ESTAT_ADD(rx_threshold_hit);
9574
9575 ESTAT_ADD(dma_readq_full);
9576 ESTAT_ADD(dma_read_prioq_full);
9577 ESTAT_ADD(tx_comp_queue_full);
9578
9579 ESTAT_ADD(ring_set_send_prod_index);
9580 ESTAT_ADD(ring_status_update);
9581 ESTAT_ADD(nic_irqs);
9582 ESTAT_ADD(nic_avoided_irqs);
9583 ESTAT_ADD(nic_tx_threshold_hit);
9584
Matt Carlson4452d092011-05-19 12:12:51 +00009585 ESTAT_ADD(mbuf_lwm_thresh_hit);
9586
Linus Torvalds1da177e2005-04-16 15:20:36 -07009587 return estats;
9588}
9589
Eric Dumazet511d2222010-07-07 20:44:24 +00009590static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9591 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009592{
9593 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009594 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009595 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9596
9597 if (!hw_stats)
9598 return old_stats;
9599
9600 stats->rx_packets = old_stats->rx_packets +
9601 get_stat64(&hw_stats->rx_ucast_packets) +
9602 get_stat64(&hw_stats->rx_mcast_packets) +
9603 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009604
Linus Torvalds1da177e2005-04-16 15:20:36 -07009605 stats->tx_packets = old_stats->tx_packets +
9606 get_stat64(&hw_stats->tx_ucast_packets) +
9607 get_stat64(&hw_stats->tx_mcast_packets) +
9608 get_stat64(&hw_stats->tx_bcast_packets);
9609
9610 stats->rx_bytes = old_stats->rx_bytes +
9611 get_stat64(&hw_stats->rx_octets);
9612 stats->tx_bytes = old_stats->tx_bytes +
9613 get_stat64(&hw_stats->tx_octets);
9614
9615 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009616 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009617 stats->tx_errors = old_stats->tx_errors +
9618 get_stat64(&hw_stats->tx_errors) +
9619 get_stat64(&hw_stats->tx_mac_errors) +
9620 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9621 get_stat64(&hw_stats->tx_discards);
9622
9623 stats->multicast = old_stats->multicast +
9624 get_stat64(&hw_stats->rx_mcast_packets);
9625 stats->collisions = old_stats->collisions +
9626 get_stat64(&hw_stats->tx_collisions);
9627
9628 stats->rx_length_errors = old_stats->rx_length_errors +
9629 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9630 get_stat64(&hw_stats->rx_undersize_packets);
9631
9632 stats->rx_over_errors = old_stats->rx_over_errors +
9633 get_stat64(&hw_stats->rxbds_empty);
9634 stats->rx_frame_errors = old_stats->rx_frame_errors +
9635 get_stat64(&hw_stats->rx_align_errors);
9636 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9637 get_stat64(&hw_stats->tx_discards);
9638 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9639 get_stat64(&hw_stats->tx_carrier_sense_errors);
9640
9641 stats->rx_crc_errors = old_stats->rx_crc_errors +
9642 calc_crc_errors(tp);
9643
John W. Linville4f63b872005-09-12 14:43:18 -07009644 stats->rx_missed_errors = old_stats->rx_missed_errors +
9645 get_stat64(&hw_stats->rx_discards);
9646
Eric Dumazetb0057c52010-10-10 19:55:52 +00009647 stats->rx_dropped = tp->rx_dropped;
9648
Linus Torvalds1da177e2005-04-16 15:20:36 -07009649 return stats;
9650}
9651
9652static inline u32 calc_crc(unsigned char *buf, int len)
9653{
9654 u32 reg;
9655 u32 tmp;
9656 int j, k;
9657
9658 reg = 0xffffffff;
9659
9660 for (j = 0; j < len; j++) {
9661 reg ^= buf[j];
9662
9663 for (k = 0; k < 8; k++) {
9664 tmp = reg & 0x01;
9665
9666 reg >>= 1;
9667
Matt Carlson859a588792010-04-05 10:19:28 +00009668 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009669 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009670 }
9671 }
9672
9673 return ~reg;
9674}
9675
9676static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9677{
9678 /* accept or reject all multicast frames */
9679 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9680 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9681 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9682 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9683}
9684
9685static void __tg3_set_rx_mode(struct net_device *dev)
9686{
9687 struct tg3 *tp = netdev_priv(dev);
9688 u32 rx_mode;
9689
9690 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9691 RX_MODE_KEEP_VLAN_TAG);
9692
Matt Carlsonbf933c82011-01-25 15:58:49 +00009693#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009694 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9695 * flag clear.
9696 */
Joe Perches63c3a662011-04-26 08:12:10 +00009697 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009698 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9699#endif
9700
9701 if (dev->flags & IFF_PROMISC) {
9702 /* Promiscuous mode. */
9703 rx_mode |= RX_MODE_PROMISC;
9704 } else if (dev->flags & IFF_ALLMULTI) {
9705 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009706 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009707 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009708 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009709 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009710 } else {
9711 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009712 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009713 u32 mc_filter[4] = { 0, };
9714 u32 regidx;
9715 u32 bit;
9716 u32 crc;
9717
Jiri Pirko22bedad32010-04-01 21:22:57 +00009718 netdev_for_each_mc_addr(ha, dev) {
9719 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009720 bit = ~crc & 0x7f;
9721 regidx = (bit & 0x60) >> 5;
9722 bit &= 0x1f;
9723 mc_filter[regidx] |= (1 << bit);
9724 }
9725
9726 tw32(MAC_HASH_REG_0, mc_filter[0]);
9727 tw32(MAC_HASH_REG_1, mc_filter[1]);
9728 tw32(MAC_HASH_REG_2, mc_filter[2]);
9729 tw32(MAC_HASH_REG_3, mc_filter[3]);
9730 }
9731
9732 if (rx_mode != tp->rx_mode) {
9733 tp->rx_mode = rx_mode;
9734 tw32_f(MAC_RX_MODE, rx_mode);
9735 udelay(10);
9736 }
9737}
9738
9739static void tg3_set_rx_mode(struct net_device *dev)
9740{
9741 struct tg3 *tp = netdev_priv(dev);
9742
Michael Chane75f7c92006-03-20 21:33:26 -08009743 if (!netif_running(dev))
9744 return;
9745
David S. Millerf47c11e2005-06-24 20:18:35 -07009746 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009747 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009748 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009749}
9750
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751static int tg3_get_regs_len(struct net_device *dev)
9752{
Matt Carlson97bd8e42011-04-13 11:05:04 +00009753 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009754}
9755
9756static void tg3_get_regs(struct net_device *dev,
9757 struct ethtool_regs *regs, void *_p)
9758{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009759 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009760
9761 regs->version = 0;
9762
Matt Carlson97bd8e42011-04-13 11:05:04 +00009763 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764
Matt Carlson80096062010-08-02 11:26:06 +00009765 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009766 return;
9767
David S. Millerf47c11e2005-06-24 20:18:35 -07009768 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009769
Matt Carlson97bd8e42011-04-13 11:05:04 +00009770 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009771
David S. Millerf47c11e2005-06-24 20:18:35 -07009772 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009773}
9774
9775static int tg3_get_eeprom_len(struct net_device *dev)
9776{
9777 struct tg3 *tp = netdev_priv(dev);
9778
9779 return tp->nvram_size;
9780}
9781
Linus Torvalds1da177e2005-04-16 15:20:36 -07009782static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9783{
9784 struct tg3 *tp = netdev_priv(dev);
9785 int ret;
9786 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009787 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009788 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009789
Joe Perches63c3a662011-04-26 08:12:10 +00009790 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +00009791 return -EINVAL;
9792
Matt Carlson80096062010-08-02 11:26:06 +00009793 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009794 return -EAGAIN;
9795
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796 offset = eeprom->offset;
9797 len = eeprom->len;
9798 eeprom->len = 0;
9799
9800 eeprom->magic = TG3_EEPROM_MAGIC;
9801
9802 if (offset & 3) {
9803 /* adjustments to start on required 4 byte boundary */
9804 b_offset = offset & 3;
9805 b_count = 4 - b_offset;
9806 if (b_count > len) {
9807 /* i.e. offset=1 len=2 */
9808 b_count = len;
9809 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009810 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009811 if (ret)
9812 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009813 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009814 len -= b_count;
9815 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009816 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009817 }
9818
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009819 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009820 pd = &data[eeprom->len];
9821 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009822 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009823 if (ret) {
9824 eeprom->len += i;
9825 return ret;
9826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009827 memcpy(pd + i, &val, 4);
9828 }
9829 eeprom->len += i;
9830
9831 if (len & 3) {
9832 /* read last bytes not ending on 4 byte boundary */
9833 pd = &data[eeprom->len];
9834 b_count = len & 3;
9835 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009836 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837 if (ret)
9838 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009839 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009840 eeprom->len += b_count;
9841 }
9842 return 0;
9843}
9844
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009845static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009846
9847static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9848{
9849 struct tg3 *tp = netdev_priv(dev);
9850 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009851 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009852 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009853 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009854
Matt Carlson80096062010-08-02 11:26:06 +00009855 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009856 return -EAGAIN;
9857
Joe Perches63c3a662011-04-26 08:12:10 +00009858 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +00009859 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009860 return -EINVAL;
9861
9862 offset = eeprom->offset;
9863 len = eeprom->len;
9864
9865 if ((b_offset = (offset & 3))) {
9866 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009867 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009868 if (ret)
9869 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009870 len += b_offset;
9871 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009872 if (len < 4)
9873 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009874 }
9875
9876 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009877 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009878 /* adjustments to end on required 4 byte boundary */
9879 odd_len = 1;
9880 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009881 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009882 if (ret)
9883 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009884 }
9885
9886 buf = data;
9887 if (b_offset || odd_len) {
9888 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009889 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009890 return -ENOMEM;
9891 if (b_offset)
9892 memcpy(buf, &start, 4);
9893 if (odd_len)
9894 memcpy(buf+len-4, &end, 4);
9895 memcpy(buf + b_offset, data, eeprom->len);
9896 }
9897
9898 ret = tg3_nvram_write_block(tp, offset, len, buf);
9899
9900 if (buf != data)
9901 kfree(buf);
9902
9903 return ret;
9904}
9905
9906static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9907{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009908 struct tg3 *tp = netdev_priv(dev);
9909
Joe Perches63c3a662011-04-26 08:12:10 +00009910 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009911 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009912 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009913 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009914 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9915 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009916 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009917
Linus Torvalds1da177e2005-04-16 15:20:36 -07009918 cmd->supported = (SUPPORTED_Autoneg);
9919
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009920 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009921 cmd->supported |= (SUPPORTED_1000baseT_Half |
9922 SUPPORTED_1000baseT_Full);
9923
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009924 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009925 cmd->supported |= (SUPPORTED_100baseT_Half |
9926 SUPPORTED_100baseT_Full |
9927 SUPPORTED_10baseT_Half |
9928 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009929 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009930 cmd->port = PORT_TP;
9931 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009932 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009933 cmd->port = PORT_FIBRE;
9934 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009935
Linus Torvalds1da177e2005-04-16 15:20:36 -07009936 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +00009937 if (tg3_flag(tp, PAUSE_AUTONEG)) {
9938 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
9939 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
9940 cmd->advertising |= ADVERTISED_Pause;
9941 } else {
9942 cmd->advertising |= ADVERTISED_Pause |
9943 ADVERTISED_Asym_Pause;
9944 }
9945 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
9946 cmd->advertising |= ADVERTISED_Asym_Pause;
9947 }
9948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009949 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +00009950 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009951 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +00009952 } else {
David Decotigny70739492011-04-27 18:32:40 +00009953 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +00009954 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009955 }
Matt Carlson882e9792009-09-01 13:21:36 +00009956 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009957 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009958 cmd->autoneg = tp->link_config.autoneg;
9959 cmd->maxtxpkt = 0;
9960 cmd->maxrxpkt = 0;
9961 return 0;
9962}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009963
Linus Torvalds1da177e2005-04-16 15:20:36 -07009964static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9965{
9966 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +00009967 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009968
Joe Perches63c3a662011-04-26 08:12:10 +00009969 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009970 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009971 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009972 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009973 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9974 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009975 }
9976
Matt Carlson7e5856b2009-02-25 14:23:01 +00009977 if (cmd->autoneg != AUTONEG_ENABLE &&
9978 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -07009979 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009980
9981 if (cmd->autoneg == AUTONEG_DISABLE &&
9982 cmd->duplex != DUPLEX_FULL &&
9983 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -07009984 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009985
Matt Carlson7e5856b2009-02-25 14:23:01 +00009986 if (cmd->autoneg == AUTONEG_ENABLE) {
9987 u32 mask = ADVERTISED_Autoneg |
9988 ADVERTISED_Pause |
9989 ADVERTISED_Asym_Pause;
9990
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009991 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009992 mask |= ADVERTISED_1000baseT_Half |
9993 ADVERTISED_1000baseT_Full;
9994
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009995 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +00009996 mask |= ADVERTISED_100baseT_Half |
9997 ADVERTISED_100baseT_Full |
9998 ADVERTISED_10baseT_Half |
9999 ADVERTISED_10baseT_Full |
10000 ADVERTISED_TP;
10001 else
10002 mask |= ADVERTISED_FIBRE;
10003
10004 if (cmd->advertising & ~mask)
10005 return -EINVAL;
10006
10007 mask &= (ADVERTISED_1000baseT_Half |
10008 ADVERTISED_1000baseT_Full |
10009 ADVERTISED_100baseT_Half |
10010 ADVERTISED_100baseT_Full |
10011 ADVERTISED_10baseT_Half |
10012 ADVERTISED_10baseT_Full);
10013
10014 cmd->advertising &= mask;
10015 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010016 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010017 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010018 return -EINVAL;
10019
10020 if (cmd->duplex != DUPLEX_FULL)
10021 return -EINVAL;
10022 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010023 if (speed != SPEED_100 &&
10024 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010025 return -EINVAL;
10026 }
10027 }
10028
David S. Millerf47c11e2005-06-24 20:18:35 -070010029 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010030
10031 tp->link_config.autoneg = cmd->autoneg;
10032 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010033 tp->link_config.advertising = (cmd->advertising |
10034 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010035 tp->link_config.speed = SPEED_INVALID;
10036 tp->link_config.duplex = DUPLEX_INVALID;
10037 } else {
10038 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010039 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010040 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010041 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010042
Michael Chan24fcad62006-12-17 17:06:46 -080010043 tp->link_config.orig_speed = tp->link_config.speed;
10044 tp->link_config.orig_duplex = tp->link_config.duplex;
10045 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10046
Linus Torvalds1da177e2005-04-16 15:20:36 -070010047 if (netif_running(dev))
10048 tg3_setup_phy(tp, 1);
10049
David S. Millerf47c11e2005-06-24 20:18:35 -070010050 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010051
Linus Torvalds1da177e2005-04-16 15:20:36 -070010052 return 0;
10053}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010054
Linus Torvalds1da177e2005-04-16 15:20:36 -070010055static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10056{
10057 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010058
Linus Torvalds1da177e2005-04-16 15:20:36 -070010059 strcpy(info->driver, DRV_MODULE_NAME);
10060 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010061 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010062 strcpy(info->bus_info, pci_name(tp->pdev));
10063}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010064
Linus Torvalds1da177e2005-04-16 15:20:36 -070010065static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10066{
10067 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010068
Joe Perches63c3a662011-04-26 08:12:10 +000010069 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010070 wol->supported = WAKE_MAGIC;
10071 else
10072 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010073 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010074 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075 wol->wolopts = WAKE_MAGIC;
10076 memset(&wol->sopass, 0, sizeof(wol->sopass));
10077}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010078
Linus Torvalds1da177e2005-04-16 15:20:36 -070010079static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10080{
10081 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010082 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010083
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084 if (wol->wolopts & ~WAKE_MAGIC)
10085 return -EINVAL;
10086 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010087 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010088 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010089
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010090 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10091
David S. Millerf47c11e2005-06-24 20:18:35 -070010092 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010093 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010094 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010095 else
Joe Perches63c3a662011-04-26 08:12:10 +000010096 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010097 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010098
Linus Torvalds1da177e2005-04-16 15:20:36 -070010099 return 0;
10100}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010101
Linus Torvalds1da177e2005-04-16 15:20:36 -070010102static u32 tg3_get_msglevel(struct net_device *dev)
10103{
10104 struct tg3 *tp = netdev_priv(dev);
10105 return tp->msg_enable;
10106}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010107
Linus Torvalds1da177e2005-04-16 15:20:36 -070010108static void tg3_set_msglevel(struct net_device *dev, u32 value)
10109{
10110 struct tg3 *tp = netdev_priv(dev);
10111 tp->msg_enable = value;
10112}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010113
Linus Torvalds1da177e2005-04-16 15:20:36 -070010114static int tg3_nway_reset(struct net_device *dev)
10115{
10116 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010117 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010118
Linus Torvalds1da177e2005-04-16 15:20:36 -070010119 if (!netif_running(dev))
10120 return -EAGAIN;
10121
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010122 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010123 return -EINVAL;
10124
Joe Perches63c3a662011-04-26 08:12:10 +000010125 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010126 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010127 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010128 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010129 } else {
10130 u32 bmcr;
10131
10132 spin_lock_bh(&tp->lock);
10133 r = -EINVAL;
10134 tg3_readphy(tp, MII_BMCR, &bmcr);
10135 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10136 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010137 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010138 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10139 BMCR_ANENABLE);
10140 r = 0;
10141 }
10142 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010143 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010144
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145 return r;
10146}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010147
Linus Torvalds1da177e2005-04-16 15:20:36 -070010148static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10149{
10150 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010151
Matt Carlson2c49a442010-09-30 10:34:35 +000010152 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010153 ering->rx_mini_max_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010154 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010155 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010156 else
10157 ering->rx_jumbo_max_pending = 0;
10158
10159 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010160
10161 ering->rx_pending = tp->rx_pending;
10162 ering->rx_mini_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010163 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010164 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10165 else
10166 ering->rx_jumbo_pending = 0;
10167
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010168 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010169}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010170
Linus Torvalds1da177e2005-04-16 15:20:36 -070010171static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10172{
10173 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010174 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010175
Matt Carlson2c49a442010-09-30 10:34:35 +000010176 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10177 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010178 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10179 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010180 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010181 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010182 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010183
Michael Chanbbe832c2005-06-24 20:20:04 -070010184 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010185 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010186 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010187 irq_sync = 1;
10188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010189
Michael Chanbbe832c2005-06-24 20:20:04 -070010190 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010191
Linus Torvalds1da177e2005-04-16 15:20:36 -070010192 tp->rx_pending = ering->rx_pending;
10193
Joe Perches63c3a662011-04-26 08:12:10 +000010194 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010195 tp->rx_pending > 63)
10196 tp->rx_pending = 63;
10197 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010198
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010199 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010200 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010201
10202 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010203 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010204 err = tg3_restart_hw(tp, 1);
10205 if (!err)
10206 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010207 }
10208
David S. Millerf47c11e2005-06-24 20:18:35 -070010209 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010210
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010211 if (irq_sync && !err)
10212 tg3_phy_start(tp);
10213
Michael Chanb9ec6c12006-07-25 16:37:27 -070010214 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010215}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010216
Linus Torvalds1da177e2005-04-16 15:20:36 -070010217static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10218{
10219 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010220
Joe Perches63c3a662011-04-26 08:12:10 +000010221 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010222
Steve Glendinninge18ce342008-12-16 02:00:00 -080010223 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010224 epause->rx_pause = 1;
10225 else
10226 epause->rx_pause = 0;
10227
Steve Glendinninge18ce342008-12-16 02:00:00 -080010228 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010229 epause->tx_pause = 1;
10230 else
10231 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010232}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010233
Linus Torvalds1da177e2005-04-16 15:20:36 -070010234static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10235{
10236 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010237 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010238
Joe Perches63c3a662011-04-26 08:12:10 +000010239 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010240 u32 newadv;
10241 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010242
Matt Carlson27121682010-02-17 15:16:57 +000010243 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010244
Matt Carlson27121682010-02-17 15:16:57 +000010245 if (!(phydev->supported & SUPPORTED_Pause) ||
10246 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010247 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010248 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010249
Matt Carlson27121682010-02-17 15:16:57 +000010250 tp->link_config.flowctrl = 0;
10251 if (epause->rx_pause) {
10252 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010253
Matt Carlson27121682010-02-17 15:16:57 +000010254 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010255 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010256 newadv = ADVERTISED_Pause;
10257 } else
10258 newadv = ADVERTISED_Pause |
10259 ADVERTISED_Asym_Pause;
10260 } else if (epause->tx_pause) {
10261 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10262 newadv = ADVERTISED_Asym_Pause;
10263 } else
10264 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010265
Matt Carlson27121682010-02-17 15:16:57 +000010266 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010267 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010268 else
Joe Perches63c3a662011-04-26 08:12:10 +000010269 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010270
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010271 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010272 u32 oldadv = phydev->advertising &
10273 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10274 if (oldadv != newadv) {
10275 phydev->advertising &=
10276 ~(ADVERTISED_Pause |
10277 ADVERTISED_Asym_Pause);
10278 phydev->advertising |= newadv;
10279 if (phydev->autoneg) {
10280 /*
10281 * Always renegotiate the link to
10282 * inform our link partner of our
10283 * flow control settings, even if the
10284 * flow control is forced. Let
10285 * tg3_adjust_link() do the final
10286 * flow control setup.
10287 */
10288 return phy_start_aneg(phydev);
10289 }
10290 }
10291
10292 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010293 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010294 } else {
10295 tp->link_config.orig_advertising &=
10296 ~(ADVERTISED_Pause |
10297 ADVERTISED_Asym_Pause);
10298 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010299 }
10300 } else {
10301 int irq_sync = 0;
10302
10303 if (netif_running(dev)) {
10304 tg3_netif_stop(tp);
10305 irq_sync = 1;
10306 }
10307
10308 tg3_full_lock(tp, irq_sync);
10309
10310 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010311 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010312 else
Joe Perches63c3a662011-04-26 08:12:10 +000010313 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010314 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010315 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010316 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010317 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010318 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010319 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010320 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010321 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010322
10323 if (netif_running(dev)) {
10324 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10325 err = tg3_restart_hw(tp, 1);
10326 if (!err)
10327 tg3_netif_start(tp);
10328 }
10329
10330 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010332
Michael Chanb9ec6c12006-07-25 16:37:27 -070010333 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010334}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010335
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010336static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010337{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010338 switch (sset) {
10339 case ETH_SS_TEST:
10340 return TG3_NUM_TEST;
10341 case ETH_SS_STATS:
10342 return TG3_NUM_STATS;
10343 default:
10344 return -EOPNOTSUPP;
10345 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010346}
10347
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010348static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010349{
10350 switch (stringset) {
10351 case ETH_SS_STATS:
10352 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10353 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010354 case ETH_SS_TEST:
10355 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10356 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010357 default:
10358 WARN_ON(1); /* we need a WARN() */
10359 break;
10360 }
10361}
10362
stephen hemminger81b87092011-04-04 08:43:50 +000010363static int tg3_set_phys_id(struct net_device *dev,
10364 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010365{
10366 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010367
10368 if (!netif_running(tp->dev))
10369 return -EAGAIN;
10370
stephen hemminger81b87092011-04-04 08:43:50 +000010371 switch (state) {
10372 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010373 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010374
stephen hemminger81b87092011-04-04 08:43:50 +000010375 case ETHTOOL_ID_ON:
10376 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10377 LED_CTRL_1000MBPS_ON |
10378 LED_CTRL_100MBPS_ON |
10379 LED_CTRL_10MBPS_ON |
10380 LED_CTRL_TRAFFIC_OVERRIDE |
10381 LED_CTRL_TRAFFIC_BLINK |
10382 LED_CTRL_TRAFFIC_LED);
10383 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010384
stephen hemminger81b87092011-04-04 08:43:50 +000010385 case ETHTOOL_ID_OFF:
10386 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10387 LED_CTRL_TRAFFIC_OVERRIDE);
10388 break;
Michael Chan4009a932005-09-05 17:52:54 -070010389
stephen hemminger81b87092011-04-04 08:43:50 +000010390 case ETHTOOL_ID_INACTIVE:
10391 tw32(MAC_LED_CTRL, tp->led_ctrl);
10392 break;
Michael Chan4009a932005-09-05 17:52:54 -070010393 }
stephen hemminger81b87092011-04-04 08:43:50 +000010394
Michael Chan4009a932005-09-05 17:52:54 -070010395 return 0;
10396}
10397
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010398static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010399 struct ethtool_stats *estats, u64 *tmp_stats)
10400{
10401 struct tg3 *tp = netdev_priv(dev);
10402 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10403}
10404
Matt Carlsonc3e94502011-04-13 11:05:08 +000010405static __be32 * tg3_vpd_readblock(struct tg3 *tp)
10406{
10407 int i;
10408 __be32 *buf;
10409 u32 offset = 0, len = 0;
10410 u32 magic, val;
10411
Joe Perches63c3a662011-04-26 08:12:10 +000010412 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010413 return NULL;
10414
10415 if (magic == TG3_EEPROM_MAGIC) {
10416 for (offset = TG3_NVM_DIR_START;
10417 offset < TG3_NVM_DIR_END;
10418 offset += TG3_NVM_DIRENT_SIZE) {
10419 if (tg3_nvram_read(tp, offset, &val))
10420 return NULL;
10421
10422 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10423 TG3_NVM_DIRTYPE_EXTVPD)
10424 break;
10425 }
10426
10427 if (offset != TG3_NVM_DIR_END) {
10428 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10429 if (tg3_nvram_read(tp, offset + 4, &offset))
10430 return NULL;
10431
10432 offset = tg3_nvram_logical_addr(tp, offset);
10433 }
10434 }
10435
10436 if (!offset || !len) {
10437 offset = TG3_NVM_VPD_OFF;
10438 len = TG3_NVM_VPD_LEN;
10439 }
10440
10441 buf = kmalloc(len, GFP_KERNEL);
10442 if (buf == NULL)
10443 return NULL;
10444
10445 if (magic == TG3_EEPROM_MAGIC) {
10446 for (i = 0; i < len; i += 4) {
10447 /* The data is in little-endian format in NVRAM.
10448 * Use the big-endian read routines to preserve
10449 * the byte order as it exists in NVRAM.
10450 */
10451 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10452 goto error;
10453 }
10454 } else {
10455 u8 *ptr;
10456 ssize_t cnt;
10457 unsigned int pos = 0;
10458
10459 ptr = (u8 *)&buf[0];
10460 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10461 cnt = pci_read_vpd(tp->pdev, pos,
10462 len - pos, ptr);
10463 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10464 cnt = 0;
10465 else if (cnt < 0)
10466 goto error;
10467 }
10468 if (pos != len)
10469 goto error;
10470 }
10471
10472 return buf;
10473
10474error:
10475 kfree(buf);
10476 return NULL;
10477}
10478
Michael Chan566f86a2005-05-29 14:56:58 -070010479#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010480#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10481#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10482#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010483#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10484#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
10485#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x4c
Michael Chanb16250e2006-09-27 16:10:14 -070010486#define NVRAM_SELFBOOT_HW_SIZE 0x20
10487#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010488
10489static int tg3_test_nvram(struct tg3 *tp)
10490{
Al Virob9fc7dc2007-12-17 22:59:57 -080010491 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010492 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010493 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010494
Joe Perches63c3a662011-04-26 08:12:10 +000010495 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010496 return 0;
10497
Matt Carlsone4f34112009-02-25 14:25:00 +000010498 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010499 return -EIO;
10500
Michael Chan1b277772006-03-20 22:27:48 -080010501 if (magic == TG3_EEPROM_MAGIC)
10502 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010503 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010504 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10505 TG3_EEPROM_SB_FORMAT_1) {
10506 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10507 case TG3_EEPROM_SB_REVISION_0:
10508 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10509 break;
10510 case TG3_EEPROM_SB_REVISION_2:
10511 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10512 break;
10513 case TG3_EEPROM_SB_REVISION_3:
10514 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10515 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010516 case TG3_EEPROM_SB_REVISION_4:
10517 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10518 break;
10519 case TG3_EEPROM_SB_REVISION_5:
10520 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10521 break;
10522 case TG3_EEPROM_SB_REVISION_6:
10523 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10524 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010525 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010526 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010527 }
10528 } else
Michael Chan1b277772006-03-20 22:27:48 -080010529 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010530 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10531 size = NVRAM_SELFBOOT_HW_SIZE;
10532 else
Michael Chan1b277772006-03-20 22:27:48 -080010533 return -EIO;
10534
10535 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010536 if (buf == NULL)
10537 return -ENOMEM;
10538
Michael Chan1b277772006-03-20 22:27:48 -080010539 err = -EIO;
10540 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010541 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10542 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010543 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010544 }
Michael Chan1b277772006-03-20 22:27:48 -080010545 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010546 goto out;
10547
Michael Chan1b277772006-03-20 22:27:48 -080010548 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010549 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010550 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010551 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010552 u8 *buf8 = (u8 *) buf, csum8 = 0;
10553
Al Virob9fc7dc2007-12-17 22:59:57 -080010554 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010555 TG3_EEPROM_SB_REVISION_2) {
10556 /* For rev 2, the csum doesn't include the MBA. */
10557 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10558 csum8 += buf8[i];
10559 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10560 csum8 += buf8[i];
10561 } else {
10562 for (i = 0; i < size; i++)
10563 csum8 += buf8[i];
10564 }
Michael Chan1b277772006-03-20 22:27:48 -080010565
Adrian Bunkad96b482006-04-05 22:21:04 -070010566 if (csum8 == 0) {
10567 err = 0;
10568 goto out;
10569 }
10570
10571 err = -EIO;
10572 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010573 }
Michael Chan566f86a2005-05-29 14:56:58 -070010574
Al Virob9fc7dc2007-12-17 22:59:57 -080010575 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010576 TG3_EEPROM_MAGIC_HW) {
10577 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010578 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010579 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010580
10581 /* Separate the parity bits and the data bytes. */
10582 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10583 if ((i == 0) || (i == 8)) {
10584 int l;
10585 u8 msk;
10586
10587 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10588 parity[k++] = buf8[i] & msk;
10589 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010590 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010591 int l;
10592 u8 msk;
10593
10594 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10595 parity[k++] = buf8[i] & msk;
10596 i++;
10597
10598 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10599 parity[k++] = buf8[i] & msk;
10600 i++;
10601 }
10602 data[j++] = buf8[i];
10603 }
10604
10605 err = -EIO;
10606 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10607 u8 hw8 = hweight8(data[i]);
10608
10609 if ((hw8 & 0x1) && parity[i])
10610 goto out;
10611 else if (!(hw8 & 0x1) && !parity[i])
10612 goto out;
10613 }
10614 err = 0;
10615 goto out;
10616 }
10617
Matt Carlson01c3a392011-03-09 16:58:20 +000010618 err = -EIO;
10619
Michael Chan566f86a2005-05-29 14:56:58 -070010620 /* Bootstrap checksum at offset 0x10 */
10621 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010622 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010623 goto out;
10624
10625 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10626 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010627 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010628 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010629
Matt Carlsonc3e94502011-04-13 11:05:08 +000010630 kfree(buf);
10631
10632 buf = tg3_vpd_readblock(tp);
10633 if (!buf)
10634 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010635
10636 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10637 PCI_VPD_LRDT_RO_DATA);
10638 if (i > 0) {
10639 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10640 if (j < 0)
10641 goto out;
10642
10643 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10644 goto out;
10645
10646 i += PCI_VPD_LRDT_TAG_SIZE;
10647 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10648 PCI_VPD_RO_KEYWORD_CHKSUM);
10649 if (j > 0) {
10650 u8 csum8 = 0;
10651
10652 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10653
10654 for (i = 0; i <= j; i++)
10655 csum8 += ((u8 *)buf)[i];
10656
10657 if (csum8)
10658 goto out;
10659 }
10660 }
10661
Michael Chan566f86a2005-05-29 14:56:58 -070010662 err = 0;
10663
10664out:
10665 kfree(buf);
10666 return err;
10667}
10668
Michael Chanca430072005-05-29 14:57:23 -070010669#define TG3_SERDES_TIMEOUT_SEC 2
10670#define TG3_COPPER_TIMEOUT_SEC 6
10671
10672static int tg3_test_link(struct tg3 *tp)
10673{
10674 int i, max;
10675
10676 if (!netif_running(tp->dev))
10677 return -ENODEV;
10678
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010679 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010680 max = TG3_SERDES_TIMEOUT_SEC;
10681 else
10682 max = TG3_COPPER_TIMEOUT_SEC;
10683
10684 for (i = 0; i < max; i++) {
10685 if (netif_carrier_ok(tp->dev))
10686 return 0;
10687
10688 if (msleep_interruptible(1000))
10689 break;
10690 }
10691
10692 return -EIO;
10693}
10694
Michael Chana71116d2005-05-29 14:58:11 -070010695/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010696static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010697{
Michael Chanb16250e2006-09-27 16:10:14 -070010698 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010699 u32 offset, read_mask, write_mask, val, save_val, read_val;
10700 static struct {
10701 u16 offset;
10702 u16 flags;
10703#define TG3_FL_5705 0x1
10704#define TG3_FL_NOT_5705 0x2
10705#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010706#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010707 u32 read_mask;
10708 u32 write_mask;
10709 } reg_tbl[] = {
10710 /* MAC Control Registers */
10711 { MAC_MODE, TG3_FL_NOT_5705,
10712 0x00000000, 0x00ef6f8c },
10713 { MAC_MODE, TG3_FL_5705,
10714 0x00000000, 0x01ef6b8c },
10715 { MAC_STATUS, TG3_FL_NOT_5705,
10716 0x03800107, 0x00000000 },
10717 { MAC_STATUS, TG3_FL_5705,
10718 0x03800100, 0x00000000 },
10719 { MAC_ADDR_0_HIGH, 0x0000,
10720 0x00000000, 0x0000ffff },
10721 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010722 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010723 { MAC_RX_MTU_SIZE, 0x0000,
10724 0x00000000, 0x0000ffff },
10725 { MAC_TX_MODE, 0x0000,
10726 0x00000000, 0x00000070 },
10727 { MAC_TX_LENGTHS, 0x0000,
10728 0x00000000, 0x00003fff },
10729 { MAC_RX_MODE, TG3_FL_NOT_5705,
10730 0x00000000, 0x000007fc },
10731 { MAC_RX_MODE, TG3_FL_5705,
10732 0x00000000, 0x000007dc },
10733 { MAC_HASH_REG_0, 0x0000,
10734 0x00000000, 0xffffffff },
10735 { MAC_HASH_REG_1, 0x0000,
10736 0x00000000, 0xffffffff },
10737 { MAC_HASH_REG_2, 0x0000,
10738 0x00000000, 0xffffffff },
10739 { MAC_HASH_REG_3, 0x0000,
10740 0x00000000, 0xffffffff },
10741
10742 /* Receive Data and Receive BD Initiator Control Registers. */
10743 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10744 0x00000000, 0xffffffff },
10745 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10746 0x00000000, 0xffffffff },
10747 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10748 0x00000000, 0x00000003 },
10749 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10750 0x00000000, 0xffffffff },
10751 { RCVDBDI_STD_BD+0, 0x0000,
10752 0x00000000, 0xffffffff },
10753 { RCVDBDI_STD_BD+4, 0x0000,
10754 0x00000000, 0xffffffff },
10755 { RCVDBDI_STD_BD+8, 0x0000,
10756 0x00000000, 0xffff0002 },
10757 { RCVDBDI_STD_BD+0xc, 0x0000,
10758 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010759
Michael Chana71116d2005-05-29 14:58:11 -070010760 /* Receive BD Initiator Control Registers. */
10761 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10762 0x00000000, 0xffffffff },
10763 { RCVBDI_STD_THRESH, TG3_FL_5705,
10764 0x00000000, 0x000003ff },
10765 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10766 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010767
Michael Chana71116d2005-05-29 14:58:11 -070010768 /* Host Coalescing Control Registers. */
10769 { HOSTCC_MODE, TG3_FL_NOT_5705,
10770 0x00000000, 0x00000004 },
10771 { HOSTCC_MODE, TG3_FL_5705,
10772 0x00000000, 0x000000f6 },
10773 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10774 0x00000000, 0xffffffff },
10775 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10776 0x00000000, 0x000003ff },
10777 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10778 0x00000000, 0xffffffff },
10779 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10780 0x00000000, 0x000003ff },
10781 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10782 0x00000000, 0xffffffff },
10783 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10784 0x00000000, 0x000000ff },
10785 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10786 0x00000000, 0xffffffff },
10787 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10788 0x00000000, 0x000000ff },
10789 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10790 0x00000000, 0xffffffff },
10791 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10792 0x00000000, 0xffffffff },
10793 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10794 0x00000000, 0xffffffff },
10795 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10796 0x00000000, 0x000000ff },
10797 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10798 0x00000000, 0xffffffff },
10799 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10800 0x00000000, 0x000000ff },
10801 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10802 0x00000000, 0xffffffff },
10803 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10804 0x00000000, 0xffffffff },
10805 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10806 0x00000000, 0xffffffff },
10807 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10808 0x00000000, 0xffffffff },
10809 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10810 0x00000000, 0xffffffff },
10811 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10812 0xffffffff, 0x00000000 },
10813 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10814 0xffffffff, 0x00000000 },
10815
10816 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010817 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010818 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010819 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010820 0x00000000, 0x007fffff },
10821 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10822 0x00000000, 0x0000003f },
10823 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10824 0x00000000, 0x000001ff },
10825 { BUFMGR_MB_HIGH_WATER, 0x0000,
10826 0x00000000, 0x000001ff },
10827 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10828 0xffffffff, 0x00000000 },
10829 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10830 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010831
Michael Chana71116d2005-05-29 14:58:11 -070010832 /* Mailbox Registers */
10833 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10834 0x00000000, 0x000001ff },
10835 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10836 0x00000000, 0x000001ff },
10837 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10838 0x00000000, 0x000007ff },
10839 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10840 0x00000000, 0x000001ff },
10841
10842 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10843 };
10844
Michael Chanb16250e2006-09-27 16:10:14 -070010845 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010846 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070010847 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000010848 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070010849 is_5750 = 1;
10850 }
Michael Chana71116d2005-05-29 14:58:11 -070010851
10852 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10853 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10854 continue;
10855
10856 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10857 continue;
10858
Joe Perches63c3a662011-04-26 08:12:10 +000010859 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070010860 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10861 continue;
10862
Michael Chanb16250e2006-09-27 16:10:14 -070010863 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10864 continue;
10865
Michael Chana71116d2005-05-29 14:58:11 -070010866 offset = (u32) reg_tbl[i].offset;
10867 read_mask = reg_tbl[i].read_mask;
10868 write_mask = reg_tbl[i].write_mask;
10869
10870 /* Save the original register content */
10871 save_val = tr32(offset);
10872
10873 /* Determine the read-only value. */
10874 read_val = save_val & read_mask;
10875
10876 /* Write zero to the register, then make sure the read-only bits
10877 * are not changed and the read/write bits are all zeros.
10878 */
10879 tw32(offset, 0);
10880
10881 val = tr32(offset);
10882
10883 /* Test the read-only and read/write bits. */
10884 if (((val & read_mask) != read_val) || (val & write_mask))
10885 goto out;
10886
10887 /* Write ones to all the bits defined by RdMask and WrMask, then
10888 * make sure the read-only bits are not changed and the
10889 * read/write bits are all ones.
10890 */
10891 tw32(offset, read_mask | write_mask);
10892
10893 val = tr32(offset);
10894
10895 /* Test the read-only bits. */
10896 if ((val & read_mask) != read_val)
10897 goto out;
10898
10899 /* Test the read/write bits. */
10900 if ((val & write_mask) != write_mask)
10901 goto out;
10902
10903 tw32(offset, save_val);
10904 }
10905
10906 return 0;
10907
10908out:
Michael Chan9f88f292006-12-07 00:22:54 -080010909 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010910 netdev_err(tp->dev,
10911 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010912 tw32(offset, save_val);
10913 return -EIO;
10914}
10915
Michael Chan7942e1d2005-05-29 14:58:36 -070010916static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10917{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010918 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010919 int i;
10920 u32 j;
10921
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010922 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010923 for (j = 0; j < len; j += 4) {
10924 u32 val;
10925
10926 tg3_write_mem(tp, offset + j, test_pattern[i]);
10927 tg3_read_mem(tp, offset + j, &val);
10928 if (val != test_pattern[i])
10929 return -EIO;
10930 }
10931 }
10932 return 0;
10933}
10934
10935static int tg3_test_memory(struct tg3 *tp)
10936{
10937 static struct mem_entry {
10938 u32 offset;
10939 u32 len;
10940 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010941 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010942 { 0x00002000, 0x1c000},
10943 { 0xffffffff, 0x00000}
10944 }, mem_tbl_5705[] = {
10945 { 0x00000100, 0x0000c},
10946 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010947 { 0x00004000, 0x00800},
10948 { 0x00006000, 0x01000},
10949 { 0x00008000, 0x02000},
10950 { 0x00010000, 0x0e000},
10951 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010952 }, mem_tbl_5755[] = {
10953 { 0x00000200, 0x00008},
10954 { 0x00004000, 0x00800},
10955 { 0x00006000, 0x00800},
10956 { 0x00008000, 0x02000},
10957 { 0x00010000, 0x0c000},
10958 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010959 }, mem_tbl_5906[] = {
10960 { 0x00000200, 0x00008},
10961 { 0x00004000, 0x00400},
10962 { 0x00006000, 0x00400},
10963 { 0x00008000, 0x01000},
10964 { 0x00010000, 0x01000},
10965 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010966 }, mem_tbl_5717[] = {
10967 { 0x00000200, 0x00008},
10968 { 0x00010000, 0x0a000},
10969 { 0x00020000, 0x13c00},
10970 { 0xffffffff, 0x00000}
10971 }, mem_tbl_57765[] = {
10972 { 0x00000200, 0x00008},
10973 { 0x00004000, 0x00800},
10974 { 0x00006000, 0x09800},
10975 { 0x00010000, 0x0a000},
10976 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010977 };
10978 struct mem_entry *mem_tbl;
10979 int err = 0;
10980 int i;
10981
Joe Perches63c3a662011-04-26 08:12:10 +000010982 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010983 mem_tbl = mem_tbl_5717;
10984 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
10985 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000010986 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080010987 mem_tbl = mem_tbl_5755;
10988 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
10989 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000010990 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080010991 mem_tbl = mem_tbl_5705;
10992 else
Michael Chan7942e1d2005-05-29 14:58:36 -070010993 mem_tbl = mem_tbl_570x;
10994
10995 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000010996 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
10997 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070010998 break;
10999 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011000
Michael Chan7942e1d2005-05-29 14:58:36 -070011001 return err;
11002}
11003
Michael Chan9f40dea2005-09-05 17:53:06 -070011004#define TG3_MAC_LOOPBACK 0
11005#define TG3_PHY_LOOPBACK 1
Matt Carlsonbb158d62011-04-25 12:42:47 +000011006#define TG3_TSO_LOOPBACK 2
11007
11008#define TG3_TSO_MSS 500
11009
11010#define TG3_TSO_IP_HDR_LEN 20
11011#define TG3_TSO_TCP_HDR_LEN 20
11012#define TG3_TSO_TCP_OPT_LEN 12
11013
11014static const u8 tg3_tso_header[] = {
110150x08, 0x00,
110160x45, 0x00, 0x00, 0x00,
110170x00, 0x00, 0x40, 0x00,
110180x40, 0x06, 0x00, 0x00,
110190x0a, 0x00, 0x00, 0x01,
110200x0a, 0x00, 0x00, 0x02,
110210x0d, 0x00, 0xe0, 0x00,
110220x00, 0x00, 0x01, 0x00,
110230x00, 0x00, 0x02, 0x00,
110240x80, 0x10, 0x10, 0x00,
110250x14, 0x09, 0x00, 0x00,
110260x01, 0x01, 0x08, 0x0a,
110270x11, 0x11, 0x11, 0x11,
110280x11, 0x11, 0x11, 0x11,
11029};
Michael Chan9f40dea2005-09-05 17:53:06 -070011030
Matt Carlson4852a862011-04-13 11:05:07 +000011031static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070011032{
Michael Chan9f40dea2005-09-05 17:53:06 -070011033 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011034 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Michael Chanc76949a2005-05-29 14:58:59 -070011035 struct sk_buff *skb, *rx_skb;
11036 u8 *tx_data;
11037 dma_addr_t map;
11038 int num_pkts, tx_len, rx_len, i, err;
11039 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011040 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011041 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011042
Matt Carlsonc8873402010-02-12 14:47:11 +000011043 tnapi = &tp->napi[0];
11044 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011045 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011046 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011047 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011048 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011049 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011050 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011051 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011052
Michael Chan9f40dea2005-09-05 17:53:06 -070011053 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070011054 /* HW errata - mac loopback fails in some cases on 5780.
11055 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000011056 * errata. Also, the MAC loopback test is deprecated for
11057 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070011058 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000011059 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011060 tg3_flag(tp, CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070011061 return 0;
11062
Matt Carlson49692ca2011-01-25 15:58:52 +000011063 mac_mode = tp->mac_mode &
11064 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
11065 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Joe Perches63c3a662011-04-26 08:12:10 +000011066 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011067 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011068 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070011069 mac_mode |= MAC_MODE_PORT_MODE_MII;
11070 else
11071 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070011072 tw32(MAC_MODE, mac_mode);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011073 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011074 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000011075 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080011076 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
11077 } else
11078 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070011079
Matt Carlson9ef8ca92007-07-11 19:48:29 -070011080 tg3_phy_toggle_automdix(tp, 0);
11081
Michael Chan3f7045c2006-09-27 16:02:29 -070011082 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070011083 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080011084
Matt Carlson49692ca2011-01-25 15:58:52 +000011085 mac_mode = tp->mac_mode &
11086 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011087 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000011088 tg3_writephy(tp, MII_TG3_FET_PTEST,
11089 MII_TG3_FET_PTEST_FRC_TX_LINK |
11090 MII_TG3_FET_PTEST_FRC_TX_LOCK);
11091 /* The write needs to be flushed for the AC131 */
11092 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11093 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080011094 mac_mode |= MAC_MODE_PORT_MODE_MII;
11095 } else
11096 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070011097
Michael Chanc94e3942005-09-27 12:12:42 -070011098 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011099 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070011100 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
11101 udelay(10);
11102 tw32_f(MAC_RX_MODE, tp->rx_mode);
11103 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011104 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000011105 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
11106 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011107 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000011108 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011109 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080011110 tg3_writephy(tp, MII_TG3_EXT_CTRL,
11111 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
11112 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011113 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000011114
11115 /* Wait for link */
11116 for (i = 0; i < 100; i++) {
11117 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11118 break;
11119 mdelay(1);
11120 }
Matt Carlson859a588792010-04-05 10:19:28 +000011121 }
Michael Chanc76949a2005-05-29 14:58:59 -070011122
11123 err = -EIO;
11124
Matt Carlson4852a862011-04-13 11:05:07 +000011125 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011126 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011127 if (!skb)
11128 return -ENOMEM;
11129
Michael Chanc76949a2005-05-29 14:58:59 -070011130 tx_data = skb_put(skb, tx_len);
11131 memcpy(tx_data, tp->dev->dev_addr, 6);
11132 memset(tx_data + 6, 0x0, 8);
11133
Matt Carlson4852a862011-04-13 11:05:07 +000011134 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011135
Matt Carlsonbb158d62011-04-25 12:42:47 +000011136 if (loopback_mode == TG3_TSO_LOOPBACK) {
11137 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11138
11139 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11140 TG3_TSO_TCP_OPT_LEN;
11141
11142 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11143 sizeof(tg3_tso_header));
11144 mss = TG3_TSO_MSS;
11145
11146 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11147 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11148
11149 /* Set the total length field in the IP header */
11150 iph->tot_len = htons((u16)(mss + hdr_len));
11151
11152 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11153 TXD_FLAG_CPU_POST_DMA);
11154
Joe Perches63c3a662011-04-26 08:12:10 +000011155 if (tg3_flag(tp, HW_TSO_1) ||
11156 tg3_flag(tp, HW_TSO_2) ||
11157 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011158 struct tcphdr *th;
11159 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11160 th = (struct tcphdr *)&tx_data[val];
11161 th->check = 0;
11162 } else
11163 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11164
Joe Perches63c3a662011-04-26 08:12:10 +000011165 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011166 mss |= (hdr_len & 0xc) << 12;
11167 if (hdr_len & 0x10)
11168 base_flags |= 0x00000010;
11169 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011170 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011171 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011172 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011173 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11174 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11175 } else {
11176 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11177 }
11178
11179 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11180 } else {
11181 num_pkts = 1;
11182 data_off = ETH_HLEN;
11183 }
11184
11185 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011186 tx_data[i] = (u8) (i & 0xff);
11187
Alexander Duyckf4188d82009-12-02 16:48:38 +000011188 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11189 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011190 dev_kfree_skb(skb);
11191 return -EIO;
11192 }
Michael Chanc76949a2005-05-29 14:58:59 -070011193
11194 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011195 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011196
11197 udelay(10);
11198
Matt Carlson898a56f2009-08-28 14:02:40 +000011199 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011200
Matt Carlsonbb158d62011-04-25 12:42:47 +000011201 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len,
11202 base_flags, (mss << 1) | 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011203
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011204 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011205
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011206 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11207 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011208
11209 udelay(10);
11210
Matt Carlson303fc922009-11-02 14:27:34 +000011211 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11212 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011213 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011214 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011215
11216 udelay(10);
11217
Matt Carlson898a56f2009-08-28 14:02:40 +000011218 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11219 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011220 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011221 (rx_idx == (rx_start_idx + num_pkts)))
11222 break;
11223 }
11224
Alexander Duyckf4188d82009-12-02 16:48:38 +000011225 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011226 dev_kfree_skb(skb);
11227
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011228 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011229 goto out;
11230
11231 if (rx_idx != rx_start_idx + num_pkts)
11232 goto out;
11233
Matt Carlsonbb158d62011-04-25 12:42:47 +000011234 val = data_off;
11235 while (rx_idx != rx_start_idx) {
11236 desc = &rnapi->rx_rcb[rx_start_idx++];
11237 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11238 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011239
Matt Carlsonbb158d62011-04-25 12:42:47 +000011240 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11241 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011242 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011243
Matt Carlsonbb158d62011-04-25 12:42:47 +000011244 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11245 - ETH_FCS_LEN;
11246
11247 if (loopback_mode != TG3_TSO_LOOPBACK) {
11248 if (rx_len != tx_len)
11249 goto out;
11250
11251 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11252 if (opaque_key != RXD_OPAQUE_RING_STD)
11253 goto out;
11254 } else {
11255 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11256 goto out;
11257 }
11258 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11259 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011260 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011261 goto out;
11262 }
11263
11264 if (opaque_key == RXD_OPAQUE_RING_STD) {
11265 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11266 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11267 mapping);
11268 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
11269 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11270 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11271 mapping);
11272 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011273 goto out;
11274
Matt Carlsonbb158d62011-04-25 12:42:47 +000011275 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11276 PCI_DMA_FROMDEVICE);
11277
11278 for (i = data_off; i < rx_len; i++, val++) {
11279 if (*(rx_skb->data + i) != (u8) (val & 0xff))
11280 goto out;
11281 }
Matt Carlson4852a862011-04-13 11:05:07 +000011282 }
11283
Michael Chanc76949a2005-05-29 14:58:59 -070011284 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011285
Michael Chanc76949a2005-05-29 14:58:59 -070011286 /* tg3_free_rings will unmap and free the rx_skb */
11287out:
11288 return err;
11289}
11290
Matt Carlson00c266b2011-04-25 12:42:46 +000011291#define TG3_STD_LOOPBACK_FAILED 1
11292#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011293#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson00c266b2011-04-25 12:42:46 +000011294
11295#define TG3_MAC_LOOPBACK_SHIFT 0
11296#define TG3_PHY_LOOPBACK_SHIFT 4
Matt Carlsonbb158d62011-04-25 12:42:47 +000011297#define TG3_LOOPBACK_FAILED 0x00000077
Michael Chan9f40dea2005-09-05 17:53:06 -070011298
11299static int tg3_test_loopback(struct tg3 *tp)
11300{
11301 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011302 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011303
11304 if (!netif_running(tp->dev))
11305 return TG3_LOOPBACK_FAILED;
11306
Matt Carlsonab789042011-01-25 15:58:54 +000011307 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11308 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11309
Michael Chanb9ec6c12006-07-25 16:37:27 -070011310 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011311 if (err) {
11312 err = TG3_LOOPBACK_FAILED;
11313 goto done;
11314 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011315
Joe Perches63c3a662011-04-26 08:12:10 +000011316 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011317 int i;
11318
11319 /* Reroute all rx packets to the 1st queue */
11320 for (i = MAC_RSS_INDIR_TBL_0;
11321 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11322 tw32(i, 0x0);
11323 }
11324
Matt Carlson6833c042008-11-21 17:18:59 -080011325 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011326 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011327 tg3_phy_toggle_apd(tp, false);
11328
Joe Perches63c3a662011-04-26 08:12:10 +000011329 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011330 int i;
11331 u32 status;
11332
11333 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11334
11335 /* Wait for up to 40 microseconds to acquire lock. */
11336 for (i = 0; i < 4; i++) {
11337 status = tr32(TG3_CPMU_MUTEX_GNT);
11338 if (status == CPMU_MUTEX_GNT_DRIVER)
11339 break;
11340 udelay(10);
11341 }
11342
Matt Carlsonab789042011-01-25 15:58:54 +000011343 if (status != CPMU_MUTEX_GNT_DRIVER) {
11344 err = TG3_LOOPBACK_FAILED;
11345 goto done;
11346 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011347
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011348 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011349 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011350 tw32(TG3_CPMU_CTRL,
11351 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11352 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011353 }
11354
Matt Carlson4852a862011-04-13 11:05:07 +000011355 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011356 err |= TG3_STD_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011357
Joe Perches63c3a662011-04-26 08:12:10 +000011358 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011359 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011360 err |= TG3_JMB_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson4852a862011-04-13 11:05:07 +000011361
Joe Perches63c3a662011-04-26 08:12:10 +000011362 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011363 tw32(TG3_CPMU_CTRL, cpmuctrl);
11364
11365 /* Release the mutex */
11366 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11367 }
11368
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011369 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011370 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson4852a862011-04-13 11:05:07 +000011371 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011372 err |= TG3_STD_LOOPBACK_FAILED <<
11373 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011374 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonbb158d62011-04-25 12:42:47 +000011375 tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_TSO_LOOPBACK))
11376 err |= TG3_TSO_LOOPBACK_FAILED <<
11377 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011378 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011379 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011380 err |= TG3_JMB_LOOPBACK_FAILED <<
11381 TG3_PHY_LOOPBACK_SHIFT;
Michael Chan9f40dea2005-09-05 17:53:06 -070011382 }
11383
Matt Carlson6833c042008-11-21 17:18:59 -080011384 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011385 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011386 tg3_phy_toggle_apd(tp, true);
11387
Matt Carlsonab789042011-01-25 15:58:54 +000011388done:
11389 tp->phy_flags |= eee_cap;
11390
Michael Chan9f40dea2005-09-05 17:53:06 -070011391 return err;
11392}
11393
Michael Chan4cafd3f2005-05-29 14:56:34 -070011394static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11395 u64 *data)
11396{
Michael Chan566f86a2005-05-29 14:56:58 -070011397 struct tg3 *tp = netdev_priv(dev);
11398
Matt Carlson80096062010-08-02 11:26:06 +000011399 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011400 tg3_power_up(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011401
Michael Chan566f86a2005-05-29 14:56:58 -070011402 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11403
11404 if (tg3_test_nvram(tp) != 0) {
11405 etest->flags |= ETH_TEST_FL_FAILED;
11406 data[0] = 1;
11407 }
Michael Chanca430072005-05-29 14:57:23 -070011408 if (tg3_test_link(tp) != 0) {
11409 etest->flags |= ETH_TEST_FL_FAILED;
11410 data[1] = 1;
11411 }
Michael Chana71116d2005-05-29 14:58:11 -070011412 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011413 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011414
Michael Chanbbe832c2005-06-24 20:20:04 -070011415 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011416 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011417 tg3_netif_stop(tp);
11418 irq_sync = 1;
11419 }
11420
11421 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011422
11423 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011424 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011425 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011426 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011427 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011428 if (!err)
11429 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011430
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011431 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080011432 tg3_phy_reset(tp);
11433
Michael Chana71116d2005-05-29 14:58:11 -070011434 if (tg3_test_registers(tp) != 0) {
11435 etest->flags |= ETH_TEST_FL_FAILED;
11436 data[2] = 1;
11437 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011438 if (tg3_test_memory(tp) != 0) {
11439 etest->flags |= ETH_TEST_FL_FAILED;
11440 data[3] = 1;
11441 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011442 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011443 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011444
David S. Millerf47c11e2005-06-24 20:18:35 -070011445 tg3_full_unlock(tp);
11446
Michael Chand4bc3922005-05-29 14:59:20 -070011447 if (tg3_test_interrupt(tp) != 0) {
11448 etest->flags |= ETH_TEST_FL_FAILED;
11449 data[5] = 1;
11450 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011451
11452 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011453
Michael Chana71116d2005-05-29 14:58:11 -070011454 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11455 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011456 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011457 err2 = tg3_restart_hw(tp, 1);
11458 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011459 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011460 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011461
11462 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011463
11464 if (irq_sync && !err2)
11465 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011466 }
Matt Carlson80096062010-08-02 11:26:06 +000011467 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011468 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011469
Michael Chan4cafd3f2005-05-29 14:56:34 -070011470}
11471
Linus Torvalds1da177e2005-04-16 15:20:36 -070011472static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11473{
11474 struct mii_ioctl_data *data = if_mii(ifr);
11475 struct tg3 *tp = netdev_priv(dev);
11476 int err;
11477
Joe Perches63c3a662011-04-26 08:12:10 +000011478 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011479 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011480 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011481 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011482 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011483 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011484 }
11485
Matt Carlson33f401a2010-04-05 10:19:27 +000011486 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011487 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011488 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011489
11490 /* fallthru */
11491 case SIOCGMIIREG: {
11492 u32 mii_regval;
11493
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011494 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011495 break; /* We have no PHY */
11496
Matt Carlson34eea5a2011-04-20 07:57:38 +000011497 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011498 return -EAGAIN;
11499
David S. Millerf47c11e2005-06-24 20:18:35 -070011500 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011501 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011502 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011503
11504 data->val_out = mii_regval;
11505
11506 return err;
11507 }
11508
11509 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011510 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011511 break; /* We have no PHY */
11512
Matt Carlson34eea5a2011-04-20 07:57:38 +000011513 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011514 return -EAGAIN;
11515
David S. Millerf47c11e2005-06-24 20:18:35 -070011516 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011517 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011518 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011519
11520 return err;
11521
11522 default:
11523 /* do nothing */
11524 break;
11525 }
11526 return -EOPNOTSUPP;
11527}
11528
David S. Miller15f98502005-05-18 22:49:26 -070011529static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11530{
11531 struct tg3 *tp = netdev_priv(dev);
11532
11533 memcpy(ec, &tp->coal, sizeof(*ec));
11534 return 0;
11535}
11536
Michael Chand244c892005-07-05 14:42:33 -070011537static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11538{
11539 struct tg3 *tp = netdev_priv(dev);
11540 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11541 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11542
Joe Perches63c3a662011-04-26 08:12:10 +000011543 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011544 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11545 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11546 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11547 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11548 }
11549
11550 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11551 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11552 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11553 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11554 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11555 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11556 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11557 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11558 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11559 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11560 return -EINVAL;
11561
11562 /* No rx interrupts will be generated if both are zero */
11563 if ((ec->rx_coalesce_usecs == 0) &&
11564 (ec->rx_max_coalesced_frames == 0))
11565 return -EINVAL;
11566
11567 /* No tx interrupts will be generated if both are zero */
11568 if ((ec->tx_coalesce_usecs == 0) &&
11569 (ec->tx_max_coalesced_frames == 0))
11570 return -EINVAL;
11571
11572 /* Only copy relevant parameters, ignore all others. */
11573 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11574 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11575 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11576 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11577 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11578 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11579 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11580 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11581 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11582
11583 if (netif_running(dev)) {
11584 tg3_full_lock(tp, 0);
11585 __tg3_set_coalesce(tp, &tp->coal);
11586 tg3_full_unlock(tp);
11587 }
11588 return 0;
11589}
11590
Jeff Garzik7282d492006-09-13 14:30:00 -040011591static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011592 .get_settings = tg3_get_settings,
11593 .set_settings = tg3_set_settings,
11594 .get_drvinfo = tg3_get_drvinfo,
11595 .get_regs_len = tg3_get_regs_len,
11596 .get_regs = tg3_get_regs,
11597 .get_wol = tg3_get_wol,
11598 .set_wol = tg3_set_wol,
11599 .get_msglevel = tg3_get_msglevel,
11600 .set_msglevel = tg3_set_msglevel,
11601 .nway_reset = tg3_nway_reset,
11602 .get_link = ethtool_op_get_link,
11603 .get_eeprom_len = tg3_get_eeprom_len,
11604 .get_eeprom = tg3_get_eeprom,
11605 .set_eeprom = tg3_set_eeprom,
11606 .get_ringparam = tg3_get_ringparam,
11607 .set_ringparam = tg3_set_ringparam,
11608 .get_pauseparam = tg3_get_pauseparam,
11609 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011610 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011611 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011612 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011613 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011614 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011615 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011616 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011617};
11618
11619static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11620{
Michael Chan1b277772006-03-20 22:27:48 -080011621 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011622
11623 tp->nvram_size = EEPROM_CHIP_SIZE;
11624
Matt Carlsone4f34112009-02-25 14:25:00 +000011625 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011626 return;
11627
Michael Chanb16250e2006-09-27 16:10:14 -070011628 if ((magic != TG3_EEPROM_MAGIC) &&
11629 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11630 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011631 return;
11632
11633 /*
11634 * Size the chip by reading offsets at increasing powers of two.
11635 * When we encounter our validation signature, we know the addressing
11636 * has wrapped around, and thus have our chip size.
11637 */
Michael Chan1b277772006-03-20 22:27:48 -080011638 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011639
11640 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011641 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011642 return;
11643
Michael Chan18201802006-03-20 22:29:15 -080011644 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011645 break;
11646
11647 cursize <<= 1;
11648 }
11649
11650 tp->nvram_size = cursize;
11651}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011652
Linus Torvalds1da177e2005-04-16 15:20:36 -070011653static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11654{
11655 u32 val;
11656
Joe Perches63c3a662011-04-26 08:12:10 +000011657 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011658 return;
11659
11660 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011661 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011662 tg3_get_eeprom_size(tp);
11663 return;
11664 }
11665
Matt Carlson6d348f22009-02-25 14:25:52 +000011666 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011667 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011668 /* This is confusing. We want to operate on the
11669 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11670 * call will read from NVRAM and byteswap the data
11671 * according to the byteswapping settings for all
11672 * other register accesses. This ensures the data we
11673 * want will always reside in the lower 16-bits.
11674 * However, the data in NVRAM is in LE format, which
11675 * means the data from the NVRAM read will always be
11676 * opposite the endianness of the CPU. The 16-bit
11677 * byteswap then brings the data to CPU endianness.
11678 */
11679 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011680 return;
11681 }
11682 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011683 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011684}
11685
11686static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11687{
11688 u32 nvcfg1;
11689
11690 nvcfg1 = tr32(NVRAM_CFG1);
11691 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000011692 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011693 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011694 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11695 tw32(NVRAM_CFG1, nvcfg1);
11696 }
11697
Matt Carlson6ff6f812011-05-19 12:12:54 +000011698 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011699 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011700 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011701 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11702 tp->nvram_jedecnum = JEDEC_ATMEL;
11703 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011704 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011705 break;
11706 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11707 tp->nvram_jedecnum = JEDEC_ATMEL;
11708 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11709 break;
11710 case FLASH_VENDOR_ATMEL_EEPROM:
11711 tp->nvram_jedecnum = JEDEC_ATMEL;
11712 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011713 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011714 break;
11715 case FLASH_VENDOR_ST:
11716 tp->nvram_jedecnum = JEDEC_ST;
11717 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011718 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011719 break;
11720 case FLASH_VENDOR_SAIFUN:
11721 tp->nvram_jedecnum = JEDEC_SAIFUN;
11722 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11723 break;
11724 case FLASH_VENDOR_SST_SMALL:
11725 case FLASH_VENDOR_SST_LARGE:
11726 tp->nvram_jedecnum = JEDEC_SST;
11727 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11728 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011729 }
Matt Carlson8590a602009-08-28 12:29:16 +000011730 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011731 tp->nvram_jedecnum = JEDEC_ATMEL;
11732 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011733 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011734 }
11735}
11736
Matt Carlsona1b950d2009-09-01 13:20:17 +000011737static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11738{
11739 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11740 case FLASH_5752PAGE_SIZE_256:
11741 tp->nvram_pagesize = 256;
11742 break;
11743 case FLASH_5752PAGE_SIZE_512:
11744 tp->nvram_pagesize = 512;
11745 break;
11746 case FLASH_5752PAGE_SIZE_1K:
11747 tp->nvram_pagesize = 1024;
11748 break;
11749 case FLASH_5752PAGE_SIZE_2K:
11750 tp->nvram_pagesize = 2048;
11751 break;
11752 case FLASH_5752PAGE_SIZE_4K:
11753 tp->nvram_pagesize = 4096;
11754 break;
11755 case FLASH_5752PAGE_SIZE_264:
11756 tp->nvram_pagesize = 264;
11757 break;
11758 case FLASH_5752PAGE_SIZE_528:
11759 tp->nvram_pagesize = 528;
11760 break;
11761 }
11762}
11763
Michael Chan361b4ac2005-04-21 17:11:21 -070011764static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11765{
11766 u32 nvcfg1;
11767
11768 nvcfg1 = tr32(NVRAM_CFG1);
11769
Michael Chane6af3012005-04-21 17:12:05 -070011770 /* NVRAM protection for TPM */
11771 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000011772 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070011773
Michael Chan361b4ac2005-04-21 17:11:21 -070011774 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011775 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11776 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11777 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011778 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011779 break;
11780 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11781 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011782 tg3_flag_set(tp, NVRAM_BUFFERED);
11783 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011784 break;
11785 case FLASH_5752VENDOR_ST_M45PE10:
11786 case FLASH_5752VENDOR_ST_M45PE20:
11787 case FLASH_5752VENDOR_ST_M45PE40:
11788 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011789 tg3_flag_set(tp, NVRAM_BUFFERED);
11790 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011791 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011792 }
11793
Joe Perches63c3a662011-04-26 08:12:10 +000011794 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011795 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011796 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011797 /* For eeprom, set pagesize to maximum eeprom size */
11798 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11799
11800 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11801 tw32(NVRAM_CFG1, nvcfg1);
11802 }
11803}
11804
Michael Chand3c7b882006-03-23 01:28:25 -080011805static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11806{
Matt Carlson989a9d22007-05-05 11:51:05 -070011807 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011808
11809 nvcfg1 = tr32(NVRAM_CFG1);
11810
11811 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011812 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011813 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070011814 protect = 1;
11815 }
Michael Chand3c7b882006-03-23 01:28:25 -080011816
Matt Carlson989a9d22007-05-05 11:51:05 -070011817 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11818 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011819 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11820 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11821 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11822 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11823 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011824 tg3_flag_set(tp, NVRAM_BUFFERED);
11825 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011826 tp->nvram_pagesize = 264;
11827 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11828 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11829 tp->nvram_size = (protect ? 0x3e200 :
11830 TG3_NVRAM_SIZE_512KB);
11831 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11832 tp->nvram_size = (protect ? 0x1f200 :
11833 TG3_NVRAM_SIZE_256KB);
11834 else
11835 tp->nvram_size = (protect ? 0x1f200 :
11836 TG3_NVRAM_SIZE_128KB);
11837 break;
11838 case FLASH_5752VENDOR_ST_M45PE10:
11839 case FLASH_5752VENDOR_ST_M45PE20:
11840 case FLASH_5752VENDOR_ST_M45PE40:
11841 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011842 tg3_flag_set(tp, NVRAM_BUFFERED);
11843 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011844 tp->nvram_pagesize = 256;
11845 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11846 tp->nvram_size = (protect ?
11847 TG3_NVRAM_SIZE_64KB :
11848 TG3_NVRAM_SIZE_128KB);
11849 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11850 tp->nvram_size = (protect ?
11851 TG3_NVRAM_SIZE_64KB :
11852 TG3_NVRAM_SIZE_256KB);
11853 else
11854 tp->nvram_size = (protect ?
11855 TG3_NVRAM_SIZE_128KB :
11856 TG3_NVRAM_SIZE_512KB);
11857 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011858 }
11859}
11860
Michael Chan1b277772006-03-20 22:27:48 -080011861static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11862{
11863 u32 nvcfg1;
11864
11865 nvcfg1 = tr32(NVRAM_CFG1);
11866
11867 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011868 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11869 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11870 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11871 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11872 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011873 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011874 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011875
Matt Carlson8590a602009-08-28 12:29:16 +000011876 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11877 tw32(NVRAM_CFG1, nvcfg1);
11878 break;
11879 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11880 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11881 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11882 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11883 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011884 tg3_flag_set(tp, NVRAM_BUFFERED);
11885 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011886 tp->nvram_pagesize = 264;
11887 break;
11888 case FLASH_5752VENDOR_ST_M45PE10:
11889 case FLASH_5752VENDOR_ST_M45PE20:
11890 case FLASH_5752VENDOR_ST_M45PE40:
11891 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011892 tg3_flag_set(tp, NVRAM_BUFFERED);
11893 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011894 tp->nvram_pagesize = 256;
11895 break;
Michael Chan1b277772006-03-20 22:27:48 -080011896 }
11897}
11898
Matt Carlson6b91fa02007-10-10 18:01:09 -070011899static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11900{
11901 u32 nvcfg1, protect = 0;
11902
11903 nvcfg1 = tr32(NVRAM_CFG1);
11904
11905 /* NVRAM protection for TPM */
11906 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011907 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011908 protect = 1;
11909 }
11910
11911 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11912 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011913 case FLASH_5761VENDOR_ATMEL_ADB021D:
11914 case FLASH_5761VENDOR_ATMEL_ADB041D:
11915 case FLASH_5761VENDOR_ATMEL_ADB081D:
11916 case FLASH_5761VENDOR_ATMEL_ADB161D:
11917 case FLASH_5761VENDOR_ATMEL_MDB021D:
11918 case FLASH_5761VENDOR_ATMEL_MDB041D:
11919 case FLASH_5761VENDOR_ATMEL_MDB081D:
11920 case FLASH_5761VENDOR_ATMEL_MDB161D:
11921 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011922 tg3_flag_set(tp, NVRAM_BUFFERED);
11923 tg3_flag_set(tp, FLASH);
11924 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000011925 tp->nvram_pagesize = 256;
11926 break;
11927 case FLASH_5761VENDOR_ST_A_M45PE20:
11928 case FLASH_5761VENDOR_ST_A_M45PE40:
11929 case FLASH_5761VENDOR_ST_A_M45PE80:
11930 case FLASH_5761VENDOR_ST_A_M45PE16:
11931 case FLASH_5761VENDOR_ST_M_M45PE20:
11932 case FLASH_5761VENDOR_ST_M_M45PE40:
11933 case FLASH_5761VENDOR_ST_M_M45PE80:
11934 case FLASH_5761VENDOR_ST_M_M45PE16:
11935 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011936 tg3_flag_set(tp, NVRAM_BUFFERED);
11937 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011938 tp->nvram_pagesize = 256;
11939 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011940 }
11941
11942 if (protect) {
11943 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11944 } else {
11945 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011946 case FLASH_5761VENDOR_ATMEL_ADB161D:
11947 case FLASH_5761VENDOR_ATMEL_MDB161D:
11948 case FLASH_5761VENDOR_ST_A_M45PE16:
11949 case FLASH_5761VENDOR_ST_M_M45PE16:
11950 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11951 break;
11952 case FLASH_5761VENDOR_ATMEL_ADB081D:
11953 case FLASH_5761VENDOR_ATMEL_MDB081D:
11954 case FLASH_5761VENDOR_ST_A_M45PE80:
11955 case FLASH_5761VENDOR_ST_M_M45PE80:
11956 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11957 break;
11958 case FLASH_5761VENDOR_ATMEL_ADB041D:
11959 case FLASH_5761VENDOR_ATMEL_MDB041D:
11960 case FLASH_5761VENDOR_ST_A_M45PE40:
11961 case FLASH_5761VENDOR_ST_M_M45PE40:
11962 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11963 break;
11964 case FLASH_5761VENDOR_ATMEL_ADB021D:
11965 case FLASH_5761VENDOR_ATMEL_MDB021D:
11966 case FLASH_5761VENDOR_ST_A_M45PE20:
11967 case FLASH_5761VENDOR_ST_M_M45PE20:
11968 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11969 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011970 }
11971 }
11972}
11973
Michael Chanb5d37722006-09-27 16:06:21 -070011974static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11975{
11976 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011977 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070011978 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11979}
11980
Matt Carlson321d32a2008-11-21 17:22:19 -080011981static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11982{
11983 u32 nvcfg1;
11984
11985 nvcfg1 = tr32(NVRAM_CFG1);
11986
11987 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11988 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11989 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11990 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011991 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080011992 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11993
11994 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11995 tw32(NVRAM_CFG1, nvcfg1);
11996 return;
11997 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11998 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11999 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12000 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12001 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12002 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12003 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12004 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012005 tg3_flag_set(tp, NVRAM_BUFFERED);
12006 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012007
12008 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12009 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12010 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12011 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12012 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12013 break;
12014 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12015 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12016 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12017 break;
12018 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12019 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12020 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12021 break;
12022 }
12023 break;
12024 case FLASH_5752VENDOR_ST_M45PE10:
12025 case FLASH_5752VENDOR_ST_M45PE20:
12026 case FLASH_5752VENDOR_ST_M45PE40:
12027 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012028 tg3_flag_set(tp, NVRAM_BUFFERED);
12029 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012030
12031 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12032 case FLASH_5752VENDOR_ST_M45PE10:
12033 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12034 break;
12035 case FLASH_5752VENDOR_ST_M45PE20:
12036 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12037 break;
12038 case FLASH_5752VENDOR_ST_M45PE40:
12039 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12040 break;
12041 }
12042 break;
12043 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012044 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012045 return;
12046 }
12047
Matt Carlsona1b950d2009-09-01 13:20:17 +000012048 tg3_nvram_get_pagesize(tp, nvcfg1);
12049 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012050 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012051}
12052
12053
12054static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12055{
12056 u32 nvcfg1;
12057
12058 nvcfg1 = tr32(NVRAM_CFG1);
12059
12060 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12061 case FLASH_5717VENDOR_ATMEL_EEPROM:
12062 case FLASH_5717VENDOR_MICRO_EEPROM:
12063 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012064 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012065 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12066
12067 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12068 tw32(NVRAM_CFG1, nvcfg1);
12069 return;
12070 case FLASH_5717VENDOR_ATMEL_MDB011D:
12071 case FLASH_5717VENDOR_ATMEL_ADB011B:
12072 case FLASH_5717VENDOR_ATMEL_ADB011D:
12073 case FLASH_5717VENDOR_ATMEL_MDB021D:
12074 case FLASH_5717VENDOR_ATMEL_ADB021B:
12075 case FLASH_5717VENDOR_ATMEL_ADB021D:
12076 case FLASH_5717VENDOR_ATMEL_45USPT:
12077 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012078 tg3_flag_set(tp, NVRAM_BUFFERED);
12079 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012080
12081 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12082 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012083 /* Detect size with tg3_nvram_get_size() */
12084 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012085 case FLASH_5717VENDOR_ATMEL_ADB021B:
12086 case FLASH_5717VENDOR_ATMEL_ADB021D:
12087 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12088 break;
12089 default:
12090 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12091 break;
12092 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012093 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012094 case FLASH_5717VENDOR_ST_M_M25PE10:
12095 case FLASH_5717VENDOR_ST_A_M25PE10:
12096 case FLASH_5717VENDOR_ST_M_M45PE10:
12097 case FLASH_5717VENDOR_ST_A_M45PE10:
12098 case FLASH_5717VENDOR_ST_M_M25PE20:
12099 case FLASH_5717VENDOR_ST_A_M25PE20:
12100 case FLASH_5717VENDOR_ST_M_M45PE20:
12101 case FLASH_5717VENDOR_ST_A_M45PE20:
12102 case FLASH_5717VENDOR_ST_25USPT:
12103 case FLASH_5717VENDOR_ST_45USPT:
12104 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012105 tg3_flag_set(tp, NVRAM_BUFFERED);
12106 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012107
12108 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12109 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012110 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012111 /* Detect size with tg3_nvram_get_size() */
12112 break;
12113 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012114 case FLASH_5717VENDOR_ST_A_M45PE20:
12115 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12116 break;
12117 default:
12118 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12119 break;
12120 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012121 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012122 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012123 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012124 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012125 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012126
12127 tg3_nvram_get_pagesize(tp, nvcfg1);
12128 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012129 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012130}
12131
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012132static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12133{
12134 u32 nvcfg1, nvmpinstrp;
12135
12136 nvcfg1 = tr32(NVRAM_CFG1);
12137 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12138
12139 switch (nvmpinstrp) {
12140 case FLASH_5720_EEPROM_HD:
12141 case FLASH_5720_EEPROM_LD:
12142 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012143 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012144
12145 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12146 tw32(NVRAM_CFG1, nvcfg1);
12147 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12148 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12149 else
12150 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12151 return;
12152 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12153 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12154 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12155 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12156 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12157 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12158 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12159 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12160 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12161 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12162 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12163 case FLASH_5720VENDOR_ATMEL_45USPT:
12164 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012165 tg3_flag_set(tp, NVRAM_BUFFERED);
12166 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012167
12168 switch (nvmpinstrp) {
12169 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12170 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12171 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12172 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12173 break;
12174 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12175 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12176 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12177 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12178 break;
12179 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12180 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12181 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12182 break;
12183 default:
12184 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12185 break;
12186 }
12187 break;
12188 case FLASH_5720VENDOR_M_ST_M25PE10:
12189 case FLASH_5720VENDOR_M_ST_M45PE10:
12190 case FLASH_5720VENDOR_A_ST_M25PE10:
12191 case FLASH_5720VENDOR_A_ST_M45PE10:
12192 case FLASH_5720VENDOR_M_ST_M25PE20:
12193 case FLASH_5720VENDOR_M_ST_M45PE20:
12194 case FLASH_5720VENDOR_A_ST_M25PE20:
12195 case FLASH_5720VENDOR_A_ST_M45PE20:
12196 case FLASH_5720VENDOR_M_ST_M25PE40:
12197 case FLASH_5720VENDOR_M_ST_M45PE40:
12198 case FLASH_5720VENDOR_A_ST_M25PE40:
12199 case FLASH_5720VENDOR_A_ST_M45PE40:
12200 case FLASH_5720VENDOR_M_ST_M25PE80:
12201 case FLASH_5720VENDOR_M_ST_M45PE80:
12202 case FLASH_5720VENDOR_A_ST_M25PE80:
12203 case FLASH_5720VENDOR_A_ST_M45PE80:
12204 case FLASH_5720VENDOR_ST_25USPT:
12205 case FLASH_5720VENDOR_ST_45USPT:
12206 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012207 tg3_flag_set(tp, NVRAM_BUFFERED);
12208 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012209
12210 switch (nvmpinstrp) {
12211 case FLASH_5720VENDOR_M_ST_M25PE20:
12212 case FLASH_5720VENDOR_M_ST_M45PE20:
12213 case FLASH_5720VENDOR_A_ST_M25PE20:
12214 case FLASH_5720VENDOR_A_ST_M45PE20:
12215 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12216 break;
12217 case FLASH_5720VENDOR_M_ST_M25PE40:
12218 case FLASH_5720VENDOR_M_ST_M45PE40:
12219 case FLASH_5720VENDOR_A_ST_M25PE40:
12220 case FLASH_5720VENDOR_A_ST_M45PE40:
12221 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12222 break;
12223 case FLASH_5720VENDOR_M_ST_M25PE80:
12224 case FLASH_5720VENDOR_M_ST_M45PE80:
12225 case FLASH_5720VENDOR_A_ST_M25PE80:
12226 case FLASH_5720VENDOR_A_ST_M45PE80:
12227 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12228 break;
12229 default:
12230 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12231 break;
12232 }
12233 break;
12234 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012235 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012236 return;
12237 }
12238
12239 tg3_nvram_get_pagesize(tp, nvcfg1);
12240 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012241 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012242}
12243
Linus Torvalds1da177e2005-04-16 15:20:36 -070012244/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12245static void __devinit tg3_nvram_init(struct tg3 *tp)
12246{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012247 tw32_f(GRC_EEPROM_ADDR,
12248 (EEPROM_ADDR_FSM_RESET |
12249 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12250 EEPROM_ADDR_CLKPERD_SHIFT)));
12251
Michael Chan9d57f012006-12-07 00:23:25 -080012252 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012253
12254 /* Enable seeprom accesses. */
12255 tw32_f(GRC_LOCAL_CTRL,
12256 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12257 udelay(100);
12258
12259 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12260 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012261 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012262
Michael Chanec41c7d2006-01-17 02:40:55 -080012263 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012264 netdev_warn(tp->dev,
12265 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012266 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012267 return;
12268 }
Michael Chane6af3012005-04-21 17:12:05 -070012269 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012270
Matt Carlson989a9d22007-05-05 11:51:05 -070012271 tp->nvram_size = 0;
12272
Michael Chan361b4ac2005-04-21 17:11:21 -070012273 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12274 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012275 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12276 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012277 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012278 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12279 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012280 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012281 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12282 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012283 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12284 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012285 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12286 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012287 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012288 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12289 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012290 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012291 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12292 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012293 else
12294 tg3_get_nvram_info(tp);
12295
Matt Carlson989a9d22007-05-05 11:51:05 -070012296 if (tp->nvram_size == 0)
12297 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012298
Michael Chane6af3012005-04-21 17:12:05 -070012299 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012300 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012301
12302 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012303 tg3_flag_clear(tp, NVRAM);
12304 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012305
12306 tg3_get_eeprom_size(tp);
12307 }
12308}
12309
Linus Torvalds1da177e2005-04-16 15:20:36 -070012310static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12311 u32 offset, u32 len, u8 *buf)
12312{
12313 int i, j, rc = 0;
12314 u32 val;
12315
12316 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012317 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012318 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012319
12320 addr = offset + i;
12321
12322 memcpy(&data, buf + i, 4);
12323
Matt Carlson62cedd12009-04-20 14:52:29 -070012324 /*
12325 * The SEEPROM interface expects the data to always be opposite
12326 * the native endian format. We accomplish this by reversing
12327 * all the operations that would have been performed on the
12328 * data from a call to tg3_nvram_read_be32().
12329 */
12330 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012331
12332 val = tr32(GRC_EEPROM_ADDR);
12333 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12334
12335 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12336 EEPROM_ADDR_READ);
12337 tw32(GRC_EEPROM_ADDR, val |
12338 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12339 (addr & EEPROM_ADDR_ADDR_MASK) |
12340 EEPROM_ADDR_START |
12341 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012342
Michael Chan9d57f012006-12-07 00:23:25 -080012343 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012344 val = tr32(GRC_EEPROM_ADDR);
12345
12346 if (val & EEPROM_ADDR_COMPLETE)
12347 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012348 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012349 }
12350 if (!(val & EEPROM_ADDR_COMPLETE)) {
12351 rc = -EBUSY;
12352 break;
12353 }
12354 }
12355
12356 return rc;
12357}
12358
12359/* offset and length are dword aligned */
12360static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12361 u8 *buf)
12362{
12363 int ret = 0;
12364 u32 pagesize = tp->nvram_pagesize;
12365 u32 pagemask = pagesize - 1;
12366 u32 nvram_cmd;
12367 u8 *tmp;
12368
12369 tmp = kmalloc(pagesize, GFP_KERNEL);
12370 if (tmp == NULL)
12371 return -ENOMEM;
12372
12373 while (len) {
12374 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012375 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012376
12377 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012378
Linus Torvalds1da177e2005-04-16 15:20:36 -070012379 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012380 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12381 (__be32 *) (tmp + j));
12382 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012383 break;
12384 }
12385 if (ret)
12386 break;
12387
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012388 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012389 size = pagesize;
12390 if (len < size)
12391 size = len;
12392
12393 len -= size;
12394
12395 memcpy(tmp + page_off, buf, size);
12396
12397 offset = offset + (pagesize - page_off);
12398
Michael Chane6af3012005-04-21 17:12:05 -070012399 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012400
12401 /*
12402 * Before we can erase the flash page, we need
12403 * to issue a special "write enable" command.
12404 */
12405 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12406
12407 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12408 break;
12409
12410 /* Erase the target page */
12411 tw32(NVRAM_ADDR, phy_addr);
12412
12413 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12414 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12415
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012416 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012417 break;
12418
12419 /* Issue another write enable to start the write. */
12420 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12421
12422 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12423 break;
12424
12425 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012426 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012427
Al Virob9fc7dc2007-12-17 22:59:57 -080012428 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012429
Al Virob9fc7dc2007-12-17 22:59:57 -080012430 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012431
12432 tw32(NVRAM_ADDR, phy_addr + j);
12433
12434 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12435 NVRAM_CMD_WR;
12436
12437 if (j == 0)
12438 nvram_cmd |= NVRAM_CMD_FIRST;
12439 else if (j == (pagesize - 4))
12440 nvram_cmd |= NVRAM_CMD_LAST;
12441
12442 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12443 break;
12444 }
12445 if (ret)
12446 break;
12447 }
12448
12449 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12450 tg3_nvram_exec_cmd(tp, nvram_cmd);
12451
12452 kfree(tmp);
12453
12454 return ret;
12455}
12456
12457/* offset and length are dword aligned */
12458static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12459 u8 *buf)
12460{
12461 int i, ret = 0;
12462
12463 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012464 u32 page_off, phy_addr, nvram_cmd;
12465 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012466
12467 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012468 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012469
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012470 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012471
Michael Chan18201802006-03-20 22:29:15 -080012472 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012473
12474 tw32(NVRAM_ADDR, phy_addr);
12475
12476 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12477
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012478 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012479 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012480 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012481 nvram_cmd |= NVRAM_CMD_LAST;
12482
12483 if (i == (len - 4))
12484 nvram_cmd |= NVRAM_CMD_LAST;
12485
Matt Carlson321d32a2008-11-21 17:22:19 -080012486 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012487 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012488 (tp->nvram_jedecnum == JEDEC_ST) &&
12489 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012490
12491 if ((ret = tg3_nvram_exec_cmd(tp,
12492 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12493 NVRAM_CMD_DONE)))
12494
12495 break;
12496 }
Joe Perches63c3a662011-04-26 08:12:10 +000012497 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012498 /* We always do complete word writes to eeprom. */
12499 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12500 }
12501
12502 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12503 break;
12504 }
12505 return ret;
12506}
12507
12508/* offset and length are dword aligned */
12509static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12510{
12511 int ret;
12512
Joe Perches63c3a662011-04-26 08:12:10 +000012513 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012514 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12515 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012516 udelay(40);
12517 }
12518
Joe Perches63c3a662011-04-26 08:12:10 +000012519 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012520 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012521 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012522 u32 grc_mode;
12523
Michael Chanec41c7d2006-01-17 02:40:55 -080012524 ret = tg3_nvram_lock(tp);
12525 if (ret)
12526 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012527
Michael Chane6af3012005-04-21 17:12:05 -070012528 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012529 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012530 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012531
12532 grc_mode = tr32(GRC_MODE);
12533 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12534
Joe Perches63c3a662011-04-26 08:12:10 +000012535 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012536 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12537 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012538 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012539 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12540 buf);
12541 }
12542
12543 grc_mode = tr32(GRC_MODE);
12544 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12545
Michael Chane6af3012005-04-21 17:12:05 -070012546 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012547 tg3_nvram_unlock(tp);
12548 }
12549
Joe Perches63c3a662011-04-26 08:12:10 +000012550 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012551 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012552 udelay(40);
12553 }
12554
12555 return ret;
12556}
12557
12558struct subsys_tbl_ent {
12559 u16 subsys_vendor, subsys_devid;
12560 u32 phy_id;
12561};
12562
Matt Carlson24daf2b2010-02-17 15:17:02 +000012563static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012564 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012565 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012566 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012567 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012568 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012569 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012570 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012571 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12572 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12573 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012574 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012575 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012576 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012577 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12578 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12579 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012580 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012581 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012582 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012583 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012584 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012585 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012586 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012587
12588 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012589 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012590 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012591 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012592 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012593 { TG3PCI_SUBVENDOR_ID_3COM,
12594 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12595 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012596 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012597 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012598 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012599
12600 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012601 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012602 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012603 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012604 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012605 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012606 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012607 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012608 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012609
12610 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012611 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012612 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012613 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012614 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012615 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12616 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12617 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012618 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012619 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012620 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012621
12622 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012623 { TG3PCI_SUBVENDOR_ID_IBM,
12624 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012625};
12626
Matt Carlson24daf2b2010-02-17 15:17:02 +000012627static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012628{
12629 int i;
12630
12631 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12632 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12633 tp->pdev->subsystem_vendor) &&
12634 (subsys_id_to_phy_id[i].subsys_devid ==
12635 tp->pdev->subsystem_device))
12636 return &subsys_id_to_phy_id[i];
12637 }
12638 return NULL;
12639}
12640
Michael Chan7d0c41e2005-04-21 17:06:20 -070012641static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012642{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012643 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012644 u16 pmcsr;
12645
12646 /* On some early chips the SRAM cannot be accessed in D3hot state,
12647 * so need make sure we're in D0.
12648 */
12649 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12650 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12651 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12652 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012653
12654 /* Make sure register accesses (indirect or otherwise)
12655 * will function correctly.
12656 */
12657 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12658 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012659
David S. Millerf49639e2006-06-09 11:58:36 -070012660 /* The memory arbiter has to be enabled in order for SRAM accesses
12661 * to succeed. Normally on powerup the tg3 chip firmware will make
12662 * sure it is enabled, but other entities such as system netboot
12663 * code might disable it.
12664 */
12665 val = tr32(MEMARB_MODE);
12666 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12667
Matt Carlson79eb6902010-02-17 15:17:03 +000012668 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012669 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12670
Gary Zambranoa85feb82007-05-05 11:52:19 -070012671 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012672 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12673 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012674
Michael Chanb5d37722006-09-27 16:06:21 -070012675 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012676 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012677 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12678 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012679 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012680 val = tr32(VCPU_CFGSHDW);
12681 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012682 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012683 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012684 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012685 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012686 device_set_wakeup_enable(&tp->pdev->dev, true);
12687 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012688 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012689 }
12690
Linus Torvalds1da177e2005-04-16 15:20:36 -070012691 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12692 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12693 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012694 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012695 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012696
12697 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12698 tp->nic_sram_data_cfg = nic_cfg;
12699
12700 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12701 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000012702 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12703 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
12704 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012705 (ver > 0) && (ver < 0x100))
12706 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12707
Matt Carlsona9daf362008-05-25 23:49:44 -070012708 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12709 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12710
Linus Torvalds1da177e2005-04-16 15:20:36 -070012711 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12712 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12713 eeprom_phy_serdes = 1;
12714
12715 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12716 if (nic_phy_id != 0) {
12717 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12718 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12719
12720 eeprom_phy_id = (id1 >> 16) << 10;
12721 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12722 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12723 } else
12724 eeprom_phy_id = 0;
12725
Michael Chan7d0c41e2005-04-21 17:06:20 -070012726 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012727 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000012728 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012729 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012730 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012731 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012732 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012733
Joe Perches63c3a662011-04-26 08:12:10 +000012734 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012735 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12736 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012737 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012738 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12739
12740 switch (led_cfg) {
12741 default:
12742 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12743 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12744 break;
12745
12746 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12747 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12748 break;
12749
12750 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12751 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012752
12753 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12754 * read on some older 5700/5701 bootcode.
12755 */
12756 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12757 ASIC_REV_5700 ||
12758 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12759 ASIC_REV_5701)
12760 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12761
Linus Torvalds1da177e2005-04-16 15:20:36 -070012762 break;
12763
12764 case SHASTA_EXT_LED_SHARED:
12765 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12766 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12767 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12768 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12769 LED_CTRL_MODE_PHY_2);
12770 break;
12771
12772 case SHASTA_EXT_LED_MAC:
12773 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12774 break;
12775
12776 case SHASTA_EXT_LED_COMBO:
12777 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12778 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12779 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12780 LED_CTRL_MODE_PHY_2);
12781 break;
12782
Stephen Hemminger855e1112008-04-16 16:37:28 -070012783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012784
12785 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12786 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12787 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12788 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12789
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012790 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12791 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012792
Michael Chan9d26e212006-12-07 00:21:14 -080012793 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000012794 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012795 if ((tp->pdev->subsystem_vendor ==
12796 PCI_VENDOR_ID_ARIMA) &&
12797 (tp->pdev->subsystem_device == 0x205a ||
12798 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000012799 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012800 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012801 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12802 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012804
12805 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000012806 tg3_flag_set(tp, ENABLE_ASF);
12807 if (tg3_flag(tp, 5750_PLUS))
12808 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012809 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012810
12811 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012812 tg3_flag(tp, 5750_PLUS))
12813 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012814
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012815 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012816 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000012817 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012818
Joe Perches63c3a662011-04-26 08:12:10 +000012819 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012820 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012821 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012822 device_set_wakeup_enable(&tp->pdev->dev, true);
12823 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012824
Linus Torvalds1da177e2005-04-16 15:20:36 -070012825 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012826 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012827
12828 /* serdes signal pre-emphasis in register 0x590 set by */
12829 /* bootcode if bit 18 is set */
12830 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012831 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012832
Joe Perches63c3a662011-04-26 08:12:10 +000012833 if ((tg3_flag(tp, 57765_PLUS) ||
12834 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12835 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012836 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012837 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012838
Joe Perches63c3a662011-04-26 08:12:10 +000012839 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012840 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012841 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012842 u32 cfg3;
12843
12844 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12845 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000012846 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070012847 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012848
Matt Carlson14417062010-02-17 15:16:59 +000012849 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000012850 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070012851 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012852 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070012853 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012854 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012855 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012856done:
Joe Perches63c3a662011-04-26 08:12:10 +000012857 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012858 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000012859 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012860 else
12861 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012862}
12863
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012864static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12865{
12866 int i;
12867 u32 val;
12868
12869 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12870 tw32(OTP_CTRL, cmd);
12871
12872 /* Wait for up to 1 ms for command to execute. */
12873 for (i = 0; i < 100; i++) {
12874 val = tr32(OTP_STATUS);
12875 if (val & OTP_STATUS_CMD_DONE)
12876 break;
12877 udelay(10);
12878 }
12879
12880 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12881}
12882
12883/* Read the gphy configuration from the OTP region of the chip. The gphy
12884 * configuration is a 32-bit value that straddles the alignment boundary.
12885 * We do two 32-bit reads and then shift and merge the results.
12886 */
12887static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12888{
12889 u32 bhalf_otp, thalf_otp;
12890
12891 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12892
12893 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12894 return 0;
12895
12896 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12897
12898 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12899 return 0;
12900
12901 thalf_otp = tr32(OTP_READ_DATA);
12902
12903 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12904
12905 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12906 return 0;
12907
12908 bhalf_otp = tr32(OTP_READ_DATA);
12909
12910 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12911}
12912
Matt Carlsone256f8a2011-03-09 16:58:24 +000012913static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12914{
12915 u32 adv = ADVERTISED_Autoneg |
12916 ADVERTISED_Pause;
12917
12918 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12919 adv |= ADVERTISED_1000baseT_Half |
12920 ADVERTISED_1000baseT_Full;
12921
12922 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12923 adv |= ADVERTISED_100baseT_Half |
12924 ADVERTISED_100baseT_Full |
12925 ADVERTISED_10baseT_Half |
12926 ADVERTISED_10baseT_Full |
12927 ADVERTISED_TP;
12928 else
12929 adv |= ADVERTISED_FIBRE;
12930
12931 tp->link_config.advertising = adv;
12932 tp->link_config.speed = SPEED_INVALID;
12933 tp->link_config.duplex = DUPLEX_INVALID;
12934 tp->link_config.autoneg = AUTONEG_ENABLE;
12935 tp->link_config.active_speed = SPEED_INVALID;
12936 tp->link_config.active_duplex = DUPLEX_INVALID;
12937 tp->link_config.orig_speed = SPEED_INVALID;
12938 tp->link_config.orig_duplex = DUPLEX_INVALID;
12939 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12940}
12941
Michael Chan7d0c41e2005-04-21 17:06:20 -070012942static int __devinit tg3_phy_probe(struct tg3 *tp)
12943{
12944 u32 hw_phy_id_1, hw_phy_id_2;
12945 u32 hw_phy_id, hw_phy_id_masked;
12946 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012947
Matt Carlsone256f8a2011-03-09 16:58:24 +000012948 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000012949 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000012950 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
12951
Joe Perches63c3a662011-04-26 08:12:10 +000012952 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012953 return tg3_phy_init(tp);
12954
Linus Torvalds1da177e2005-04-16 15:20:36 -070012955 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012956 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012957 */
12958 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012959 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012960 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012961 } else {
12962 /* Now read the physical PHY_ID from the chip and verify
12963 * that it is sane. If it doesn't look good, we fall back
12964 * to either the hard-coded table based PHY_ID and failing
12965 * that the value found in the eeprom area.
12966 */
12967 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12968 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12969
12970 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12971 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12972 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12973
Matt Carlson79eb6902010-02-17 15:17:03 +000012974 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012975 }
12976
Matt Carlson79eb6902010-02-17 15:17:03 +000012977 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012978 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012979 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012980 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012981 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012982 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012983 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012984 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012985 /* Do nothing, phy ID already set up in
12986 * tg3_get_eeprom_hw_cfg().
12987 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012988 } else {
12989 struct subsys_tbl_ent *p;
12990
12991 /* No eeprom signature? Try the hardcoded
12992 * subsys device table.
12993 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012994 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012995 if (!p)
12996 return -ENODEV;
12997
12998 tp->phy_id = p->phy_id;
12999 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013000 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013001 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013002 }
13003 }
13004
Matt Carlsona6b68da2010-12-06 08:28:52 +000013005 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
13006 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
13007 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13008 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13009 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013010 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13011
Matt Carlsone256f8a2011-03-09 16:58:24 +000013012 tg3_phy_init_link_config(tp);
13013
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013014 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013015 !tg3_flag(tp, ENABLE_APE) &&
13016 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013017 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013018
13019 tg3_readphy(tp, MII_BMSR, &bmsr);
13020 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13021 (bmsr & BMSR_LSTATUS))
13022 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013023
Linus Torvalds1da177e2005-04-16 15:20:36 -070013024 err = tg3_phy_reset(tp);
13025 if (err)
13026 return err;
13027
Matt Carlson42b64a42011-05-19 12:12:49 +000013028 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013029
Michael Chan3600d912006-12-07 00:21:48 -080013030 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13031 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13032 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13033 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013034 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13035 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013036
13037 tg3_writephy(tp, MII_BMCR,
13038 BMCR_ANENABLE | BMCR_ANRESTART);
13039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013040 }
13041
13042skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013043 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013044 err = tg3_init_5401phy_dsp(tp);
13045 if (err)
13046 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013047
Linus Torvalds1da177e2005-04-16 15:20:36 -070013048 err = tg3_init_5401phy_dsp(tp);
13049 }
13050
Linus Torvalds1da177e2005-04-16 15:20:36 -070013051 return err;
13052}
13053
Matt Carlson184b8902010-04-05 10:19:25 +000013054static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013055{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013056 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013057 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000013058 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013059
Matt Carlsonc3e94502011-04-13 11:05:08 +000013060 vpd_data = (u8 *)tg3_vpd_readblock(tp);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013061 if (!vpd_data)
13062 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013063
Matt Carlson4181b2c2010-02-26 14:04:45 +000013064 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
13065 PCI_VPD_LRDT_RO_DATA);
13066 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013067 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013068
13069 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13070 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13071 i += PCI_VPD_LRDT_TAG_SIZE;
13072
13073 if (block_end > TG3_NVM_VPD_LEN)
13074 goto out_not_found;
13075
Matt Carlson184b8902010-04-05 10:19:25 +000013076 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13077 PCI_VPD_RO_KEYWORD_MFR_ID);
13078 if (j > 0) {
13079 len = pci_vpd_info_field_size(&vpd_data[j]);
13080
13081 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13082 if (j + len > block_end || len != 4 ||
13083 memcmp(&vpd_data[j], "1028", 4))
13084 goto partno;
13085
13086 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13087 PCI_VPD_RO_KEYWORD_VENDOR0);
13088 if (j < 0)
13089 goto partno;
13090
13091 len = pci_vpd_info_field_size(&vpd_data[j]);
13092
13093 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13094 if (j + len > block_end)
13095 goto partno;
13096
13097 memcpy(tp->fw_ver, &vpd_data[j], len);
13098 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
13099 }
13100
13101partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013102 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13103 PCI_VPD_RO_KEYWORD_PARTNO);
13104 if (i < 0)
13105 goto out_not_found;
13106
13107 len = pci_vpd_info_field_size(&vpd_data[i]);
13108
13109 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13110 if (len > TG3_BPN_SIZE ||
13111 (len + i) > TG3_NVM_VPD_LEN)
13112 goto out_not_found;
13113
13114 memcpy(tp->board_part_number, &vpd_data[i], len);
13115
Linus Torvalds1da177e2005-04-16 15:20:36 -070013116out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013117 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013118 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013119 return;
13120
13121out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013122 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13123 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13124 strcpy(tp->board_part_number, "BCM5717");
13125 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13126 strcpy(tp->board_part_number, "BCM5718");
13127 else
13128 goto nomatch;
13129 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13130 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13131 strcpy(tp->board_part_number, "BCM57780");
13132 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13133 strcpy(tp->board_part_number, "BCM57760");
13134 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13135 strcpy(tp->board_part_number, "BCM57790");
13136 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13137 strcpy(tp->board_part_number, "BCM57788");
13138 else
13139 goto nomatch;
13140 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13141 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13142 strcpy(tp->board_part_number, "BCM57761");
13143 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13144 strcpy(tp->board_part_number, "BCM57765");
13145 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13146 strcpy(tp->board_part_number, "BCM57781");
13147 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13148 strcpy(tp->board_part_number, "BCM57785");
13149 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13150 strcpy(tp->board_part_number, "BCM57791");
13151 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13152 strcpy(tp->board_part_number, "BCM57795");
13153 else
13154 goto nomatch;
13155 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013156 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013157 } else {
13158nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013159 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013161}
13162
Matt Carlson9c8a6202007-10-21 16:16:08 -070013163static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13164{
13165 u32 val;
13166
Matt Carlsone4f34112009-02-25 14:25:00 +000013167 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013168 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013169 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013170 val != 0)
13171 return 0;
13172
13173 return 1;
13174}
13175
Matt Carlsonacd9c112009-02-25 14:26:33 +000013176static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13177{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013178 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013179 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013180 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013181
13182 if (tg3_nvram_read(tp, 0xc, &offset) ||
13183 tg3_nvram_read(tp, 0x4, &start))
13184 return;
13185
13186 offset = tg3_nvram_logical_addr(tp, offset);
13187
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013188 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013189 return;
13190
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013191 if ((val & 0xfc000000) == 0x0c000000) {
13192 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013193 return;
13194
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013195 if (val == 0)
13196 newver = true;
13197 }
13198
Matt Carlson75f99362010-04-05 10:19:24 +000013199 dst_off = strlen(tp->fw_ver);
13200
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013201 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013202 if (TG3_VER_SIZE - dst_off < 16 ||
13203 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013204 return;
13205
13206 offset = offset + ver_offset - start;
13207 for (i = 0; i < 16; i += 4) {
13208 __be32 v;
13209 if (tg3_nvram_read_be32(tp, offset + i, &v))
13210 return;
13211
Matt Carlson75f99362010-04-05 10:19:24 +000013212 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013213 }
13214 } else {
13215 u32 major, minor;
13216
13217 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13218 return;
13219
13220 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13221 TG3_NVM_BCVER_MAJSFT;
13222 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013223 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13224 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013225 }
13226}
13227
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013228static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13229{
13230 u32 val, major, minor;
13231
13232 /* Use native endian representation */
13233 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13234 return;
13235
13236 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13237 TG3_NVM_HWSB_CFG1_MAJSFT;
13238 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13239 TG3_NVM_HWSB_CFG1_MINSFT;
13240
13241 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13242}
13243
Matt Carlsondfe00d72008-11-21 17:19:41 -080013244static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13245{
13246 u32 offset, major, minor, build;
13247
Matt Carlson75f99362010-04-05 10:19:24 +000013248 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013249
13250 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13251 return;
13252
13253 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13254 case TG3_EEPROM_SB_REVISION_0:
13255 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13256 break;
13257 case TG3_EEPROM_SB_REVISION_2:
13258 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13259 break;
13260 case TG3_EEPROM_SB_REVISION_3:
13261 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13262 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013263 case TG3_EEPROM_SB_REVISION_4:
13264 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13265 break;
13266 case TG3_EEPROM_SB_REVISION_5:
13267 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13268 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013269 case TG3_EEPROM_SB_REVISION_6:
13270 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13271 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013272 default:
13273 return;
13274 }
13275
Matt Carlsone4f34112009-02-25 14:25:00 +000013276 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013277 return;
13278
13279 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13280 TG3_EEPROM_SB_EDH_BLD_SHFT;
13281 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13282 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13283 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13284
13285 if (minor > 99 || build > 26)
13286 return;
13287
Matt Carlson75f99362010-04-05 10:19:24 +000013288 offset = strlen(tp->fw_ver);
13289 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13290 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013291
13292 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013293 offset = strlen(tp->fw_ver);
13294 if (offset < TG3_VER_SIZE - 1)
13295 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013296 }
13297}
13298
Matt Carlsonacd9c112009-02-25 14:26:33 +000013299static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013300{
13301 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013302 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013303
13304 for (offset = TG3_NVM_DIR_START;
13305 offset < TG3_NVM_DIR_END;
13306 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013307 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013308 return;
13309
13310 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13311 break;
13312 }
13313
13314 if (offset == TG3_NVM_DIR_END)
13315 return;
13316
Joe Perches63c3a662011-04-26 08:12:10 +000013317 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013318 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013319 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013320 return;
13321
Matt Carlsone4f34112009-02-25 14:25:00 +000013322 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013323 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013324 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013325 return;
13326
13327 offset += val - start;
13328
Matt Carlsonacd9c112009-02-25 14:26:33 +000013329 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013330
Matt Carlsonacd9c112009-02-25 14:26:33 +000013331 tp->fw_ver[vlen++] = ',';
13332 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013333
13334 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013335 __be32 v;
13336 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013337 return;
13338
Al Virob9fc7dc2007-12-17 22:59:57 -080013339 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013340
Matt Carlsonacd9c112009-02-25 14:26:33 +000013341 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13342 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013343 break;
13344 }
13345
Matt Carlsonacd9c112009-02-25 14:26:33 +000013346 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13347 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013348 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013349}
13350
Matt Carlson7fd76442009-02-25 14:27:20 +000013351static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13352{
13353 int vlen;
13354 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013355 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013356
Joe Perches63c3a662011-04-26 08:12:10 +000013357 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013358 return;
13359
13360 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13361 if (apedata != APE_SEG_SIG_MAGIC)
13362 return;
13363
13364 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13365 if (!(apedata & APE_FW_STATUS_READY))
13366 return;
13367
13368 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13369
Matt Carlsondc6d0742010-09-15 08:59:55 +000013370 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013371 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013372 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013373 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013374 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013375 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013376
Matt Carlson7fd76442009-02-25 14:27:20 +000013377 vlen = strlen(tp->fw_ver);
13378
Matt Carlsonecc79642010-08-02 11:26:01 +000013379 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13380 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013381 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13382 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13383 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13384 (apedata & APE_FW_VERSION_BLDMSK));
13385}
13386
Matt Carlsonacd9c112009-02-25 14:26:33 +000013387static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13388{
13389 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013390 bool vpd_vers = false;
13391
13392 if (tp->fw_ver[0] != 0)
13393 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013394
Joe Perches63c3a662011-04-26 08:12:10 +000013395 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013396 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013397 return;
13398 }
13399
Matt Carlsonacd9c112009-02-25 14:26:33 +000013400 if (tg3_nvram_read(tp, 0, &val))
13401 return;
13402
13403 if (val == TG3_EEPROM_MAGIC)
13404 tg3_read_bc_ver(tp);
13405 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13406 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013407 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13408 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013409 else
13410 return;
13411
Joe Perches63c3a662011-04-26 08:12:10 +000013412 if (!tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013413 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013414
13415 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013416
Matt Carlson75f99362010-04-05 10:19:24 +000013417done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013418 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013419}
13420
Michael Chan7544b092007-05-05 13:08:32 -070013421static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13422
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013423static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13424{
Joe Perches63c3a662011-04-26 08:12:10 +000013425 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013426 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013427 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013428 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013429 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013430 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013431}
13432
Matt Carlson41434702011-03-09 16:58:22 +000013433static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013434 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13435 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13436 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13437 { },
13438};
13439
Linus Torvalds1da177e2005-04-16 15:20:36 -070013440static int __devinit tg3_get_invariants(struct tg3 *tp)
13441{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013442 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013443 u32 pci_state_reg, grc_misc_cfg;
13444 u32 val;
13445 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013446 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013447
Linus Torvalds1da177e2005-04-16 15:20:36 -070013448 /* Force memory write invalidate off. If we leave it on,
13449 * then on 5700_BX chips we have to enable a workaround.
13450 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13451 * to match the cacheline size. The Broadcom driver have this
13452 * workaround but turns MWI off all the times so never uses
13453 * it. This seems to suggest that the workaround is insufficient.
13454 */
13455 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13456 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13457 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13458
13459 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13460 * has the register indirect write enable bit set before
13461 * we try to access any of the MMIO registers. It is also
13462 * critical that the PCI-X hw workaround situation is decided
13463 * before that as well.
13464 */
13465 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13466 &misc_ctrl_reg);
13467
13468 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13469 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013470 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13471 u32 prod_id_asic_rev;
13472
Matt Carlson5001e2f2009-11-13 13:03:51 +000013473 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13474 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013475 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13476 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013477 pci_read_config_dword(tp->pdev,
13478 TG3PCI_GEN2_PRODID_ASICREV,
13479 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013480 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13481 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13482 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13483 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13484 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13485 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13486 pci_read_config_dword(tp->pdev,
13487 TG3PCI_GEN15_PRODID_ASICREV,
13488 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013489 else
13490 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13491 &prod_id_asic_rev);
13492
Matt Carlson321d32a2008-11-21 17:22:19 -080013493 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013495
Michael Chanff645be2005-04-21 17:09:53 -070013496 /* Wrong chip ID in 5752 A0. This code can be removed later
13497 * as A0 is not in production.
13498 */
13499 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13500 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13501
Michael Chan68929142005-08-09 20:17:14 -070013502 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13503 * we need to disable memory and use config. cycles
13504 * only to access all registers. The 5702/03 chips
13505 * can mistakenly decode the special cycles from the
13506 * ICH chipsets as memory write cycles, causing corruption
13507 * of register and memory space. Only certain ICH bridges
13508 * will drive special cycles with non-zero data during the
13509 * address phase which can fall within the 5703's address
13510 * range. This is not an ICH bug as the PCI spec allows
13511 * non-zero address during special cycles. However, only
13512 * these ICH bridges are known to drive non-zero addresses
13513 * during special cycles.
13514 *
13515 * Since special cycles do not cross PCI bridges, we only
13516 * enable this workaround if the 5703 is on the secondary
13517 * bus of these ICH bridges.
13518 */
13519 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13520 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13521 static struct tg3_dev_id {
13522 u32 vendor;
13523 u32 device;
13524 u32 rev;
13525 } ich_chipsets[] = {
13526 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13527 PCI_ANY_ID },
13528 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13529 PCI_ANY_ID },
13530 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13531 0xa },
13532 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13533 PCI_ANY_ID },
13534 { },
13535 };
13536 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13537 struct pci_dev *bridge = NULL;
13538
13539 while (pci_id->vendor != 0) {
13540 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13541 bridge);
13542 if (!bridge) {
13543 pci_id++;
13544 continue;
13545 }
13546 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013547 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013548 continue;
13549 }
13550 if (bridge->subordinate &&
13551 (bridge->subordinate->number ==
13552 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013553 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013554 pci_dev_put(bridge);
13555 break;
13556 }
13557 }
13558 }
13559
Matt Carlson6ff6f812011-05-19 12:12:54 +000013560 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013561 static struct tg3_dev_id {
13562 u32 vendor;
13563 u32 device;
13564 } bridge_chipsets[] = {
13565 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13566 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13567 { },
13568 };
13569 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13570 struct pci_dev *bridge = NULL;
13571
13572 while (pci_id->vendor != 0) {
13573 bridge = pci_get_device(pci_id->vendor,
13574 pci_id->device,
13575 bridge);
13576 if (!bridge) {
13577 pci_id++;
13578 continue;
13579 }
13580 if (bridge->subordinate &&
13581 (bridge->subordinate->number <=
13582 tp->pdev->bus->number) &&
13583 (bridge->subordinate->subordinate >=
13584 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013585 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013586 pci_dev_put(bridge);
13587 break;
13588 }
13589 }
13590 }
13591
Michael Chan4a29cc22006-03-19 13:21:12 -080013592 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13593 * DMA addresses > 40-bit. This bridge may have other additional
13594 * 57xx devices behind it in some 4-port NIC designs for example.
13595 * Any tg3 device found behind the bridge will also need the 40-bit
13596 * DMA workaround.
13597 */
Michael Chana4e2b342005-10-26 15:46:52 -070013598 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13599 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013600 tg3_flag_set(tp, 5780_CLASS);
13601 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013602 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000013603 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013604 struct pci_dev *bridge = NULL;
13605
13606 do {
13607 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13608 PCI_DEVICE_ID_SERVERWORKS_EPB,
13609 bridge);
13610 if (bridge && bridge->subordinate &&
13611 (bridge->subordinate->number <=
13612 tp->pdev->bus->number) &&
13613 (bridge->subordinate->subordinate >=
13614 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013615 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013616 pci_dev_put(bridge);
13617 break;
13618 }
13619 } while (bridge);
13620 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013621
Linus Torvalds1da177e2005-04-16 15:20:36 -070013622 /* Initialize misc host control in PCI block. */
13623 tp->misc_host_ctrl |= (misc_ctrl_reg &
13624 MISC_HOST_CTRL_CHIPREV);
13625 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13626 tp->misc_host_ctrl);
13627
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013628 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13629 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013630 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13631 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013632 tp->pdev_peer = tg3_find_peer(tp);
13633
Matt Carlsonc885e822010-08-02 11:25:57 +000013634 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013635 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13636 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013637 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013638
13639 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013640 tg3_flag(tp, 5717_PLUS))
13641 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013642
Matt Carlson321d32a2008-11-21 17:22:19 -080013643 /* Intentionally exclude ASIC_REV_5906 */
13644 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080013645 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013646 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013647 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013648 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013649 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013650 tg3_flag(tp, 57765_PLUS))
13651 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013652
13653 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13654 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013655 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013656 tg3_flag(tp, 5755_PLUS) ||
13657 tg3_flag(tp, 5780_CLASS))
13658 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013659
Matt Carlson6ff6f812011-05-19 12:12:54 +000013660 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013661 tg3_flag(tp, 5750_PLUS))
13662 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013663
Matt Carlson507399f2009-11-13 13:03:37 +000013664 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013665 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013666 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013667 else if (tg3_flag(tp, 57765_PLUS))
13668 tg3_flag_set(tp, HW_TSO_3);
13669 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013670 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013671 tg3_flag_set(tp, HW_TSO_2);
13672 else if (tg3_flag(tp, 5750_PLUS)) {
13673 tg3_flag_set(tp, HW_TSO_1);
13674 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013675 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13676 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013677 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013678 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13679 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13680 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013681 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013682 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13683 tp->fw_needed = FIRMWARE_TG3TSO5;
13684 else
13685 tp->fw_needed = FIRMWARE_TG3TSO;
13686 }
13687
Matt Carlsondabc5c62011-05-19 12:12:52 +000013688 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013689 if (tg3_flag(tp, HW_TSO_1) ||
13690 tg3_flag(tp, HW_TSO_2) ||
13691 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsondabc5c62011-05-19 12:12:52 +000013692 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13693 tg3_flag_set(tp, TSO_CAPABLE);
13694 else {
13695 tg3_flag_clear(tp, TSO_CAPABLE);
13696 tg3_flag_clear(tp, TSO_BUG);
13697 tp->fw_needed = NULL;
13698 }
13699
13700 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
13701 tp->fw_needed = FIRMWARE_TG3;
13702
Matt Carlson507399f2009-11-13 13:03:37 +000013703 tp->irq_max = 1;
13704
Joe Perches63c3a662011-04-26 08:12:10 +000013705 if (tg3_flag(tp, 5750_PLUS)) {
13706 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013707 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13708 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13709 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13710 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13711 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000013712 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013713
Joe Perches63c3a662011-04-26 08:12:10 +000013714 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013715 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013716 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070013717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013718
Joe Perches63c3a662011-04-26 08:12:10 +000013719 if (tg3_flag(tp, 57765_PLUS)) {
13720 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000013721 tp->irq_max = TG3_IRQ_MAX_VECS;
13722 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013723 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013724
Matt Carlson2ffcc982011-05-19 12:12:44 +000013725 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000013726 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013727
Joe Perches63c3a662011-04-26 08:12:10 +000013728 if (tg3_flag(tp, 5717_PLUS))
13729 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000013730
Joe Perches63c3a662011-04-26 08:12:10 +000013731 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013732 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Joe Perches63c3a662011-04-26 08:12:10 +000013733 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000013734
Joe Perches63c3a662011-04-26 08:12:10 +000013735 if (!tg3_flag(tp, 5705_PLUS) ||
13736 tg3_flag(tp, 5780_CLASS) ||
13737 tg3_flag(tp, USE_JUMBO_BDFLAG))
13738 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070013739
Matt Carlson52f44902008-11-21 17:17:04 -080013740 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13741 &pci_state_reg);
13742
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013743 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13744 if (tp->pcie_cap != 0) {
13745 u16 lnkctl;
13746
Joe Perches63c3a662011-04-26 08:12:10 +000013747 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013748
Matt Carlsoncf790032010-11-24 08:31:48 +000013749 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013750 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13751 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013752 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013753
13754 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013755
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013756 pci_read_config_word(tp->pdev,
13757 tp->pcie_cap + PCI_EXP_LNKCTL,
13758 &lnkctl);
13759 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000013760 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13761 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013762 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000013763 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000013764 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013765 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013766 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013767 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13768 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000013769 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000013770 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013771 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080013772 }
Matt Carlson52f44902008-11-21 17:17:04 -080013773 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +000013774 tg3_flag_set(tp, PCI_EXPRESS);
13775 } else if (!tg3_flag(tp, 5705_PLUS) ||
13776 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080013777 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13778 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013779 dev_err(&tp->pdev->dev,
13780 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013781 return -EIO;
13782 }
13783
13784 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000013785 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080013786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013787
Michael Chan399de502005-10-03 14:02:39 -070013788 /* If we have an AMD 762 or VIA K8T800 chipset, write
13789 * reordering to the mailbox registers done by the host
13790 * controller can cause major troubles. We read back from
13791 * every mailbox register write to force the writes to be
13792 * posted to the chip in order.
13793 */
Matt Carlson41434702011-03-09 16:58:22 +000013794 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013795 !tg3_flag(tp, PCI_EXPRESS))
13796 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070013797
Matt Carlson69fc4052008-12-21 20:19:57 -080013798 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13799 &tp->pci_cacheline_sz);
13800 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13801 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013802 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13803 tp->pci_lat_timer < 64) {
13804 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013805 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13806 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013807 }
13808
Matt Carlson52f44902008-11-21 17:17:04 -080013809 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13810 /* 5700 BX chips need to have their TX producer index
13811 * mailboxes written twice to workaround a bug.
13812 */
Joe Perches63c3a662011-04-26 08:12:10 +000013813 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070013814
Matt Carlson52f44902008-11-21 17:17:04 -080013815 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013816 *
13817 * The workaround is to use indirect register accesses
13818 * for all chip writes not to mailbox registers.
13819 */
Joe Perches63c3a662011-04-26 08:12:10 +000013820 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013821 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013822
Joe Perches63c3a662011-04-26 08:12:10 +000013823 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013824
13825 /* The chip can have it's power management PCI config
13826 * space registers clobbered due to this bug.
13827 * So explicitly force the chip into D0 here.
13828 */
Matt Carlson9974a352007-10-07 23:27:28 -070013829 pci_read_config_dword(tp->pdev,
13830 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013831 &pm_reg);
13832 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13833 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013834 pci_write_config_dword(tp->pdev,
13835 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013836 pm_reg);
13837
13838 /* Also, force SERR#/PERR# in PCI command. */
13839 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13840 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13841 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13842 }
13843 }
13844
Linus Torvalds1da177e2005-04-16 15:20:36 -070013845 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013846 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013847 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013848 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013849
13850 /* Chip-specific fixup from Broadcom driver */
13851 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13852 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13853 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13854 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13855 }
13856
Michael Chan1ee582d2005-08-09 20:16:46 -070013857 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013858 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013859 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013860 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013861 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013862 tp->write32_tx_mbox = tg3_write32;
13863 tp->write32_rx_mbox = tg3_write32;
13864
13865 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000013866 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070013867 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013868 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013869 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070013870 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13871 /*
13872 * Back to back register writes can cause problems on these
13873 * chips, the workaround is to read back all reg writes
13874 * except those to mailbox regs.
13875 *
13876 * See tg3_write_indirect_reg32().
13877 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013878 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013879 }
13880
Joe Perches63c3a662011-04-26 08:12:10 +000013881 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070013882 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000013883 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070013884 tp->write32_rx_mbox = tg3_write_flush_reg32;
13885 }
Michael Chan20094932005-08-09 20:16:32 -070013886
Joe Perches63c3a662011-04-26 08:12:10 +000013887 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070013888 tp->read32 = tg3_read_indirect_reg32;
13889 tp->write32 = tg3_write_indirect_reg32;
13890 tp->read32_mbox = tg3_read_indirect_mbox;
13891 tp->write32_mbox = tg3_write_indirect_mbox;
13892 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13893 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13894
13895 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013896 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013897
13898 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13899 pci_cmd &= ~PCI_COMMAND_MEMORY;
13900 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13901 }
Michael Chanb5d37722006-09-27 16:06:21 -070013902 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13903 tp->read32_mbox = tg3_read32_mbox_5906;
13904 tp->write32_mbox = tg3_write32_mbox_5906;
13905 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13906 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13907 }
Michael Chan68929142005-08-09 20:17:14 -070013908
Michael Chanbbadf502006-04-06 21:46:34 -070013909 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013910 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070013911 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013912 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000013913 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070013914
Michael Chan7d0c41e2005-04-21 17:06:20 -070013915 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000013916 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013917 * determined before calling tg3_set_power_state() so that
13918 * we know whether or not to switch out of Vaux power.
13919 * When the flag is set, it means that GPIO1 is used for eeprom
13920 * write protect and also implies that it is a LOM where GPIOs
13921 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013922 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013923 tg3_get_eeprom_hw_cfg(tp);
13924
Joe Perches63c3a662011-04-26 08:12:10 +000013925 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070013926 /* Allow reads and writes to the
13927 * APE register and memory space.
13928 */
13929 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013930 PCISTATE_ALLOW_APE_SHMEM_WR |
13931 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013932 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13933 pci_state_reg);
13934 }
13935
Matt Carlson9936bcf2007-10-10 18:03:07 -070013936 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013937 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013938 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013939 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013940 tg3_flag(tp, 57765_PLUS))
13941 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013942
Matt Carlsonbea8a632011-04-25 12:42:49 +000013943 /* Set up tp->grc_local_ctrl before calling tg3_power_up().
Michael Chan314fba32005-04-21 17:07:04 -070013944 * GPIO1 driven high will bring 5700's external PHY out of reset.
13945 * It is also used as eeprom write protect on LOMs.
13946 */
13947 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013948 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013949 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070013950 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13951 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013952 /* Unused GPIO3 must be driven as output on 5752 because there
13953 * are no pull-up resistors on unused GPIO pins.
13954 */
13955 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13956 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013957
Matt Carlson321d32a2008-11-21 17:22:19 -080013958 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013959 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13960 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013961 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13962
Matt Carlson8d519ab2009-04-20 06:58:01 +000013963 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13964 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013965 /* Turn off the debug UART. */
13966 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000013967 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013968 /* Keep VMain power. */
13969 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13970 GRC_LCLCTRL_GPIO_OUTPUT0;
13971 }
13972
Linus Torvalds1da177e2005-04-16 15:20:36 -070013973 /* Force the chip into D0. */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013974 err = tg3_power_up(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013975 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013976 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013977 return err;
13978 }
13979
Linus Torvalds1da177e2005-04-16 15:20:36 -070013980 /* Derive initial jumbo mode from MTU assigned in
13981 * ether_setup() via the alloc_etherdev() call
13982 */
Joe Perches63c3a662011-04-26 08:12:10 +000013983 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
13984 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013985
13986 /* Determine WakeOnLan speed to use. */
13987 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13988 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13989 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13990 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000013991 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013992 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013993 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013994 }
13995
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013996 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013997 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013998
Linus Torvalds1da177e2005-04-16 15:20:36 -070013999 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014000 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14001 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014002 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014003 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014004 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14005 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14006 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014007
14008 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14009 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014010 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014011 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014012 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014013
Joe Perches63c3a662011-04-26 08:12:10 +000014014 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014015 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014016 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014017 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014018 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014019 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014020 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014021 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14022 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014023 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14024 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014025 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014026 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014027 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014028 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014029 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014031
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014032 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14033 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14034 tp->phy_otp = tg3_read_otp_phycfg(tp);
14035 if (tp->phy_otp == 0)
14036 tp->phy_otp = TG3_OTP_DEFAULT;
14037 }
14038
Joe Perches63c3a662011-04-26 08:12:10 +000014039 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014040 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14041 else
14042 tp->mi_mode = MAC_MI_MODE_BASE;
14043
Linus Torvalds1da177e2005-04-16 15:20:36 -070014044 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014045 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14046 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14047 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14048
Matt Carlson4d958472011-04-20 07:57:35 +000014049 /* Set these bits to enable statistics workaround. */
14050 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14051 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14052 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14053 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14054 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14055 }
14056
Matt Carlson321d32a2008-11-21 17:22:19 -080014057 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14058 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014059 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014060
Matt Carlson158d7ab2008-05-29 01:37:54 -070014061 err = tg3_mdio_init(tp);
14062 if (err)
14063 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014064
14065 /* Initialize data/descriptor byte/word swapping. */
14066 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014067 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14068 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14069 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14070 GRC_MODE_B2HRX_ENABLE |
14071 GRC_MODE_HTX2B_ENABLE |
14072 GRC_MODE_HOST_STACKUP);
14073 else
14074 val &= GRC_MODE_HOST_STACKUP;
14075
Linus Torvalds1da177e2005-04-16 15:20:36 -070014076 tw32(GRC_MODE, val | tp->grc_mode);
14077
14078 tg3_switch_clocks(tp);
14079
14080 /* Clear this out for sanity. */
14081 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14082
14083 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14084 &pci_state_reg);
14085 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014086 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014087 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14088
14089 if (chiprevid == CHIPREV_ID_5701_A0 ||
14090 chiprevid == CHIPREV_ID_5701_B0 ||
14091 chiprevid == CHIPREV_ID_5701_B2 ||
14092 chiprevid == CHIPREV_ID_5701_B5) {
14093 void __iomem *sram_base;
14094
14095 /* Write some dummy words into the SRAM status block
14096 * area, see if it reads back correctly. If the return
14097 * value is bad, force enable the PCIX workaround.
14098 */
14099 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14100
14101 writel(0x00000000, sram_base);
14102 writel(0x00000000, sram_base + 4);
14103 writel(0xffffffff, sram_base + 4);
14104 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014105 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014106 }
14107 }
14108
14109 udelay(50);
14110 tg3_nvram_init(tp);
14111
14112 grc_misc_cfg = tr32(GRC_MISC_CFG);
14113 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14114
Linus Torvalds1da177e2005-04-16 15:20:36 -070014115 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14116 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14117 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014118 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014119
Joe Perches63c3a662011-04-26 08:12:10 +000014120 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014121 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014122 tg3_flag_set(tp, TAGGED_STATUS);
14123 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014124 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14125 HOSTCC_MODE_CLRTICK_TXBD);
14126
14127 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14128 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14129 tp->misc_host_ctrl);
14130 }
14131
Matt Carlson3bda1252008-08-15 14:08:22 -070014132 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014133 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014134 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014135 else
14136 tp->mac_mode = TG3_DEF_MAC_MODE;
14137
Linus Torvalds1da177e2005-04-16 15:20:36 -070014138 /* these are limited to 10/100 only */
14139 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14140 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14141 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14142 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14143 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14144 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14145 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14146 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14147 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014148 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14149 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014150 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014151 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14152 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014153 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14154 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014155
14156 err = tg3_phy_probe(tp);
14157 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014158 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014159 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014160 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014161 }
14162
Matt Carlson184b8902010-04-05 10:19:25 +000014163 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014164 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014165
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014166 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14167 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014168 } else {
14169 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014170 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014171 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014172 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014173 }
14174
14175 /* 5700 {AX,BX} chips have a broken status block link
14176 * change bit implementation, so we must use the
14177 * status register in those cases.
14178 */
14179 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014180 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014181 else
Joe Perches63c3a662011-04-26 08:12:10 +000014182 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014183
14184 /* The led_ctrl is set during tg3_phy_probe, here we might
14185 * have to force the link status polling mechanism based
14186 * upon subsystem IDs.
14187 */
14188 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014189 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014190 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14191 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014192 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014193 }
14194
14195 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014196 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014197 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014198 else
Joe Perches63c3a662011-04-26 08:12:10 +000014199 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014200
Matt Carlsonbf933c82011-01-25 15:58:49 +000014201 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014202 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014203 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014204 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014205 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014206#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014207 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014208#endif
14209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014210
Matt Carlson2c49a442010-09-30 10:34:35 +000014211 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14212 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014213 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14214
Matt Carlson2c49a442010-09-30 10:34:35 +000014215 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014216
14217 /* Increment the rx prod index on the rx std ring by at most
14218 * 8 for these chips to workaround hw errata.
14219 */
14220 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14221 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14222 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14223 tp->rx_std_max_post = 8;
14224
Joe Perches63c3a662011-04-26 08:12:10 +000014225 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014226 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14227 PCIE_PWR_MGMT_L1_THRESH_MSK;
14228
Linus Torvalds1da177e2005-04-16 15:20:36 -070014229 return err;
14230}
14231
David S. Miller49b6e95f2007-03-29 01:38:42 -070014232#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014233static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14234{
14235 struct net_device *dev = tp->dev;
14236 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014237 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014238 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014239 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014240
David S. Miller49b6e95f2007-03-29 01:38:42 -070014241 addr = of_get_property(dp, "local-mac-address", &len);
14242 if (addr && len == 6) {
14243 memcpy(dev->dev_addr, addr, 6);
14244 memcpy(dev->perm_addr, dev->dev_addr, 6);
14245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014246 }
14247 return -ENODEV;
14248}
14249
14250static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14251{
14252 struct net_device *dev = tp->dev;
14253
14254 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014255 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014256 return 0;
14257}
14258#endif
14259
14260static int __devinit tg3_get_device_address(struct tg3 *tp)
14261{
14262 struct net_device *dev = tp->dev;
14263 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014264 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014265
David S. Miller49b6e95f2007-03-29 01:38:42 -070014266#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014267 if (!tg3_get_macaddr_sparc(tp))
14268 return 0;
14269#endif
14270
14271 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014272 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014273 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014274 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14275 mac_offset = 0xcc;
14276 if (tg3_nvram_lock(tp))
14277 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14278 else
14279 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014280 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlsona50d0792010-06-05 17:24:37 +000014281 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014282 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000014283 if (PCI_FUNC(tp->pdev->devfn) > 1)
14284 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014285 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014286 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014287
14288 /* First try to get it from MAC address mailbox. */
14289 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14290 if ((hi >> 16) == 0x484b) {
14291 dev->dev_addr[0] = (hi >> 8) & 0xff;
14292 dev->dev_addr[1] = (hi >> 0) & 0xff;
14293
14294 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14295 dev->dev_addr[2] = (lo >> 24) & 0xff;
14296 dev->dev_addr[3] = (lo >> 16) & 0xff;
14297 dev->dev_addr[4] = (lo >> 8) & 0xff;
14298 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014299
Michael Chan008652b2006-03-27 23:14:53 -080014300 /* Some old bootcode may report a 0 MAC address in SRAM */
14301 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14302 }
14303 if (!addr_ok) {
14304 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014305 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014306 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014307 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014308 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14309 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014310 }
14311 /* Finally just fetch it out of the MAC control regs. */
14312 else {
14313 hi = tr32(MAC_ADDR_0_HIGH);
14314 lo = tr32(MAC_ADDR_0_LOW);
14315
14316 dev->dev_addr[5] = lo & 0xff;
14317 dev->dev_addr[4] = (lo >> 8) & 0xff;
14318 dev->dev_addr[3] = (lo >> 16) & 0xff;
14319 dev->dev_addr[2] = (lo >> 24) & 0xff;
14320 dev->dev_addr[1] = hi & 0xff;
14321 dev->dev_addr[0] = (hi >> 8) & 0xff;
14322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014323 }
14324
14325 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014326#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014327 if (!tg3_get_default_macaddr_sparc(tp))
14328 return 0;
14329#endif
14330 return -EINVAL;
14331 }
John W. Linville2ff43692005-09-12 14:44:20 -070014332 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014333 return 0;
14334}
14335
David S. Miller59e6b432005-05-18 22:50:10 -070014336#define BOUNDARY_SINGLE_CACHELINE 1
14337#define BOUNDARY_MULTI_CACHELINE 2
14338
14339static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14340{
14341 int cacheline_size;
14342 u8 byte;
14343 int goal;
14344
14345 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14346 if (byte == 0)
14347 cacheline_size = 1024;
14348 else
14349 cacheline_size = (int) byte * 4;
14350
14351 /* On 5703 and later chips, the boundary bits have no
14352 * effect.
14353 */
14354 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14355 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014356 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014357 goto out;
14358
14359#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14360 goal = BOUNDARY_MULTI_CACHELINE;
14361#else
14362#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14363 goal = BOUNDARY_SINGLE_CACHELINE;
14364#else
14365 goal = 0;
14366#endif
14367#endif
14368
Joe Perches63c3a662011-04-26 08:12:10 +000014369 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014370 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14371 goto out;
14372 }
14373
David S. Miller59e6b432005-05-18 22:50:10 -070014374 if (!goal)
14375 goto out;
14376
14377 /* PCI controllers on most RISC systems tend to disconnect
14378 * when a device tries to burst across a cache-line boundary.
14379 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14380 *
14381 * Unfortunately, for PCI-E there are only limited
14382 * write-side controls for this, and thus for reads
14383 * we will still get the disconnects. We'll also waste
14384 * these PCI cycles for both read and write for chips
14385 * other than 5700 and 5701 which do not implement the
14386 * boundary bits.
14387 */
Joe Perches63c3a662011-04-26 08:12:10 +000014388 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014389 switch (cacheline_size) {
14390 case 16:
14391 case 32:
14392 case 64:
14393 case 128:
14394 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14395 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14396 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14397 } else {
14398 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14399 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14400 }
14401 break;
14402
14403 case 256:
14404 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14405 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14406 break;
14407
14408 default:
14409 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14410 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14411 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014412 }
Joe Perches63c3a662011-04-26 08:12:10 +000014413 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014414 switch (cacheline_size) {
14415 case 16:
14416 case 32:
14417 case 64:
14418 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14419 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14420 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14421 break;
14422 }
14423 /* fallthrough */
14424 case 128:
14425 default:
14426 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14427 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14428 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014429 }
David S. Miller59e6b432005-05-18 22:50:10 -070014430 } else {
14431 switch (cacheline_size) {
14432 case 16:
14433 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14434 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14435 DMA_RWCTRL_WRITE_BNDRY_16);
14436 break;
14437 }
14438 /* fallthrough */
14439 case 32:
14440 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14441 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14442 DMA_RWCTRL_WRITE_BNDRY_32);
14443 break;
14444 }
14445 /* fallthrough */
14446 case 64:
14447 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14448 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14449 DMA_RWCTRL_WRITE_BNDRY_64);
14450 break;
14451 }
14452 /* fallthrough */
14453 case 128:
14454 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14455 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14456 DMA_RWCTRL_WRITE_BNDRY_128);
14457 break;
14458 }
14459 /* fallthrough */
14460 case 256:
14461 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14462 DMA_RWCTRL_WRITE_BNDRY_256);
14463 break;
14464 case 512:
14465 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14466 DMA_RWCTRL_WRITE_BNDRY_512);
14467 break;
14468 case 1024:
14469 default:
14470 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14471 DMA_RWCTRL_WRITE_BNDRY_1024);
14472 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014473 }
David S. Miller59e6b432005-05-18 22:50:10 -070014474 }
14475
14476out:
14477 return val;
14478}
14479
Linus Torvalds1da177e2005-04-16 15:20:36 -070014480static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14481{
14482 struct tg3_internal_buffer_desc test_desc;
14483 u32 sram_dma_descs;
14484 int i, ret;
14485
14486 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14487
14488 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14489 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14490 tw32(RDMAC_STATUS, 0);
14491 tw32(WDMAC_STATUS, 0);
14492
14493 tw32(BUFMGR_MODE, 0);
14494 tw32(FTQ_RESET, 0);
14495
14496 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14497 test_desc.addr_lo = buf_dma & 0xffffffff;
14498 test_desc.nic_mbuf = 0x00002100;
14499 test_desc.len = size;
14500
14501 /*
14502 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14503 * the *second* time the tg3 driver was getting loaded after an
14504 * initial scan.
14505 *
14506 * Broadcom tells me:
14507 * ...the DMA engine is connected to the GRC block and a DMA
14508 * reset may affect the GRC block in some unpredictable way...
14509 * The behavior of resets to individual blocks has not been tested.
14510 *
14511 * Broadcom noted the GRC reset will also reset all sub-components.
14512 */
14513 if (to_device) {
14514 test_desc.cqid_sqid = (13 << 8) | 2;
14515
14516 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14517 udelay(40);
14518 } else {
14519 test_desc.cqid_sqid = (16 << 8) | 7;
14520
14521 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14522 udelay(40);
14523 }
14524 test_desc.flags = 0x00000005;
14525
14526 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14527 u32 val;
14528
14529 val = *(((u32 *)&test_desc) + i);
14530 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14531 sram_dma_descs + (i * sizeof(u32)));
14532 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14533 }
14534 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14535
Matt Carlson859a588792010-04-05 10:19:28 +000014536 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014537 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000014538 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014539 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014540
14541 ret = -ENODEV;
14542 for (i = 0; i < 40; i++) {
14543 u32 val;
14544
14545 if (to_device)
14546 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14547 else
14548 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14549 if ((val & 0xffff) == sram_dma_descs) {
14550 ret = 0;
14551 break;
14552 }
14553
14554 udelay(100);
14555 }
14556
14557 return ret;
14558}
14559
David S. Millerded73402005-05-23 13:59:47 -070014560#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014561
Matt Carlson41434702011-03-09 16:58:22 +000014562static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014563 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14564 { },
14565};
14566
Linus Torvalds1da177e2005-04-16 15:20:36 -070014567static int __devinit tg3_test_dma(struct tg3 *tp)
14568{
14569 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014570 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014571 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014572
Matt Carlson4bae65c2010-11-24 08:31:52 +000014573 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14574 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014575 if (!buf) {
14576 ret = -ENOMEM;
14577 goto out_nofree;
14578 }
14579
14580 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14581 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14582
David S. Miller59e6b432005-05-18 22:50:10 -070014583 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014584
Joe Perches63c3a662011-04-26 08:12:10 +000014585 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014586 goto out;
14587
Joe Perches63c3a662011-04-26 08:12:10 +000014588 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014589 /* DMA read watermark not used on PCIE */
14590 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014591 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014592 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14593 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014594 tp->dma_rwctrl |= 0x003f0000;
14595 else
14596 tp->dma_rwctrl |= 0x003f000f;
14597 } else {
14598 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14599 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14600 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014601 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014602
Michael Chan4a29cc22006-03-19 13:21:12 -080014603 /* If the 5704 is behind the EPB bridge, we can
14604 * do the less restrictive ONE_DMA workaround for
14605 * better performance.
14606 */
Joe Perches63c3a662011-04-26 08:12:10 +000014607 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014608 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14609 tp->dma_rwctrl |= 0x8000;
14610 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014611 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14612
Michael Chan49afdeb2007-02-13 12:17:03 -080014613 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14614 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014615 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014616 tp->dma_rwctrl |=
14617 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14618 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14619 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014620 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14621 /* 5780 always in PCIX mode */
14622 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014623 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14624 /* 5714 always in PCIX mode */
14625 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014626 } else {
14627 tp->dma_rwctrl |= 0x001b000f;
14628 }
14629 }
14630
14631 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14632 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14633 tp->dma_rwctrl &= 0xfffffff0;
14634
14635 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14636 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14637 /* Remove this if it causes problems for some boards. */
14638 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14639
14640 /* On 5700/5701 chips, we need to set this bit.
14641 * Otherwise the chip will issue cacheline transactions
14642 * to streamable DMA memory with not all the byte
14643 * enables turned on. This is an error on several
14644 * RISC PCI controllers, in particular sparc64.
14645 *
14646 * On 5703/5704 chips, this bit has been reassigned
14647 * a different meaning. In particular, it is used
14648 * on those chips to enable a PCI-X workaround.
14649 */
14650 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14651 }
14652
14653 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14654
14655#if 0
14656 /* Unneeded, already done by tg3_get_invariants. */
14657 tg3_switch_clocks(tp);
14658#endif
14659
Linus Torvalds1da177e2005-04-16 15:20:36 -070014660 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14661 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14662 goto out;
14663
David S. Miller59e6b432005-05-18 22:50:10 -070014664 /* It is best to perform DMA test with maximum write burst size
14665 * to expose the 5700/5701 write DMA bug.
14666 */
14667 saved_dma_rwctrl = tp->dma_rwctrl;
14668 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14669 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14670
Linus Torvalds1da177e2005-04-16 15:20:36 -070014671 while (1) {
14672 u32 *p = buf, i;
14673
14674 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14675 p[i] = i;
14676
14677 /* Send the buffer to the chip. */
14678 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14679 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014680 dev_err(&tp->pdev->dev,
14681 "%s: Buffer write failed. err = %d\n",
14682 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014683 break;
14684 }
14685
14686#if 0
14687 /* validate data reached card RAM correctly. */
14688 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14689 u32 val;
14690 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14691 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014692 dev_err(&tp->pdev->dev,
14693 "%s: Buffer corrupted on device! "
14694 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014695 /* ret = -ENODEV here? */
14696 }
14697 p[i] = 0;
14698 }
14699#endif
14700 /* Now read it back. */
14701 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14702 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014703 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14704 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014705 break;
14706 }
14707
14708 /* Verify it. */
14709 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14710 if (p[i] == i)
14711 continue;
14712
David S. Miller59e6b432005-05-18 22:50:10 -070014713 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14714 DMA_RWCTRL_WRITE_BNDRY_16) {
14715 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014716 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14717 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14718 break;
14719 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014720 dev_err(&tp->pdev->dev,
14721 "%s: Buffer corrupted on read back! "
14722 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014723 ret = -ENODEV;
14724 goto out;
14725 }
14726 }
14727
14728 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14729 /* Success. */
14730 ret = 0;
14731 break;
14732 }
14733 }
David S. Miller59e6b432005-05-18 22:50:10 -070014734 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14735 DMA_RWCTRL_WRITE_BNDRY_16) {
14736 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014737 * now look for chipsets that are known to expose the
14738 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014739 */
Matt Carlson41434702011-03-09 16:58:22 +000014740 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014741 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14742 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014743 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014744 /* Safe to use the calculated DMA boundary. */
14745 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014746 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014747
David S. Miller59e6b432005-05-18 22:50:10 -070014748 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014750
14751out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014752 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014753out_nofree:
14754 return ret;
14755}
14756
Linus Torvalds1da177e2005-04-16 15:20:36 -070014757static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14758{
Joe Perches63c3a662011-04-26 08:12:10 +000014759 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000014760 tp->bufmgr_config.mbuf_read_dma_low_water =
14761 DEFAULT_MB_RDMA_LOW_WATER_5705;
14762 tp->bufmgr_config.mbuf_mac_rx_low_water =
14763 DEFAULT_MB_MACRX_LOW_WATER_57765;
14764 tp->bufmgr_config.mbuf_high_water =
14765 DEFAULT_MB_HIGH_WATER_57765;
14766
14767 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14768 DEFAULT_MB_RDMA_LOW_WATER_5705;
14769 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14770 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14771 tp->bufmgr_config.mbuf_high_water_jumbo =
14772 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000014773 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014774 tp->bufmgr_config.mbuf_read_dma_low_water =
14775 DEFAULT_MB_RDMA_LOW_WATER_5705;
14776 tp->bufmgr_config.mbuf_mac_rx_low_water =
14777 DEFAULT_MB_MACRX_LOW_WATER_5705;
14778 tp->bufmgr_config.mbuf_high_water =
14779 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014780 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14781 tp->bufmgr_config.mbuf_mac_rx_low_water =
14782 DEFAULT_MB_MACRX_LOW_WATER_5906;
14783 tp->bufmgr_config.mbuf_high_water =
14784 DEFAULT_MB_HIGH_WATER_5906;
14785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014786
Michael Chanfdfec1722005-07-25 12:31:48 -070014787 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14788 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14789 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14790 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14791 tp->bufmgr_config.mbuf_high_water_jumbo =
14792 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14793 } else {
14794 tp->bufmgr_config.mbuf_read_dma_low_water =
14795 DEFAULT_MB_RDMA_LOW_WATER;
14796 tp->bufmgr_config.mbuf_mac_rx_low_water =
14797 DEFAULT_MB_MACRX_LOW_WATER;
14798 tp->bufmgr_config.mbuf_high_water =
14799 DEFAULT_MB_HIGH_WATER;
14800
14801 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14802 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14803 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14804 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14805 tp->bufmgr_config.mbuf_high_water_jumbo =
14806 DEFAULT_MB_HIGH_WATER_JUMBO;
14807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014808
14809 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14810 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14811}
14812
14813static char * __devinit tg3_phy_string(struct tg3 *tp)
14814{
Matt Carlson79eb6902010-02-17 15:17:03 +000014815 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14816 case TG3_PHY_ID_BCM5400: return "5400";
14817 case TG3_PHY_ID_BCM5401: return "5401";
14818 case TG3_PHY_ID_BCM5411: return "5411";
14819 case TG3_PHY_ID_BCM5701: return "5701";
14820 case TG3_PHY_ID_BCM5703: return "5703";
14821 case TG3_PHY_ID_BCM5704: return "5704";
14822 case TG3_PHY_ID_BCM5705: return "5705";
14823 case TG3_PHY_ID_BCM5750: return "5750";
14824 case TG3_PHY_ID_BCM5752: return "5752";
14825 case TG3_PHY_ID_BCM5714: return "5714";
14826 case TG3_PHY_ID_BCM5780: return "5780";
14827 case TG3_PHY_ID_BCM5755: return "5755";
14828 case TG3_PHY_ID_BCM5787: return "5787";
14829 case TG3_PHY_ID_BCM5784: return "5784";
14830 case TG3_PHY_ID_BCM5756: return "5722/5756";
14831 case TG3_PHY_ID_BCM5906: return "5906";
14832 case TG3_PHY_ID_BCM5761: return "5761";
14833 case TG3_PHY_ID_BCM5718C: return "5718C";
14834 case TG3_PHY_ID_BCM5718S: return "5718S";
14835 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014836 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000014837 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014838 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014839 case 0: return "serdes";
14840 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014842}
14843
Michael Chanf9804dd2005-09-27 12:13:10 -070014844static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14845{
Joe Perches63c3a662011-04-26 08:12:10 +000014846 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014847 strcpy(str, "PCI Express");
14848 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000014849 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014850 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14851
14852 strcpy(str, "PCIX:");
14853
14854 if ((clock_ctrl == 7) ||
14855 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14856 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14857 strcat(str, "133MHz");
14858 else if (clock_ctrl == 0)
14859 strcat(str, "33MHz");
14860 else if (clock_ctrl == 2)
14861 strcat(str, "50MHz");
14862 else if (clock_ctrl == 4)
14863 strcat(str, "66MHz");
14864 else if (clock_ctrl == 6)
14865 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014866 } else {
14867 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000014868 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070014869 strcat(str, "66MHz");
14870 else
14871 strcat(str, "33MHz");
14872 }
Joe Perches63c3a662011-04-26 08:12:10 +000014873 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070014874 strcat(str, ":32-bit");
14875 else
14876 strcat(str, ":64-bit");
14877 return str;
14878}
14879
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014880static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014881{
14882 struct pci_dev *peer;
14883 unsigned int func, devnr = tp->pdev->devfn & ~7;
14884
14885 for (func = 0; func < 8; func++) {
14886 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14887 if (peer && peer != tp->pdev)
14888 break;
14889 pci_dev_put(peer);
14890 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014891 /* 5704 can be configured in single-port mode, set peer to
14892 * tp->pdev in that case.
14893 */
14894 if (!peer) {
14895 peer = tp->pdev;
14896 return peer;
14897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014898
14899 /*
14900 * We don't need to keep the refcount elevated; there's no way
14901 * to remove one half of this device without removing the other
14902 */
14903 pci_dev_put(peer);
14904
14905 return peer;
14906}
14907
David S. Miller15f98502005-05-18 22:49:26 -070014908static void __devinit tg3_init_coal(struct tg3 *tp)
14909{
14910 struct ethtool_coalesce *ec = &tp->coal;
14911
14912 memset(ec, 0, sizeof(*ec));
14913 ec->cmd = ETHTOOL_GCOALESCE;
14914 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14915 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14916 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14917 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14918 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14919 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14920 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14921 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14922 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14923
14924 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14925 HOSTCC_MODE_CLRTICK_TXBD)) {
14926 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14927 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14928 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14929 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14930 }
Michael Chand244c892005-07-05 14:42:33 -070014931
Joe Perches63c3a662011-04-26 08:12:10 +000014932 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070014933 ec->rx_coalesce_usecs_irq = 0;
14934 ec->tx_coalesce_usecs_irq = 0;
14935 ec->stats_block_coalesce_usecs = 0;
14936 }
David S. Miller15f98502005-05-18 22:49:26 -070014937}
14938
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014939static const struct net_device_ops tg3_netdev_ops = {
14940 .ndo_open = tg3_open,
14941 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014942 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014943 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014944 .ndo_validate_addr = eth_validate_addr,
14945 .ndo_set_multicast_list = tg3_set_rx_mode,
14946 .ndo_set_mac_address = tg3_set_mac_addr,
14947 .ndo_do_ioctl = tg3_ioctl,
14948 .ndo_tx_timeout = tg3_tx_timeout,
14949 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000014950 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000014951 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080014952#ifdef CONFIG_NET_POLL_CONTROLLER
14953 .ndo_poll_controller = tg3_poll_controller,
14954#endif
14955};
14956
Linus Torvalds1da177e2005-04-16 15:20:36 -070014957static int __devinit tg3_init_one(struct pci_dev *pdev,
14958 const struct pci_device_id *ent)
14959{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014960 struct net_device *dev;
14961 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014962 int i, err, pm_cap;
14963 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014964 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014965 u64 dma_mask, persist_dma_mask;
Matt Carlson0da06062011-05-19 12:12:53 +000014966 u32 features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014967
Joe Perches05dbe002010-02-17 19:44:19 +000014968 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014969
14970 err = pci_enable_device(pdev);
14971 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014972 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014973 return err;
14974 }
14975
Linus Torvalds1da177e2005-04-16 15:20:36 -070014976 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14977 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014978 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014979 goto err_out_disable_pdev;
14980 }
14981
14982 pci_set_master(pdev);
14983
14984 /* Find power-management capability. */
14985 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14986 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014987 dev_err(&pdev->dev,
14988 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014989 err = -EIO;
14990 goto err_out_free_res;
14991 }
14992
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014993 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014994 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014995 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014996 err = -ENOMEM;
14997 goto err_out_free_res;
14998 }
14999
Linus Torvalds1da177e2005-04-16 15:20:36 -070015000 SET_NETDEV_DEV(dev, &pdev->dev);
15001
Linus Torvalds1da177e2005-04-16 15:20:36 -070015002 tp = netdev_priv(dev);
15003 tp->pdev = pdev;
15004 tp->dev = dev;
15005 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015006 tp->rx_mode = TG3_DEF_RX_MODE;
15007 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015008
Linus Torvalds1da177e2005-04-16 15:20:36 -070015009 if (tg3_debug > 0)
15010 tp->msg_enable = tg3_debug;
15011 else
15012 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15013
15014 /* The word/byte swap controls here control register access byte
15015 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15016 * setting below.
15017 */
15018 tp->misc_host_ctrl =
15019 MISC_HOST_CTRL_MASK_PCI_INT |
15020 MISC_HOST_CTRL_WORD_SWAP |
15021 MISC_HOST_CTRL_INDIR_ACCESS |
15022 MISC_HOST_CTRL_PCISTATE_RW;
15023
15024 /* The NONFRM (non-frame) byte/word swap controls take effect
15025 * on descriptor entries, anything which isn't packet data.
15026 *
15027 * The StrongARM chips on the board (one for tx, one for rx)
15028 * are running in big-endian mode.
15029 */
15030 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15031 GRC_MODE_WSWAP_NONFRM_DATA);
15032#ifdef __BIG_ENDIAN
15033 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15034#endif
15035 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015036 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015037 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015038
Matt Carlsond5fe4882008-11-21 17:20:32 -080015039 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015040 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015041 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015042 err = -ENOMEM;
15043 goto err_out_free_dev;
15044 }
15045
Linus Torvalds1da177e2005-04-16 15:20:36 -070015046 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15047 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015048
Linus Torvalds1da177e2005-04-16 15:20:36 -070015049 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015050 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015051 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015052 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015053
15054 err = tg3_get_invariants(tp);
15055 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015056 dev_err(&pdev->dev,
15057 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015058 goto err_out_iounmap;
15059 }
15060
Michael Chan4a29cc22006-03-19 13:21:12 -080015061 /* The EPB bridge inside 5714, 5715, and 5780 and any
15062 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015063 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15064 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15065 * do DMA address check in tg3_start_xmit().
15066 */
Joe Perches63c3a662011-04-26 08:12:10 +000015067 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015068 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015069 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015070 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015071#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015072 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015073#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015074 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015075 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015076
15077 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015078 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015079 err = pci_set_dma_mask(pdev, dma_mask);
15080 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015081 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015082 err = pci_set_consistent_dma_mask(pdev,
15083 persist_dma_mask);
15084 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015085 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15086 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015087 goto err_out_iounmap;
15088 }
15089 }
15090 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015091 if (err || dma_mask == DMA_BIT_MASK(32)) {
15092 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015093 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015094 dev_err(&pdev->dev,
15095 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015096 goto err_out_iounmap;
15097 }
15098 }
15099
Michael Chanfdfec1722005-07-25 12:31:48 -070015100 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015101
Matt Carlson0da06062011-05-19 12:12:53 +000015102 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15103
15104 /* 5700 B0 chips do not support checksumming correctly due
15105 * to hardware bugs.
15106 */
15107 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15108 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15109
15110 if (tg3_flag(tp, 5755_PLUS))
15111 features |= NETIF_F_IPV6_CSUM;
15112 }
15113
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015114 /* TSO is on by default on chips that support hardware TSO.
15115 * Firmware TSO on older chips gives lower performance, so it
15116 * is off by default, but can be enabled using ethtool.
15117 */
Joe Perches63c3a662011-04-26 08:12:10 +000015118 if ((tg3_flag(tp, HW_TSO_1) ||
15119 tg3_flag(tp, HW_TSO_2) ||
15120 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015121 (features & NETIF_F_IP_CSUM))
15122 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015123 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015124 if (features & NETIF_F_IPV6_CSUM)
15125 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015126 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015127 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015128 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15129 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015130 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015131 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015132 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015134
Matt Carlsond542fe22011-05-19 16:02:43 +000015135 dev->features |= features;
15136 dev->vlan_features |= features;
15137
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015138 /*
15139 * Add loopback capability only for a subset of devices that support
15140 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15141 * loopback for the remaining devices.
15142 */
15143 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15144 !tg3_flag(tp, CPMU_PRESENT))
15145 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015146 features |= NETIF_F_LOOPBACK;
15147
Matt Carlson0da06062011-05-19 12:12:53 +000015148 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015149
Linus Torvalds1da177e2005-04-16 15:20:36 -070015150 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015151 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015152 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015153 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015154 tp->rx_pending = 63;
15155 }
15156
Linus Torvalds1da177e2005-04-16 15:20:36 -070015157 err = tg3_get_device_address(tp);
15158 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015159 dev_err(&pdev->dev,
15160 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000015161 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015162 }
15163
Joe Perches63c3a662011-04-26 08:12:10 +000015164 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson63532392008-11-03 16:49:57 -080015165 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080015166 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015167 dev_err(&pdev->dev,
15168 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015169 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000015170 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015171 }
15172
15173 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000015174
Joe Perches63c3a662011-04-26 08:12:10 +000015175 if (tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000015176 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015177 }
15178
Matt Carlsonc88864d2007-11-12 21:07:01 -080015179 /*
15180 * Reset chip in case UNDI or EFI driver did not shutdown
15181 * DMA self test will enable WDMAC and we'll see (spurious)
15182 * pending DMA on the PCI bus at that point.
15183 */
15184 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15185 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15186 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15187 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15188 }
15189
15190 err = tg3_test_dma(tp);
15191 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015192 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015193 goto err_out_apeunmap;
15194 }
15195
Matt Carlson78f90dc2009-11-13 13:03:42 +000015196 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15197 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15198 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015199 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015200 struct tg3_napi *tnapi = &tp->napi[i];
15201
15202 tnapi->tp = tp;
15203 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15204
15205 tnapi->int_mbox = intmbx;
15206 if (i < 4)
15207 intmbx += 0x8;
15208 else
15209 intmbx += 0x4;
15210
15211 tnapi->consmbox = rcvmbx;
15212 tnapi->prodmbox = sndmbx;
15213
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015214 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015215 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015216 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015217 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015218
Joe Perches63c3a662011-04-26 08:12:10 +000015219 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015220 break;
15221
15222 /*
15223 * If we support MSIX, we'll be using RSS. If we're using
15224 * RSS, the first vector only handles link interrupts and the
15225 * remaining vectors handle rx and tx interrupts. Reuse the
15226 * mailbox values for the next iteration. The values we setup
15227 * above are still useful for the single vectored mode.
15228 */
15229 if (!i)
15230 continue;
15231
15232 rcvmbx += 0x8;
15233
15234 if (sndmbx & 0x4)
15235 sndmbx -= 0x4;
15236 else
15237 sndmbx += 0xc;
15238 }
15239
Matt Carlsonc88864d2007-11-12 21:07:01 -080015240 tg3_init_coal(tp);
15241
Michael Chanc49a1562006-12-17 17:07:29 -080015242 pci_set_drvdata(pdev, dev);
15243
Linus Torvalds1da177e2005-04-16 15:20:36 -070015244 err = register_netdev(dev);
15245 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015246 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015247 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015248 }
15249
Joe Perches05dbe002010-02-17 19:44:19 +000015250 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15251 tp->board_part_number,
15252 tp->pci_chip_rev_id,
15253 tg3_bus_string(tp, str),
15254 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015255
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015256 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015257 struct phy_device *phydev;
15258 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015259 netdev_info(dev,
15260 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015261 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015262 } else {
15263 char *ethtype;
15264
15265 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15266 ethtype = "10/100Base-TX";
15267 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15268 ethtype = "1000Base-SX";
15269 else
15270 ethtype = "10/100/1000Base-T";
15271
Matt Carlson5129c3a2010-04-05 10:19:23 +000015272 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015273 "(WireSpeed[%d], EEE[%d])\n",
15274 tg3_phy_string(tp), ethtype,
15275 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15276 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015277 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015278
Joe Perches05dbe002010-02-17 19:44:19 +000015279 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015280 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015281 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015282 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015283 tg3_flag(tp, ENABLE_ASF) != 0,
15284 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015285 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15286 tp->dma_rwctrl,
15287 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15288 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015289
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015290 pci_save_state(pdev);
15291
Linus Torvalds1da177e2005-04-16 15:20:36 -070015292 return 0;
15293
Matt Carlson0d3031d2007-10-10 18:02:43 -070015294err_out_apeunmap:
15295 if (tp->aperegs) {
15296 iounmap(tp->aperegs);
15297 tp->aperegs = NULL;
15298 }
15299
Linus Torvalds1da177e2005-04-16 15:20:36 -070015300err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015301 if (tp->regs) {
15302 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015303 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015305
15306err_out_free_dev:
15307 free_netdev(dev);
15308
15309err_out_free_res:
15310 pci_release_regions(pdev);
15311
15312err_out_disable_pdev:
15313 pci_disable_device(pdev);
15314 pci_set_drvdata(pdev, NULL);
15315 return err;
15316}
15317
15318static void __devexit tg3_remove_one(struct pci_dev *pdev)
15319{
15320 struct net_device *dev = pci_get_drvdata(pdev);
15321
15322 if (dev) {
15323 struct tg3 *tp = netdev_priv(dev);
15324
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015325 if (tp->fw)
15326 release_firmware(tp->fw);
15327
Tejun Heo23f333a2010-12-12 16:45:14 +010015328 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015329
Joe Perches63c3a662011-04-26 08:12:10 +000015330 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015331 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015332 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015333 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015334
Linus Torvalds1da177e2005-04-16 15:20:36 -070015335 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015336 if (tp->aperegs) {
15337 iounmap(tp->aperegs);
15338 tp->aperegs = NULL;
15339 }
Michael Chan68929142005-08-09 20:17:14 -070015340 if (tp->regs) {
15341 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015342 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015344 free_netdev(dev);
15345 pci_release_regions(pdev);
15346 pci_disable_device(pdev);
15347 pci_set_drvdata(pdev, NULL);
15348 }
15349}
15350
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015351#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015352static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015353{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015354 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015355 struct net_device *dev = pci_get_drvdata(pdev);
15356 struct tg3 *tp = netdev_priv(dev);
15357 int err;
15358
15359 if (!netif_running(dev))
15360 return 0;
15361
Tejun Heo23f333a2010-12-12 16:45:14 +010015362 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015363 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015364 tg3_netif_stop(tp);
15365
15366 del_timer_sync(&tp->timer);
15367
David S. Millerf47c11e2005-06-24 20:18:35 -070015368 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015369 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015370 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015371
15372 netif_device_detach(dev);
15373
David S. Millerf47c11e2005-06-24 20:18:35 -070015374 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015375 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015376 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015377 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015378
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015379 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015380 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015381 int err2;
15382
David S. Millerf47c11e2005-06-24 20:18:35 -070015383 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015384
Joe Perches63c3a662011-04-26 08:12:10 +000015385 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015386 err2 = tg3_restart_hw(tp, 1);
15387 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015389
15390 tp->timer.expires = jiffies + tp->timer_offset;
15391 add_timer(&tp->timer);
15392
15393 netif_device_attach(dev);
15394 tg3_netif_start(tp);
15395
Michael Chanb9ec6c12006-07-25 16:37:27 -070015396out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015397 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015398
15399 if (!err2)
15400 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015401 }
15402
15403 return err;
15404}
15405
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015406static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015407{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015408 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015409 struct net_device *dev = pci_get_drvdata(pdev);
15410 struct tg3 *tp = netdev_priv(dev);
15411 int err;
15412
15413 if (!netif_running(dev))
15414 return 0;
15415
Linus Torvalds1da177e2005-04-16 15:20:36 -070015416 netif_device_attach(dev);
15417
David S. Millerf47c11e2005-06-24 20:18:35 -070015418 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015419
Joe Perches63c3a662011-04-26 08:12:10 +000015420 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015421 err = tg3_restart_hw(tp, 1);
15422 if (err)
15423 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015424
15425 tp->timer.expires = jiffies + tp->timer_offset;
15426 add_timer(&tp->timer);
15427
Linus Torvalds1da177e2005-04-16 15:20:36 -070015428 tg3_netif_start(tp);
15429
Michael Chanb9ec6c12006-07-25 16:37:27 -070015430out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015431 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015432
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015433 if (!err)
15434 tg3_phy_start(tp);
15435
Michael Chanb9ec6c12006-07-25 16:37:27 -070015436 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015437}
15438
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015439static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015440#define TG3_PM_OPS (&tg3_pm_ops)
15441
15442#else
15443
15444#define TG3_PM_OPS NULL
15445
15446#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015447
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015448/**
15449 * tg3_io_error_detected - called when PCI error is detected
15450 * @pdev: Pointer to PCI device
15451 * @state: The current pci connection state
15452 *
15453 * This function is called after a PCI bus error affecting
15454 * this device has been detected.
15455 */
15456static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15457 pci_channel_state_t state)
15458{
15459 struct net_device *netdev = pci_get_drvdata(pdev);
15460 struct tg3 *tp = netdev_priv(netdev);
15461 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15462
15463 netdev_info(netdev, "PCI I/O error detected\n");
15464
15465 rtnl_lock();
15466
15467 if (!netif_running(netdev))
15468 goto done;
15469
15470 tg3_phy_stop(tp);
15471
15472 tg3_netif_stop(tp);
15473
15474 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015475 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015476
15477 /* Want to make sure that the reset task doesn't run */
15478 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015479 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15480 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015481
15482 netif_device_detach(netdev);
15483
15484 /* Clean up software state, even if MMIO is blocked */
15485 tg3_full_lock(tp, 0);
15486 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15487 tg3_full_unlock(tp);
15488
15489done:
15490 if (state == pci_channel_io_perm_failure)
15491 err = PCI_ERS_RESULT_DISCONNECT;
15492 else
15493 pci_disable_device(pdev);
15494
15495 rtnl_unlock();
15496
15497 return err;
15498}
15499
15500/**
15501 * tg3_io_slot_reset - called after the pci bus has been reset.
15502 * @pdev: Pointer to PCI device
15503 *
15504 * Restart the card from scratch, as if from a cold-boot.
15505 * At this point, the card has exprienced a hard reset,
15506 * followed by fixups by BIOS, and has its config space
15507 * set up identically to what it was at cold boot.
15508 */
15509static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15510{
15511 struct net_device *netdev = pci_get_drvdata(pdev);
15512 struct tg3 *tp = netdev_priv(netdev);
15513 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15514 int err;
15515
15516 rtnl_lock();
15517
15518 if (pci_enable_device(pdev)) {
15519 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15520 goto done;
15521 }
15522
15523 pci_set_master(pdev);
15524 pci_restore_state(pdev);
15525 pci_save_state(pdev);
15526
15527 if (!netif_running(netdev)) {
15528 rc = PCI_ERS_RESULT_RECOVERED;
15529 goto done;
15530 }
15531
15532 err = tg3_power_up(tp);
15533 if (err) {
15534 netdev_err(netdev, "Failed to restore register access.\n");
15535 goto done;
15536 }
15537
15538 rc = PCI_ERS_RESULT_RECOVERED;
15539
15540done:
15541 rtnl_unlock();
15542
15543 return rc;
15544}
15545
15546/**
15547 * tg3_io_resume - called when traffic can start flowing again.
15548 * @pdev: Pointer to PCI device
15549 *
15550 * This callback is called when the error recovery driver tells
15551 * us that its OK to resume normal operation.
15552 */
15553static void tg3_io_resume(struct pci_dev *pdev)
15554{
15555 struct net_device *netdev = pci_get_drvdata(pdev);
15556 struct tg3 *tp = netdev_priv(netdev);
15557 int err;
15558
15559 rtnl_lock();
15560
15561 if (!netif_running(netdev))
15562 goto done;
15563
15564 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015565 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015566 err = tg3_restart_hw(tp, 1);
15567 tg3_full_unlock(tp);
15568 if (err) {
15569 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15570 goto done;
15571 }
15572
15573 netif_device_attach(netdev);
15574
15575 tp->timer.expires = jiffies + tp->timer_offset;
15576 add_timer(&tp->timer);
15577
15578 tg3_netif_start(tp);
15579
15580 tg3_phy_start(tp);
15581
15582done:
15583 rtnl_unlock();
15584}
15585
15586static struct pci_error_handlers tg3_err_handler = {
15587 .error_detected = tg3_io_error_detected,
15588 .slot_reset = tg3_io_slot_reset,
15589 .resume = tg3_io_resume
15590};
15591
Linus Torvalds1da177e2005-04-16 15:20:36 -070015592static struct pci_driver tg3_driver = {
15593 .name = DRV_MODULE_NAME,
15594 .id_table = tg3_pci_tbl,
15595 .probe = tg3_init_one,
15596 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015597 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015598 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015599};
15600
15601static int __init tg3_init(void)
15602{
Jeff Garzik29917622006-08-19 17:48:59 -040015603 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015604}
15605
15606static void __exit tg3_cleanup(void)
15607{
15608 pci_unregister_driver(&tg3_driver);
15609}
15610
15611module_init(tg3_init);
15612module_exit(tg3_cleanup);