blob: b93ba3d2192a72d23054942adfe47c236f227c7e [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
Matt Carlson520b2752011-06-13 13:39:02 +0000110#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* length of time before we decide the hardware is borked,
113 * and dev->tx_timeout() should be called to fix the problem
114 */
Joe Perches63c3a662011-04-26 08:12:10 +0000115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define TG3_TX_TIMEOUT (5 * HZ)
117
118/* hardware minimum and maximum for a single frame's data payload */
119#define TG3_MIN_MTU 60
120#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000121 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/* These numbers seem to be hard coded in the NIC firmware somehow.
124 * You can't change the ring sizes, but you can change where you place
125 * them in the NIC onboard memory.
126 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000127#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000128 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000129 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000131#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000132 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000133 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000135#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/* Do not place this n-ring entries value into the tp struct itself,
138 * we really want to expose these constants to GCC so that modulo et
139 * al. operations are done with shifts and masks instead of with
140 * hw multiply/modulo instructions. Another solution would be to
141 * replace things like '% foo' with '& (foo - 1)'.
142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144#define TG3_TX_RING_SIZE 512
145#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
146
Matt Carlson2c49a442010-09-30 10:34:35 +0000147#define TG3_RX_STD_RING_BYTES(tp) \
148 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
149#define TG3_RX_JMB_RING_BYTES(tp) \
150 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
151#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000152 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
154 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
156
Matt Carlson287be122009-08-28 13:58:46 +0000157#define TG3_DMA_BYTE_ENAB 64
158
159#define TG3_RX_STD_DMA_SZ 1536
160#define TG3_RX_JMB_DMA_SZ 9046
161
162#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
163
164#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
165#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Matt Carlson2c49a442010-09-30 10:34:35 +0000167#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
168 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000169
Matt Carlson2c49a442010-09-30 10:34:35 +0000170#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
171 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000172
Matt Carlsond2757fc2010-04-12 06:58:27 +0000173/* Due to a hardware bug, the 5701 can only DMA to memory addresses
174 * that are at least dword aligned when used in PCIX mode. The driver
175 * works around this bug by double copying the packet. This workaround
176 * is built into the normal double copy length check for efficiency.
177 *
178 * However, the double copy is only necessary on those architectures
179 * where unaligned memory accesses are inefficient. For those architectures
180 * where unaligned memory accesses incur little penalty, we can reintegrate
181 * the 5701 in the normal rx path. Doing so saves a device structure
182 * dereference by hardcoding the double copy threshold in place.
183 */
184#define TG3_RX_COPY_THRESHOLD 256
185#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
186 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
187#else
188 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
189#endif
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000192#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Matt Carlsonad829262008-11-21 17:16:16 -0800194#define TG3_RAW_IP_ALIGN 2
195
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000196#define TG3_FW_UPDATE_TIMEOUT_SEC 5
197
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800198#define FIRMWARE_TG3 "tigon/tg3.bin"
199#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
200#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000203 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
206MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
207MODULE_LICENSE("GPL");
208MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800209MODULE_FIRMWARE(FIRMWARE_TG3);
210MODULE_FIRMWARE(FIRMWARE_TG3TSO);
211MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
214module_param(tg3_debug, int, 0);
215MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
216
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000217static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700291 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
292 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
293 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
294 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
295 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
296 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
297 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000298 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700299 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
302MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
303
Andreas Mohr50da8592006-08-14 23:54:30 -0700304static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000306} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 { "rx_octets" },
308 { "rx_fragments" },
309 { "rx_ucast_packets" },
310 { "rx_mcast_packets" },
311 { "rx_bcast_packets" },
312 { "rx_fcs_errors" },
313 { "rx_align_errors" },
314 { "rx_xon_pause_rcvd" },
315 { "rx_xoff_pause_rcvd" },
316 { "rx_mac_ctrl_rcvd" },
317 { "rx_xoff_entered" },
318 { "rx_frame_too_long_errors" },
319 { "rx_jabbers" },
320 { "rx_undersize_packets" },
321 { "rx_in_length_errors" },
322 { "rx_out_length_errors" },
323 { "rx_64_or_less_octet_packets" },
324 { "rx_65_to_127_octet_packets" },
325 { "rx_128_to_255_octet_packets" },
326 { "rx_256_to_511_octet_packets" },
327 { "rx_512_to_1023_octet_packets" },
328 { "rx_1024_to_1522_octet_packets" },
329 { "rx_1523_to_2047_octet_packets" },
330 { "rx_2048_to_4095_octet_packets" },
331 { "rx_4096_to_8191_octet_packets" },
332 { "rx_8192_to_9022_octet_packets" },
333
334 { "tx_octets" },
335 { "tx_collisions" },
336
337 { "tx_xon_sent" },
338 { "tx_xoff_sent" },
339 { "tx_flow_control" },
340 { "tx_mac_errors" },
341 { "tx_single_collisions" },
342 { "tx_mult_collisions" },
343 { "tx_deferred" },
344 { "tx_excessive_collisions" },
345 { "tx_late_collisions" },
346 { "tx_collide_2times" },
347 { "tx_collide_3times" },
348 { "tx_collide_4times" },
349 { "tx_collide_5times" },
350 { "tx_collide_6times" },
351 { "tx_collide_7times" },
352 { "tx_collide_8times" },
353 { "tx_collide_9times" },
354 { "tx_collide_10times" },
355 { "tx_collide_11times" },
356 { "tx_collide_12times" },
357 { "tx_collide_13times" },
358 { "tx_collide_14times" },
359 { "tx_collide_15times" },
360 { "tx_ucast_packets" },
361 { "tx_mcast_packets" },
362 { "tx_bcast_packets" },
363 { "tx_carrier_sense_errors" },
364 { "tx_discards" },
365 { "tx_errors" },
366
367 { "dma_writeq_full" },
368 { "dma_write_prioq_full" },
369 { "rxbds_empty" },
370 { "rx_discards" },
371 { "rx_errors" },
372 { "rx_threshold_hit" },
373
374 { "dma_readq_full" },
375 { "dma_read_prioq_full" },
376 { "tx_comp_queue_full" },
377
378 { "ring_set_send_prod_index" },
379 { "ring_status_update" },
380 { "nic_irqs" },
381 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000382 { "nic_tx_threshold_hit" },
383
384 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385};
386
Matt Carlson48fa55a2011-04-13 11:05:06 +0000387#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
388
389
Andreas Mohr50da8592006-08-14 23:54:30 -0700390static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700391 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000392} ethtool_test_keys[] = {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700393 { "nvram test (online) " },
394 { "link test (online) " },
395 { "register test (offline)" },
396 { "memory test (offline)" },
397 { "loopback test (offline)" },
398 { "interrupt test (offline)" },
399};
400
Matt Carlson48fa55a2011-04-13 11:05:06 +0000401#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
402
403
Michael Chanb401e9e2005-12-19 16:27:04 -0800404static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
405{
406 writel(val, tp->regs + off);
407}
408
409static u32 tg3_read32(struct tg3 *tp, u32 off)
410{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000411 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800412}
413
Matt Carlson0d3031d2007-10-10 18:02:43 -0700414static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
415{
416 writel(val, tp->aperegs + off);
417}
418
419static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
420{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000421 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
425{
Michael Chan68929142005-08-09 20:17:14 -0700426 unsigned long flags;
427
428 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700429 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
430 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700431 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700432}
433
434static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
435{
436 writel(val, tp->regs + off);
437 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Michael Chan68929142005-08-09 20:17:14 -0700440static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
441{
442 unsigned long flags;
443 u32 val;
444
445 spin_lock_irqsave(&tp->indirect_lock, flags);
446 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
447 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
448 spin_unlock_irqrestore(&tp->indirect_lock, flags);
449 return val;
450}
451
452static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
453{
454 unsigned long flags;
455
456 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
457 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
458 TG3_64BIT_REG_LOW, val);
459 return;
460 }
Matt Carlson66711e62009-11-13 13:03:49 +0000461 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700462 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
463 TG3_64BIT_REG_LOW, val);
464 return;
465 }
466
467 spin_lock_irqsave(&tp->indirect_lock, flags);
468 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
469 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
470 spin_unlock_irqrestore(&tp->indirect_lock, flags);
471
472 /* In indirect mode when disabling interrupts, we also need
473 * to clear the interrupt bit in the GRC local ctrl register.
474 */
475 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
476 (val == 0x1)) {
477 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
478 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
479 }
480}
481
482static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
483{
484 unsigned long flags;
485 u32 val;
486
487 spin_lock_irqsave(&tp->indirect_lock, flags);
488 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
489 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
490 spin_unlock_irqrestore(&tp->indirect_lock, flags);
491 return val;
492}
493
Michael Chanb401e9e2005-12-19 16:27:04 -0800494/* usec_wait specifies the wait time in usec when writing to certain registers
495 * where it is unsafe to read back the register without some delay.
496 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
497 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
498 */
499static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Joe Perches63c3a662011-04-26 08:12:10 +0000501 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800502 /* Non-posted methods */
503 tp->write32(tp, off, val);
504 else {
505 /* Posted method */
506 tg3_write32(tp, off, val);
507 if (usec_wait)
508 udelay(usec_wait);
509 tp->read32(tp, off);
510 }
511 /* Wait again after the read for the posted method to guarantee that
512 * the wait time is met.
513 */
514 if (usec_wait)
515 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
Michael Chan09ee9292005-08-09 20:17:00 -0700518static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
519{
520 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000521 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700522 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700523}
524
Michael Chan20094932005-08-09 20:16:32 -0700525static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 void __iomem *mbox = tp->regs + off;
528 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000529 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000531 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 readl(mbox);
533}
534
Michael Chanb5d37722006-09-27 16:06:21 -0700535static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
536{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000537 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700538}
539
540static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
541{
542 writel(val, tp->regs + off + GRCMBOX_BASE);
543}
544
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000545#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700546#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000547#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
548#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
549#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700550
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000551#define tw32(reg, val) tp->write32(tp, reg, val)
552#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
553#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
554#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
557{
Michael Chan68929142005-08-09 20:17:14 -0700558 unsigned long flags;
559
Matt Carlson6ff6f812011-05-19 12:12:54 +0000560 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700561 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
562 return;
563
Michael Chan68929142005-08-09 20:17:14 -0700564 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000565 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700566 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
567 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Michael Chanbbadf502006-04-06 21:46:34 -0700569 /* Always leave this as zero. */
570 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
571 } else {
572 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
573 tw32_f(TG3PCI_MEM_WIN_DATA, val);
574
575 /* Always leave this as zero. */
576 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
577 }
Michael Chan68929142005-08-09 20:17:14 -0700578 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
582{
Michael Chan68929142005-08-09 20:17:14 -0700583 unsigned long flags;
584
Matt Carlson6ff6f812011-05-19 12:12:54 +0000585 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700586 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
587 *val = 0;
588 return;
589 }
590
Michael Chan68929142005-08-09 20:17:14 -0700591 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000592 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700593 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
594 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Michael Chanbbadf502006-04-06 21:46:34 -0700596 /* Always leave this as zero. */
597 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
598 } else {
599 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
600 *val = tr32(TG3PCI_MEM_WIN_DATA);
601
602 /* Always leave this as zero. */
603 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
604 }
Michael Chan68929142005-08-09 20:17:14 -0700605 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Matt Carlson0d3031d2007-10-10 18:02:43 -0700608static void tg3_ape_lock_init(struct tg3 *tp)
609{
610 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000611 u32 regbase, bit;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000612
613 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
614 regbase = TG3_APE_LOCK_GRANT;
615 else
616 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700617
618 /* Make sure the driver hasn't any stale locks. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000619 for (i = 0; i < 8; i++) {
620 if (i == TG3_APE_LOCK_GPIO)
621 continue;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000622 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000623 }
624
625 /* Clear the correct bit of the GPIO lock too. */
626 if (!tp->pci_fn)
627 bit = APE_LOCK_GRANT_DRIVER;
628 else
629 bit = 1 << tp->pci_fn;
630
631 tg3_ape_write32(tp, regbase + 4 * TG3_APE_LOCK_GPIO, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700632}
633
634static int tg3_ape_lock(struct tg3 *tp, int locknum)
635{
636 int i, off;
637 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000638 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700639
Joe Perches63c3a662011-04-26 08:12:10 +0000640 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700641 return 0;
642
643 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000644 case TG3_APE_LOCK_GPIO:
645 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
646 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000647 case TG3_APE_LOCK_GRC:
648 case TG3_APE_LOCK_MEM:
649 break;
650 default:
651 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700652 }
653
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000654 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
655 req = TG3_APE_LOCK_REQ;
656 gnt = TG3_APE_LOCK_GRANT;
657 } else {
658 req = TG3_APE_PER_LOCK_REQ;
659 gnt = TG3_APE_PER_LOCK_GRANT;
660 }
661
Matt Carlson0d3031d2007-10-10 18:02:43 -0700662 off = 4 * locknum;
663
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000664 if (locknum != TG3_APE_LOCK_GPIO || !tp->pci_fn)
665 bit = APE_LOCK_REQ_DRIVER;
666 else
667 bit = 1 << tp->pci_fn;
668
669 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700670
671 /* Wait for up to 1 millisecond to acquire lock. */
672 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000673 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000674 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700675 break;
676 udelay(10);
677 }
678
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000679 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700680 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000681 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700682 ret = -EBUSY;
683 }
684
685 return ret;
686}
687
688static void tg3_ape_unlock(struct tg3 *tp, int locknum)
689{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000690 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700691
Joe Perches63c3a662011-04-26 08:12:10 +0000692 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700693 return;
694
695 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000696 case TG3_APE_LOCK_GPIO:
697 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
698 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000699 case TG3_APE_LOCK_GRC:
700 case TG3_APE_LOCK_MEM:
701 break;
702 default:
703 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700704 }
705
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000706 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
707 gnt = TG3_APE_LOCK_GRANT;
708 else
709 gnt = TG3_APE_PER_LOCK_GRANT;
710
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000711 if (locknum != TG3_APE_LOCK_GPIO || !tp->pci_fn)
712 bit = APE_LOCK_GRANT_DRIVER;
713 else
714 bit = 1 << tp->pci_fn;
715
716 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719static void tg3_disable_ints(struct tg3 *tp)
720{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000721 int i;
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 tw32(TG3PCI_MISC_HOST_CTRL,
724 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000725 for (i = 0; i < tp->irq_max; i++)
726 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729static void tg3_enable_ints(struct tg3 *tp)
730{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000731 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000732
Michael Chanbbe832c2005-06-24 20:20:04 -0700733 tp->irq_sync = 0;
734 wmb();
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 tw32(TG3PCI_MISC_HOST_CTRL,
737 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000738
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000739 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000740 for (i = 0; i < tp->irq_cnt; i++) {
741 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000742
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000743 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000744 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000745 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
746
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000747 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000748 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000749
750 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000751 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000752 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
753 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
754 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000755 tw32(HOSTCC_MODE, tp->coal_now);
756
757 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Matt Carlson17375d22009-08-28 14:02:18 +0000760static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700761{
Matt Carlson17375d22009-08-28 14:02:18 +0000762 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000763 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700764 unsigned int work_exists = 0;
765
766 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000767 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700768 if (sblk->status & SD_STATUS_LINK_CHG)
769 work_exists = 1;
770 }
771 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000772 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000773 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700774 work_exists = 1;
775
776 return work_exists;
777}
778
Matt Carlson17375d22009-08-28 14:02:18 +0000779/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700780 * similar to tg3_enable_ints, but it accurately determines whether there
781 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400782 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 */
Matt Carlson17375d22009-08-28 14:02:18 +0000784static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Matt Carlson17375d22009-08-28 14:02:18 +0000786 struct tg3 *tp = tnapi->tp;
787
Matt Carlson898a56f2009-08-28 14:02:40 +0000788 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 mmiowb();
790
David S. Millerfac9b832005-05-18 22:46:34 -0700791 /* When doing tagged status, this work check is unnecessary.
792 * The last_tag we write above tells the chip which piece of
793 * work we've completed.
794 */
Joe Perches63c3a662011-04-26 08:12:10 +0000795 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700796 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000797 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800static void tg3_switch_clocks(struct tg3 *tp)
801{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000802 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 u32 orig_clock_ctrl;
804
Joe Perches63c3a662011-04-26 08:12:10 +0000805 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700806 return;
807
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000808 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 orig_clock_ctrl = clock_ctrl;
811 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
812 CLOCK_CTRL_CLKRUN_OENABLE |
813 0x1f);
814 tp->pci_clock_ctrl = clock_ctrl;
815
Joe Perches63c3a662011-04-26 08:12:10 +0000816 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800818 tw32_wait_f(TG3PCI_CLOCK_CTRL,
819 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800822 tw32_wait_f(TG3PCI_CLOCK_CTRL,
823 clock_ctrl |
824 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
825 40);
826 tw32_wait_f(TG3PCI_CLOCK_CTRL,
827 clock_ctrl | (CLOCK_CTRL_ALTCLK),
828 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800830 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833#define PHY_BUSY_LOOPS 5000
834
835static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
836{
837 u32 frame_val;
838 unsigned int loops;
839 int ret;
840
841 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
842 tw32_f(MAC_MI_MODE,
843 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
844 udelay(80);
845 }
846
847 *val = 0x0;
848
Matt Carlson882e9792009-09-01 13:21:36 +0000849 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 MI_COM_PHY_ADDR_MASK);
851 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
852 MI_COM_REG_ADDR_MASK);
853 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 tw32_f(MAC_MI_COM, frame_val);
856
857 loops = PHY_BUSY_LOOPS;
858 while (loops != 0) {
859 udelay(10);
860 frame_val = tr32(MAC_MI_COM);
861
862 if ((frame_val & MI_COM_BUSY) == 0) {
863 udelay(5);
864 frame_val = tr32(MAC_MI_COM);
865 break;
866 }
867 loops -= 1;
868 }
869
870 ret = -EBUSY;
871 if (loops != 0) {
872 *val = frame_val & MI_COM_DATA_MASK;
873 ret = 0;
874 }
875
876 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
877 tw32_f(MAC_MI_MODE, tp->mi_mode);
878 udelay(80);
879 }
880
881 return ret;
882}
883
884static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
885{
886 u32 frame_val;
887 unsigned int loops;
888 int ret;
889
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000890 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +0000891 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -0700892 return 0;
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
895 tw32_f(MAC_MI_MODE,
896 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
897 udelay(80);
898 }
899
Matt Carlson882e9792009-09-01 13:21:36 +0000900 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 MI_COM_PHY_ADDR_MASK);
902 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
903 MI_COM_REG_ADDR_MASK);
904 frame_val |= (val & MI_COM_DATA_MASK);
905 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 tw32_f(MAC_MI_COM, frame_val);
908
909 loops = PHY_BUSY_LOOPS;
910 while (loops != 0) {
911 udelay(10);
912 frame_val = tr32(MAC_MI_COM);
913 if ((frame_val & MI_COM_BUSY) == 0) {
914 udelay(5);
915 frame_val = tr32(MAC_MI_COM);
916 break;
917 }
918 loops -= 1;
919 }
920
921 ret = -EBUSY;
922 if (loops != 0)
923 ret = 0;
924
925 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
926 tw32_f(MAC_MI_MODE, tp->mi_mode);
927 udelay(80);
928 }
929
930 return ret;
931}
932
Matt Carlsonb0988c12011-04-20 07:57:39 +0000933static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
934{
935 int err;
936
937 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
938 if (err)
939 goto done;
940
941 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
942 if (err)
943 goto done;
944
945 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
946 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
947 if (err)
948 goto done;
949
950 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
951
952done:
953 return err;
954}
955
956static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
957{
958 int err;
959
960 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
961 if (err)
962 goto done;
963
964 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
965 if (err)
966 goto done;
967
968 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
969 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
970 if (err)
971 goto done;
972
973 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
974
975done:
976 return err;
977}
978
979static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
980{
981 int err;
982
983 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
984 if (!err)
985 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
986
987 return err;
988}
989
990static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
991{
992 int err;
993
994 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
995 if (!err)
996 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
997
998 return err;
999}
1000
Matt Carlson15ee95c2011-04-20 07:57:40 +00001001static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1002{
1003 int err;
1004
1005 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1006 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1007 MII_TG3_AUXCTL_SHDWSEL_MISC);
1008 if (!err)
1009 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1010
1011 return err;
1012}
1013
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001014static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1015{
1016 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1017 set |= MII_TG3_AUXCTL_MISC_WREN;
1018
1019 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1020}
1021
Matt Carlson1d36ba42011-04-20 07:57:42 +00001022#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1023 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1024 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1025 MII_TG3_AUXCTL_ACTL_TX_6DB)
1026
1027#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1028 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1029 MII_TG3_AUXCTL_ACTL_TX_6DB);
1030
Matt Carlson95e28692008-05-25 23:44:14 -07001031static int tg3_bmcr_reset(struct tg3 *tp)
1032{
1033 u32 phy_control;
1034 int limit, err;
1035
1036 /* OK, reset it, and poll the BMCR_RESET bit until it
1037 * clears or we time out.
1038 */
1039 phy_control = BMCR_RESET;
1040 err = tg3_writephy(tp, MII_BMCR, phy_control);
1041 if (err != 0)
1042 return -EBUSY;
1043
1044 limit = 5000;
1045 while (limit--) {
1046 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1047 if (err != 0)
1048 return -EBUSY;
1049
1050 if ((phy_control & BMCR_RESET) == 0) {
1051 udelay(40);
1052 break;
1053 }
1054 udelay(10);
1055 }
Roel Kluind4675b52009-02-12 16:33:27 -08001056 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001057 return -EBUSY;
1058
1059 return 0;
1060}
1061
Matt Carlson158d7ab2008-05-29 01:37:54 -07001062static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1063{
Francois Romieu3d165432009-01-19 16:56:50 -08001064 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001065 u32 val;
1066
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001067 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001068
1069 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001070 val = -EIO;
1071
1072 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001073
1074 return val;
1075}
1076
1077static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1078{
Francois Romieu3d165432009-01-19 16:56:50 -08001079 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001080 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001081
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001082 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001083
1084 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001085 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001086
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001087 spin_unlock_bh(&tp->lock);
1088
1089 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001090}
1091
1092static int tg3_mdio_reset(struct mii_bus *bp)
1093{
1094 return 0;
1095}
1096
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001097static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001098{
1099 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001100 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001101
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001102 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001103 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001104 case PHY_ID_BCM50610:
1105 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001106 val = MAC_PHYCFG2_50610_LED_MODES;
1107 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001108 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001109 val = MAC_PHYCFG2_AC131_LED_MODES;
1110 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001111 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001112 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1113 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001114 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001115 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1116 break;
1117 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001118 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001119 }
1120
1121 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1122 tw32(MAC_PHYCFG2, val);
1123
1124 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001125 val &= ~(MAC_PHYCFG1_RGMII_INT |
1126 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1127 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001128 tw32(MAC_PHYCFG1, val);
1129
1130 return;
1131 }
1132
Joe Perches63c3a662011-04-26 08:12:10 +00001133 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001134 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1135 MAC_PHYCFG2_FMODE_MASK_MASK |
1136 MAC_PHYCFG2_GMODE_MASK_MASK |
1137 MAC_PHYCFG2_ACT_MASK_MASK |
1138 MAC_PHYCFG2_QUAL_MASK_MASK |
1139 MAC_PHYCFG2_INBAND_ENABLE;
1140
1141 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001142
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001143 val = tr32(MAC_PHYCFG1);
1144 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1145 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001146 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1147 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001148 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001149 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001150 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1151 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001152 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1153 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1154 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001155
Matt Carlsona9daf362008-05-25 23:49:44 -07001156 val = tr32(MAC_EXT_RGMII_MODE);
1157 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1158 MAC_RGMII_MODE_RX_QUALITY |
1159 MAC_RGMII_MODE_RX_ACTIVITY |
1160 MAC_RGMII_MODE_RX_ENG_DET |
1161 MAC_RGMII_MODE_TX_ENABLE |
1162 MAC_RGMII_MODE_TX_LOWPWR |
1163 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001164 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1165 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001166 val |= MAC_RGMII_MODE_RX_INT_B |
1167 MAC_RGMII_MODE_RX_QUALITY |
1168 MAC_RGMII_MODE_RX_ACTIVITY |
1169 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001170 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001171 val |= MAC_RGMII_MODE_TX_ENABLE |
1172 MAC_RGMII_MODE_TX_LOWPWR |
1173 MAC_RGMII_MODE_TX_RESET;
1174 }
1175 tw32(MAC_EXT_RGMII_MODE, val);
1176}
1177
Matt Carlson158d7ab2008-05-29 01:37:54 -07001178static void tg3_mdio_start(struct tg3 *tp)
1179{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001180 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1181 tw32_f(MAC_MI_MODE, tp->mi_mode);
1182 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001183
Joe Perches63c3a662011-04-26 08:12:10 +00001184 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001185 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1186 tg3_mdio_config_5785(tp);
1187}
1188
1189static int tg3_mdio_init(struct tg3 *tp)
1190{
1191 int i;
1192 u32 reg;
1193 struct phy_device *phydev;
1194
Joe Perches63c3a662011-04-26 08:12:10 +00001195 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001196 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001197
Matt Carlson69f11c92011-07-13 09:27:30 +00001198 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001199
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001200 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1201 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1202 else
1203 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1204 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001205 if (is_serdes)
1206 tp->phy_addr += 7;
1207 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001208 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001209
Matt Carlson158d7ab2008-05-29 01:37:54 -07001210 tg3_mdio_start(tp);
1211
Joe Perches63c3a662011-04-26 08:12:10 +00001212 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001213 return 0;
1214
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001215 tp->mdio_bus = mdiobus_alloc();
1216 if (tp->mdio_bus == NULL)
1217 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001218
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001219 tp->mdio_bus->name = "tg3 mdio bus";
1220 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001221 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001222 tp->mdio_bus->priv = tp;
1223 tp->mdio_bus->parent = &tp->pdev->dev;
1224 tp->mdio_bus->read = &tg3_mdio_read;
1225 tp->mdio_bus->write = &tg3_mdio_write;
1226 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001227 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001228 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001229
1230 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001231 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001232
1233 /* The bus registration will look for all the PHYs on the mdio bus.
1234 * Unfortunately, it does not ensure the PHY is powered up before
1235 * accessing the PHY ID registers. A chip reset is the
1236 * quickest way to bring the device back to an operational state..
1237 */
1238 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1239 tg3_bmcr_reset(tp);
1240
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001241 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001242 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001243 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001244 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001245 return i;
1246 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001247
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001248 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001249
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001250 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001251 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001252 mdiobus_unregister(tp->mdio_bus);
1253 mdiobus_free(tp->mdio_bus);
1254 return -ENODEV;
1255 }
1256
1257 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001258 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001259 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001260 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001261 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001262 case PHY_ID_BCM50610:
1263 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001264 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001265 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001266 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001267 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001268 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001269 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001270 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001271 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001272 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001273 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001274 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001275 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001276 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001277 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001278 case PHY_ID_RTL8201E:
1279 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001280 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e092009-11-02 14:31:11 +00001281 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001282 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001283 break;
1284 }
1285
Joe Perches63c3a662011-04-26 08:12:10 +00001286 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001287
1288 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1289 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001290
1291 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001292}
1293
1294static void tg3_mdio_fini(struct tg3 *tp)
1295{
Joe Perches63c3a662011-04-26 08:12:10 +00001296 if (tg3_flag(tp, MDIOBUS_INITED)) {
1297 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001298 mdiobus_unregister(tp->mdio_bus);
1299 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001300 }
1301}
1302
Matt Carlson95e28692008-05-25 23:44:14 -07001303/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001304static inline void tg3_generate_fw_event(struct tg3 *tp)
1305{
1306 u32 val;
1307
1308 val = tr32(GRC_RX_CPU_EVENT);
1309 val |= GRC_RX_CPU_DRIVER_EVENT;
1310 tw32_f(GRC_RX_CPU_EVENT, val);
1311
1312 tp->last_event_jiffies = jiffies;
1313}
1314
1315#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1316
1317/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001318static void tg3_wait_for_event_ack(struct tg3 *tp)
1319{
1320 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001321 unsigned int delay_cnt;
1322 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001323
Matt Carlson4ba526c2008-08-15 14:10:04 -07001324 /* If enough time has passed, no wait is necessary. */
1325 time_remain = (long)(tp->last_event_jiffies + 1 +
1326 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1327 (long)jiffies;
1328 if (time_remain < 0)
1329 return;
1330
1331 /* Check if we can shorten the wait time. */
1332 delay_cnt = jiffies_to_usecs(time_remain);
1333 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1334 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1335 delay_cnt = (delay_cnt >> 3) + 1;
1336
1337 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001338 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1339 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001340 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001341 }
1342}
1343
1344/* tp->lock is held. */
1345static void tg3_ump_link_report(struct tg3 *tp)
1346{
1347 u32 reg;
1348 u32 val;
1349
Joe Perches63c3a662011-04-26 08:12:10 +00001350 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001351 return;
1352
1353 tg3_wait_for_event_ack(tp);
1354
1355 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1356
1357 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1358
1359 val = 0;
1360 if (!tg3_readphy(tp, MII_BMCR, &reg))
1361 val = reg << 16;
1362 if (!tg3_readphy(tp, MII_BMSR, &reg))
1363 val |= (reg & 0xffff);
1364 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1365
1366 val = 0;
1367 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1368 val = reg << 16;
1369 if (!tg3_readphy(tp, MII_LPA, &reg))
1370 val |= (reg & 0xffff);
1371 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1372
1373 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001374 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001375 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1376 val = reg << 16;
1377 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1378 val |= (reg & 0xffff);
1379 }
1380 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1381
1382 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1383 val = reg << 16;
1384 else
1385 val = 0;
1386 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1387
Matt Carlson4ba526c2008-08-15 14:10:04 -07001388 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001389}
1390
1391static void tg3_link_report(struct tg3 *tp)
1392{
1393 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001394 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001395 tg3_ump_link_report(tp);
1396 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001397 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1398 (tp->link_config.active_speed == SPEED_1000 ?
1399 1000 :
1400 (tp->link_config.active_speed == SPEED_100 ?
1401 100 : 10)),
1402 (tp->link_config.active_duplex == DUPLEX_FULL ?
1403 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001404
Joe Perches05dbe002010-02-17 19:44:19 +00001405 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1406 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1407 "on" : "off",
1408 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1409 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001410
1411 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1412 netdev_info(tp->dev, "EEE is %s\n",
1413 tp->setlpicnt ? "enabled" : "disabled");
1414
Matt Carlson95e28692008-05-25 23:44:14 -07001415 tg3_ump_link_report(tp);
1416 }
1417}
1418
1419static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1420{
1421 u16 miireg;
1422
Steve Glendinninge18ce342008-12-16 02:00:00 -08001423 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001424 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001425 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001426 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001427 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001428 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1429 else
1430 miireg = 0;
1431
1432 return miireg;
1433}
1434
1435static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1436{
1437 u16 miireg;
1438
Steve Glendinninge18ce342008-12-16 02:00:00 -08001439 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001440 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001441 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001442 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001443 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001444 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1445 else
1446 miireg = 0;
1447
1448 return miireg;
1449}
1450
Matt Carlson95e28692008-05-25 23:44:14 -07001451static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1452{
1453 u8 cap = 0;
1454
1455 if (lcladv & ADVERTISE_1000XPAUSE) {
1456 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1457 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001458 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001459 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001460 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001461 } else {
1462 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001463 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001464 }
1465 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1466 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001467 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001468 }
1469
1470 return cap;
1471}
1472
Matt Carlsonf51f3562008-05-25 23:45:08 -07001473static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001474{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001475 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001476 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001477 u32 old_rx_mode = tp->rx_mode;
1478 u32 old_tx_mode = tp->tx_mode;
1479
Joe Perches63c3a662011-04-26 08:12:10 +00001480 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001481 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001482 else
1483 autoneg = tp->link_config.autoneg;
1484
Joe Perches63c3a662011-04-26 08:12:10 +00001485 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001486 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001487 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001488 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001489 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001490 } else
1491 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001492
Matt Carlsonf51f3562008-05-25 23:45:08 -07001493 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001494
Steve Glendinninge18ce342008-12-16 02:00:00 -08001495 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001496 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1497 else
1498 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1499
Matt Carlsonf51f3562008-05-25 23:45:08 -07001500 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001501 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001502
Steve Glendinninge18ce342008-12-16 02:00:00 -08001503 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001504 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1505 else
1506 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1507
Matt Carlsonf51f3562008-05-25 23:45:08 -07001508 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001509 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001510}
1511
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001512static void tg3_adjust_link(struct net_device *dev)
1513{
1514 u8 oldflowctrl, linkmesg = 0;
1515 u32 mac_mode, lcl_adv, rmt_adv;
1516 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001517 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001518
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001519 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001520
1521 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1522 MAC_MODE_HALF_DUPLEX);
1523
1524 oldflowctrl = tp->link_config.active_flowctrl;
1525
1526 if (phydev->link) {
1527 lcl_adv = 0;
1528 rmt_adv = 0;
1529
1530 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1531 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001532 else if (phydev->speed == SPEED_1000 ||
1533 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001534 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001535 else
1536 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001537
1538 if (phydev->duplex == DUPLEX_HALF)
1539 mac_mode |= MAC_MODE_HALF_DUPLEX;
1540 else {
1541 lcl_adv = tg3_advert_flowctrl_1000T(
1542 tp->link_config.flowctrl);
1543
1544 if (phydev->pause)
1545 rmt_adv = LPA_PAUSE_CAP;
1546 if (phydev->asym_pause)
1547 rmt_adv |= LPA_PAUSE_ASYM;
1548 }
1549
1550 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1551 } else
1552 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1553
1554 if (mac_mode != tp->mac_mode) {
1555 tp->mac_mode = mac_mode;
1556 tw32_f(MAC_MODE, tp->mac_mode);
1557 udelay(40);
1558 }
1559
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001560 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1561 if (phydev->speed == SPEED_10)
1562 tw32(MAC_MI_STAT,
1563 MAC_MI_STAT_10MBPS_MODE |
1564 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1565 else
1566 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1567 }
1568
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001569 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1570 tw32(MAC_TX_LENGTHS,
1571 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1572 (6 << TX_LENGTHS_IPG_SHIFT) |
1573 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1574 else
1575 tw32(MAC_TX_LENGTHS,
1576 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1577 (6 << TX_LENGTHS_IPG_SHIFT) |
1578 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1579
1580 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1581 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1582 phydev->speed != tp->link_config.active_speed ||
1583 phydev->duplex != tp->link_config.active_duplex ||
1584 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001585 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001586
1587 tp->link_config.active_speed = phydev->speed;
1588 tp->link_config.active_duplex = phydev->duplex;
1589
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001590 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001591
1592 if (linkmesg)
1593 tg3_link_report(tp);
1594}
1595
1596static int tg3_phy_init(struct tg3 *tp)
1597{
1598 struct phy_device *phydev;
1599
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001600 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001601 return 0;
1602
1603 /* Bring the PHY back to a known state. */
1604 tg3_bmcr_reset(tp);
1605
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001606 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001607
1608 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001609 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001610 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001611 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001612 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001613 return PTR_ERR(phydev);
1614 }
1615
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001616 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001617 switch (phydev->interface) {
1618 case PHY_INTERFACE_MODE_GMII:
1619 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001620 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001621 phydev->supported &= (PHY_GBIT_FEATURES |
1622 SUPPORTED_Pause |
1623 SUPPORTED_Asym_Pause);
1624 break;
1625 }
1626 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001627 case PHY_INTERFACE_MODE_MII:
1628 phydev->supported &= (PHY_BASIC_FEATURES |
1629 SUPPORTED_Pause |
1630 SUPPORTED_Asym_Pause);
1631 break;
1632 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001633 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001634 return -EINVAL;
1635 }
1636
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001637 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001638
1639 phydev->advertising = phydev->supported;
1640
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001641 return 0;
1642}
1643
1644static void tg3_phy_start(struct tg3 *tp)
1645{
1646 struct phy_device *phydev;
1647
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001648 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001649 return;
1650
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001651 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001652
Matt Carlson80096062010-08-02 11:26:06 +00001653 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1654 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001655 phydev->speed = tp->link_config.orig_speed;
1656 phydev->duplex = tp->link_config.orig_duplex;
1657 phydev->autoneg = tp->link_config.orig_autoneg;
1658 phydev->advertising = tp->link_config.orig_advertising;
1659 }
1660
1661 phy_start(phydev);
1662
1663 phy_start_aneg(phydev);
1664}
1665
1666static void tg3_phy_stop(struct tg3 *tp)
1667{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001668 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001669 return;
1670
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001671 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001672}
1673
1674static void tg3_phy_fini(struct tg3 *tp)
1675{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001676 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001677 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001678 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001679 }
1680}
1681
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001682static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1683{
1684 u32 phytest;
1685
1686 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1687 u32 phy;
1688
1689 tg3_writephy(tp, MII_TG3_FET_TEST,
1690 phytest | MII_TG3_FET_SHADOW_EN);
1691 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1692 if (enable)
1693 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1694 else
1695 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1696 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1697 }
1698 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1699 }
1700}
1701
Matt Carlson6833c042008-11-21 17:18:59 -08001702static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1703{
1704 u32 reg;
1705
Joe Perches63c3a662011-04-26 08:12:10 +00001706 if (!tg3_flag(tp, 5705_PLUS) ||
1707 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001708 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001709 return;
1710
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001711 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001712 tg3_phy_fet_toggle_apd(tp, enable);
1713 return;
1714 }
1715
Matt Carlson6833c042008-11-21 17:18:59 -08001716 reg = MII_TG3_MISC_SHDW_WREN |
1717 MII_TG3_MISC_SHDW_SCR5_SEL |
1718 MII_TG3_MISC_SHDW_SCR5_LPED |
1719 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1720 MII_TG3_MISC_SHDW_SCR5_SDTL |
1721 MII_TG3_MISC_SHDW_SCR5_C125OE;
1722 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1723 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1724
1725 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1726
1727
1728 reg = MII_TG3_MISC_SHDW_WREN |
1729 MII_TG3_MISC_SHDW_APD_SEL |
1730 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1731 if (enable)
1732 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1733
1734 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1735}
1736
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001737static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1738{
1739 u32 phy;
1740
Joe Perches63c3a662011-04-26 08:12:10 +00001741 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001742 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001743 return;
1744
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001745 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001746 u32 ephy;
1747
Matt Carlson535ef6e2009-08-25 10:09:36 +00001748 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1749 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1750
1751 tg3_writephy(tp, MII_TG3_FET_TEST,
1752 ephy | MII_TG3_FET_SHADOW_EN);
1753 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001754 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001755 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001756 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001757 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1758 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001759 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001760 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001761 }
1762 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00001763 int ret;
1764
1765 ret = tg3_phy_auxctl_read(tp,
1766 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
1767 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001768 if (enable)
1769 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1770 else
1771 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001772 tg3_phy_auxctl_write(tp,
1773 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001774 }
1775 }
1776}
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778static void tg3_phy_set_wirespeed(struct tg3 *tp)
1779{
Matt Carlson15ee95c2011-04-20 07:57:40 +00001780 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 u32 val;
1782
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001783 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return;
1785
Matt Carlson15ee95c2011-04-20 07:57:40 +00001786 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
1787 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001788 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
1789 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
1791
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001792static void tg3_phy_apply_otp(struct tg3 *tp)
1793{
1794 u32 otp, phy;
1795
1796 if (!tp->phy_otp)
1797 return;
1798
1799 otp = tp->phy_otp;
1800
Matt Carlson1d36ba42011-04-20 07:57:42 +00001801 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
1802 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001803
1804 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1805 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1806 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1807
1808 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1809 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1810 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1811
1812 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1813 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1814 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1815
1816 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1817 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1818
1819 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1820 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1821
1822 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1823 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1824 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1825
Matt Carlson1d36ba42011-04-20 07:57:42 +00001826 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001827}
1828
Matt Carlson52b02d02010-10-14 10:37:41 +00001829static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1830{
1831 u32 val;
1832
1833 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1834 return;
1835
1836 tp->setlpicnt = 0;
1837
1838 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1839 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001840 tp->link_config.active_duplex == DUPLEX_FULL &&
1841 (tp->link_config.active_speed == SPEED_100 ||
1842 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001843 u32 eeectl;
1844
1845 if (tp->link_config.active_speed == SPEED_1000)
1846 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1847 else
1848 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1849
1850 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1851
Matt Carlson3110f5f52010-12-06 08:28:50 +00001852 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1853 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001854
Matt Carlsonb0c59432011-05-19 12:12:48 +00001855 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
1856 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00001857 tp->setlpicnt = 2;
1858 }
1859
1860 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00001861 if (current_link_up == 1 &&
1862 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
1863 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
1864 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1865 }
1866
Matt Carlson52b02d02010-10-14 10:37:41 +00001867 val = tr32(TG3_CPMU_EEE_MODE);
1868 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1869 }
1870}
1871
Matt Carlsonb0c59432011-05-19 12:12:48 +00001872static void tg3_phy_eee_enable(struct tg3 *tp)
1873{
1874 u32 val;
1875
1876 if (tp->link_config.active_speed == SPEED_1000 &&
1877 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1878 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
1879 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
1880 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00001881 val = MII_TG3_DSP_TAP26_ALNOKO |
1882 MII_TG3_DSP_TAP26_RMRXSTO;
1883 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00001884 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1885 }
1886
1887 val = tr32(TG3_CPMU_EEE_MODE);
1888 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
1889}
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891static int tg3_wait_macro_done(struct tg3 *tp)
1892{
1893 int limit = 100;
1894
1895 while (limit--) {
1896 u32 tmp32;
1897
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001898 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if ((tmp32 & 0x1000) == 0)
1900 break;
1901 }
1902 }
Roel Kluind4675b52009-02-12 16:33:27 -08001903 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return -EBUSY;
1905
1906 return 0;
1907}
1908
1909static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1910{
1911 static const u32 test_pat[4][6] = {
1912 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1913 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1914 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1915 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1916 };
1917 int chan;
1918
1919 for (chan = 0; chan < 4; chan++) {
1920 int i;
1921
1922 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1923 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001924 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 for (i = 0; i < 6; i++)
1927 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1928 test_pat[chan][i]);
1929
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001930 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (tg3_wait_macro_done(tp)) {
1932 *resetp = 1;
1933 return -EBUSY;
1934 }
1935
1936 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1937 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001938 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (tg3_wait_macro_done(tp)) {
1940 *resetp = 1;
1941 return -EBUSY;
1942 }
1943
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001944 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (tg3_wait_macro_done(tp)) {
1946 *resetp = 1;
1947 return -EBUSY;
1948 }
1949
1950 for (i = 0; i < 6; i += 2) {
1951 u32 low, high;
1952
1953 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1954 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1955 tg3_wait_macro_done(tp)) {
1956 *resetp = 1;
1957 return -EBUSY;
1958 }
1959 low &= 0x7fff;
1960 high &= 0x000f;
1961 if (low != test_pat[chan][i] ||
1962 high != test_pat[chan][i+1]) {
1963 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1964 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1965 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1966
1967 return -EBUSY;
1968 }
1969 }
1970 }
1971
1972 return 0;
1973}
1974
1975static int tg3_phy_reset_chanpat(struct tg3 *tp)
1976{
1977 int chan;
1978
1979 for (chan = 0; chan < 4; chan++) {
1980 int i;
1981
1982 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1983 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001984 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 for (i = 0; i < 6; i++)
1986 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001987 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if (tg3_wait_macro_done(tp))
1989 return -EBUSY;
1990 }
1991
1992 return 0;
1993}
1994
1995static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1996{
1997 u32 reg32, phy9_orig;
1998 int retries, do_phy_reset, err;
1999
2000 retries = 10;
2001 do_phy_reset = 1;
2002 do {
2003 if (do_phy_reset) {
2004 err = tg3_bmcr_reset(tp);
2005 if (err)
2006 return err;
2007 do_phy_reset = 0;
2008 }
2009
2010 /* Disable transmitter and interrupt. */
2011 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2012 continue;
2013
2014 reg32 |= 0x3000;
2015 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2016
2017 /* Set full-duplex, 1000 mbps. */
2018 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002019 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002022 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 continue;
2024
Matt Carlson221c5632011-06-13 13:39:01 +00002025 tg3_writephy(tp, MII_CTRL1000,
2026 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Matt Carlson1d36ba42011-04-20 07:57:42 +00002028 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2029 if (err)
2030 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002033 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2036 if (!err)
2037 break;
2038 } while (--retries);
2039
2040 err = tg3_phy_reset_chanpat(tp);
2041 if (err)
2042 return err;
2043
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002044 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002047 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Matt Carlson1d36ba42011-04-20 07:57:42 +00002049 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Matt Carlson221c5632011-06-13 13:39:01 +00002051 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2054 reg32 &= ~0x3000;
2055 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2056 } else if (!err)
2057 err = -EBUSY;
2058
2059 return err;
2060}
2061
2062/* This will reset the tigon3 PHY if there is no valid
2063 * link unless the FORCE argument is non-zero.
2064 */
2065static int tg3_phy_reset(struct tg3 *tp)
2066{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002067 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 int err;
2069
Michael Chan60189dd2006-12-17 17:08:07 -08002070 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002071 val = tr32(GRC_MISC_CFG);
2072 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2073 udelay(40);
2074 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002075 err = tg3_readphy(tp, MII_BMSR, &val);
2076 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (err != 0)
2078 return -EBUSY;
2079
Michael Chanc8e1e822006-04-29 18:55:17 -07002080 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2081 netif_carrier_off(tp->dev);
2082 tg3_link_report(tp);
2083 }
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2086 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2087 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2088 err = tg3_phy_reset_5703_4_5(tp);
2089 if (err)
2090 return err;
2091 goto out;
2092 }
2093
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002094 cpmuctrl = 0;
2095 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2096 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2097 cpmuctrl = tr32(TG3_CPMU_CTRL);
2098 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2099 tw32(TG3_CPMU_CTRL,
2100 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2101 }
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 err = tg3_bmcr_reset(tp);
2104 if (err)
2105 return err;
2106
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002107 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002108 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2109 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002110
2111 tw32(TG3_CPMU_CTRL, cpmuctrl);
2112 }
2113
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002114 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2115 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002116 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2117 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2118 CPMU_LSPD_1000MB_MACCLK_12_5) {
2119 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2120 udelay(40);
2121 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2122 }
2123 }
2124
Joe Perches63c3a662011-04-26 08:12:10 +00002125 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002126 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002127 return 0;
2128
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002129 tg3_phy_apply_otp(tp);
2130
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002131 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002132 tg3_phy_toggle_apd(tp, true);
2133 else
2134 tg3_phy_toggle_apd(tp, false);
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002137 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2138 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002139 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2140 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002141 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002143
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002144 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002145 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2146 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002148
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002149 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002150 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2151 tg3_phydsp_write(tp, 0x000a, 0x310b);
2152 tg3_phydsp_write(tp, 0x201f, 0x9506);
2153 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2154 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2155 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002156 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002157 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2158 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2159 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2160 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2161 tg3_writephy(tp, MII_TG3_TEST1,
2162 MII_TG3_TEST1_TRIM_EN | 0x4);
2163 } else
2164 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2165
2166 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2167 }
Michael Chanc424cb22006-04-29 18:56:34 -07002168 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /* Set Extended packet length bit (bit 14) on all chips that */
2171 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002172 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002174 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002175 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002177 err = tg3_phy_auxctl_read(tp,
2178 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2179 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002180 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2181 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
2183
2184 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2185 * jumbo frames transmission.
2186 */
Joe Perches63c3a662011-04-26 08:12:10 +00002187 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002188 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002189 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002190 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
2192
Michael Chan715116a2006-09-27 16:09:25 -07002193 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002194 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002195 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002196 }
2197
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002198 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 tg3_phy_set_wirespeed(tp);
2200 return 0;
2201}
2202
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002203#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2204#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2205#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2206 TG3_GPIO_MSG_NEED_VAUX)
2207#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2208 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2209 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2210 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2211 (TG3_GPIO_MSG_DRVR_PRES << 12))
2212
2213#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2214 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2215 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2216 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2217 (TG3_GPIO_MSG_NEED_VAUX << 12))
2218
2219static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2220{
2221 u32 status, shift;
2222
2223 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2224 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2225 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2226 else
2227 status = tr32(TG3_CPMU_DRV_STATUS);
2228
2229 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2230 status &= ~(TG3_GPIO_MSG_MASK << shift);
2231 status |= (newstat << shift);
2232
2233 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2234 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2235 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2236 else
2237 tw32(TG3_CPMU_DRV_STATUS, status);
2238
2239 return status >> TG3_APE_GPIO_MSG_SHIFT;
2240}
2241
Matt Carlson520b2752011-06-13 13:39:02 +00002242static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2243{
2244 if (!tg3_flag(tp, IS_NIC))
2245 return 0;
2246
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002247 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2248 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2249 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2250 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2251 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002252
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002253 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2254
2255 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2256 TG3_GRC_LCLCTL_PWRSW_DELAY);
2257
2258 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2259 } else {
2260 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2261 TG3_GRC_LCLCTL_PWRSW_DELAY);
2262 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002263
Matt Carlson520b2752011-06-13 13:39:02 +00002264 return 0;
2265}
2266
2267static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2268{
2269 u32 grc_local_ctrl;
2270
2271 if (!tg3_flag(tp, IS_NIC) ||
2272 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2273 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2274 return;
2275
2276 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2277
2278 tw32_wait_f(GRC_LOCAL_CTRL,
2279 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2280 TG3_GRC_LCLCTL_PWRSW_DELAY);
2281
2282 tw32_wait_f(GRC_LOCAL_CTRL,
2283 grc_local_ctrl,
2284 TG3_GRC_LCLCTL_PWRSW_DELAY);
2285
2286 tw32_wait_f(GRC_LOCAL_CTRL,
2287 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2288 TG3_GRC_LCLCTL_PWRSW_DELAY);
2289}
2290
2291static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2292{
2293 if (!tg3_flag(tp, IS_NIC))
2294 return;
2295
2296 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2297 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2298 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2299 (GRC_LCLCTRL_GPIO_OE0 |
2300 GRC_LCLCTRL_GPIO_OE1 |
2301 GRC_LCLCTRL_GPIO_OE2 |
2302 GRC_LCLCTRL_GPIO_OUTPUT0 |
2303 GRC_LCLCTRL_GPIO_OUTPUT1),
2304 TG3_GRC_LCLCTL_PWRSW_DELAY);
2305 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2306 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2307 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2308 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2309 GRC_LCLCTRL_GPIO_OE1 |
2310 GRC_LCLCTRL_GPIO_OE2 |
2311 GRC_LCLCTRL_GPIO_OUTPUT0 |
2312 GRC_LCLCTRL_GPIO_OUTPUT1 |
2313 tp->grc_local_ctrl;
2314 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2315 TG3_GRC_LCLCTL_PWRSW_DELAY);
2316
2317 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2318 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2319 TG3_GRC_LCLCTL_PWRSW_DELAY);
2320
2321 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2322 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2323 TG3_GRC_LCLCTL_PWRSW_DELAY);
2324 } else {
2325 u32 no_gpio2;
2326 u32 grc_local_ctrl = 0;
2327
2328 /* Workaround to prevent overdrawing Amps. */
2329 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2330 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2331 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2332 grc_local_ctrl,
2333 TG3_GRC_LCLCTL_PWRSW_DELAY);
2334 }
2335
2336 /* On 5753 and variants, GPIO2 cannot be used. */
2337 no_gpio2 = tp->nic_sram_data_cfg &
2338 NIC_SRAM_DATA_CFG_NO_GPIO2;
2339
2340 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2341 GRC_LCLCTRL_GPIO_OE1 |
2342 GRC_LCLCTRL_GPIO_OE2 |
2343 GRC_LCLCTRL_GPIO_OUTPUT1 |
2344 GRC_LCLCTRL_GPIO_OUTPUT2;
2345 if (no_gpio2) {
2346 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2347 GRC_LCLCTRL_GPIO_OUTPUT2);
2348 }
2349 tw32_wait_f(GRC_LOCAL_CTRL,
2350 tp->grc_local_ctrl | grc_local_ctrl,
2351 TG3_GRC_LCLCTL_PWRSW_DELAY);
2352
2353 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2354
2355 tw32_wait_f(GRC_LOCAL_CTRL,
2356 tp->grc_local_ctrl | grc_local_ctrl,
2357 TG3_GRC_LCLCTL_PWRSW_DELAY);
2358
2359 if (!no_gpio2) {
2360 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2361 tw32_wait_f(GRC_LOCAL_CTRL,
2362 tp->grc_local_ctrl | grc_local_ctrl,
2363 TG3_GRC_LCLCTL_PWRSW_DELAY);
2364 }
2365 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002366}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002367
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002368static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002369{
2370 u32 msg = 0;
2371
2372 /* Serialize power state transitions */
2373 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2374 return;
2375
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002376 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002377 msg = TG3_GPIO_MSG_NEED_VAUX;
2378
2379 msg = tg3_set_function_status(tp, msg);
2380
2381 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2382 goto done;
2383
2384 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2385 tg3_pwrsrc_switch_to_vaux(tp);
2386 else
2387 tg3_pwrsrc_die_with_vmain(tp);
2388
2389done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002390 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002391}
2392
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002393static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
Matt Carlson683644b2011-03-09 16:58:23 +00002395 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Matt Carlson334355a2010-01-20 16:58:10 +00002397 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002398 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlson334355a2010-01-20 16:58:10 +00002399 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return;
2401
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002402 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2403 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2404 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002405 tg3_frob_aux_power_5717(tp, include_wol ?
2406 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002407 return;
2408 }
2409
2410 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002411 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002413 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002414
Michael Chanbc1c7562006-03-20 17:48:03 -08002415 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002416 if (dev_peer) {
2417 struct tg3 *tp_peer = netdev_priv(dev_peer);
2418
Joe Perches63c3a662011-04-26 08:12:10 +00002419 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002420 return;
2421
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002422 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002423 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002424 need_vaux = true;
2425 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002428 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2429 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002430 need_vaux = true;
2431
Matt Carlson520b2752011-06-13 13:39:02 +00002432 if (need_vaux)
2433 tg3_pwrsrc_switch_to_vaux(tp);
2434 else
2435 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002438static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2439{
2440 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2441 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002442 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002443 if (speed != SPEED_10)
2444 return 1;
2445 } else if (speed == SPEED_10)
2446 return 1;
2447
2448 return 0;
2449}
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451static int tg3_setup_phy(struct tg3 *, int);
2452
2453#define RESET_KIND_SHUTDOWN 0
2454#define RESET_KIND_INIT 1
2455#define RESET_KIND_SUSPEND 2
2456
2457static void tg3_write_sig_post_reset(struct tg3 *, int);
2458static int tg3_halt_cpu(struct tg3 *, u32);
2459
Matt Carlson0a459aa2008-11-03 16:54:15 -08002460static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002461{
Matt Carlsonce057f02007-11-12 21:08:03 -08002462 u32 val;
2463
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002464 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002465 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2466 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2467 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2468
2469 sg_dig_ctrl |=
2470 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2471 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2472 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2473 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002474 return;
Michael Chan51297242007-02-13 12:17:57 -08002475 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002476
Michael Chan60189dd2006-12-17 17:08:07 -08002477 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002478 tg3_bmcr_reset(tp);
2479 val = tr32(GRC_MISC_CFG);
2480 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2481 udelay(40);
2482 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002483 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002484 u32 phytest;
2485 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2486 u32 phy;
2487
2488 tg3_writephy(tp, MII_ADVERTISE, 0);
2489 tg3_writephy(tp, MII_BMCR,
2490 BMCR_ANENABLE | BMCR_ANRESTART);
2491
2492 tg3_writephy(tp, MII_TG3_FET_TEST,
2493 phytest | MII_TG3_FET_SHADOW_EN);
2494 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2495 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2496 tg3_writephy(tp,
2497 MII_TG3_FET_SHDW_AUXMODE4,
2498 phy);
2499 }
2500 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2501 }
2502 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002503 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002504 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2505 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002506
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002507 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2508 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2509 MII_TG3_AUXCTL_PCTL_VREG_11V;
2510 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002511 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002512
Michael Chan15c3b692006-03-22 01:06:52 -08002513 /* The PHY should not be powered down on some chips because
2514 * of bugs.
2515 */
2516 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2517 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2518 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002519 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002520 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002521
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002522 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2523 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002524 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2525 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2526 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2527 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2528 }
2529
Michael Chan15c3b692006-03-22 01:06:52 -08002530 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2531}
2532
Matt Carlson3f007892008-11-03 16:51:36 -08002533/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002534static int tg3_nvram_lock(struct tg3 *tp)
2535{
Joe Perches63c3a662011-04-26 08:12:10 +00002536 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002537 int i;
2538
2539 if (tp->nvram_lock_cnt == 0) {
2540 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2541 for (i = 0; i < 8000; i++) {
2542 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2543 break;
2544 udelay(20);
2545 }
2546 if (i == 8000) {
2547 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2548 return -ENODEV;
2549 }
2550 }
2551 tp->nvram_lock_cnt++;
2552 }
2553 return 0;
2554}
2555
2556/* tp->lock is held. */
2557static void tg3_nvram_unlock(struct tg3 *tp)
2558{
Joe Perches63c3a662011-04-26 08:12:10 +00002559 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002560 if (tp->nvram_lock_cnt > 0)
2561 tp->nvram_lock_cnt--;
2562 if (tp->nvram_lock_cnt == 0)
2563 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2564 }
2565}
2566
2567/* tp->lock is held. */
2568static void tg3_enable_nvram_access(struct tg3 *tp)
2569{
Joe Perches63c3a662011-04-26 08:12:10 +00002570 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002571 u32 nvaccess = tr32(NVRAM_ACCESS);
2572
2573 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2574 }
2575}
2576
2577/* tp->lock is held. */
2578static void tg3_disable_nvram_access(struct tg3 *tp)
2579{
Joe Perches63c3a662011-04-26 08:12:10 +00002580 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002581 u32 nvaccess = tr32(NVRAM_ACCESS);
2582
2583 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2584 }
2585}
2586
2587static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2588 u32 offset, u32 *val)
2589{
2590 u32 tmp;
2591 int i;
2592
2593 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2594 return -EINVAL;
2595
2596 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2597 EEPROM_ADDR_DEVID_MASK |
2598 EEPROM_ADDR_READ);
2599 tw32(GRC_EEPROM_ADDR,
2600 tmp |
2601 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2602 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2603 EEPROM_ADDR_ADDR_MASK) |
2604 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2605
2606 for (i = 0; i < 1000; i++) {
2607 tmp = tr32(GRC_EEPROM_ADDR);
2608
2609 if (tmp & EEPROM_ADDR_COMPLETE)
2610 break;
2611 msleep(1);
2612 }
2613 if (!(tmp & EEPROM_ADDR_COMPLETE))
2614 return -EBUSY;
2615
Matt Carlson62cedd12009-04-20 14:52:29 -07002616 tmp = tr32(GRC_EEPROM_DATA);
2617
2618 /*
2619 * The data will always be opposite the native endian
2620 * format. Perform a blind byteswap to compensate.
2621 */
2622 *val = swab32(tmp);
2623
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002624 return 0;
2625}
2626
2627#define NVRAM_CMD_TIMEOUT 10000
2628
2629static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2630{
2631 int i;
2632
2633 tw32(NVRAM_CMD, nvram_cmd);
2634 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2635 udelay(10);
2636 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2637 udelay(10);
2638 break;
2639 }
2640 }
2641
2642 if (i == NVRAM_CMD_TIMEOUT)
2643 return -EBUSY;
2644
2645 return 0;
2646}
2647
2648static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2649{
Joe Perches63c3a662011-04-26 08:12:10 +00002650 if (tg3_flag(tp, NVRAM) &&
2651 tg3_flag(tp, NVRAM_BUFFERED) &&
2652 tg3_flag(tp, FLASH) &&
2653 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002654 (tp->nvram_jedecnum == JEDEC_ATMEL))
2655
2656 addr = ((addr / tp->nvram_pagesize) <<
2657 ATMEL_AT45DB0X1B_PAGE_POS) +
2658 (addr % tp->nvram_pagesize);
2659
2660 return addr;
2661}
2662
2663static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2664{
Joe Perches63c3a662011-04-26 08:12:10 +00002665 if (tg3_flag(tp, NVRAM) &&
2666 tg3_flag(tp, NVRAM_BUFFERED) &&
2667 tg3_flag(tp, FLASH) &&
2668 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002669 (tp->nvram_jedecnum == JEDEC_ATMEL))
2670
2671 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2672 tp->nvram_pagesize) +
2673 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2674
2675 return addr;
2676}
2677
Matt Carlsone4f34112009-02-25 14:25:00 +00002678/* NOTE: Data read in from NVRAM is byteswapped according to
2679 * the byteswapping settings for all other register accesses.
2680 * tg3 devices are BE devices, so on a BE machine, the data
2681 * returned will be exactly as it is seen in NVRAM. On a LE
2682 * machine, the 32-bit value will be byteswapped.
2683 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002684static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2685{
2686 int ret;
2687
Joe Perches63c3a662011-04-26 08:12:10 +00002688 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002689 return tg3_nvram_read_using_eeprom(tp, offset, val);
2690
2691 offset = tg3_nvram_phys_addr(tp, offset);
2692
2693 if (offset > NVRAM_ADDR_MSK)
2694 return -EINVAL;
2695
2696 ret = tg3_nvram_lock(tp);
2697 if (ret)
2698 return ret;
2699
2700 tg3_enable_nvram_access(tp);
2701
2702 tw32(NVRAM_ADDR, offset);
2703 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2704 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2705
2706 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002707 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002708
2709 tg3_disable_nvram_access(tp);
2710
2711 tg3_nvram_unlock(tp);
2712
2713 return ret;
2714}
2715
Matt Carlsona9dc5292009-02-25 14:25:30 +00002716/* Ensures NVRAM data is in bytestream format. */
2717static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002718{
2719 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002720 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002721 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002722 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002723 return res;
2724}
2725
2726/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002727static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2728{
2729 u32 addr_high, addr_low;
2730 int i;
2731
2732 addr_high = ((tp->dev->dev_addr[0] << 8) |
2733 tp->dev->dev_addr[1]);
2734 addr_low = ((tp->dev->dev_addr[2] << 24) |
2735 (tp->dev->dev_addr[3] << 16) |
2736 (tp->dev->dev_addr[4] << 8) |
2737 (tp->dev->dev_addr[5] << 0));
2738 for (i = 0; i < 4; i++) {
2739 if (i == 1 && skip_mac_1)
2740 continue;
2741 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2742 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2743 }
2744
2745 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2746 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2747 for (i = 0; i < 12; i++) {
2748 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2749 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2750 }
2751 }
2752
2753 addr_high = (tp->dev->dev_addr[0] +
2754 tp->dev->dev_addr[1] +
2755 tp->dev->dev_addr[2] +
2756 tp->dev->dev_addr[3] +
2757 tp->dev->dev_addr[4] +
2758 tp->dev->dev_addr[5]) &
2759 TX_BACKOFF_SEED_MASK;
2760 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2761}
2762
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002763static void tg3_enable_register_access(struct tg3 *tp)
2764{
2765 /*
2766 * Make sure register accesses (indirect or otherwise) will function
2767 * correctly.
2768 */
2769 pci_write_config_dword(tp->pdev,
2770 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2771}
2772
2773static int tg3_power_up(struct tg3 *tp)
2774{
Matt Carlsonbed98292011-07-13 09:27:29 +00002775 int err;
2776
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002777 tg3_enable_register_access(tp);
2778
Matt Carlsonbed98292011-07-13 09:27:29 +00002779 err = pci_set_power_state(tp->pdev, PCI_D0);
2780 if (!err) {
2781 /* Switch out of Vaux if it is a NIC */
2782 tg3_pwrsrc_switch_to_vmain(tp);
2783 } else {
2784 netdev_err(tp->dev, "Transition to D0 failed\n");
2785 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002786
Matt Carlsonbed98292011-07-13 09:27:29 +00002787 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002788}
2789
2790static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
2792 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002793 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002795 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002796
2797 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00002798 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002799 u16 lnkctl;
2800
2801 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00002802 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002803 &lnkctl);
2804 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2805 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00002806 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002807 lnkctl);
2808 }
2809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2811 tw32(TG3PCI_MISC_HOST_CTRL,
2812 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2813
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002814 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00002815 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002816
Joe Perches63c3a662011-04-26 08:12:10 +00002817 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002818 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002819 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002820 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002821 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002822 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002823
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002824 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002825
Matt Carlson80096062010-08-02 11:26:06 +00002826 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002827
2828 tp->link_config.orig_speed = phydev->speed;
2829 tp->link_config.orig_duplex = phydev->duplex;
2830 tp->link_config.orig_autoneg = phydev->autoneg;
2831 tp->link_config.orig_advertising = phydev->advertising;
2832
2833 advertising = ADVERTISED_TP |
2834 ADVERTISED_Pause |
2835 ADVERTISED_Autoneg |
2836 ADVERTISED_10baseT_Half;
2837
Joe Perches63c3a662011-04-26 08:12:10 +00002838 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
2839 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002840 advertising |=
2841 ADVERTISED_100baseT_Half |
2842 ADVERTISED_100baseT_Full |
2843 ADVERTISED_10baseT_Full;
2844 else
2845 advertising |= ADVERTISED_10baseT_Full;
2846 }
2847
2848 phydev->advertising = advertising;
2849
2850 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002851
2852 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002853 if (phyid != PHY_ID_BCMAC131) {
2854 phyid &= PHY_BCM_OUI_MASK;
2855 if (phyid == PHY_BCM_OUI_1 ||
2856 phyid == PHY_BCM_OUI_2 ||
2857 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002858 do_low_power = true;
2859 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002860 }
Matt Carlsondd477002008-05-25 23:45:58 -07002861 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002862 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002863
Matt Carlson80096062010-08-02 11:26:06 +00002864 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2865 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002866 tp->link_config.orig_speed = tp->link_config.speed;
2867 tp->link_config.orig_duplex = tp->link_config.duplex;
2868 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002871 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002872 tp->link_config.speed = SPEED_10;
2873 tp->link_config.duplex = DUPLEX_HALF;
2874 tp->link_config.autoneg = AUTONEG_ENABLE;
2875 tg3_setup_phy(tp, 0);
2876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
2878
Michael Chanb5d37722006-09-27 16:06:21 -07002879 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2880 u32 val;
2881
2882 val = tr32(GRC_VCPU_EXT_CTRL);
2883 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00002884 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002885 int i;
2886 u32 val;
2887
2888 for (i = 0; i < 200; i++) {
2889 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2890 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2891 break;
2892 msleep(1);
2893 }
2894 }
Joe Perches63c3a662011-04-26 08:12:10 +00002895 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07002896 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2897 WOL_DRV_STATE_SHUTDOWN |
2898 WOL_DRV_WOL |
2899 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002900
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002901 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 u32 mac_mode;
2903
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002904 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002905 if (do_low_power &&
2906 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
2907 tg3_phy_auxctl_write(tp,
2908 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
2909 MII_TG3_AUXCTL_PCTL_WOL_EN |
2910 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2911 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07002912 udelay(40);
2913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002915 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002916 mac_mode = MAC_MODE_PORT_MODE_GMII;
2917 else
2918 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002920 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2921 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2922 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00002923 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002924 SPEED_100 : SPEED_10;
2925 if (tg3_5700_link_polarity(tp, speed))
2926 mac_mode |= MAC_MODE_LINK_POLARITY;
2927 else
2928 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 } else {
2931 mac_mode = MAC_MODE_PORT_MODE_TBI;
2932 }
2933
Joe Perches63c3a662011-04-26 08:12:10 +00002934 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 tw32(MAC_LED_CTRL, tp->led_ctrl);
2936
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002937 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00002938 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
2939 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002940 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Joe Perches63c3a662011-04-26 08:12:10 +00002942 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002943 mac_mode |= MAC_MODE_APE_TX_EN |
2944 MAC_MODE_APE_RX_EN |
2945 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 tw32_f(MAC_MODE, mac_mode);
2948 udelay(100);
2949
2950 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2951 udelay(10);
2952 }
2953
Joe Perches63c3a662011-04-26 08:12:10 +00002954 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2956 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2957 u32 base_val;
2958
2959 base_val = tp->pci_clock_ctrl;
2960 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2961 CLOCK_CTRL_TXCLK_DISABLE);
2962
Michael Chanb401e9e2005-12-19 16:27:04 -08002963 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2964 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00002965 } else if (tg3_flag(tp, 5780_CLASS) ||
2966 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00002967 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002968 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00002969 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 u32 newbits1, newbits2;
2971
2972 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2973 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2974 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2975 CLOCK_CTRL_TXCLK_DISABLE |
2976 CLOCK_CTRL_ALTCLK);
2977 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00002978 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 newbits1 = CLOCK_CTRL_625_CORE;
2980 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2981 } else {
2982 newbits1 = CLOCK_CTRL_ALTCLK;
2983 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2984 }
2985
Michael Chanb401e9e2005-12-19 16:27:04 -08002986 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2987 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Michael Chanb401e9e2005-12-19 16:27:04 -08002989 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2990 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Joe Perches63c3a662011-04-26 08:12:10 +00002992 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 u32 newbits3;
2994
2995 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2996 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2997 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2998 CLOCK_CTRL_TXCLK_DISABLE |
2999 CLOCK_CTRL_44MHZ_CORE);
3000 } else {
3001 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3002 }
3003
Michael Chanb401e9e2005-12-19 16:27:04 -08003004 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3005 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 }
3007 }
3008
Joe Perches63c3a662011-04-26 08:12:10 +00003009 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003010 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003011
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003012 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 /* Workaround for unstable PLL clock */
3015 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3016 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3017 u32 val = tr32(0x7d00);
3018
3019 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3020 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003021 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003022 int err;
3023
3024 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003026 if (!err)
3027 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
3030
Michael Chanbbadf502006-04-06 21:46:34 -07003031 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return 0;
3034}
3035
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003036static void tg3_power_down(struct tg3 *tp)
3037{
3038 tg3_power_down_prepare(tp);
3039
Joe Perches63c3a662011-04-26 08:12:10 +00003040 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003041 pci_set_power_state(tp->pdev, PCI_D3hot);
3042}
3043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3045{
3046 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3047 case MII_TG3_AUX_STAT_10HALF:
3048 *speed = SPEED_10;
3049 *duplex = DUPLEX_HALF;
3050 break;
3051
3052 case MII_TG3_AUX_STAT_10FULL:
3053 *speed = SPEED_10;
3054 *duplex = DUPLEX_FULL;
3055 break;
3056
3057 case MII_TG3_AUX_STAT_100HALF:
3058 *speed = SPEED_100;
3059 *duplex = DUPLEX_HALF;
3060 break;
3061
3062 case MII_TG3_AUX_STAT_100FULL:
3063 *speed = SPEED_100;
3064 *duplex = DUPLEX_FULL;
3065 break;
3066
3067 case MII_TG3_AUX_STAT_1000HALF:
3068 *speed = SPEED_1000;
3069 *duplex = DUPLEX_HALF;
3070 break;
3071
3072 case MII_TG3_AUX_STAT_1000FULL:
3073 *speed = SPEED_1000;
3074 *duplex = DUPLEX_FULL;
3075 break;
3076
3077 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003078 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003079 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3080 SPEED_10;
3081 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3082 DUPLEX_HALF;
3083 break;
3084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 *speed = SPEED_INVALID;
3086 *duplex = DUPLEX_INVALID;
3087 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089}
3090
Matt Carlson42b64a42011-05-19 12:12:49 +00003091static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
Matt Carlson42b64a42011-05-19 12:12:49 +00003093 int err = 0;
3094 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Matt Carlson42b64a42011-05-19 12:12:49 +00003096 new_adv = ADVERTISE_CSMA;
3097 if (advertise & ADVERTISED_10baseT_Half)
3098 new_adv |= ADVERTISE_10HALF;
3099 if (advertise & ADVERTISED_10baseT_Full)
3100 new_adv |= ADVERTISE_10FULL;
3101 if (advertise & ADVERTISED_100baseT_Half)
3102 new_adv |= ADVERTISE_100HALF;
3103 if (advertise & ADVERTISED_100baseT_Full)
3104 new_adv |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Matt Carlson42b64a42011-05-19 12:12:49 +00003106 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Matt Carlson42b64a42011-05-19 12:12:49 +00003108 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3109 if (err)
3110 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Matt Carlson42b64a42011-05-19 12:12:49 +00003112 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3113 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003114
Matt Carlson42b64a42011-05-19 12:12:49 +00003115 new_adv = 0;
3116 if (advertise & ADVERTISED_1000baseT_Half)
Matt Carlson221c5632011-06-13 13:39:01 +00003117 new_adv |= ADVERTISE_1000HALF;
Matt Carlson42b64a42011-05-19 12:12:49 +00003118 if (advertise & ADVERTISED_1000baseT_Full)
Matt Carlson221c5632011-06-13 13:39:01 +00003119 new_adv |= ADVERTISE_1000FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003120
Matt Carlson42b64a42011-05-19 12:12:49 +00003121 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3122 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
Matt Carlson221c5632011-06-13 13:39:01 +00003123 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Matt Carlson221c5632011-06-13 13:39:01 +00003125 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
Matt Carlson42b64a42011-05-19 12:12:49 +00003126 if (err)
3127 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003128
Matt Carlson42b64a42011-05-19 12:12:49 +00003129 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3130 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
Matt Carlson42b64a42011-05-19 12:12:49 +00003132 tw32(TG3_CPMU_EEE_MODE,
3133 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003134
Matt Carlson42b64a42011-05-19 12:12:49 +00003135 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3136 if (!err) {
3137 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003138
Matt Carlsona6b68da2010-12-06 08:28:52 +00003139 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003140 /* Advertise 100-BaseTX EEE ability */
3141 if (advertise & ADVERTISED_100baseT_Full)
3142 val |= MDIO_AN_EEE_ADV_100TX;
3143 /* Advertise 1000-BaseT EEE ability */
3144 if (advertise & ADVERTISED_1000baseT_Full)
3145 val |= MDIO_AN_EEE_ADV_1000T;
3146 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003147 if (err)
3148 val = 0;
3149
3150 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3151 case ASIC_REV_5717:
3152 case ASIC_REV_57765:
3153 case ASIC_REV_5719:
3154 /* If we advertised any eee advertisements above... */
3155 if (val)
3156 val = MII_TG3_DSP_TAP26_ALNOKO |
3157 MII_TG3_DSP_TAP26_RMRXSTO |
3158 MII_TG3_DSP_TAP26_OPCSINPT;
3159 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3160 /* Fall through */
3161 case ASIC_REV_5720:
3162 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3163 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3164 MII_TG3_DSP_CH34TP2_HIBW01);
3165 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003166
Matt Carlson42b64a42011-05-19 12:12:49 +00003167 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3168 if (!err)
3169 err = err2;
3170 }
3171
3172done:
3173 return err;
3174}
3175
3176static void tg3_phy_copper_begin(struct tg3 *tp)
3177{
3178 u32 new_adv;
3179 int i;
3180
3181 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3182 new_adv = ADVERTISED_10baseT_Half |
3183 ADVERTISED_10baseT_Full;
3184 if (tg3_flag(tp, WOL_SPEED_100MB))
3185 new_adv |= ADVERTISED_100baseT_Half |
3186 ADVERTISED_100baseT_Full;
3187
3188 tg3_phy_autoneg_cfg(tp, new_adv,
3189 FLOW_CTRL_TX | FLOW_CTRL_RX);
3190 } else if (tp->link_config.speed == SPEED_INVALID) {
3191 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3192 tp->link_config.advertising &=
3193 ~(ADVERTISED_1000baseT_Half |
3194 ADVERTISED_1000baseT_Full);
3195
3196 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3197 tp->link_config.flowctrl);
3198 } else {
3199 /* Asking for a specific link mode. */
3200 if (tp->link_config.speed == SPEED_1000) {
3201 if (tp->link_config.duplex == DUPLEX_FULL)
3202 new_adv = ADVERTISED_1000baseT_Full;
3203 else
3204 new_adv = ADVERTISED_1000baseT_Half;
3205 } else if (tp->link_config.speed == SPEED_100) {
3206 if (tp->link_config.duplex == DUPLEX_FULL)
3207 new_adv = ADVERTISED_100baseT_Full;
3208 else
3209 new_adv = ADVERTISED_100baseT_Half;
3210 } else {
3211 if (tp->link_config.duplex == DUPLEX_FULL)
3212 new_adv = ADVERTISED_10baseT_Full;
3213 else
3214 new_adv = ADVERTISED_10baseT_Half;
3215 }
3216
3217 tg3_phy_autoneg_cfg(tp, new_adv,
3218 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003219 }
3220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3222 tp->link_config.speed != SPEED_INVALID) {
3223 u32 bmcr, orig_bmcr;
3224
3225 tp->link_config.active_speed = tp->link_config.speed;
3226 tp->link_config.active_duplex = tp->link_config.duplex;
3227
3228 bmcr = 0;
3229 switch (tp->link_config.speed) {
3230 default:
3231 case SPEED_10:
3232 break;
3233
3234 case SPEED_100:
3235 bmcr |= BMCR_SPEED100;
3236 break;
3237
3238 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003239 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
3243 if (tp->link_config.duplex == DUPLEX_FULL)
3244 bmcr |= BMCR_FULLDPLX;
3245
3246 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3247 (bmcr != orig_bmcr)) {
3248 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3249 for (i = 0; i < 1500; i++) {
3250 u32 tmp;
3251
3252 udelay(10);
3253 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3254 tg3_readphy(tp, MII_BMSR, &tmp))
3255 continue;
3256 if (!(tmp & BMSR_LSTATUS)) {
3257 udelay(40);
3258 break;
3259 }
3260 }
3261 tg3_writephy(tp, MII_BMCR, bmcr);
3262 udelay(40);
3263 }
3264 } else {
3265 tg3_writephy(tp, MII_BMCR,
3266 BMCR_ANENABLE | BMCR_ANRESTART);
3267 }
3268}
3269
3270static int tg3_init_5401phy_dsp(struct tg3 *tp)
3271{
3272 int err;
3273
3274 /* Turn off tap power management. */
3275 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003276 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003278 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3279 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3280 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3281 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3282 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
3284 udelay(40);
3285
3286 return err;
3287}
3288
Michael Chan3600d912006-12-07 00:21:48 -08003289static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
Michael Chan3600d912006-12-07 00:21:48 -08003291 u32 adv_reg, all_mask = 0;
3292
3293 if (mask & ADVERTISED_10baseT_Half)
3294 all_mask |= ADVERTISE_10HALF;
3295 if (mask & ADVERTISED_10baseT_Full)
3296 all_mask |= ADVERTISE_10FULL;
3297 if (mask & ADVERTISED_100baseT_Half)
3298 all_mask |= ADVERTISE_100HALF;
3299 if (mask & ADVERTISED_100baseT_Full)
3300 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
3302 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3303 return 0;
3304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 if ((adv_reg & all_mask) != all_mask)
3306 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003307 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 u32 tg3_ctrl;
3309
Michael Chan3600d912006-12-07 00:21:48 -08003310 all_mask = 0;
3311 if (mask & ADVERTISED_1000baseT_Half)
3312 all_mask |= ADVERTISE_1000HALF;
3313 if (mask & ADVERTISED_1000baseT_Full)
3314 all_mask |= ADVERTISE_1000FULL;
3315
Matt Carlson221c5632011-06-13 13:39:01 +00003316 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 return 0;
3318
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 if ((tg3_ctrl & all_mask) != all_mask)
3320 return 0;
3321 }
3322 return 1;
3323}
3324
Matt Carlsonef167e22007-12-20 20:10:01 -08003325static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3326{
3327 u32 curadv, reqadv;
3328
3329 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3330 return 1;
3331
3332 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3333 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3334
3335 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3336 if (curadv != reqadv)
3337 return 0;
3338
Joe Perches63c3a662011-04-26 08:12:10 +00003339 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003340 tg3_readphy(tp, MII_LPA, rmtadv);
3341 } else {
3342 /* Reprogram the advertisement register, even if it
3343 * does not affect the current link. If the link
3344 * gets renegotiated in the future, we can save an
3345 * additional renegotiation cycle by advertising
3346 * it correctly in the first place.
3347 */
3348 if (curadv != reqadv) {
3349 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3350 ADVERTISE_PAUSE_ASYM);
3351 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3352 }
3353 }
3354
3355 return 1;
3356}
3357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3359{
3360 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003361 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003362 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 u16 current_speed;
3364 u8 current_duplex;
3365 int i, err;
3366
3367 tw32(MAC_EVENT, 0);
3368
3369 tw32_f(MAC_STATUS,
3370 (MAC_STATUS_SYNC_CHANGED |
3371 MAC_STATUS_CFG_CHANGED |
3372 MAC_STATUS_MI_COMPLETION |
3373 MAC_STATUS_LNKSTATE_CHANGED));
3374 udelay(40);
3375
Matt Carlson8ef21422008-05-02 16:47:53 -07003376 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3377 tw32_f(MAC_MI_MODE,
3378 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3379 udelay(80);
3380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003382 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
3384 /* Some third-party PHYs need to be reset on link going
3385 * down.
3386 */
3387 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3388 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3389 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3390 netif_carrier_ok(tp->dev)) {
3391 tg3_readphy(tp, MII_BMSR, &bmsr);
3392 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3393 !(bmsr & BMSR_LSTATUS))
3394 force_reset = 1;
3395 }
3396 if (force_reset)
3397 tg3_phy_reset(tp);
3398
Matt Carlson79eb6902010-02-17 15:17:03 +00003399 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 tg3_readphy(tp, MII_BMSR, &bmsr);
3401 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003402 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 bmsr = 0;
3404
3405 if (!(bmsr & BMSR_LSTATUS)) {
3406 err = tg3_init_5401phy_dsp(tp);
3407 if (err)
3408 return err;
3409
3410 tg3_readphy(tp, MII_BMSR, &bmsr);
3411 for (i = 0; i < 1000; i++) {
3412 udelay(10);
3413 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3414 (bmsr & BMSR_LSTATUS)) {
3415 udelay(40);
3416 break;
3417 }
3418 }
3419
Matt Carlson79eb6902010-02-17 15:17:03 +00003420 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3421 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 !(bmsr & BMSR_LSTATUS) &&
3423 tp->link_config.active_speed == SPEED_1000) {
3424 err = tg3_phy_reset(tp);
3425 if (!err)
3426 err = tg3_init_5401phy_dsp(tp);
3427 if (err)
3428 return err;
3429 }
3430 }
3431 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3432 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3433 /* 5701 {A0,B0} CRC bug workaround */
3434 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003435 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3436 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3437 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 }
3439
3440 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003441 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3442 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003444 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003446 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3448
3449 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3450 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3451 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3452 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3453 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3454 else
3455 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3456 }
3457
3458 current_link_up = 0;
3459 current_speed = SPEED_INVALID;
3460 current_duplex = DUPLEX_INVALID;
3461
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003462 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003463 err = tg3_phy_auxctl_read(tp,
3464 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3465 &val);
3466 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003467 tg3_phy_auxctl_write(tp,
3468 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3469 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 goto relink;
3471 }
3472 }
3473
3474 bmsr = 0;
3475 for (i = 0; i < 100; i++) {
3476 tg3_readphy(tp, MII_BMSR, &bmsr);
3477 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3478 (bmsr & BMSR_LSTATUS))
3479 break;
3480 udelay(40);
3481 }
3482
3483 if (bmsr & BMSR_LSTATUS) {
3484 u32 aux_stat, bmcr;
3485
3486 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3487 for (i = 0; i < 2000; i++) {
3488 udelay(10);
3489 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3490 aux_stat)
3491 break;
3492 }
3493
3494 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3495 &current_speed,
3496 &current_duplex);
3497
3498 bmcr = 0;
3499 for (i = 0; i < 200; i++) {
3500 tg3_readphy(tp, MII_BMCR, &bmcr);
3501 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3502 continue;
3503 if (bmcr && bmcr != 0x7fff)
3504 break;
3505 udelay(10);
3506 }
3507
Matt Carlsonef167e22007-12-20 20:10:01 -08003508 lcl_adv = 0;
3509 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
Matt Carlsonef167e22007-12-20 20:10:01 -08003511 tp->link_config.active_speed = current_speed;
3512 tp->link_config.active_duplex = current_duplex;
3513
3514 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3515 if ((bmcr & BMCR_ANENABLE) &&
3516 tg3_copper_is_advertising_all(tp,
3517 tp->link_config.advertising)) {
3518 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3519 &rmt_adv))
3520 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 }
3522 } else {
3523 if (!(bmcr & BMCR_ANENABLE) &&
3524 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003525 tp->link_config.duplex == current_duplex &&
3526 tp->link_config.flowctrl ==
3527 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 }
3530 }
3531
Matt Carlsonef167e22007-12-20 20:10:01 -08003532 if (current_link_up == 1 &&
3533 tp->link_config.active_duplex == DUPLEX_FULL)
3534 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 }
3536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537relink:
Matt Carlson80096062010-08-02 11:26:06 +00003538 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 tg3_phy_copper_begin(tp);
3540
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003541 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00003542 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
3543 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 current_link_up = 1;
3545 }
3546
3547 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3548 if (current_link_up == 1) {
3549 if (tp->link_config.active_speed == SPEED_100 ||
3550 tp->link_config.active_speed == SPEED_10)
3551 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3552 else
3553 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003554 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003555 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3556 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3558
3559 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3560 if (tp->link_config.active_duplex == DUPLEX_HALF)
3561 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3562
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003564 if (current_link_up == 1 &&
3565 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003567 else
3568 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
3570
3571 /* ??? Without this setting Netgear GA302T PHY does not
3572 * ??? send/receive packets...
3573 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003574 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3576 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3577 tw32_f(MAC_MI_MODE, tp->mi_mode);
3578 udelay(80);
3579 }
3580
3581 tw32_f(MAC_MODE, tp->mac_mode);
3582 udelay(40);
3583
Matt Carlson52b02d02010-10-14 10:37:41 +00003584 tg3_phy_eee_adjust(tp, current_link_up);
3585
Joe Perches63c3a662011-04-26 08:12:10 +00003586 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 /* Polled via timer. */
3588 tw32_f(MAC_EVENT, 0);
3589 } else {
3590 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3591 }
3592 udelay(40);
3593
3594 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3595 current_link_up == 1 &&
3596 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00003597 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 udelay(120);
3599 tw32_f(MAC_STATUS,
3600 (MAC_STATUS_SYNC_CHANGED |
3601 MAC_STATUS_CFG_CHANGED));
3602 udelay(40);
3603 tg3_write_mem(tp,
3604 NIC_SRAM_FIRMWARE_MBOX,
3605 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3606 }
3607
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003608 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00003609 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003610 u16 oldlnkctl, newlnkctl;
3611
3612 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003613 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003614 &oldlnkctl);
3615 if (tp->link_config.active_speed == SPEED_100 ||
3616 tp->link_config.active_speed == SPEED_10)
3617 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3618 else
3619 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3620 if (newlnkctl != oldlnkctl)
3621 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003622 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003623 newlnkctl);
3624 }
3625
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 if (current_link_up != netif_carrier_ok(tp->dev)) {
3627 if (current_link_up)
3628 netif_carrier_on(tp->dev);
3629 else
3630 netif_carrier_off(tp->dev);
3631 tg3_link_report(tp);
3632 }
3633
3634 return 0;
3635}
3636
3637struct tg3_fiber_aneginfo {
3638 int state;
3639#define ANEG_STATE_UNKNOWN 0
3640#define ANEG_STATE_AN_ENABLE 1
3641#define ANEG_STATE_RESTART_INIT 2
3642#define ANEG_STATE_RESTART 3
3643#define ANEG_STATE_DISABLE_LINK_OK 4
3644#define ANEG_STATE_ABILITY_DETECT_INIT 5
3645#define ANEG_STATE_ABILITY_DETECT 6
3646#define ANEG_STATE_ACK_DETECT_INIT 7
3647#define ANEG_STATE_ACK_DETECT 8
3648#define ANEG_STATE_COMPLETE_ACK_INIT 9
3649#define ANEG_STATE_COMPLETE_ACK 10
3650#define ANEG_STATE_IDLE_DETECT_INIT 11
3651#define ANEG_STATE_IDLE_DETECT 12
3652#define ANEG_STATE_LINK_OK 13
3653#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3654#define ANEG_STATE_NEXT_PAGE_WAIT 15
3655
3656 u32 flags;
3657#define MR_AN_ENABLE 0x00000001
3658#define MR_RESTART_AN 0x00000002
3659#define MR_AN_COMPLETE 0x00000004
3660#define MR_PAGE_RX 0x00000008
3661#define MR_NP_LOADED 0x00000010
3662#define MR_TOGGLE_TX 0x00000020
3663#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3664#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3665#define MR_LP_ADV_SYM_PAUSE 0x00000100
3666#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3667#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3668#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3669#define MR_LP_ADV_NEXT_PAGE 0x00001000
3670#define MR_TOGGLE_RX 0x00002000
3671#define MR_NP_RX 0x00004000
3672
3673#define MR_LINK_OK 0x80000000
3674
3675 unsigned long link_time, cur_time;
3676
3677 u32 ability_match_cfg;
3678 int ability_match_count;
3679
3680 char ability_match, idle_match, ack_match;
3681
3682 u32 txconfig, rxconfig;
3683#define ANEG_CFG_NP 0x00000080
3684#define ANEG_CFG_ACK 0x00000040
3685#define ANEG_CFG_RF2 0x00000020
3686#define ANEG_CFG_RF1 0x00000010
3687#define ANEG_CFG_PS2 0x00000001
3688#define ANEG_CFG_PS1 0x00008000
3689#define ANEG_CFG_HD 0x00004000
3690#define ANEG_CFG_FD 0x00002000
3691#define ANEG_CFG_INVAL 0x00001f06
3692
3693};
3694#define ANEG_OK 0
3695#define ANEG_DONE 1
3696#define ANEG_TIMER_ENAB 2
3697#define ANEG_FAILED -1
3698
3699#define ANEG_STATE_SETTLE_TIME 10000
3700
3701static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3702 struct tg3_fiber_aneginfo *ap)
3703{
Matt Carlson5be73b42007-12-20 20:09:29 -08003704 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 unsigned long delta;
3706 u32 rx_cfg_reg;
3707 int ret;
3708
3709 if (ap->state == ANEG_STATE_UNKNOWN) {
3710 ap->rxconfig = 0;
3711 ap->link_time = 0;
3712 ap->cur_time = 0;
3713 ap->ability_match_cfg = 0;
3714 ap->ability_match_count = 0;
3715 ap->ability_match = 0;
3716 ap->idle_match = 0;
3717 ap->ack_match = 0;
3718 }
3719 ap->cur_time++;
3720
3721 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3722 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3723
3724 if (rx_cfg_reg != ap->ability_match_cfg) {
3725 ap->ability_match_cfg = rx_cfg_reg;
3726 ap->ability_match = 0;
3727 ap->ability_match_count = 0;
3728 } else {
3729 if (++ap->ability_match_count > 1) {
3730 ap->ability_match = 1;
3731 ap->ability_match_cfg = rx_cfg_reg;
3732 }
3733 }
3734 if (rx_cfg_reg & ANEG_CFG_ACK)
3735 ap->ack_match = 1;
3736 else
3737 ap->ack_match = 0;
3738
3739 ap->idle_match = 0;
3740 } else {
3741 ap->idle_match = 1;
3742 ap->ability_match_cfg = 0;
3743 ap->ability_match_count = 0;
3744 ap->ability_match = 0;
3745 ap->ack_match = 0;
3746
3747 rx_cfg_reg = 0;
3748 }
3749
3750 ap->rxconfig = rx_cfg_reg;
3751 ret = ANEG_OK;
3752
Matt Carlson33f401a2010-04-05 10:19:27 +00003753 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 case ANEG_STATE_UNKNOWN:
3755 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3756 ap->state = ANEG_STATE_AN_ENABLE;
3757
3758 /* fallthru */
3759 case ANEG_STATE_AN_ENABLE:
3760 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3761 if (ap->flags & MR_AN_ENABLE) {
3762 ap->link_time = 0;
3763 ap->cur_time = 0;
3764 ap->ability_match_cfg = 0;
3765 ap->ability_match_count = 0;
3766 ap->ability_match = 0;
3767 ap->idle_match = 0;
3768 ap->ack_match = 0;
3769
3770 ap->state = ANEG_STATE_RESTART_INIT;
3771 } else {
3772 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3773 }
3774 break;
3775
3776 case ANEG_STATE_RESTART_INIT:
3777 ap->link_time = ap->cur_time;
3778 ap->flags &= ~(MR_NP_LOADED);
3779 ap->txconfig = 0;
3780 tw32(MAC_TX_AUTO_NEG, 0);
3781 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3782 tw32_f(MAC_MODE, tp->mac_mode);
3783 udelay(40);
3784
3785 ret = ANEG_TIMER_ENAB;
3786 ap->state = ANEG_STATE_RESTART;
3787
3788 /* fallthru */
3789 case ANEG_STATE_RESTART:
3790 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00003791 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00003793 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 break;
3796
3797 case ANEG_STATE_DISABLE_LINK_OK:
3798 ret = ANEG_DONE;
3799 break;
3800
3801 case ANEG_STATE_ABILITY_DETECT_INIT:
3802 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003803 ap->txconfig = ANEG_CFG_FD;
3804 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3805 if (flowctrl & ADVERTISE_1000XPAUSE)
3806 ap->txconfig |= ANEG_CFG_PS1;
3807 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3808 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3810 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3811 tw32_f(MAC_MODE, tp->mac_mode);
3812 udelay(40);
3813
3814 ap->state = ANEG_STATE_ABILITY_DETECT;
3815 break;
3816
3817 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00003818 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 break;
3821
3822 case ANEG_STATE_ACK_DETECT_INIT:
3823 ap->txconfig |= ANEG_CFG_ACK;
3824 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3825 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3826 tw32_f(MAC_MODE, tp->mac_mode);
3827 udelay(40);
3828
3829 ap->state = ANEG_STATE_ACK_DETECT;
3830
3831 /* fallthru */
3832 case ANEG_STATE_ACK_DETECT:
3833 if (ap->ack_match != 0) {
3834 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3835 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3836 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3837 } else {
3838 ap->state = ANEG_STATE_AN_ENABLE;
3839 }
3840 } else if (ap->ability_match != 0 &&
3841 ap->rxconfig == 0) {
3842 ap->state = ANEG_STATE_AN_ENABLE;
3843 }
3844 break;
3845
3846 case ANEG_STATE_COMPLETE_ACK_INIT:
3847 if (ap->rxconfig & ANEG_CFG_INVAL) {
3848 ret = ANEG_FAILED;
3849 break;
3850 }
3851 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3852 MR_LP_ADV_HALF_DUPLEX |
3853 MR_LP_ADV_SYM_PAUSE |
3854 MR_LP_ADV_ASYM_PAUSE |
3855 MR_LP_ADV_REMOTE_FAULT1 |
3856 MR_LP_ADV_REMOTE_FAULT2 |
3857 MR_LP_ADV_NEXT_PAGE |
3858 MR_TOGGLE_RX |
3859 MR_NP_RX);
3860 if (ap->rxconfig & ANEG_CFG_FD)
3861 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3862 if (ap->rxconfig & ANEG_CFG_HD)
3863 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3864 if (ap->rxconfig & ANEG_CFG_PS1)
3865 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3866 if (ap->rxconfig & ANEG_CFG_PS2)
3867 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3868 if (ap->rxconfig & ANEG_CFG_RF1)
3869 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3870 if (ap->rxconfig & ANEG_CFG_RF2)
3871 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3872 if (ap->rxconfig & ANEG_CFG_NP)
3873 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3874
3875 ap->link_time = ap->cur_time;
3876
3877 ap->flags ^= (MR_TOGGLE_TX);
3878 if (ap->rxconfig & 0x0008)
3879 ap->flags |= MR_TOGGLE_RX;
3880 if (ap->rxconfig & ANEG_CFG_NP)
3881 ap->flags |= MR_NP_RX;
3882 ap->flags |= MR_PAGE_RX;
3883
3884 ap->state = ANEG_STATE_COMPLETE_ACK;
3885 ret = ANEG_TIMER_ENAB;
3886 break;
3887
3888 case ANEG_STATE_COMPLETE_ACK:
3889 if (ap->ability_match != 0 &&
3890 ap->rxconfig == 0) {
3891 ap->state = ANEG_STATE_AN_ENABLE;
3892 break;
3893 }
3894 delta = ap->cur_time - ap->link_time;
3895 if (delta > ANEG_STATE_SETTLE_TIME) {
3896 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3897 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3898 } else {
3899 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3900 !(ap->flags & MR_NP_RX)) {
3901 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3902 } else {
3903 ret = ANEG_FAILED;
3904 }
3905 }
3906 }
3907 break;
3908
3909 case ANEG_STATE_IDLE_DETECT_INIT:
3910 ap->link_time = ap->cur_time;
3911 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3912 tw32_f(MAC_MODE, tp->mac_mode);
3913 udelay(40);
3914
3915 ap->state = ANEG_STATE_IDLE_DETECT;
3916 ret = ANEG_TIMER_ENAB;
3917 break;
3918
3919 case ANEG_STATE_IDLE_DETECT:
3920 if (ap->ability_match != 0 &&
3921 ap->rxconfig == 0) {
3922 ap->state = ANEG_STATE_AN_ENABLE;
3923 break;
3924 }
3925 delta = ap->cur_time - ap->link_time;
3926 if (delta > ANEG_STATE_SETTLE_TIME) {
3927 /* XXX another gem from the Broadcom driver :( */
3928 ap->state = ANEG_STATE_LINK_OK;
3929 }
3930 break;
3931
3932 case ANEG_STATE_LINK_OK:
3933 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3934 ret = ANEG_DONE;
3935 break;
3936
3937 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3938 /* ??? unimplemented */
3939 break;
3940
3941 case ANEG_STATE_NEXT_PAGE_WAIT:
3942 /* ??? unimplemented */
3943 break;
3944
3945 default:
3946 ret = ANEG_FAILED;
3947 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949
3950 return ret;
3951}
3952
Matt Carlson5be73b42007-12-20 20:09:29 -08003953static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954{
3955 int res = 0;
3956 struct tg3_fiber_aneginfo aninfo;
3957 int status = ANEG_FAILED;
3958 unsigned int tick;
3959 u32 tmp;
3960
3961 tw32_f(MAC_TX_AUTO_NEG, 0);
3962
3963 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3964 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3965 udelay(40);
3966
3967 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3968 udelay(40);
3969
3970 memset(&aninfo, 0, sizeof(aninfo));
3971 aninfo.flags |= MR_AN_ENABLE;
3972 aninfo.state = ANEG_STATE_UNKNOWN;
3973 aninfo.cur_time = 0;
3974 tick = 0;
3975 while (++tick < 195000) {
3976 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3977 if (status == ANEG_DONE || status == ANEG_FAILED)
3978 break;
3979
3980 udelay(1);
3981 }
3982
3983 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3984 tw32_f(MAC_MODE, tp->mac_mode);
3985 udelay(40);
3986
Matt Carlson5be73b42007-12-20 20:09:29 -08003987 *txflags = aninfo.txconfig;
3988 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
3990 if (status == ANEG_DONE &&
3991 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3992 MR_LP_ADV_FULL_DUPLEX)))
3993 res = 1;
3994
3995 return res;
3996}
3997
3998static void tg3_init_bcm8002(struct tg3 *tp)
3999{
4000 u32 mac_status = tr32(MAC_STATUS);
4001 int i;
4002
4003 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004004 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 !(mac_status & MAC_STATUS_PCS_SYNCED))
4006 return;
4007
4008 /* Set PLL lock range. */
4009 tg3_writephy(tp, 0x16, 0x8007);
4010
4011 /* SW reset */
4012 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4013
4014 /* Wait for reset to complete. */
4015 /* XXX schedule_timeout() ... */
4016 for (i = 0; i < 500; i++)
4017 udelay(10);
4018
4019 /* Config mode; select PMA/Ch 1 regs. */
4020 tg3_writephy(tp, 0x10, 0x8411);
4021
4022 /* Enable auto-lock and comdet, select txclk for tx. */
4023 tg3_writephy(tp, 0x11, 0x0a10);
4024
4025 tg3_writephy(tp, 0x18, 0x00a0);
4026 tg3_writephy(tp, 0x16, 0x41ff);
4027
4028 /* Assert and deassert POR. */
4029 tg3_writephy(tp, 0x13, 0x0400);
4030 udelay(40);
4031 tg3_writephy(tp, 0x13, 0x0000);
4032
4033 tg3_writephy(tp, 0x11, 0x0a50);
4034 udelay(40);
4035 tg3_writephy(tp, 0x11, 0x0a10);
4036
4037 /* Wait for signal to stabilize */
4038 /* XXX schedule_timeout() ... */
4039 for (i = 0; i < 15000; i++)
4040 udelay(10);
4041
4042 /* Deselect the channel register so we can read the PHYID
4043 * later.
4044 */
4045 tg3_writephy(tp, 0x10, 0x8011);
4046}
4047
4048static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4049{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004050 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 u32 sg_dig_ctrl, sg_dig_status;
4052 u32 serdes_cfg, expected_sg_dig_ctrl;
4053 int workaround, port_a;
4054 int current_link_up;
4055
4056 serdes_cfg = 0;
4057 expected_sg_dig_ctrl = 0;
4058 workaround = 0;
4059 port_a = 1;
4060 current_link_up = 0;
4061
4062 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4063 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4064 workaround = 1;
4065 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4066 port_a = 0;
4067
4068 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4069 /* preserve bits 20-23 for voltage regulator */
4070 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4071 }
4072
4073 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4074
4075 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004076 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 if (workaround) {
4078 u32 val = serdes_cfg;
4079
4080 if (port_a)
4081 val |= 0xc010000;
4082 else
4083 val |= 0x4010000;
4084 tw32_f(MAC_SERDES_CFG, val);
4085 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004086
4087 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 }
4089 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4090 tg3_setup_flow_control(tp, 0, 0);
4091 current_link_up = 1;
4092 }
4093 goto out;
4094 }
4095
4096 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004097 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
Matt Carlson82cd3d12007-12-20 20:09:00 -08004099 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4100 if (flowctrl & ADVERTISE_1000XPAUSE)
4101 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4102 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4103 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
4105 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004106 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004107 tp->serdes_counter &&
4108 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4109 MAC_STATUS_RCVD_CFG)) ==
4110 MAC_STATUS_PCS_SYNCED)) {
4111 tp->serdes_counter--;
4112 current_link_up = 1;
4113 goto out;
4114 }
4115restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 if (workaround)
4117 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004118 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 udelay(5);
4120 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4121
Michael Chan3d3ebe72006-09-27 15:59:15 -07004122 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004123 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4125 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004126 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 mac_status = tr32(MAC_STATUS);
4128
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004129 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004131 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
Matt Carlson82cd3d12007-12-20 20:09:00 -08004133 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4134 local_adv |= ADVERTISE_1000XPAUSE;
4135 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4136 local_adv |= ADVERTISE_1000XPSE_ASYM;
4137
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004138 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004139 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004140 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004141 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
4143 tg3_setup_flow_control(tp, local_adv, remote_adv);
4144 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004145 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004146 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004147 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004148 if (tp->serdes_counter)
4149 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 else {
4151 if (workaround) {
4152 u32 val = serdes_cfg;
4153
4154 if (port_a)
4155 val |= 0xc010000;
4156 else
4157 val |= 0x4010000;
4158
4159 tw32_f(MAC_SERDES_CFG, val);
4160 }
4161
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004162 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 udelay(40);
4164
4165 /* Link parallel detection - link is up */
4166 /* only if we have PCS_SYNC and not */
4167 /* receiving config code words */
4168 mac_status = tr32(MAC_STATUS);
4169 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4170 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4171 tg3_setup_flow_control(tp, 0, 0);
4172 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004173 tp->phy_flags |=
4174 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004175 tp->serdes_counter =
4176 SERDES_PARALLEL_DET_TIMEOUT;
4177 } else
4178 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 }
4180 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004181 } else {
4182 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004183 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 }
4185
4186out:
4187 return current_link_up;
4188}
4189
4190static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4191{
4192 int current_link_up = 0;
4193
Michael Chan5cf64b82007-05-05 12:11:21 -07004194 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
4197 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004198 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004200
Matt Carlson5be73b42007-12-20 20:09:29 -08004201 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4202 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Matt Carlson5be73b42007-12-20 20:09:29 -08004204 if (txflags & ANEG_CFG_PS1)
4205 local_adv |= ADVERTISE_1000XPAUSE;
4206 if (txflags & ANEG_CFG_PS2)
4207 local_adv |= ADVERTISE_1000XPSE_ASYM;
4208
4209 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4210 remote_adv |= LPA_1000XPAUSE;
4211 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4212 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
4214 tg3_setup_flow_control(tp, local_adv, remote_adv);
4215
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 current_link_up = 1;
4217 }
4218 for (i = 0; i < 30; i++) {
4219 udelay(20);
4220 tw32_f(MAC_STATUS,
4221 (MAC_STATUS_SYNC_CHANGED |
4222 MAC_STATUS_CFG_CHANGED));
4223 udelay(40);
4224 if ((tr32(MAC_STATUS) &
4225 (MAC_STATUS_SYNC_CHANGED |
4226 MAC_STATUS_CFG_CHANGED)) == 0)
4227 break;
4228 }
4229
4230 mac_status = tr32(MAC_STATUS);
4231 if (current_link_up == 0 &&
4232 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4233 !(mac_status & MAC_STATUS_RCVD_CFG))
4234 current_link_up = 1;
4235 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004236 tg3_setup_flow_control(tp, 0, 0);
4237
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 /* Forcing 1000FD link up. */
4239 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
4241 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4242 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004243
4244 tw32_f(MAC_MODE, tp->mac_mode);
4245 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 }
4247
4248out:
4249 return current_link_up;
4250}
4251
4252static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4253{
4254 u32 orig_pause_cfg;
4255 u16 orig_active_speed;
4256 u8 orig_active_duplex;
4257 u32 mac_status;
4258 int current_link_up;
4259 int i;
4260
Matt Carlson8d018622007-12-20 20:05:44 -08004261 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 orig_active_speed = tp->link_config.active_speed;
4263 orig_active_duplex = tp->link_config.active_duplex;
4264
Joe Perches63c3a662011-04-26 08:12:10 +00004265 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004267 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 mac_status = tr32(MAC_STATUS);
4269 mac_status &= (MAC_STATUS_PCS_SYNCED |
4270 MAC_STATUS_SIGNAL_DET |
4271 MAC_STATUS_CFG_CHANGED |
4272 MAC_STATUS_RCVD_CFG);
4273 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4274 MAC_STATUS_SIGNAL_DET)) {
4275 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4276 MAC_STATUS_CFG_CHANGED));
4277 return 0;
4278 }
4279 }
4280
4281 tw32_f(MAC_TX_AUTO_NEG, 0);
4282
4283 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4284 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4285 tw32_f(MAC_MODE, tp->mac_mode);
4286 udelay(40);
4287
Matt Carlson79eb6902010-02-17 15:17:03 +00004288 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 tg3_init_bcm8002(tp);
4290
4291 /* Enable link change event even when serdes polling. */
4292 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4293 udelay(40);
4294
4295 current_link_up = 0;
4296 mac_status = tr32(MAC_STATUS);
4297
Joe Perches63c3a662011-04-26 08:12:10 +00004298 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4300 else
4301 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4302
Matt Carlson898a56f2009-08-28 14:02:40 +00004303 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004305 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
4307 for (i = 0; i < 100; i++) {
4308 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4309 MAC_STATUS_CFG_CHANGED));
4310 udelay(5);
4311 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004312 MAC_STATUS_CFG_CHANGED |
4313 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 break;
4315 }
4316
4317 mac_status = tr32(MAC_STATUS);
4318 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4319 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004320 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4321 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 tw32_f(MAC_MODE, (tp->mac_mode |
4323 MAC_MODE_SEND_CONFIGS));
4324 udelay(1);
4325 tw32_f(MAC_MODE, tp->mac_mode);
4326 }
4327 }
4328
4329 if (current_link_up == 1) {
4330 tp->link_config.active_speed = SPEED_1000;
4331 tp->link_config.active_duplex = DUPLEX_FULL;
4332 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4333 LED_CTRL_LNKLED_OVERRIDE |
4334 LED_CTRL_1000MBPS_ON));
4335 } else {
4336 tp->link_config.active_speed = SPEED_INVALID;
4337 tp->link_config.active_duplex = DUPLEX_INVALID;
4338 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4339 LED_CTRL_LNKLED_OVERRIDE |
4340 LED_CTRL_TRAFFIC_OVERRIDE));
4341 }
4342
4343 if (current_link_up != netif_carrier_ok(tp->dev)) {
4344 if (current_link_up)
4345 netif_carrier_on(tp->dev);
4346 else
4347 netif_carrier_off(tp->dev);
4348 tg3_link_report(tp);
4349 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004350 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 if (orig_pause_cfg != now_pause_cfg ||
4352 orig_active_speed != tp->link_config.active_speed ||
4353 orig_active_duplex != tp->link_config.active_duplex)
4354 tg3_link_report(tp);
4355 }
4356
4357 return 0;
4358}
4359
Michael Chan747e8f82005-07-25 12:33:22 -07004360static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4361{
4362 int current_link_up, err = 0;
4363 u32 bmsr, bmcr;
4364 u16 current_speed;
4365 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004366 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004367
4368 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4369 tw32_f(MAC_MODE, tp->mac_mode);
4370 udelay(40);
4371
4372 tw32(MAC_EVENT, 0);
4373
4374 tw32_f(MAC_STATUS,
4375 (MAC_STATUS_SYNC_CHANGED |
4376 MAC_STATUS_CFG_CHANGED |
4377 MAC_STATUS_MI_COMPLETION |
4378 MAC_STATUS_LNKSTATE_CHANGED));
4379 udelay(40);
4380
4381 if (force_reset)
4382 tg3_phy_reset(tp);
4383
4384 current_link_up = 0;
4385 current_speed = SPEED_INVALID;
4386 current_duplex = DUPLEX_INVALID;
4387
4388 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4389 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004390 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4391 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4392 bmsr |= BMSR_LSTATUS;
4393 else
4394 bmsr &= ~BMSR_LSTATUS;
4395 }
Michael Chan747e8f82005-07-25 12:33:22 -07004396
4397 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4398
4399 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004400 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004401 /* do nothing, just check for link up at the end */
4402 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4403 u32 adv, new_adv;
4404
4405 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4406 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4407 ADVERTISE_1000XPAUSE |
4408 ADVERTISE_1000XPSE_ASYM |
4409 ADVERTISE_SLCT);
4410
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004411 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004412
4413 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4414 new_adv |= ADVERTISE_1000XHALF;
4415 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4416 new_adv |= ADVERTISE_1000XFULL;
4417
4418 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4419 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4420 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4421 tg3_writephy(tp, MII_BMCR, bmcr);
4422
4423 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004424 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004425 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004426
4427 return err;
4428 }
4429 } else {
4430 u32 new_bmcr;
4431
4432 bmcr &= ~BMCR_SPEED1000;
4433 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4434
4435 if (tp->link_config.duplex == DUPLEX_FULL)
4436 new_bmcr |= BMCR_FULLDPLX;
4437
4438 if (new_bmcr != bmcr) {
4439 /* BMCR_SPEED1000 is a reserved bit that needs
4440 * to be set on write.
4441 */
4442 new_bmcr |= BMCR_SPEED1000;
4443
4444 /* Force a linkdown */
4445 if (netif_carrier_ok(tp->dev)) {
4446 u32 adv;
4447
4448 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4449 adv &= ~(ADVERTISE_1000XFULL |
4450 ADVERTISE_1000XHALF |
4451 ADVERTISE_SLCT);
4452 tg3_writephy(tp, MII_ADVERTISE, adv);
4453 tg3_writephy(tp, MII_BMCR, bmcr |
4454 BMCR_ANRESTART |
4455 BMCR_ANENABLE);
4456 udelay(10);
4457 netif_carrier_off(tp->dev);
4458 }
4459 tg3_writephy(tp, MII_BMCR, new_bmcr);
4460 bmcr = new_bmcr;
4461 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4462 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004463 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4464 ASIC_REV_5714) {
4465 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4466 bmsr |= BMSR_LSTATUS;
4467 else
4468 bmsr &= ~BMSR_LSTATUS;
4469 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004470 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004471 }
4472 }
4473
4474 if (bmsr & BMSR_LSTATUS) {
4475 current_speed = SPEED_1000;
4476 current_link_up = 1;
4477 if (bmcr & BMCR_FULLDPLX)
4478 current_duplex = DUPLEX_FULL;
4479 else
4480 current_duplex = DUPLEX_HALF;
4481
Matt Carlsonef167e22007-12-20 20:10:01 -08004482 local_adv = 0;
4483 remote_adv = 0;
4484
Michael Chan747e8f82005-07-25 12:33:22 -07004485 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004486 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004487
4488 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4489 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4490 common = local_adv & remote_adv;
4491 if (common & (ADVERTISE_1000XHALF |
4492 ADVERTISE_1000XFULL)) {
4493 if (common & ADVERTISE_1000XFULL)
4494 current_duplex = DUPLEX_FULL;
4495 else
4496 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004497 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004498 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004499 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004500 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004501 }
Michael Chan747e8f82005-07-25 12:33:22 -07004502 }
4503 }
4504
Matt Carlsonef167e22007-12-20 20:10:01 -08004505 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4506 tg3_setup_flow_control(tp, local_adv, remote_adv);
4507
Michael Chan747e8f82005-07-25 12:33:22 -07004508 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4509 if (tp->link_config.active_duplex == DUPLEX_HALF)
4510 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4511
4512 tw32_f(MAC_MODE, tp->mac_mode);
4513 udelay(40);
4514
4515 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4516
4517 tp->link_config.active_speed = current_speed;
4518 tp->link_config.active_duplex = current_duplex;
4519
4520 if (current_link_up != netif_carrier_ok(tp->dev)) {
4521 if (current_link_up)
4522 netif_carrier_on(tp->dev);
4523 else {
4524 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004525 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004526 }
4527 tg3_link_report(tp);
4528 }
4529 return err;
4530}
4531
4532static void tg3_serdes_parallel_detect(struct tg3 *tp)
4533{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004534 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004535 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004536 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004537 return;
4538 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004539
Michael Chan747e8f82005-07-25 12:33:22 -07004540 if (!netif_carrier_ok(tp->dev) &&
4541 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4542 u32 bmcr;
4543
4544 tg3_readphy(tp, MII_BMCR, &bmcr);
4545 if (bmcr & BMCR_ANENABLE) {
4546 u32 phy1, phy2;
4547
4548 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004549 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4550 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004551
4552 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004553 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4554 MII_TG3_DSP_EXP1_INT_STAT);
4555 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4556 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004557
4558 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4559 /* We have signal detect and not receiving
4560 * config code words, link is up by parallel
4561 * detection.
4562 */
4563
4564 bmcr &= ~BMCR_ANENABLE;
4565 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4566 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004567 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004568 }
4569 }
Matt Carlson859a5882010-04-05 10:19:28 +00004570 } else if (netif_carrier_ok(tp->dev) &&
4571 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004572 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004573 u32 phy2;
4574
4575 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004576 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4577 MII_TG3_DSP_EXP1_INT_STAT);
4578 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004579 if (phy2 & 0x20) {
4580 u32 bmcr;
4581
4582 /* Config code words received, turn on autoneg. */
4583 tg3_readphy(tp, MII_BMCR, &bmcr);
4584 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4585
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004586 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004587
4588 }
4589 }
4590}
4591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4593{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004594 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 int err;
4596
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004597 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004599 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004600 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00004601 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004604 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004605 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004606
4607 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4608 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4609 scale = 65;
4610 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4611 scale = 6;
4612 else
4613 scale = 12;
4614
4615 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4616 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4617 tw32(GRC_MISC_CFG, val);
4618 }
4619
Matt Carlsonf2096f92011-04-05 14:22:48 +00004620 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4621 (6 << TX_LENGTHS_IPG_SHIFT);
4622 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4623 val |= tr32(MAC_TX_LENGTHS) &
4624 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4625 TX_LENGTHS_CNT_DWN_VAL_MSK);
4626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 if (tp->link_config.active_speed == SPEED_1000 &&
4628 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004629 tw32(MAC_TX_LENGTHS, val |
4630 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004632 tw32(MAC_TX_LENGTHS, val |
4633 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
Joe Perches63c3a662011-04-26 08:12:10 +00004635 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 if (netif_carrier_ok(tp->dev)) {
4637 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004638 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 } else {
4640 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4641 }
4642 }
4643
Joe Perches63c3a662011-04-26 08:12:10 +00004644 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004645 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004646 if (!netif_carrier_ok(tp->dev))
4647 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4648 tp->pwrmgmt_thresh;
4649 else
4650 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4651 tw32(PCIE_PWR_MGMT_THRESH, val);
4652 }
4653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 return err;
4655}
4656
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004657static inline int tg3_irq_sync(struct tg3 *tp)
4658{
4659 return tp->irq_sync;
4660}
4661
Matt Carlson97bd8e42011-04-13 11:05:04 +00004662static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4663{
4664 int i;
4665
4666 dst = (u32 *)((u8 *)dst + off);
4667 for (i = 0; i < len; i += sizeof(u32))
4668 *dst++ = tr32(off + i);
4669}
4670
4671static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4672{
4673 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4674 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4675 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4676 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4677 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4678 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4679 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4680 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4681 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4682 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4683 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4684 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4685 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4686 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4687 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4688 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4689 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4690 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4691 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4692
Joe Perches63c3a662011-04-26 08:12:10 +00004693 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004694 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4695
4696 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4697 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4698 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4699 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4700 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4701 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4702 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4703 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4704
Joe Perches63c3a662011-04-26 08:12:10 +00004705 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004706 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4707 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4708 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4709 }
4710
4711 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4712 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4713 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4714 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4715 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4716
Joe Perches63c3a662011-04-26 08:12:10 +00004717 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004718 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4719}
4720
4721static void tg3_dump_state(struct tg3 *tp)
4722{
4723 int i;
4724 u32 *regs;
4725
4726 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4727 if (!regs) {
4728 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4729 return;
4730 }
4731
Joe Perches63c3a662011-04-26 08:12:10 +00004732 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004733 /* Read up to but not including private PCI registers */
4734 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4735 regs[i / sizeof(u32)] = tr32(i);
4736 } else
4737 tg3_dump_legacy_regs(tp, regs);
4738
4739 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4740 if (!regs[i + 0] && !regs[i + 1] &&
4741 !regs[i + 2] && !regs[i + 3])
4742 continue;
4743
4744 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4745 i * 4,
4746 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4747 }
4748
4749 kfree(regs);
4750
4751 for (i = 0; i < tp->irq_cnt; i++) {
4752 struct tg3_napi *tnapi = &tp->napi[i];
4753
4754 /* SW status block */
4755 netdev_err(tp->dev,
4756 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4757 i,
4758 tnapi->hw_status->status,
4759 tnapi->hw_status->status_tag,
4760 tnapi->hw_status->rx_jumbo_consumer,
4761 tnapi->hw_status->rx_consumer,
4762 tnapi->hw_status->rx_mini_consumer,
4763 tnapi->hw_status->idx[0].rx_producer,
4764 tnapi->hw_status->idx[0].tx_consumer);
4765
4766 netdev_err(tp->dev,
4767 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4768 i,
4769 tnapi->last_tag, tnapi->last_irq_tag,
4770 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4771 tnapi->rx_rcb_ptr,
4772 tnapi->prodring.rx_std_prod_idx,
4773 tnapi->prodring.rx_std_cons_idx,
4774 tnapi->prodring.rx_jmb_prod_idx,
4775 tnapi->prodring.rx_jmb_cons_idx);
4776 }
4777}
4778
Michael Chandf3e6542006-05-26 17:48:07 -07004779/* This is called whenever we suspect that the system chipset is re-
4780 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4781 * is bogus tx completions. We try to recover by setting the
4782 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4783 * in the workqueue.
4784 */
4785static void tg3_tx_recover(struct tg3 *tp)
4786{
Joe Perches63c3a662011-04-26 08:12:10 +00004787 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07004788 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4789
Matt Carlson5129c3a2010-04-05 10:19:23 +00004790 netdev_warn(tp->dev,
4791 "The system may be re-ordering memory-mapped I/O "
4792 "cycles to the network device, attempting to recover. "
4793 "Please report the problem to the driver maintainer "
4794 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004795
4796 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00004797 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07004798 spin_unlock(&tp->lock);
4799}
4800
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004801static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004802{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004803 /* Tell compiler to fetch tx indices from memory. */
4804 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004805 return tnapi->tx_pending -
4806 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004807}
4808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809/* Tigon3 never reports partial packet sends. So we do not
4810 * need special logic to handle SKBs that have not had all
4811 * of their frags sent yet, like SunGEM does.
4812 */
Matt Carlson17375d22009-08-28 14:02:18 +00004813static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814{
Matt Carlson17375d22009-08-28 14:02:18 +00004815 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004816 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004817 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004818 struct netdev_queue *txq;
4819 int index = tnapi - tp->napi;
4820
Joe Perches63c3a662011-04-26 08:12:10 +00004821 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004822 index--;
4823
4824 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
4826 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00004827 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004829 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
Michael Chandf3e6542006-05-26 17:48:07 -07004831 if (unlikely(skb == NULL)) {
4832 tg3_tx_recover(tp);
4833 return;
4834 }
4835
Alexander Duyckf4188d82009-12-02 16:48:38 +00004836 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004837 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004838 skb_headlen(skb),
4839 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
4841 ri->skb = NULL;
4842
Matt Carlsone01ee142011-07-27 14:20:50 +00004843 while (ri->fragmented) {
4844 ri->fragmented = false;
4845 sw_idx = NEXT_TX(sw_idx);
4846 ri = &tnapi->tx_buffers[sw_idx];
4847 }
4848
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 sw_idx = NEXT_TX(sw_idx);
4850
4851 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004852 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004853 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4854 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004855
4856 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004857 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004858 skb_shinfo(skb)->frags[i].size,
4859 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00004860
4861 while (ri->fragmented) {
4862 ri->fragmented = false;
4863 sw_idx = NEXT_TX(sw_idx);
4864 ri = &tnapi->tx_buffers[sw_idx];
4865 }
4866
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 sw_idx = NEXT_TX(sw_idx);
4868 }
4869
David S. Millerf47c11e2005-06-24 20:18:35 -07004870 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004871
4872 if (unlikely(tx_bug)) {
4873 tg3_tx_recover(tp);
4874 return;
4875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 }
4877
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004878 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
Michael Chan1b2a7202006-08-07 21:46:02 -07004880 /* Need to make the tx_cons update visible to tg3_start_xmit()
4881 * before checking for netif_queue_stopped(). Without the
4882 * memory barrier, there is a small possibility that tg3_start_xmit()
4883 * will miss it and cause the queue to be stopped forever.
4884 */
4885 smp_mb();
4886
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004887 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004888 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004889 __netif_tx_lock(txq, smp_processor_id());
4890 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004891 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004892 netif_tx_wake_queue(txq);
4893 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895}
4896
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004897static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4898{
4899 if (!ri->skb)
4900 return;
4901
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004902 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004903 map_sz, PCI_DMA_FROMDEVICE);
4904 dev_kfree_skb_any(ri->skb);
4905 ri->skb = NULL;
4906}
4907
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908/* Returns size of skb allocated or < 0 on error.
4909 *
4910 * We only need to fill in the address because the other members
4911 * of the RX descriptor are invariant, see tg3_init_rings.
4912 *
4913 * Note the purposeful assymetry of cpu vs. chip accesses. For
4914 * posting buffers we only dirty the first cache line of the RX
4915 * descriptor (containing the address). Whereas for the RX status
4916 * buffers the cpu only reads the last cacheline of the RX descriptor
4917 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4918 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004919static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004920 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921{
4922 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004923 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 struct sk_buff *skb;
4925 dma_addr_t mapping;
4926 int skb_size, dest_idx;
4927
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 switch (opaque_key) {
4929 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004930 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004931 desc = &tpr->rx_std[dest_idx];
4932 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004933 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 break;
4935
4936 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004937 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004938 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004939 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004940 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 break;
4942
4943 default:
4944 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946
4947 /* Do not overwrite any of the map or rp information
4948 * until we are sure we can commit to a new buffer.
4949 *
4950 * Callers depend upon this behavior and assume that
4951 * we leave everything unchanged if we fail.
4952 */
Matt Carlson287be122009-08-28 13:58:46 +00004953 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 if (skb == NULL)
4955 return -ENOMEM;
4956
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 skb_reserve(skb, tp->rx_offset);
4958
Matt Carlson287be122009-08-28 13:58:46 +00004959 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004961 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4962 dev_kfree_skb(skb);
4963 return -EIO;
4964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
4966 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004967 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 desc->addr_hi = ((u64)mapping >> 32);
4970 desc->addr_lo = ((u64)mapping & 0xffffffff);
4971
4972 return skb_size;
4973}
4974
4975/* We only need to move over in the address because the other
4976 * members of the RX descriptor are invariant. See notes above
4977 * tg3_alloc_rx_skb for full details.
4978 */
Matt Carlsona3896162009-11-13 13:03:44 +00004979static void tg3_recycle_rx(struct tg3_napi *tnapi,
4980 struct tg3_rx_prodring_set *dpr,
4981 u32 opaque_key, int src_idx,
4982 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983{
Matt Carlson17375d22009-08-28 14:02:18 +00004984 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4986 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004987 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004988 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
4990 switch (opaque_key) {
4991 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004992 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004993 dest_desc = &dpr->rx_std[dest_idx];
4994 dest_map = &dpr->rx_std_buffers[dest_idx];
4995 src_desc = &spr->rx_std[src_idx];
4996 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 break;
4998
4999 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005000 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005001 dest_desc = &dpr->rx_jmb[dest_idx].std;
5002 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5003 src_desc = &spr->rx_jmb[src_idx].std;
5004 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 break;
5006
5007 default:
5008 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010
5011 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005012 dma_unmap_addr_set(dest_map, mapping,
5013 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 dest_desc->addr_hi = src_desc->addr_hi;
5015 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005016
5017 /* Ensure that the update to the skb happens after the physical
5018 * addresses have been transferred to the new BD location.
5019 */
5020 smp_wmb();
5021
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 src_map->skb = NULL;
5023}
5024
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025/* The RX ring scheme is composed of multiple rings which post fresh
5026 * buffers to the chip, and one special ring the chip uses to report
5027 * status back to the host.
5028 *
5029 * The special ring reports the status of received packets to the
5030 * host. The chip does not write into the original descriptor the
5031 * RX buffer was obtained from. The chip simply takes the original
5032 * descriptor as provided by the host, updates the status and length
5033 * field, then writes this into the next status ring entry.
5034 *
5035 * Each ring the host uses to post buffers to the chip is described
5036 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5037 * it is first placed into the on-chip ram. When the packet's length
5038 * is known, it walks down the TG3_BDINFO entries to select the ring.
5039 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5040 * which is within the range of the new packet's length is chosen.
5041 *
5042 * The "separate ring for rx status" scheme may sound queer, but it makes
5043 * sense from a cache coherency perspective. If only the host writes
5044 * to the buffer post rings, and only the chip writes to the rx status
5045 * rings, then cache lines never move beyond shared-modified state.
5046 * If both the host and chip were to write into the same ring, cache line
5047 * eviction could occur since both entities want it in an exclusive state.
5048 */
Matt Carlson17375d22009-08-28 14:02:18 +00005049static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050{
Matt Carlson17375d22009-08-28 14:02:18 +00005051 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005052 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005053 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005054 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005055 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005057 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005059 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 /*
5061 * We need to order the read of hw_idx and the read of
5062 * the opaque cookie.
5063 */
5064 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 work_mask = 0;
5066 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005067 std_prod_idx = tpr->rx_std_prod_idx;
5068 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005070 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005071 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 unsigned int len;
5073 struct sk_buff *skb;
5074 dma_addr_t dma_addr;
5075 u32 opaque_key, desc_idx, *post_ptr;
5076
5077 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5078 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5079 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005080 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005081 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00005082 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00005083 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005084 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005086 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005087 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00005088 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00005089 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005090 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092
5093 work_mask |= opaque_key;
5094
5095 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5096 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5097 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005098 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 desc_idx, *post_ptr);
5100 drop_it_no_recycle:
5101 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005102 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 goto next_pkt;
5104 }
5105
Matt Carlsonad829262008-11-21 17:16:16 -08005106 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5107 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
Matt Carlsond2757fc2010-04-12 06:58:27 +00005109 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 int skb_size;
5111
Matt Carlson86b21e52009-11-13 13:03:45 +00005112 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005113 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 if (skb_size < 0)
5115 goto drop_it;
5116
Matt Carlson287be122009-08-28 13:58:46 +00005117 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 PCI_DMA_FROMDEVICE);
5119
Matt Carlson61e800c2010-02-17 15:16:54 +00005120 /* Ensure that the update to the skb happens
5121 * after the usage of the old DMA mapping.
5122 */
5123 smp_wmb();
5124
5125 ri->skb = NULL;
5126
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 skb_put(skb, len);
5128 } else {
5129 struct sk_buff *copy_skb;
5130
Matt Carlsona3896162009-11-13 13:03:44 +00005131 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 desc_idx, *post_ptr);
5133
Matt Carlsonbf933c82011-01-25 15:58:49 +00005134 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00005135 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 if (copy_skb == NULL)
5137 goto drop_it_no_recycle;
5138
Matt Carlsonbf933c82011-01-25 15:58:49 +00005139 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 skb_put(copy_skb, len);
5141 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03005142 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
5144
5145 /* We'll reuse the original ring buffer. */
5146 skb = copy_skb;
5147 }
5148
Michał Mirosławdc668912011-04-07 03:35:07 +00005149 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5151 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5152 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5153 skb->ip_summed = CHECKSUM_UNNECESSARY;
5154 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005155 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156
5157 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005158
5159 if (len > (tp->dev->mtu + ETH_HLEN) &&
5160 skb->protocol != htons(ETH_P_8021Q)) {
5161 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005162 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005163 }
5164
Matt Carlson9dc7a112010-04-12 06:58:28 +00005165 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005166 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5167 __vlan_hwaccel_put_tag(skb,
5168 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005169
Matt Carlsonbf933c82011-01-25 15:58:49 +00005170 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 received++;
5173 budget--;
5174
5175next_pkt:
5176 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005177
5178 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005179 tpr->rx_std_prod_idx = std_prod_idx &
5180 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005181 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5182 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005183 work_mask &= ~RXD_OPAQUE_RING_STD;
5184 rx_std_posted = 0;
5185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005187 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005188 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005189
5190 /* Refresh hw_idx to see if there is new work */
5191 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005192 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005193 rmb();
5194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 }
5196
5197 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005198 tnapi->rx_rcb_ptr = sw_idx;
5199 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200
5201 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005202 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005203 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005204 tpr->rx_std_prod_idx = std_prod_idx &
5205 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005206 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5207 tpr->rx_std_prod_idx);
5208 }
5209 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005210 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5211 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005212 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5213 tpr->rx_jmb_prod_idx);
5214 }
5215 mmiowb();
5216 } else if (work_mask) {
5217 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5218 * updated before the producer indices can be updated.
5219 */
5220 smp_wmb();
5221
Matt Carlson2c49a442010-09-30 10:34:35 +00005222 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5223 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005224
Matt Carlsone4af1af2010-02-12 14:47:05 +00005225 if (tnapi != &tp->napi[1])
5226 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228
5229 return received;
5230}
5231
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005232static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005235 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005236 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5237
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 if (sblk->status & SD_STATUS_LINK_CHG) {
5239 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005240 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005241 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005242 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005243 tw32_f(MAC_STATUS,
5244 (MAC_STATUS_SYNC_CHANGED |
5245 MAC_STATUS_CFG_CHANGED |
5246 MAC_STATUS_MI_COMPLETION |
5247 MAC_STATUS_LNKSTATE_CHANGED));
5248 udelay(40);
5249 } else
5250 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005251 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 }
5253 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005254}
5255
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005256static int tg3_rx_prodring_xfer(struct tg3 *tp,
5257 struct tg3_rx_prodring_set *dpr,
5258 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005259{
5260 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005261 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005262
5263 while (1) {
5264 src_prod_idx = spr->rx_std_prod_idx;
5265
5266 /* Make sure updates to the rx_std_buffers[] entries and the
5267 * standard producer index are seen in the correct order.
5268 */
5269 smp_rmb();
5270
5271 if (spr->rx_std_cons_idx == src_prod_idx)
5272 break;
5273
5274 if (spr->rx_std_cons_idx < src_prod_idx)
5275 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5276 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005277 cpycnt = tp->rx_std_ring_mask + 1 -
5278 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005279
Matt Carlson2c49a442010-09-30 10:34:35 +00005280 cpycnt = min(cpycnt,
5281 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005282
5283 si = spr->rx_std_cons_idx;
5284 di = dpr->rx_std_prod_idx;
5285
Matt Carlsone92967b2010-02-12 14:47:06 +00005286 for (i = di; i < di + cpycnt; i++) {
5287 if (dpr->rx_std_buffers[i].skb) {
5288 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005289 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005290 break;
5291 }
5292 }
5293
5294 if (!cpycnt)
5295 break;
5296
5297 /* Ensure that updates to the rx_std_buffers ring and the
5298 * shadowed hardware producer ring from tg3_recycle_skb() are
5299 * ordered correctly WRT the skb check above.
5300 */
5301 smp_rmb();
5302
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005303 memcpy(&dpr->rx_std_buffers[di],
5304 &spr->rx_std_buffers[si],
5305 cpycnt * sizeof(struct ring_info));
5306
5307 for (i = 0; i < cpycnt; i++, di++, si++) {
5308 struct tg3_rx_buffer_desc *sbd, *dbd;
5309 sbd = &spr->rx_std[si];
5310 dbd = &dpr->rx_std[di];
5311 dbd->addr_hi = sbd->addr_hi;
5312 dbd->addr_lo = sbd->addr_lo;
5313 }
5314
Matt Carlson2c49a442010-09-30 10:34:35 +00005315 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5316 tp->rx_std_ring_mask;
5317 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5318 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005319 }
5320
5321 while (1) {
5322 src_prod_idx = spr->rx_jmb_prod_idx;
5323
5324 /* Make sure updates to the rx_jmb_buffers[] entries and
5325 * the jumbo producer index are seen in the correct order.
5326 */
5327 smp_rmb();
5328
5329 if (spr->rx_jmb_cons_idx == src_prod_idx)
5330 break;
5331
5332 if (spr->rx_jmb_cons_idx < src_prod_idx)
5333 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5334 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005335 cpycnt = tp->rx_jmb_ring_mask + 1 -
5336 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005337
5338 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005339 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005340
5341 si = spr->rx_jmb_cons_idx;
5342 di = dpr->rx_jmb_prod_idx;
5343
Matt Carlsone92967b2010-02-12 14:47:06 +00005344 for (i = di; i < di + cpycnt; i++) {
5345 if (dpr->rx_jmb_buffers[i].skb) {
5346 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005347 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005348 break;
5349 }
5350 }
5351
5352 if (!cpycnt)
5353 break;
5354
5355 /* Ensure that updates to the rx_jmb_buffers ring and the
5356 * shadowed hardware producer ring from tg3_recycle_skb() are
5357 * ordered correctly WRT the skb check above.
5358 */
5359 smp_rmb();
5360
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005361 memcpy(&dpr->rx_jmb_buffers[di],
5362 &spr->rx_jmb_buffers[si],
5363 cpycnt * sizeof(struct ring_info));
5364
5365 for (i = 0; i < cpycnt; i++, di++, si++) {
5366 struct tg3_rx_buffer_desc *sbd, *dbd;
5367 sbd = &spr->rx_jmb[si].std;
5368 dbd = &dpr->rx_jmb[di].std;
5369 dbd->addr_hi = sbd->addr_hi;
5370 dbd->addr_lo = sbd->addr_lo;
5371 }
5372
Matt Carlson2c49a442010-09-30 10:34:35 +00005373 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5374 tp->rx_jmb_ring_mask;
5375 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5376 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005377 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005378
5379 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005380}
5381
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005382static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5383{
5384 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
5386 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005387 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005388 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005389 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005390 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 }
5392
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 /* run RX thread, within the bounds set by NAPI.
5394 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005395 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005397 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005398 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
Joe Perches63c3a662011-04-26 08:12:10 +00005400 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005401 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005402 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005403 u32 std_prod_idx = dpr->rx_std_prod_idx;
5404 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005405
Matt Carlsone4af1af2010-02-12 14:47:05 +00005406 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005407 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005408 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005409
5410 wmb();
5411
Matt Carlsone4af1af2010-02-12 14:47:05 +00005412 if (std_prod_idx != dpr->rx_std_prod_idx)
5413 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5414 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005415
Matt Carlsone4af1af2010-02-12 14:47:05 +00005416 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5417 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5418 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005419
5420 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005421
5422 if (err)
5423 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005424 }
5425
David S. Miller6f535762007-10-11 18:08:29 -07005426 return work_done;
5427}
David S. Millerf7383c22005-05-18 22:50:53 -07005428
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005429static int tg3_poll_msix(struct napi_struct *napi, int budget)
5430{
5431 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5432 struct tg3 *tp = tnapi->tp;
5433 int work_done = 0;
5434 struct tg3_hw_status *sblk = tnapi->hw_status;
5435
5436 while (1) {
5437 work_done = tg3_poll_work(tnapi, work_done, budget);
5438
Joe Perches63c3a662011-04-26 08:12:10 +00005439 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005440 goto tx_recovery;
5441
5442 if (unlikely(work_done >= budget))
5443 break;
5444
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005445 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005446 * to tell the hw how much work has been processed,
5447 * so we must read it before checking for more work.
5448 */
5449 tnapi->last_tag = sblk->status_tag;
5450 tnapi->last_irq_tag = tnapi->last_tag;
5451 rmb();
5452
5453 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005454 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5455 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005456 napi_complete(napi);
5457 /* Reenable interrupts. */
5458 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5459 mmiowb();
5460 break;
5461 }
5462 }
5463
5464 return work_done;
5465
5466tx_recovery:
5467 /* work_done is guaranteed to be less than budget. */
5468 napi_complete(napi);
5469 schedule_work(&tp->reset_task);
5470 return work_done;
5471}
5472
Matt Carlsone64de4e2011-04-13 11:05:05 +00005473static void tg3_process_error(struct tg3 *tp)
5474{
5475 u32 val;
5476 bool real_error = false;
5477
Joe Perches63c3a662011-04-26 08:12:10 +00005478 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005479 return;
5480
5481 /* Check Flow Attention register */
5482 val = tr32(HOSTCC_FLOW_ATTN);
5483 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5484 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5485 real_error = true;
5486 }
5487
5488 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5489 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5490 real_error = true;
5491 }
5492
5493 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5494 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5495 real_error = true;
5496 }
5497
5498 if (!real_error)
5499 return;
5500
5501 tg3_dump_state(tp);
5502
Joe Perches63c3a662011-04-26 08:12:10 +00005503 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005504 schedule_work(&tp->reset_task);
5505}
5506
David S. Miller6f535762007-10-11 18:08:29 -07005507static int tg3_poll(struct napi_struct *napi, int budget)
5508{
Matt Carlson8ef04422009-08-28 14:01:37 +00005509 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5510 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005511 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005512 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005513
5514 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005515 if (sblk->status & SD_STATUS_ERROR)
5516 tg3_process_error(tp);
5517
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005518 tg3_poll_link(tp);
5519
Matt Carlson17375d22009-08-28 14:02:18 +00005520 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005521
Joe Perches63c3a662011-04-26 08:12:10 +00005522 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07005523 goto tx_recovery;
5524
5525 if (unlikely(work_done >= budget))
5526 break;
5527
Joe Perches63c3a662011-04-26 08:12:10 +00005528 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00005529 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005530 * to tell the hw how much work has been processed,
5531 * so we must read it before checking for more work.
5532 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005533 tnapi->last_tag = sblk->status_tag;
5534 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005535 rmb();
5536 } else
5537 sblk->status &= ~SD_STATUS_UPDATED;
5538
Matt Carlson17375d22009-08-28 14:02:18 +00005539 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005540 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005541 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005542 break;
5543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 }
5545
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005546 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005547
5548tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005549 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005550 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005551 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005552 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553}
5554
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005555static void tg3_napi_disable(struct tg3 *tp)
5556{
5557 int i;
5558
5559 for (i = tp->irq_cnt - 1; i >= 0; i--)
5560 napi_disable(&tp->napi[i].napi);
5561}
5562
5563static void tg3_napi_enable(struct tg3 *tp)
5564{
5565 int i;
5566
5567 for (i = 0; i < tp->irq_cnt; i++)
5568 napi_enable(&tp->napi[i].napi);
5569}
5570
5571static void tg3_napi_init(struct tg3 *tp)
5572{
5573 int i;
5574
5575 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5576 for (i = 1; i < tp->irq_cnt; i++)
5577 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5578}
5579
5580static void tg3_napi_fini(struct tg3 *tp)
5581{
5582 int i;
5583
5584 for (i = 0; i < tp->irq_cnt; i++)
5585 netif_napi_del(&tp->napi[i].napi);
5586}
5587
5588static inline void tg3_netif_stop(struct tg3 *tp)
5589{
5590 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5591 tg3_napi_disable(tp);
5592 netif_tx_disable(tp->dev);
5593}
5594
5595static inline void tg3_netif_start(struct tg3 *tp)
5596{
5597 /* NOTE: unconditional netif_tx_wake_all_queues is only
5598 * appropriate so long as all callers are assured to
5599 * have free tx slots (such as after tg3_init_hw)
5600 */
5601 netif_tx_wake_all_queues(tp->dev);
5602
5603 tg3_napi_enable(tp);
5604 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5605 tg3_enable_ints(tp);
5606}
5607
David S. Millerf47c11e2005-06-24 20:18:35 -07005608static void tg3_irq_quiesce(struct tg3 *tp)
5609{
Matt Carlson4f125f42009-09-01 12:55:02 +00005610 int i;
5611
David S. Millerf47c11e2005-06-24 20:18:35 -07005612 BUG_ON(tp->irq_sync);
5613
5614 tp->irq_sync = 1;
5615 smp_mb();
5616
Matt Carlson4f125f42009-09-01 12:55:02 +00005617 for (i = 0; i < tp->irq_cnt; i++)
5618 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005619}
5620
David S. Millerf47c11e2005-06-24 20:18:35 -07005621/* Fully shutdown all tg3 driver activity elsewhere in the system.
5622 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5623 * with as well. Most of the time, this is not necessary except when
5624 * shutting down the device.
5625 */
5626static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5627{
Michael Chan46966542007-07-11 19:47:19 -07005628 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005629 if (irq_sync)
5630 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005631}
5632
5633static inline void tg3_full_unlock(struct tg3 *tp)
5634{
David S. Millerf47c11e2005-06-24 20:18:35 -07005635 spin_unlock_bh(&tp->lock);
5636}
5637
Michael Chanfcfa0a32006-03-20 22:28:41 -08005638/* One-shot MSI handler - Chip automatically disables interrupt
5639 * after sending MSI so driver doesn't have to do it.
5640 */
David Howells7d12e782006-10-05 14:55:46 +01005641static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005642{
Matt Carlson09943a12009-08-28 14:01:57 +00005643 struct tg3_napi *tnapi = dev_id;
5644 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005645
Matt Carlson898a56f2009-08-28 14:02:40 +00005646 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005647 if (tnapi->rx_rcb)
5648 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005649
5650 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005651 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005652
5653 return IRQ_HANDLED;
5654}
5655
Michael Chan88b06bc2005-04-21 17:13:25 -07005656/* MSI ISR - No need to check for interrupt sharing and no need to
5657 * flush status block and interrupt mailbox. PCI ordering rules
5658 * guarantee that MSI will arrive after the status block.
5659 */
David Howells7d12e782006-10-05 14:55:46 +01005660static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07005661{
Matt Carlson09943a12009-08-28 14:01:57 +00005662 struct tg3_napi *tnapi = dev_id;
5663 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07005664
Matt Carlson898a56f2009-08-28 14:02:40 +00005665 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005666 if (tnapi->rx_rcb)
5667 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07005668 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005669 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07005670 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005671 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07005672 * NIC to stop sending us irqs, engaging "in-intr-handler"
5673 * event coalescing.
5674 */
5675 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005676 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005677 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005678
Michael Chan88b06bc2005-04-21 17:13:25 -07005679 return IRQ_RETVAL(1);
5680}
5681
David Howells7d12e782006-10-05 14:55:46 +01005682static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683{
Matt Carlson09943a12009-08-28 14:01:57 +00005684 struct tg3_napi *tnapi = dev_id;
5685 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005686 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 unsigned int handled = 1;
5688
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 /* In INTx mode, it is possible for the interrupt to arrive at
5690 * the CPU before the status block posted prior to the interrupt.
5691 * Reading the PCI State register will confirm whether the
5692 * interrupt is ours and will flush the status block.
5693 */
Michael Chand18edcb2007-03-24 20:57:11 -07005694 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00005695 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005696 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5697 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005698 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005699 }
Michael Chand18edcb2007-03-24 20:57:11 -07005700 }
5701
5702 /*
5703 * Writing any value to intr-mbox-0 clears PCI INTA# and
5704 * chip-internal interrupt pending events.
5705 * Writing non-zero to intr-mbox-0 additional tells the
5706 * NIC to stop sending us irqs, engaging "in-intr-handler"
5707 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005708 *
5709 * Flush the mailbox to de-assert the IRQ immediately to prevent
5710 * spurious interrupts. The flush impacts performance but
5711 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005712 */
Michael Chanc04cb342007-05-07 00:26:15 -07005713 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005714 if (tg3_irq_sync(tp))
5715 goto out;
5716 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005717 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005718 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005719 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005720 } else {
5721 /* No work, shared interrupt perhaps? re-enable
5722 * interrupts, and flush that PCI write
5723 */
5724 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5725 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005726 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005727out:
David S. Millerfac9b832005-05-18 22:46:34 -07005728 return IRQ_RETVAL(handled);
5729}
5730
David Howells7d12e782006-10-05 14:55:46 +01005731static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005732{
Matt Carlson09943a12009-08-28 14:01:57 +00005733 struct tg3_napi *tnapi = dev_id;
5734 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005735 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005736 unsigned int handled = 1;
5737
David S. Millerfac9b832005-05-18 22:46:34 -07005738 /* In INTx mode, it is possible for the interrupt to arrive at
5739 * the CPU before the status block posted prior to the interrupt.
5740 * Reading the PCI State register will confirm whether the
5741 * interrupt is ours and will flush the status block.
5742 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005743 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00005744 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005745 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5746 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005747 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 }
Michael Chand18edcb2007-03-24 20:57:11 -07005749 }
5750
5751 /*
5752 * writing any value to intr-mbox-0 clears PCI INTA# and
5753 * chip-internal interrupt pending events.
5754 * writing non-zero to intr-mbox-0 additional tells the
5755 * NIC to stop sending us irqs, engaging "in-intr-handler"
5756 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005757 *
5758 * Flush the mailbox to de-assert the IRQ immediately to prevent
5759 * spurious interrupts. The flush impacts performance but
5760 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005761 */
Michael Chanc04cb342007-05-07 00:26:15 -07005762 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005763
5764 /*
5765 * In a shared interrupt configuration, sometimes other devices'
5766 * interrupts will scream. We record the current status tag here
5767 * so that the above check can report that the screaming interrupts
5768 * are unhandled. Eventually they will be silenced.
5769 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005770 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005771
Michael Chand18edcb2007-03-24 20:57:11 -07005772 if (tg3_irq_sync(tp))
5773 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005774
Matt Carlson72334482009-08-28 14:03:01 +00005775 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005776
Matt Carlson09943a12009-08-28 14:01:57 +00005777 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005778
David S. Millerf47c11e2005-06-24 20:18:35 -07005779out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 return IRQ_RETVAL(handled);
5781}
5782
Michael Chan79381092005-04-21 17:13:59 -07005783/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005784static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005785{
Matt Carlson09943a12009-08-28 14:01:57 +00005786 struct tg3_napi *tnapi = dev_id;
5787 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005788 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005789
Michael Chanf9804dd2005-09-27 12:13:10 -07005790 if ((sblk->status & SD_STATUS_UPDATED) ||
5791 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005792 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005793 return IRQ_RETVAL(1);
5794 }
5795 return IRQ_RETVAL(0);
5796}
5797
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005798static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005799static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
Michael Chanb9ec6c12006-07-25 16:37:27 -07005801/* Restart hardware after configuration changes, self-test, etc.
5802 * Invoked with tp->lock held.
5803 */
5804static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005805 __releases(tp->lock)
5806 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005807{
5808 int err;
5809
5810 err = tg3_init_hw(tp, reset_phy);
5811 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005812 netdev_err(tp->dev,
5813 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005814 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5815 tg3_full_unlock(tp);
5816 del_timer_sync(&tp->timer);
5817 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005818 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005819 dev_close(tp->dev);
5820 tg3_full_lock(tp, 0);
5821 }
5822 return err;
5823}
5824
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825#ifdef CONFIG_NET_POLL_CONTROLLER
5826static void tg3_poll_controller(struct net_device *dev)
5827{
Matt Carlson4f125f42009-09-01 12:55:02 +00005828 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07005829 struct tg3 *tp = netdev_priv(dev);
5830
Matt Carlson4f125f42009-09-01 12:55:02 +00005831 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005832 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833}
5834#endif
5835
David Howellsc4028952006-11-22 14:57:56 +00005836static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837{
David Howellsc4028952006-11-22 14:57:56 +00005838 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005839 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 unsigned int restart_timer;
5841
Michael Chan7faa0062006-02-02 17:29:28 -08005842 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005843
5844 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005845 tg3_full_unlock(tp);
5846 return;
5847 }
5848
5849 tg3_full_unlock(tp);
5850
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005851 tg3_phy_stop(tp);
5852
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 tg3_netif_stop(tp);
5854
David S. Millerf47c11e2005-06-24 20:18:35 -07005855 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
Joe Perches63c3a662011-04-26 08:12:10 +00005857 restart_timer = tg3_flag(tp, RESTART_TIMER);
5858 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
Joe Perches63c3a662011-04-26 08:12:10 +00005860 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07005861 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5862 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00005863 tg3_flag_set(tp, MBOX_WRITE_REORDER);
5864 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005865 }
5866
Michael Chan944d9802005-05-29 14:57:48 -07005867 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005868 err = tg3_init_hw(tp, 1);
5869 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005870 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
5872 tg3_netif_start(tp);
5873
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 if (restart_timer)
5875 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005876
Michael Chanb9ec6c12006-07-25 16:37:27 -07005877out:
Michael Chan7faa0062006-02-02 17:29:28 -08005878 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005879
5880 if (!err)
5881 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882}
5883
5884static void tg3_tx_timeout(struct net_device *dev)
5885{
5886 struct tg3 *tp = netdev_priv(dev);
5887
Michael Chanb0408752007-02-13 12:18:30 -08005888 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005889 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005890 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892
5893 schedule_work(&tp->reset_task);
5894}
5895
Michael Chanc58ec932005-09-17 00:46:27 -07005896/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5897static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5898{
5899 u32 base = (u32) mapping & 0xffffffff;
5900
Eric Dumazet807540b2010-09-23 05:40:09 +00005901 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005902}
5903
Michael Chan72f2afb2006-03-06 19:28:35 -08005904/* Test for DMA addresses > 40-bit */
5905static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5906 int len)
5907{
5908#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00005909 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00005910 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005911 return 0;
5912#else
5913 return 0;
5914#endif
5915}
5916
Matt Carlsond1a3b732011-07-27 14:20:51 +00005917static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00005918 dma_addr_t mapping, u32 len, u32 flags,
5919 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00005920{
Matt Carlson92cd3a12011-07-27 14:20:47 +00005921 txbd->addr_hi = ((u64) mapping >> 32);
5922 txbd->addr_lo = ((u64) mapping & 0xffffffff);
5923 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
5924 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00005925}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
Matt Carlson84b67b22011-07-27 14:20:52 +00005927static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00005928 dma_addr_t map, u32 len, u32 flags,
5929 u32 mss, u32 vlan)
5930{
5931 struct tg3 *tp = tnapi->tp;
5932 bool hwbug = false;
5933
5934 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
5935 hwbug = 1;
5936
5937 if (tg3_4g_overflow_test(map, len))
5938 hwbug = 1;
5939
5940 if (tg3_40bit_overflow_test(tp, map, len))
5941 hwbug = 1;
5942
Matt Carlson84b67b22011-07-27 14:20:52 +00005943 if (*budget) {
5944 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
5945 len, flags, mss, vlan);
5946 (*budget)--;
5947 } else
5948 hwbug = 1;
5949
5950 *entry = NEXT_TX(*entry);
Matt Carlsond1a3b732011-07-27 14:20:51 +00005951
5952 return hwbug;
5953}
5954
Matt Carlson0d681b22011-07-27 14:20:49 +00005955static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00005956{
5957 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00005958 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00005959 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00005960
Matt Carlson0d681b22011-07-27 14:20:49 +00005961 skb = txb->skb;
5962 txb->skb = NULL;
5963
Matt Carlson432aa7e2011-05-19 12:12:45 +00005964 pci_unmap_single(tnapi->tp->pdev,
5965 dma_unmap_addr(txb, mapping),
5966 skb_headlen(skb),
5967 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005968
5969 while (txb->fragmented) {
5970 txb->fragmented = false;
5971 entry = NEXT_TX(entry);
5972 txb = &tnapi->tx_buffers[entry];
5973 }
5974
Matt Carlson9a2e0fb2011-06-02 13:01:39 +00005975 for (i = 0; i < last; i++) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005976 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5977
5978 entry = NEXT_TX(entry);
5979 txb = &tnapi->tx_buffers[entry];
5980
5981 pci_unmap_page(tnapi->tp->pdev,
5982 dma_unmap_addr(txb, mapping),
5983 frag->size, PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00005984
5985 while (txb->fragmented) {
5986 txb->fragmented = false;
5987 entry = NEXT_TX(entry);
5988 txb = &tnapi->tx_buffers[entry];
5989 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00005990 }
5991}
5992
Michael Chan72f2afb2006-03-06 19:28:35 -08005993/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005994static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
Matt Carlson432aa7e2011-05-19 12:12:45 +00005995 struct sk_buff *skb,
Matt Carlson84b67b22011-07-27 14:20:52 +00005996 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00005997 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005999 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07006000 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006001 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006002 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003
Matt Carlson41588ba2008-04-19 18:12:33 -07006004 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6005 new_skb = skb_copy(skb, GFP_ATOMIC);
6006 else {
6007 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6008
6009 new_skb = skb_copy_expand(skb,
6010 skb_headroom(skb) + more_headroom,
6011 skb_tailroom(skb), GFP_ATOMIC);
6012 }
6013
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006015 ret = -1;
6016 } else {
6017 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006018 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6019 PCI_DMA_TODEVICE);
6020 /* Make sure the mapping succeeded */
6021 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006022 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006023 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006024 } else {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006025 base_flags |= TXD_FLAG_END;
6026
Matt Carlson84b67b22011-07-27 14:20:52 +00006027 tnapi->tx_buffers[*entry].skb = new_skb;
6028 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006029 mapping, new_addr);
6030
Matt Carlson84b67b22011-07-27 14:20:52 +00006031 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006032 new_skb->len, base_flags,
6033 mss, vlan)) {
Matt Carlson84b67b22011-07-27 14:20:52 +00006034 tg3_tx_skb_unmap(tnapi, *entry, 0);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006035 dev_kfree_skb(new_skb);
6036 ret = -1;
6037 }
Michael Chanc58ec932005-09-17 00:46:27 -07006038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 }
6040
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 dev_kfree_skb(skb);
6042
Michael Chanc58ec932005-09-17 00:46:27 -07006043 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044}
6045
Matt Carlson2ffcc982011-05-19 12:12:44 +00006046static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006047
6048/* Use GSO to workaround a rare TSO bug that may be triggered when the
6049 * TSO header is greater than 80 bytes.
6050 */
6051static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6052{
6053 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006054 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006055
6056 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006057 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006058 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006059
6060 /* netif_tx_stop_queue() must be done before checking
6061 * checking tx index in tg3_tx_avail() below, because in
6062 * tg3_tx(), we update tx index before checking for
6063 * netif_tx_queue_stopped().
6064 */
6065 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006066 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006067 return NETDEV_TX_BUSY;
6068
6069 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006070 }
6071
6072 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006073 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006074 goto tg3_tso_bug_end;
6075
6076 do {
6077 nskb = segs;
6078 segs = segs->next;
6079 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006080 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006081 } while (segs);
6082
6083tg3_tso_bug_end:
6084 dev_kfree_skb(skb);
6085
6086 return NETDEV_TX_OK;
6087}
Michael Chan52c0fd82006-06-29 20:15:54 -07006088
Michael Chan5a6f3072006-03-20 22:28:05 -08006089/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006090 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006091 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006092static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006093{
6094 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006095 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006096 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006097 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006098 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006099 struct tg3_napi *tnapi;
6100 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006101 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006102
Matt Carlson24f4efd2009-11-13 13:03:35 +00006103 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6104 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006105 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006106 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
Matt Carlson84b67b22011-07-27 14:20:52 +00006108 budget = tg3_tx_avail(tnapi);
6109
Michael Chan00b70502006-06-17 21:58:45 -07006110 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006111 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006112 * interrupt. Furthermore, IRQ processing runs lockless so we have
6113 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006115 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006116 if (!netif_tx_queue_stopped(txq)) {
6117 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006118
6119 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006120 netdev_err(dev,
6121 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 return NETDEV_TX_BUSY;
6124 }
6125
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006126 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006128 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006130
Matt Carlsonbe98da62010-07-11 09:31:46 +00006131 mss = skb_shinfo(skb)->gso_size;
6132 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006133 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006134 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135
6136 if (skb_header_cloned(skb) &&
6137 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
6138 dev_kfree_skb(skb);
6139 goto out_unlock;
6140 }
6141
Matt Carlson34195c32010-07-11 09:31:42 +00006142 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006143 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144
Matt Carlson02e96082010-09-15 08:59:59 +00006145 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006146 hdr_len = skb_headlen(skb) - ETH_HLEN;
6147 } else {
6148 u32 ip_tcp_len;
6149
6150 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6151 hdr_len = ip_tcp_len + tcp_opt_len;
6152
6153 iph->check = 0;
6154 iph->tot_len = htons(mss + hdr_len);
6155 }
6156
Michael Chan52c0fd82006-06-29 20:15:54 -07006157 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006158 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006159 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006160
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6162 TXD_FLAG_CPU_POST_DMA);
6163
Joe Perches63c3a662011-04-26 08:12:10 +00006164 if (tg3_flag(tp, HW_TSO_1) ||
6165 tg3_flag(tp, HW_TSO_2) ||
6166 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006167 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006169 } else
6170 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6171 iph->daddr, 0,
6172 IPPROTO_TCP,
6173 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174
Joe Perches63c3a662011-04-26 08:12:10 +00006175 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006176 mss |= (hdr_len & 0xc) << 12;
6177 if (hdr_len & 0x10)
6178 base_flags |= 0x00000010;
6179 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006180 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006181 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006182 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006183 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006184 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 int tsflags;
6186
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006187 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 mss |= (tsflags << 11);
6189 }
6190 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006191 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 int tsflags;
6193
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006194 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 base_flags |= tsflags << 12;
6196 }
6197 }
6198 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006199
Matt Carlson92cd3a12011-07-27 14:20:47 +00006200#ifdef BCM_KERNEL_SUPPORTS_8021Q
6201 if (vlan_tx_tag_present(skb)) {
6202 base_flags |= TXD_FLAG_VLAN;
6203 vlan = vlan_tx_tag_get(skb);
6204 }
6205#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206
Joe Perches63c3a662011-04-26 08:12:10 +00006207 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006208 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006209 base_flags |= TXD_FLAG_JMB_PKT;
6210
Alexander Duyckf4188d82009-12-02 16:48:38 +00006211 len = skb_headlen(skb);
6212
6213 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6214 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006215 dev_kfree_skb(skb);
6216 goto out_unlock;
6217 }
6218
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006219 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006220 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221
6222 would_hit_hwbug = 0;
6223
Joe Perches63c3a662011-04-26 08:12:10 +00006224 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006225 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
Matt Carlson84b67b22011-07-27 14:20:52 +00006227 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006228 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
6229 mss, vlan))
6230 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 /* Now loop through additional data fragments, and queue them. */
6233 if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006234 u32 tmp_mss = mss;
6235
6236 if (!tg3_flag(tp, HW_TSO_1) &&
6237 !tg3_flag(tp, HW_TSO_2) &&
6238 !tg3_flag(tp, HW_TSO_3))
6239 tmp_mss = 0;
6240
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 last = skb_shinfo(skb)->nr_frags - 1;
6242 for (i = 0; i <= last; i++) {
6243 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6244
6245 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006246 mapping = pci_map_page(tp->pdev,
6247 frag->page,
6248 frag->page_offset,
6249 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006251 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006252 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006253 mapping);
6254 if (pci_dma_mapping_error(tp->pdev, mapping))
6255 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256
Matt Carlson84b67b22011-07-27 14:20:52 +00006257 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
6258 len, base_flags |
6259 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsond1a3b732011-07-27 14:20:51 +00006260 tmp_mss, vlan))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006261 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 }
6263 }
6264
6265 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006266 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267
6268 /* If the workaround fails due to memory/mapping
6269 * failure, silently drop this packet.
6270 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006271 entry = tnapi->tx_prod;
6272 budget = tg3_tx_avail(tnapi);
6273 if (tigon3_dma_hwbug_workaround(tnapi, skb, &entry, &budget,
6274 base_flags, mss, vlan))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 }
6277
Richard Cochrand515b452011-06-19 03:31:41 +00006278 skb_tx_timestamp(skb);
6279
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006281 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006283 tnapi->tx_prod = entry;
6284 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006285 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006286
6287 /* netif_tx_stop_queue() must be done before checking
6288 * checking tx index in tg3_tx_avail() below, because in
6289 * tg3_tx(), we update tx index before checking for
6290 * netif_tx_queue_stopped().
6291 */
6292 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006293 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006294 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296
6297out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006298 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299
6300 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006301
6302dma_error:
Matt Carlson0d681b22011-07-27 14:20:49 +00006303 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Alexander Duyckf4188d82009-12-02 16:48:38 +00006304 dev_kfree_skb(skb);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006305 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006306 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307}
6308
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006309static void tg3_set_loopback(struct net_device *dev, u32 features)
6310{
6311 struct tg3 *tp = netdev_priv(dev);
6312
6313 if (features & NETIF_F_LOOPBACK) {
6314 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6315 return;
6316
6317 /*
6318 * Clear MAC_MODE_HALF_DUPLEX or you won't get packets back in
6319 * loopback mode if Half-Duplex mode was negotiated earlier.
6320 */
6321 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
6322
6323 /* Enable internal MAC loopback mode */
6324 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6325 spin_lock_bh(&tp->lock);
6326 tw32(MAC_MODE, tp->mac_mode);
6327 netif_carrier_on(tp->dev);
6328 spin_unlock_bh(&tp->lock);
6329 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6330 } else {
6331 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6332 return;
6333
6334 /* Disable internal MAC loopback mode */
6335 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6336 spin_lock_bh(&tp->lock);
6337 tw32(MAC_MODE, tp->mac_mode);
6338 /* Force link status check */
6339 tg3_setup_phy(tp, 1);
6340 spin_unlock_bh(&tp->lock);
6341 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6342 }
6343}
6344
Michał Mirosławdc668912011-04-07 03:35:07 +00006345static u32 tg3_fix_features(struct net_device *dev, u32 features)
6346{
6347 struct tg3 *tp = netdev_priv(dev);
6348
Joe Perches63c3a662011-04-26 08:12:10 +00006349 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006350 features &= ~NETIF_F_ALL_TSO;
6351
6352 return features;
6353}
6354
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006355static int tg3_set_features(struct net_device *dev, u32 features)
6356{
6357 u32 changed = dev->features ^ features;
6358
6359 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6360 tg3_set_loopback(dev, features);
6361
6362 return 0;
6363}
6364
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6366 int new_mtu)
6367{
6368 dev->mtu = new_mtu;
6369
Michael Chanef7f5ec2005-07-25 12:32:25 -07006370 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006371 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006372 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006373 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006374 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006375 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006376 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006377 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006378 if (tg3_flag(tp, 5780_CLASS)) {
6379 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00006380 netdev_update_features(dev);
6381 }
Joe Perches63c3a662011-04-26 08:12:10 +00006382 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384}
6385
6386static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6387{
6388 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006389 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390
6391 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6392 return -EINVAL;
6393
6394 if (!netif_running(dev)) {
6395 /* We'll just catch it later when the
6396 * device is up'd.
6397 */
6398 tg3_set_mtu(dev, tp, new_mtu);
6399 return 0;
6400 }
6401
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006402 tg3_phy_stop(tp);
6403
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006405
6406 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407
Michael Chan944d9802005-05-29 14:57:48 -07006408 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409
6410 tg3_set_mtu(dev, tp, new_mtu);
6411
Michael Chanb9ec6c12006-07-25 16:37:27 -07006412 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413
Michael Chanb9ec6c12006-07-25 16:37:27 -07006414 if (!err)
6415 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416
David S. Millerf47c11e2005-06-24 20:18:35 -07006417 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006419 if (!err)
6420 tg3_phy_start(tp);
6421
Michael Chanb9ec6c12006-07-25 16:37:27 -07006422 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423}
6424
Matt Carlson21f581a2009-08-28 14:00:25 +00006425static void tg3_rx_prodring_free(struct tg3 *tp,
6426 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 int i;
6429
Matt Carlson8fea32b2010-09-15 08:59:58 +00006430 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006431 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006432 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006433 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6434 tp->rx_pkt_map_sz);
6435
Joe Perches63c3a662011-04-26 08:12:10 +00006436 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006437 for (i = tpr->rx_jmb_cons_idx;
6438 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006439 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006440 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6441 TG3_RX_JMB_MAP_SZ);
6442 }
6443 }
6444
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006445 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447
Matt Carlson2c49a442010-09-30 10:34:35 +00006448 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006449 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6450 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451
Joe Perches63c3a662011-04-26 08:12:10 +00006452 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006453 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006454 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6455 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 }
6457}
6458
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006459/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 *
6461 * The chip has been shut down and the driver detached from
6462 * the networking, so no interrupts or new tx packets will
6463 * end up in the driver. tp->{tx,}lock are held and thus
6464 * we may not sleep.
6465 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006466static int tg3_rx_prodring_alloc(struct tg3 *tp,
6467 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468{
Matt Carlson287be122009-08-28 13:58:46 +00006469 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006471 tpr->rx_std_cons_idx = 0;
6472 tpr->rx_std_prod_idx = 0;
6473 tpr->rx_jmb_cons_idx = 0;
6474 tpr->rx_jmb_prod_idx = 0;
6475
Matt Carlson8fea32b2010-09-15 08:59:58 +00006476 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006477 memset(&tpr->rx_std_buffers[0], 0,
6478 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006479 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006480 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006481 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006482 goto done;
6483 }
6484
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006486 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487
Matt Carlson287be122009-08-28 13:58:46 +00006488 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00006489 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006490 tp->dev->mtu > ETH_DATA_LEN)
6491 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6492 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006493
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494 /* Initialize invariants of the rings, we only set this
6495 * stuff once. This works because the card does not
6496 * write into the rx buffer posting rings.
6497 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006498 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 struct tg3_rx_buffer_desc *rxd;
6500
Matt Carlson21f581a2009-08-28 14:00:25 +00006501 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006502 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6504 rxd->opaque = (RXD_OPAQUE_RING_STD |
6505 (i << RXD_OPAQUE_INDEX_SHIFT));
6506 }
6507
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006508 /* Now allocate fresh SKBs for each rx ring. */
6509 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006510 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006511 netdev_warn(tp->dev,
6512 "Using a smaller RX standard ring. Only "
6513 "%d out of %d buffers were allocated "
6514 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006515 if (i == 0)
6516 goto initfail;
6517 tp->rx_pending = i;
6518 break;
6519 }
6520 }
6521
Joe Perches63c3a662011-04-26 08:12:10 +00006522 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006523 goto done;
6524
Matt Carlson2c49a442010-09-30 10:34:35 +00006525 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006526
Joe Perches63c3a662011-04-26 08:12:10 +00006527 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00006528 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529
Matt Carlson2c49a442010-09-30 10:34:35 +00006530 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006531 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532
Matt Carlson0d86df82010-02-17 15:17:00 +00006533 rxd = &tpr->rx_jmb[i].std;
6534 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6535 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6536 RXD_FLAG_JUMBO;
6537 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6538 (i << RXD_OPAQUE_INDEX_SHIFT));
6539 }
6540
6541 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6542 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006543 netdev_warn(tp->dev,
6544 "Using a smaller RX jumbo ring. Only %d "
6545 "out of %d buffers were allocated "
6546 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006547 if (i == 0)
6548 goto initfail;
6549 tp->rx_jumbo_pending = i;
6550 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551 }
6552 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006553
6554done:
Michael Chan32d8c572006-07-25 16:38:29 -07006555 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006556
6557initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006558 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006559 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560}
6561
Matt Carlson21f581a2009-08-28 14:00:25 +00006562static void tg3_rx_prodring_fini(struct tg3 *tp,
6563 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564{
Matt Carlson21f581a2009-08-28 14:00:25 +00006565 kfree(tpr->rx_std_buffers);
6566 tpr->rx_std_buffers = NULL;
6567 kfree(tpr->rx_jmb_buffers);
6568 tpr->rx_jmb_buffers = NULL;
6569 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006570 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6571 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006572 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006574 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006575 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6576 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006577 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006579}
6580
Matt Carlson21f581a2009-08-28 14:00:25 +00006581static int tg3_rx_prodring_init(struct tg3 *tp,
6582 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006583{
Matt Carlson2c49a442010-09-30 10:34:35 +00006584 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6585 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006586 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006587 return -ENOMEM;
6588
Matt Carlson4bae65c2010-11-24 08:31:52 +00006589 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6590 TG3_RX_STD_RING_BYTES(tp),
6591 &tpr->rx_std_mapping,
6592 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006593 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006594 goto err_out;
6595
Joe Perches63c3a662011-04-26 08:12:10 +00006596 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006597 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006598 GFP_KERNEL);
6599 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006600 goto err_out;
6601
Matt Carlson4bae65c2010-11-24 08:31:52 +00006602 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6603 TG3_RX_JMB_RING_BYTES(tp),
6604 &tpr->rx_jmb_mapping,
6605 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006606 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006607 goto err_out;
6608 }
6609
6610 return 0;
6611
6612err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006613 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006614 return -ENOMEM;
6615}
6616
6617/* Free up pending packets in all rx/tx rings.
6618 *
6619 * The chip has been shut down and the driver detached from
6620 * the networking, so no interrupts or new tx packets will
6621 * end up in the driver. tp->{tx,}lock is not held and we are not
6622 * in an interrupt context and thus may sleep.
6623 */
6624static void tg3_free_rings(struct tg3 *tp)
6625{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006626 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006627
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006628 for (j = 0; j < tp->irq_cnt; j++) {
6629 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006630
Matt Carlson8fea32b2010-09-15 08:59:58 +00006631 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006632
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006633 if (!tnapi->tx_buffers)
6634 continue;
6635
Matt Carlson0d681b22011-07-27 14:20:49 +00006636 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
6637 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006638
Matt Carlson0d681b22011-07-27 14:20:49 +00006639 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006640 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006641
Matt Carlson0d681b22011-07-27 14:20:49 +00006642 tg3_tx_skb_unmap(tnapi, i, skb_shinfo(skb)->nr_frags);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006643
6644 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006645 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006646 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006647}
6648
6649/* Initialize tx/rx rings for packet processing.
6650 *
6651 * The chip has been shut down and the driver detached from
6652 * the networking, so no interrupts or new tx packets will
6653 * end up in the driver. tp->{tx,}lock are held and thus
6654 * we may not sleep.
6655 */
6656static int tg3_init_rings(struct tg3 *tp)
6657{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006658 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006659
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006660 /* Free up all the SKBs. */
6661 tg3_free_rings(tp);
6662
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006663 for (i = 0; i < tp->irq_cnt; i++) {
6664 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006665
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006666 tnapi->last_tag = 0;
6667 tnapi->last_irq_tag = 0;
6668 tnapi->hw_status->status = 0;
6669 tnapi->hw_status->status_tag = 0;
6670 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6671
6672 tnapi->tx_prod = 0;
6673 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006674 if (tnapi->tx_ring)
6675 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006676
6677 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006678 if (tnapi->rx_rcb)
6679 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006680
Matt Carlson8fea32b2010-09-15 08:59:58 +00006681 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006682 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006683 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006684 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006685 }
Matt Carlson72334482009-08-28 14:03:01 +00006686
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006687 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006688}
6689
6690/*
6691 * Must not be invoked with interrupt sources disabled and
6692 * the hardware shutdown down.
6693 */
6694static void tg3_free_consistent(struct tg3 *tp)
6695{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006696 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006697
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006698 for (i = 0; i < tp->irq_cnt; i++) {
6699 struct tg3_napi *tnapi = &tp->napi[i];
6700
6701 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006702 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006703 tnapi->tx_ring, tnapi->tx_desc_mapping);
6704 tnapi->tx_ring = NULL;
6705 }
6706
6707 kfree(tnapi->tx_buffers);
6708 tnapi->tx_buffers = NULL;
6709
6710 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006711 dma_free_coherent(&tp->pdev->dev,
6712 TG3_RX_RCB_RING_BYTES(tp),
6713 tnapi->rx_rcb,
6714 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006715 tnapi->rx_rcb = NULL;
6716 }
6717
Matt Carlson8fea32b2010-09-15 08:59:58 +00006718 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6719
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006720 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006721 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6722 tnapi->hw_status,
6723 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006724 tnapi->hw_status = NULL;
6725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006727
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006729 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6730 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 tp->hw_stats = NULL;
6732 }
6733}
6734
6735/*
6736 * Must not be invoked with interrupt sources disabled and
6737 * the hardware shutdown down. Can sleep.
6738 */
6739static int tg3_alloc_consistent(struct tg3 *tp)
6740{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006741 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006742
Matt Carlson4bae65c2010-11-24 08:31:52 +00006743 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6744 sizeof(struct tg3_hw_stats),
6745 &tp->stats_mapping,
6746 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 if (!tp->hw_stats)
6748 goto err_out;
6749
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6751
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006752 for (i = 0; i < tp->irq_cnt; i++) {
6753 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006754 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006755
Matt Carlson4bae65c2010-11-24 08:31:52 +00006756 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6757 TG3_HW_STATUS_SIZE,
6758 &tnapi->status_mapping,
6759 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006760 if (!tnapi->hw_status)
6761 goto err_out;
6762
6763 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006764 sblk = tnapi->hw_status;
6765
Matt Carlson8fea32b2010-09-15 08:59:58 +00006766 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6767 goto err_out;
6768
Matt Carlson19cfaec2009-12-03 08:36:20 +00006769 /* If multivector TSS is enabled, vector 0 does not handle
6770 * tx interrupts. Don't allocate any resources for it.
6771 */
Joe Perches63c3a662011-04-26 08:12:10 +00006772 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
6773 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006774 tnapi->tx_buffers = kzalloc(
6775 sizeof(struct tg3_tx_ring_info) *
6776 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006777 if (!tnapi->tx_buffers)
6778 goto err_out;
6779
Matt Carlson4bae65c2010-11-24 08:31:52 +00006780 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6781 TG3_TX_RING_BYTES,
6782 &tnapi->tx_desc_mapping,
6783 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006784 if (!tnapi->tx_ring)
6785 goto err_out;
6786 }
6787
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006788 /*
6789 * When RSS is enabled, the status block format changes
6790 * slightly. The "rx_jumbo_consumer", "reserved",
6791 * and "rx_mini_consumer" members get mapped to the
6792 * other three rx return ring producer indexes.
6793 */
6794 switch (i) {
6795 default:
6796 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6797 break;
6798 case 2:
6799 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6800 break;
6801 case 3:
6802 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6803 break;
6804 case 4:
6805 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6806 break;
6807 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006808
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006809 /*
6810 * If multivector RSS is enabled, vector 0 does not handle
6811 * rx or tx interrupts. Don't allocate any resources for it.
6812 */
Joe Perches63c3a662011-04-26 08:12:10 +00006813 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006814 continue;
6815
Matt Carlson4bae65c2010-11-24 08:31:52 +00006816 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6817 TG3_RX_RCB_RING_BYTES(tp),
6818 &tnapi->rx_rcb_mapping,
6819 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006820 if (!tnapi->rx_rcb)
6821 goto err_out;
6822
6823 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006824 }
6825
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 return 0;
6827
6828err_out:
6829 tg3_free_consistent(tp);
6830 return -ENOMEM;
6831}
6832
6833#define MAX_WAIT_CNT 1000
6834
6835/* To stop a block, clear the enable bit and poll till it
6836 * clears. tp->lock is held.
6837 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006838static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839{
6840 unsigned int i;
6841 u32 val;
6842
Joe Perches63c3a662011-04-26 08:12:10 +00006843 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 switch (ofs) {
6845 case RCVLSC_MODE:
6846 case DMAC_MODE:
6847 case MBFREE_MODE:
6848 case BUFMGR_MODE:
6849 case MEMARB_MODE:
6850 /* We can't enable/disable these bits of the
6851 * 5705/5750, just say success.
6852 */
6853 return 0;
6854
6855 default:
6856 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858 }
6859
6860 val = tr32(ofs);
6861 val &= ~enable_bit;
6862 tw32_f(ofs, val);
6863
6864 for (i = 0; i < MAX_WAIT_CNT; i++) {
6865 udelay(100);
6866 val = tr32(ofs);
6867 if ((val & enable_bit) == 0)
6868 break;
6869 }
6870
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006871 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006872 dev_err(&tp->pdev->dev,
6873 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6874 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 return -ENODEV;
6876 }
6877
6878 return 0;
6879}
6880
6881/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006882static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883{
6884 int i, err;
6885
6886 tg3_disable_ints(tp);
6887
6888 tp->rx_mode &= ~RX_MODE_ENABLE;
6889 tw32_f(MAC_RX_MODE, tp->rx_mode);
6890 udelay(10);
6891
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006892 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6893 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6894 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6895 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6896 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6897 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006899 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6900 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6901 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6902 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6903 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6904 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6905 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906
6907 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6908 tw32_f(MAC_MODE, tp->mac_mode);
6909 udelay(40);
6910
6911 tp->tx_mode &= ~TX_MODE_ENABLE;
6912 tw32_f(MAC_TX_MODE, tp->tx_mode);
6913
6914 for (i = 0; i < MAX_WAIT_CNT; i++) {
6915 udelay(100);
6916 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6917 break;
6918 }
6919 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006920 dev_err(&tp->pdev->dev,
6921 "%s timed out, TX_MODE_ENABLE will not clear "
6922 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006923 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 }
6925
Michael Chane6de8ad2005-05-05 14:42:41 -07006926 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006927 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6928 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929
6930 tw32(FTQ_RESET, 0xffffffff);
6931 tw32(FTQ_RESET, 0x00000000);
6932
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006933 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6934 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006936 for (i = 0; i < tp->irq_cnt; i++) {
6937 struct tg3_napi *tnapi = &tp->napi[i];
6938 if (tnapi->hw_status)
6939 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 if (tp->hw_stats)
6942 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6943
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944 return err;
6945}
6946
Matt Carlson0d3031d2007-10-10 18:02:43 -07006947static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6948{
6949 int i;
6950 u32 apedata;
6951
Matt Carlsondc6d0742010-09-15 08:59:55 +00006952 /* NCSI does not support APE events */
Joe Perches63c3a662011-04-26 08:12:10 +00006953 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsondc6d0742010-09-15 08:59:55 +00006954 return;
6955
Matt Carlson0d3031d2007-10-10 18:02:43 -07006956 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6957 if (apedata != APE_SEG_SIG_MAGIC)
6958 return;
6959
6960 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006961 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006962 return;
6963
6964 /* Wait for up to 1 millisecond for APE to service previous event. */
6965 for (i = 0; i < 10; i++) {
6966 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6967 return;
6968
6969 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6970
6971 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6972 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6973 event | APE_EVENT_STATUS_EVENT_PENDING);
6974
6975 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6976
6977 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6978 break;
6979
6980 udelay(100);
6981 }
6982
6983 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6984 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6985}
6986
6987static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6988{
6989 u32 event;
6990 u32 apedata;
6991
Joe Perches63c3a662011-04-26 08:12:10 +00006992 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006993 return;
6994
6995 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006996 case RESET_KIND_INIT:
6997 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6998 APE_HOST_SEG_SIG_MAGIC);
6999 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
7000 APE_HOST_SEG_LEN_MAGIC);
7001 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
7002 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
7003 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00007004 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00007005 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
7006 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00007007 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
7008 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07007009
Matt Carlson33f401a2010-04-05 10:19:27 +00007010 event = APE_EVENT_STATUS_STATE_START;
7011 break;
7012 case RESET_KIND_SHUTDOWN:
7013 /* With the interface we are currently using,
7014 * APE does not track driver state. Wiping
7015 * out the HOST SEGMENT SIGNATURE forces
7016 * the APE to assume OS absent status.
7017 */
7018 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08007019
Matt Carlsondc6d0742010-09-15 08:59:55 +00007020 if (device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007021 tg3_flag(tp, WOL_ENABLE)) {
Matt Carlsondc6d0742010-09-15 08:59:55 +00007022 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
7023 TG3_APE_HOST_WOL_SPEED_AUTO);
7024 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
7025 } else
7026 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
7027
7028 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
7029
Matt Carlson33f401a2010-04-05 10:19:27 +00007030 event = APE_EVENT_STATUS_STATE_UNLOAD;
7031 break;
7032 case RESET_KIND_SUSPEND:
7033 event = APE_EVENT_STATUS_STATE_SUSPEND;
7034 break;
7035 default:
7036 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007037 }
7038
7039 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
7040
7041 tg3_ape_send_event(tp, event);
7042}
7043
Michael Chane6af3012005-04-21 17:12:05 -07007044/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
7046{
David S. Millerf49639e2006-06-09 11:58:36 -07007047 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
7048 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049
Joe Perches63c3a662011-04-26 08:12:10 +00007050 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 switch (kind) {
7052 case RESET_KIND_INIT:
7053 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7054 DRV_STATE_START);
7055 break;
7056
7057 case RESET_KIND_SHUTDOWN:
7058 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7059 DRV_STATE_UNLOAD);
7060 break;
7061
7062 case RESET_KIND_SUSPEND:
7063 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7064 DRV_STATE_SUSPEND);
7065 break;
7066
7067 default:
7068 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007071
7072 if (kind == RESET_KIND_INIT ||
7073 kind == RESET_KIND_SUSPEND)
7074 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075}
7076
7077/* tp->lock is held. */
7078static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
7079{
Joe Perches63c3a662011-04-26 08:12:10 +00007080 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 switch (kind) {
7082 case RESET_KIND_INIT:
7083 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7084 DRV_STATE_START_DONE);
7085 break;
7086
7087 case RESET_KIND_SHUTDOWN:
7088 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7089 DRV_STATE_UNLOAD_DONE);
7090 break;
7091
7092 default:
7093 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007096
7097 if (kind == RESET_KIND_SHUTDOWN)
7098 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099}
7100
7101/* tp->lock is held. */
7102static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
7103{
Joe Perches63c3a662011-04-26 08:12:10 +00007104 if (tg3_flag(tp, ENABLE_ASF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 switch (kind) {
7106 case RESET_KIND_INIT:
7107 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7108 DRV_STATE_START);
7109 break;
7110
7111 case RESET_KIND_SHUTDOWN:
7112 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7113 DRV_STATE_UNLOAD);
7114 break;
7115
7116 case RESET_KIND_SUSPEND:
7117 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7118 DRV_STATE_SUSPEND);
7119 break;
7120
7121 default:
7122 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 }
7125}
7126
Michael Chan7a6f4362006-09-27 16:03:31 -07007127static int tg3_poll_fw(struct tg3 *tp)
7128{
7129 int i;
7130 u32 val;
7131
Michael Chanb5d37722006-09-27 16:06:21 -07007132 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08007133 /* Wait up to 20ms for init done. */
7134 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07007135 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
7136 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08007137 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07007138 }
7139 return -ENODEV;
7140 }
7141
Michael Chan7a6f4362006-09-27 16:03:31 -07007142 /* Wait for firmware initialization to complete. */
7143 for (i = 0; i < 100000; i++) {
7144 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
7145 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
7146 break;
7147 udelay(10);
7148 }
7149
7150 /* Chip might not be fitted with firmware. Some Sun onboard
7151 * parts are configured like that. So don't signal the timeout
7152 * of the above loop as an error, but do report the lack of
7153 * running firmware once.
7154 */
Joe Perches63c3a662011-04-26 08:12:10 +00007155 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
7156 tg3_flag_set(tp, NO_FWARE_REPORTED);
Michael Chan7a6f4362006-09-27 16:03:31 -07007157
Joe Perches05dbe002010-02-17 19:44:19 +00007158 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07007159 }
7160
Matt Carlson6b10c162010-02-12 14:47:08 +00007161 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7162 /* The 57765 A0 needs a little more
7163 * time to do some important work.
7164 */
7165 mdelay(10);
7166 }
7167
Michael Chan7a6f4362006-09-27 16:03:31 -07007168 return 0;
7169}
7170
Michael Chanee6a99b2007-07-18 21:49:10 -07007171/* Save PCI command register before chip reset */
7172static void tg3_save_pci_state(struct tg3 *tp)
7173{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007174 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007175}
7176
7177/* Restore PCI state after chip reset */
7178static void tg3_restore_pci_state(struct tg3 *tp)
7179{
7180 u32 val;
7181
7182 /* Re-enable indirect register accesses. */
7183 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7184 tp->misc_host_ctrl);
7185
7186 /* Set MAX PCI retry to zero. */
7187 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7188 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007189 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007190 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007191 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007192 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007193 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00007194 PCISTATE_ALLOW_APE_SHMEM_WR |
7195 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007196 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7197
Matt Carlson8a6eac92007-10-21 16:17:55 -07007198 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007199
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007200 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007201 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007202 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007203 else {
7204 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7205 tp->pci_cacheline_sz);
7206 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7207 tp->pci_lat_timer);
7208 }
Michael Chan114342f2007-10-15 02:12:26 -07007209 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007210
Michael Chanee6a99b2007-07-18 21:49:10 -07007211 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007212 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007213 u16 pcix_cmd;
7214
7215 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7216 &pcix_cmd);
7217 pcix_cmd &= ~PCI_X_CMD_ERO;
7218 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7219 pcix_cmd);
7220 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007221
Joe Perches63c3a662011-04-26 08:12:10 +00007222 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007223
7224 /* Chip reset on 5780 will reset MSI enable bit,
7225 * so need to restore it.
7226 */
Joe Perches63c3a662011-04-26 08:12:10 +00007227 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007228 u16 ctrl;
7229
7230 pci_read_config_word(tp->pdev,
7231 tp->msi_cap + PCI_MSI_FLAGS,
7232 &ctrl);
7233 pci_write_config_word(tp->pdev,
7234 tp->msi_cap + PCI_MSI_FLAGS,
7235 ctrl | PCI_MSI_FLAGS_ENABLE);
7236 val = tr32(MSGINT_MODE);
7237 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7238 }
7239 }
7240}
7241
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242static void tg3_stop_fw(struct tg3 *);
7243
7244/* tp->lock is held. */
7245static int tg3_chip_reset(struct tg3 *tp)
7246{
7247 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007248 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007249 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250
David S. Millerf49639e2006-06-09 11:58:36 -07007251 tg3_nvram_lock(tp);
7252
Matt Carlson77b483f2008-08-15 14:07:24 -07007253 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7254
David S. Millerf49639e2006-06-09 11:58:36 -07007255 /* No matching tg3_nvram_unlock() after this because
7256 * chip reset below will undo the nvram lock.
7257 */
7258 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259
Michael Chanee6a99b2007-07-18 21:49:10 -07007260 /* GRC_MISC_CFG core clock reset will clear the memory
7261 * enable bit in PCI register 4 and the MSI enable bit
7262 * on some chips, so we save relevant registers here.
7263 */
7264 tg3_save_pci_state(tp);
7265
Michael Chand9ab5ad2006-03-20 22:27:35 -08007266 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007267 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007268 tw32(GRC_FASTBOOT_PC, 0);
7269
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270 /*
7271 * We must avoid the readl() that normally takes place.
7272 * It locks machines, causes machine checks, and other
7273 * fun things. So, temporarily disable the 5701
7274 * hardware workaround, while we do the reset.
7275 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007276 write_op = tp->write32;
7277 if (write_op == tg3_write_flush_reg32)
7278 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279
Michael Chand18edcb2007-03-24 20:57:11 -07007280 /* Prevent the irq handler from reading or writing PCI registers
7281 * during chip reset when the memory enable bit in the PCI command
7282 * register may be cleared. The chip does not generate interrupt
7283 * at this time, but the irq handler may still be called due to irq
7284 * sharing or irqpoll.
7285 */
Joe Perches63c3a662011-04-26 08:12:10 +00007286 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007287 for (i = 0; i < tp->irq_cnt; i++) {
7288 struct tg3_napi *tnapi = &tp->napi[i];
7289 if (tnapi->hw_status) {
7290 tnapi->hw_status->status = 0;
7291 tnapi->hw_status->status_tag = 0;
7292 }
7293 tnapi->last_tag = 0;
7294 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007295 }
Michael Chand18edcb2007-03-24 20:57:11 -07007296 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007297
7298 for (i = 0; i < tp->irq_cnt; i++)
7299 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007300
Matt Carlson255ca312009-08-25 10:07:27 +00007301 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7302 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7303 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7304 }
7305
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306 /* do the reset */
7307 val = GRC_MISC_CFG_CORECLK_RESET;
7308
Joe Perches63c3a662011-04-26 08:12:10 +00007309 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007310 /* Force PCIe 1.0a mode */
7311 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007312 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007313 tr32(TG3_PCIE_PHY_TSTCTL) ==
7314 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7315 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7316
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7318 tw32(GRC_MISC_CFG, (1 << 29));
7319 val |= (1 << 29);
7320 }
7321 }
7322
Michael Chanb5d37722006-09-27 16:06:21 -07007323 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7324 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7325 tw32(GRC_VCPU_EXT_CTRL,
7326 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7327 }
7328
Matt Carlsonf37500d2010-08-02 11:25:59 +00007329 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007330 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007332
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 tw32(GRC_MISC_CFG, val);
7334
Michael Chan1ee582d2005-08-09 20:16:46 -07007335 /* restore 5701 hardware bug workaround write method */
7336 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337
7338 /* Unfortunately, we have to delay before the PCI read back.
7339 * Some 575X chips even will not respond to a PCI cfg access
7340 * when the reset command is given to the chip.
7341 *
7342 * How do these hardware designers expect things to work
7343 * properly if the PCI write is posted for a long period
7344 * of time? It is always necessary to have some method by
7345 * which a register read back can occur to push the write
7346 * out which does the reset.
7347 *
7348 * For most tg3 variants the trick below was working.
7349 * Ho hum...
7350 */
7351 udelay(120);
7352
7353 /* Flush PCI posted writes. The normal MMIO registers
7354 * are inaccessible at this time so this is the only
7355 * way to make this reliably (actually, this is no longer
7356 * the case, see above). I tried to use indirect
7357 * register read/write but this upset some 5701 variants.
7358 */
7359 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7360
7361 udelay(120);
7362
Jon Mason708ebb32011-06-27 12:56:50 +00007363 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007364 u16 val16;
7365
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7367 int i;
7368 u32 cfg_val;
7369
7370 /* Wait for link training to complete. */
7371 for (i = 0; i < 5000; i++)
7372 udelay(100);
7373
7374 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7375 pci_write_config_dword(tp->pdev, 0xc4,
7376 cfg_val | (1 << 15));
7377 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007378
Matt Carlsone7126992009-08-25 10:08:16 +00007379 /* Clear the "no snoop" and "relaxed ordering" bits. */
7380 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007381 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007382 &val16);
7383 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7384 PCI_EXP_DEVCTL_NOSNOOP_EN);
7385 /*
7386 * Older PCIe devices only support the 128 byte
7387 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007388 */
Joe Perches63c3a662011-04-26 08:12:10 +00007389 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007390 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007391 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007392 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007393 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007394
Matt Carlsoncf790032010-11-24 08:31:48 +00007395 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007396
7397 /* Clear error status */
7398 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007399 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007400 PCI_EXP_DEVSTA_CED |
7401 PCI_EXP_DEVSTA_NFED |
7402 PCI_EXP_DEVSTA_FED |
7403 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007404 }
7405
Michael Chanee6a99b2007-07-18 21:49:10 -07007406 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007407
Joe Perches63c3a662011-04-26 08:12:10 +00007408 tg3_flag_clear(tp, CHIP_RESETTING);
7409 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007410
Michael Chanee6a99b2007-07-18 21:49:10 -07007411 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007412 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007413 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007414 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007415
7416 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7417 tg3_stop_fw(tp);
7418 tw32(0x5000, 0x400);
7419 }
7420
7421 tw32(GRC_MODE, tp->grc_mode);
7422
7423 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007424 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425
7426 tw32(0xc4, val | (1 << 15));
7427 }
7428
7429 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7430 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7431 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7432 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7433 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7434 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7435 }
7436
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007437 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007438 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007439 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007440 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007441 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007442 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007444 val = 0;
7445
7446 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447 udelay(40);
7448
Matt Carlson77b483f2008-08-15 14:07:24 -07007449 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7450
Michael Chan7a6f4362006-09-27 16:03:31 -07007451 err = tg3_poll_fw(tp);
7452 if (err)
7453 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454
Matt Carlson0a9140c2009-08-28 12:27:50 +00007455 tg3_mdio_start(tp);
7456
Joe Perches63c3a662011-04-26 08:12:10 +00007457 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007458 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7459 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007460 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007461 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007462
7463 tw32(0x7c00, val | (1 << 25));
7464 }
7465
Matt Carlsond78b59f2011-04-05 14:22:46 +00007466 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7467 val = tr32(TG3_CPMU_CLCK_ORIDE);
7468 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7469 }
7470
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007472 tg3_flag_clear(tp, ENABLE_ASF);
7473 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7475 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7476 u32 nic_cfg;
7477
7478 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7479 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007480 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007481 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007482 if (tg3_flag(tp, 5750_PLUS))
7483 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 }
7485 }
7486
7487 return 0;
7488}
7489
7490/* tp->lock is held. */
7491static void tg3_stop_fw(struct tg3 *tp)
7492{
Joe Perches63c3a662011-04-26 08:12:10 +00007493 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007494 /* Wait for RX cpu to ACK the previous event. */
7495 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496
7497 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007498
7499 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500
Matt Carlson7c5026a2008-05-02 16:49:29 -07007501 /* Wait for RX cpu to ACK this event. */
7502 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 }
7504}
7505
7506/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007507static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508{
7509 int err;
7510
7511 tg3_stop_fw(tp);
7512
Michael Chan944d9802005-05-29 14:57:48 -07007513 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007515 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516 err = tg3_chip_reset(tp);
7517
Matt Carlsondaba2a62009-04-20 06:58:52 +00007518 __tg3_set_mac_addr(tp, 0);
7519
Michael Chan944d9802005-05-29 14:57:48 -07007520 tg3_write_sig_legacy(tp, kind);
7521 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522
7523 if (err)
7524 return err;
7525
7526 return 0;
7527}
7528
Linus Torvalds1da177e2005-04-16 15:20:36 -07007529#define RX_CPU_SCRATCH_BASE 0x30000
7530#define RX_CPU_SCRATCH_SIZE 0x04000
7531#define TX_CPU_SCRATCH_BASE 0x34000
7532#define TX_CPU_SCRATCH_SIZE 0x04000
7533
7534/* tp->lock is held. */
7535static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7536{
7537 int i;
7538
Joe Perches63c3a662011-04-26 08:12:10 +00007539 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007540
Michael Chanb5d37722006-09-27 16:06:21 -07007541 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7542 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7543
7544 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7545 return 0;
7546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 if (offset == RX_CPU_BASE) {
7548 for (i = 0; i < 10000; i++) {
7549 tw32(offset + CPU_STATE, 0xffffffff);
7550 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7551 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7552 break;
7553 }
7554
7555 tw32(offset + CPU_STATE, 0xffffffff);
7556 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7557 udelay(10);
7558 } else {
7559 for (i = 0; i < 10000; i++) {
7560 tw32(offset + CPU_STATE, 0xffffffff);
7561 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7562 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7563 break;
7564 }
7565 }
7566
7567 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007568 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7569 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007570 return -ENODEV;
7571 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007572
7573 /* Clear firmware's nvram arbitration. */
Joe Perches63c3a662011-04-26 08:12:10 +00007574 if (tg3_flag(tp, NVRAM))
Michael Chanec41c7d2006-01-17 02:40:55 -08007575 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576 return 0;
7577}
7578
7579struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007580 unsigned int fw_base;
7581 unsigned int fw_len;
7582 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583};
7584
7585/* tp->lock is held. */
7586static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7587 int cpu_scratch_size, struct fw_info *info)
7588{
Michael Chanec41c7d2006-01-17 02:40:55 -08007589 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590 void (*write_op)(struct tg3 *, u32, u32);
7591
Joe Perches63c3a662011-04-26 08:12:10 +00007592 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007593 netdev_err(tp->dev,
7594 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007595 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007596 return -EINVAL;
7597 }
7598
Joe Perches63c3a662011-04-26 08:12:10 +00007599 if (tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007600 write_op = tg3_write_mem;
7601 else
7602 write_op = tg3_write_indirect_reg32;
7603
Michael Chan1b628152005-05-29 14:59:49 -07007604 /* It is possible that bootcode is still loading at this point.
7605 * Get the nvram lock first before halting the cpu.
7606 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007607 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007608 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007609 if (!lock_err)
7610 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611 if (err)
7612 goto out;
7613
7614 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7615 write_op(tp, cpu_scratch_base + i, 0);
7616 tw32(cpu_base + CPU_STATE, 0xffffffff);
7617 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007618 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007620 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007622 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623
7624 err = 0;
7625
7626out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007627 return err;
7628}
7629
7630/* tp->lock is held. */
7631static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7632{
7633 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007634 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635 int err, i;
7636
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007637 fw_data = (void *)tp->fw->data;
7638
7639 /* Firmware blob starts with version numbers, followed by
7640 start address and length. We are setting complete length.
7641 length = end_address_of_bss - start_address_of_text.
7642 Remainder is the blob to be loaded contiguously
7643 from start address. */
7644
7645 info.fw_base = be32_to_cpu(fw_data[1]);
7646 info.fw_len = tp->fw->size - 12;
7647 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648
7649 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7650 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7651 &info);
7652 if (err)
7653 return err;
7654
7655 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7656 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7657 &info);
7658 if (err)
7659 return err;
7660
7661 /* Now startup only the RX cpu. */
7662 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007663 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007664
7665 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007666 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667 break;
7668 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7669 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007670 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671 udelay(1000);
7672 }
7673 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007674 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7675 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007676 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677 return -ENODEV;
7678 }
7679 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7680 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7681
7682 return 0;
7683}
7684
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685/* tp->lock is held. */
7686static int tg3_load_tso_firmware(struct tg3 *tp)
7687{
7688 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007689 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7691 int err, i;
7692
Joe Perches63c3a662011-04-26 08:12:10 +00007693 if (tg3_flag(tp, HW_TSO_1) ||
7694 tg3_flag(tp, HW_TSO_2) ||
7695 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007696 return 0;
7697
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007698 fw_data = (void *)tp->fw->data;
7699
7700 /* Firmware blob starts with version numbers, followed by
7701 start address and length. We are setting complete length.
7702 length = end_address_of_bss - start_address_of_text.
7703 Remainder is the blob to be loaded contiguously
7704 from start address. */
7705
7706 info.fw_base = be32_to_cpu(fw_data[1]);
7707 cpu_scratch_size = tp->fw_len;
7708 info.fw_len = tp->fw->size - 12;
7709 info.fw_data = &fw_data[3];
7710
Linus Torvalds1da177e2005-04-16 15:20:36 -07007711 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712 cpu_base = RX_CPU_BASE;
7713 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715 cpu_base = TX_CPU_BASE;
7716 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7717 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7718 }
7719
7720 err = tg3_load_firmware_cpu(tp, cpu_base,
7721 cpu_scratch_base, cpu_scratch_size,
7722 &info);
7723 if (err)
7724 return err;
7725
7726 /* Now startup the cpu. */
7727 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007728 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007729
7730 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007731 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007732 break;
7733 tw32(cpu_base + CPU_STATE, 0xffffffff);
7734 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007735 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736 udelay(1000);
7737 }
7738 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007739 netdev_err(tp->dev,
7740 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007741 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007742 return -ENODEV;
7743 }
7744 tw32(cpu_base + CPU_STATE, 0xffffffff);
7745 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7746 return 0;
7747}
7748
Linus Torvalds1da177e2005-04-16 15:20:36 -07007749
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750static int tg3_set_mac_addr(struct net_device *dev, void *p)
7751{
7752 struct tg3 *tp = netdev_priv(dev);
7753 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007754 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755
Michael Chanf9804dd2005-09-27 12:13:10 -07007756 if (!is_valid_ether_addr(addr->sa_data))
7757 return -EINVAL;
7758
Linus Torvalds1da177e2005-04-16 15:20:36 -07007759 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7760
Michael Chane75f7c92006-03-20 21:33:26 -08007761 if (!netif_running(dev))
7762 return 0;
7763
Joe Perches63c3a662011-04-26 08:12:10 +00007764 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007765 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007766
Michael Chan986e0ae2007-05-05 12:10:20 -07007767 addr0_high = tr32(MAC_ADDR_0_HIGH);
7768 addr0_low = tr32(MAC_ADDR_0_LOW);
7769 addr1_high = tr32(MAC_ADDR_1_HIGH);
7770 addr1_low = tr32(MAC_ADDR_1_LOW);
7771
7772 /* Skip MAC addr 1 if ASF is using it. */
7773 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7774 !(addr1_high == 0 && addr1_low == 0))
7775 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007776 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007777 spin_lock_bh(&tp->lock);
7778 __tg3_set_mac_addr(tp, skip_mac_1);
7779 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780
Michael Chanb9ec6c12006-07-25 16:37:27 -07007781 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782}
7783
7784/* tp->lock is held. */
7785static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7786 dma_addr_t mapping, u32 maxlen_flags,
7787 u32 nic_addr)
7788{
7789 tg3_write_mem(tp,
7790 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7791 ((u64) mapping >> 32));
7792 tg3_write_mem(tp,
7793 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7794 ((u64) mapping & 0xffffffff));
7795 tg3_write_mem(tp,
7796 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7797 maxlen_flags);
7798
Joe Perches63c3a662011-04-26 08:12:10 +00007799 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800 tg3_write_mem(tp,
7801 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7802 nic_addr);
7803}
7804
7805static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007806static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007807{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007808 int i;
7809
Joe Perches63c3a662011-04-26 08:12:10 +00007810 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007811 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7812 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7813 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007814 } else {
7815 tw32(HOSTCC_TXCOL_TICKS, 0);
7816 tw32(HOSTCC_TXMAX_FRAMES, 0);
7817 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007818 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007819
Joe Perches63c3a662011-04-26 08:12:10 +00007820 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007821 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7822 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7823 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7824 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007825 tw32(HOSTCC_RXCOL_TICKS, 0);
7826 tw32(HOSTCC_RXMAX_FRAMES, 0);
7827 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007828 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007829
Joe Perches63c3a662011-04-26 08:12:10 +00007830 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07007831 u32 val = ec->stats_block_coalesce_usecs;
7832
Matt Carlsonb6080e12009-09-01 13:12:00 +00007833 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7834 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7835
David S. Miller15f98502005-05-18 22:49:26 -07007836 if (!netif_carrier_ok(tp->dev))
7837 val = 0;
7838
7839 tw32(HOSTCC_STAT_COAL_TICKS, val);
7840 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007841
7842 for (i = 0; i < tp->irq_cnt - 1; i++) {
7843 u32 reg;
7844
7845 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7846 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007847 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7848 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007849 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7850 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007851
Joe Perches63c3a662011-04-26 08:12:10 +00007852 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007853 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7854 tw32(reg, ec->tx_coalesce_usecs);
7855 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7856 tw32(reg, ec->tx_max_coalesced_frames);
7857 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7858 tw32(reg, ec->tx_max_coalesced_frames_irq);
7859 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007860 }
7861
7862 for (; i < tp->irq_max - 1; i++) {
7863 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007864 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007865 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007866
Joe Perches63c3a662011-04-26 08:12:10 +00007867 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007868 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7869 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7870 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7871 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007872 }
David S. Miller15f98502005-05-18 22:49:26 -07007873}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874
7875/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007876static void tg3_rings_reset(struct tg3 *tp)
7877{
7878 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007879 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007880 struct tg3_napi *tnapi = &tp->napi[0];
7881
7882 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007883 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007884 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00007885 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00007886 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007887 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7888 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007889 else
7890 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7891
7892 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7893 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7894 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7895 BDINFO_FLAGS_DISABLED);
7896
7897
7898 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007899 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007900 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00007901 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007902 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007903 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7904 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007905 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7906 else
7907 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7908
7909 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7910 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7911 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7912 BDINFO_FLAGS_DISABLED);
7913
7914 /* Disable interrupts */
7915 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00007916 tp->napi[0].chk_msi_cnt = 0;
7917 tp->napi[0].last_rx_cons = 0;
7918 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007919
7920 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00007921 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007922 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007923 tp->napi[i].tx_prod = 0;
7924 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007925 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007926 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007927 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7928 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00007929 tp->napi[0].chk_msi_cnt = 0;
7930 tp->napi[i].last_rx_cons = 0;
7931 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007932 }
Joe Perches63c3a662011-04-26 08:12:10 +00007933 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007934 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007935 } else {
7936 tp->napi[0].tx_prod = 0;
7937 tp->napi[0].tx_cons = 0;
7938 tw32_mailbox(tp->napi[0].prodmbox, 0);
7939 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7940 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007941
7942 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00007943 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00007944 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7945 for (i = 0; i < 16; i++)
7946 tw32_tx_mbox(mbox + i * 8, 0);
7947 }
7948
7949 txrcb = NIC_SRAM_SEND_RCB;
7950 rxrcb = NIC_SRAM_RCV_RET_RCB;
7951
7952 /* Clear status block in ram. */
7953 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7954
7955 /* Set status block DMA address */
7956 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7957 ((u64) tnapi->status_mapping >> 32));
7958 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7959 ((u64) tnapi->status_mapping & 0xffffffff));
7960
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007961 if (tnapi->tx_ring) {
7962 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7963 (TG3_TX_RING_SIZE <<
7964 BDINFO_FLAGS_MAXLEN_SHIFT),
7965 NIC_SRAM_TX_BUFFER_DESC);
7966 txrcb += TG3_BDINFO_SIZE;
7967 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007968
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007969 if (tnapi->rx_rcb) {
7970 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007971 (tp->rx_ret_ring_mask + 1) <<
7972 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007973 rxrcb += TG3_BDINFO_SIZE;
7974 }
7975
7976 stblk = HOSTCC_STATBLCK_RING1;
7977
7978 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7979 u64 mapping = (u64)tnapi->status_mapping;
7980 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7981 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7982
7983 /* Clear status block in ram. */
7984 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7985
Matt Carlson19cfaec2009-12-03 08:36:20 +00007986 if (tnapi->tx_ring) {
7987 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7988 (TG3_TX_RING_SIZE <<
7989 BDINFO_FLAGS_MAXLEN_SHIFT),
7990 NIC_SRAM_TX_BUFFER_DESC);
7991 txrcb += TG3_BDINFO_SIZE;
7992 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007993
7994 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007995 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007996 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7997
7998 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007999 rxrcb += TG3_BDINFO_SIZE;
8000 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008001}
8002
Matt Carlsoneb07a942011-04-20 07:57:36 +00008003static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8004{
8005 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8006
Joe Perches63c3a662011-04-26 08:12:10 +00008007 if (!tg3_flag(tp, 5750_PLUS) ||
8008 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008009 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
8010 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8011 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8012 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8013 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8014 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8015 else
8016 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8017
8018 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8019 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8020
8021 val = min(nic_rep_thresh, host_rep_thresh);
8022 tw32(RCVBDI_STD_THRESH, val);
8023
Joe Perches63c3a662011-04-26 08:12:10 +00008024 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008025 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8026
Joe Perches63c3a662011-04-26 08:12:10 +00008027 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008028 return;
8029
Joe Perches63c3a662011-04-26 08:12:10 +00008030 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008031 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
8032 else
8033 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
8034
8035 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8036
8037 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8038 tw32(RCVBDI_JUMBO_THRESH, val);
8039
Joe Perches63c3a662011-04-26 08:12:10 +00008040 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008041 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8042}
8043
Matt Carlson2d31eca2009-09-01 12:53:31 +00008044/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008045static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008046{
8047 u32 val, rdmac_mode;
8048 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008049 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008050
8051 tg3_disable_ints(tp);
8052
8053 tg3_stop_fw(tp);
8054
8055 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8056
Joe Perches63c3a662011-04-26 08:12:10 +00008057 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008058 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059
Matt Carlson699c0192010-12-06 08:28:51 +00008060 /* Enable MAC control of LPI */
8061 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8062 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8063 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8064 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8065
8066 tw32_f(TG3_CPMU_EEE_CTRL,
8067 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8068
Matt Carlsona386b902010-12-06 08:28:53 +00008069 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8070 TG3_CPMU_EEEMD_LPI_IN_TX |
8071 TG3_CPMU_EEEMD_LPI_IN_RX |
8072 TG3_CPMU_EEEMD_EEE_ENABLE;
8073
8074 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8075 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8076
Joe Perches63c3a662011-04-26 08:12:10 +00008077 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008078 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8079
8080 tw32_f(TG3_CPMU_EEE_MODE, val);
8081
8082 tw32_f(TG3_CPMU_EEE_DBTMR1,
8083 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8084 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8085
8086 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008087 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008088 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008089 }
8090
Matt Carlson603f1172010-02-12 14:47:10 +00008091 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008092 tg3_phy_reset(tp);
8093
Linus Torvalds1da177e2005-04-16 15:20:36 -07008094 err = tg3_chip_reset(tp);
8095 if (err)
8096 return err;
8097
8098 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8099
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008100 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008101 val = tr32(TG3_CPMU_CTRL);
8102 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8103 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008104
8105 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8106 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8107 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8108 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8109
8110 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8111 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8112 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8113 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8114
8115 val = tr32(TG3_CPMU_HST_ACC);
8116 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8117 val |= CPMU_HST_ACC_MACCLK_6_25;
8118 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008119 }
8120
Matt Carlson33466d92009-04-20 06:57:41 +00008121 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8122 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8123 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8124 PCIE_PWR_MGMT_L1_THRESH_4MS;
8125 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008126
8127 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8128 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8129
8130 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008131
Matt Carlsonf40386c2009-11-02 14:24:02 +00008132 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8133 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008134 }
8135
Joe Perches63c3a662011-04-26 08:12:10 +00008136 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b05902010-01-20 16:58:02 +00008137 u32 grc_mode = tr32(GRC_MODE);
8138
8139 /* Access the lower 1K of PL PCIE block registers. */
8140 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8141 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8142
8143 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8144 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8145 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8146
8147 tw32(GRC_MODE, grc_mode);
8148 }
8149
Matt Carlson5093eed2010-11-24 08:31:45 +00008150 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8151 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8152 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008153
Matt Carlson5093eed2010-11-24 08:31:45 +00008154 /* Access the lower 1K of PL PCIE block registers. */
8155 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8156 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008157
Matt Carlson5093eed2010-11-24 08:31:45 +00008158 val = tr32(TG3_PCIE_TLDLPL_PORT +
8159 TG3_PCIE_PL_LO_PHYCTL5);
8160 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8161 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008162
Matt Carlson5093eed2010-11-24 08:31:45 +00008163 tw32(GRC_MODE, grc_mode);
8164 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008165
Matt Carlson1ff30a52011-05-19 12:12:46 +00008166 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8167 u32 grc_mode = tr32(GRC_MODE);
8168
8169 /* Access the lower 1K of DL PCIE block registers. */
8170 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8171 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8172
8173 val = tr32(TG3_PCIE_TLDLPL_PORT +
8174 TG3_PCIE_DL_LO_FTSMAX);
8175 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8176 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8177 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8178
8179 tw32(GRC_MODE, grc_mode);
8180 }
8181
Matt Carlsona977dbe2010-04-12 06:58:26 +00008182 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8183 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8184 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8185 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008186 }
8187
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188 /* This works around an issue with Athlon chipsets on
8189 * B3 tigon3 silicon. This bit has no effect on any
8190 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008191 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192 */
Joe Perches63c3a662011-04-26 08:12:10 +00008193 if (!tg3_flag(tp, CPMU_PRESENT)) {
8194 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008195 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8196 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008198
8199 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008200 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201 val = tr32(TG3PCI_PCISTATE);
8202 val |= PCISTATE_RETRY_SAME_DMA;
8203 tw32(TG3PCI_PCISTATE, val);
8204 }
8205
Joe Perches63c3a662011-04-26 08:12:10 +00008206 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008207 /* Allow reads and writes to the
8208 * APE register and memory space.
8209 */
8210 val = tr32(TG3PCI_PCISTATE);
8211 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00008212 PCISTATE_ALLOW_APE_SHMEM_WR |
8213 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008214 tw32(TG3PCI_PCISTATE, val);
8215 }
8216
Linus Torvalds1da177e2005-04-16 15:20:36 -07008217 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8218 /* Enable some hw fixes. */
8219 val = tr32(TG3PCI_MSI_DATA);
8220 val |= (1 << 26) | (1 << 28) | (1 << 29);
8221 tw32(TG3PCI_MSI_DATA, val);
8222 }
8223
8224 /* Descriptor ring init may make accesses to the
8225 * NIC SRAM area to setup the TX descriptors, so we
8226 * can only do this after the hardware has been
8227 * successfully reset.
8228 */
Michael Chan32d8c572006-07-25 16:38:29 -07008229 err = tg3_init_rings(tp);
8230 if (err)
8231 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008232
Joe Perches63c3a662011-04-26 08:12:10 +00008233 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008234 val = tr32(TG3PCI_DMA_RW_CTRL) &
8235 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008236 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8237 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008238 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8239 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8240 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008241 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8242 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8243 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008244 /* This value is determined during the probe time DMA
8245 * engine test, tg3_test_dma.
8246 */
8247 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249
8250 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8251 GRC_MODE_4X_NIC_SEND_RINGS |
8252 GRC_MODE_NO_TX_PHDR_CSUM |
8253 GRC_MODE_NO_RX_PHDR_CSUM);
8254 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008255
8256 /* Pseudo-header checksum is done by hardware logic and not
8257 * the offload processers, so make the chip do the pseudo-
8258 * header checksums on receive. For transmit it is more
8259 * convenient to do the pseudo-header checksum in software
8260 * as Linux does that on transmit for us in all cases.
8261 */
8262 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263
8264 tw32(GRC_MODE,
8265 tp->grc_mode |
8266 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8267
8268 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8269 val = tr32(GRC_MISC_CFG);
8270 val &= ~0xff;
8271 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8272 tw32(GRC_MISC_CFG, val);
8273
8274 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008275 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008276 /* Do nothing. */
8277 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8278 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8279 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8280 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8281 else
8282 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8283 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8284 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008285 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008286 int fw_len;
8287
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008288 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008289 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8290 tw32(BUFMGR_MB_POOL_ADDR,
8291 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8292 tw32(BUFMGR_MB_POOL_SIZE,
8293 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008295
Michael Chan0f893dc2005-07-25 12:30:38 -07008296 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008297 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8298 tp->bufmgr_config.mbuf_read_dma_low_water);
8299 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8300 tp->bufmgr_config.mbuf_mac_rx_low_water);
8301 tw32(BUFMGR_MB_HIGH_WATER,
8302 tp->bufmgr_config.mbuf_high_water);
8303 } else {
8304 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8305 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8306 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8307 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8308 tw32(BUFMGR_MB_HIGH_WATER,
8309 tp->bufmgr_config.mbuf_high_water_jumbo);
8310 }
8311 tw32(BUFMGR_DMA_LOW_WATER,
8312 tp->bufmgr_config.dma_low_water);
8313 tw32(BUFMGR_DMA_HIGH_WATER,
8314 tp->bufmgr_config.dma_high_water);
8315
Matt Carlsond309a462010-09-30 10:34:31 +00008316 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8317 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8318 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008319 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8320 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8321 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8322 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008323 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008324 for (i = 0; i < 2000; i++) {
8325 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8326 break;
8327 udelay(10);
8328 }
8329 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008330 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008331 return -ENODEV;
8332 }
8333
Matt Carlsoneb07a942011-04-20 07:57:36 +00008334 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8335 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008336
Matt Carlsoneb07a942011-04-20 07:57:36 +00008337 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338
8339 /* Initialize TG3_BDINFO's at:
8340 * RCVDBDI_STD_BD: standard eth size rx ring
8341 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8342 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8343 *
8344 * like so:
8345 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8346 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8347 * ring attribute flags
8348 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8349 *
8350 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8351 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8352 *
8353 * The size of each ring is fixed in the firmware, but the location is
8354 * configurable.
8355 */
8356 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008357 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008358 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008359 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008360 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008361 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8362 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008363
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008364 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008365 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008366 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8367 BDINFO_FLAGS_DISABLED);
8368
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008369 /* Program the jumbo buffer descriptor ring control
8370 * blocks on those devices that have them.
8371 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008372 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008373 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008374
Joe Perches63c3a662011-04-26 08:12:10 +00008375 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008376 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008377 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008379 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008380 val = TG3_RX_JMB_RING_SIZE(tp) <<
8381 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008383 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008384 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008385 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008386 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8387 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008388 } else {
8389 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8390 BDINFO_FLAGS_DISABLED);
8391 }
8392
Joe Perches63c3a662011-04-26 08:12:10 +00008393 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008394 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008395 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008396 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008397 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008398 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8399 val |= (TG3_RX_STD_DMA_SZ << 2);
8400 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008401 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008402 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008403 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008404
8405 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008406
Matt Carlson411da642009-11-13 13:03:46 +00008407 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008408 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008409
Joe Perches63c3a662011-04-26 08:12:10 +00008410 tpr->rx_jmb_prod_idx =
8411 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008412 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413
Matt Carlson2d31eca2009-09-01 12:53:31 +00008414 tg3_rings_reset(tp);
8415
Linus Torvalds1da177e2005-04-16 15:20:36 -07008416 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008417 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008418
8419 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008420 tw32(MAC_RX_MTU_SIZE,
8421 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008422
8423 /* The slot time is changed by tg3_setup_phy if we
8424 * run at gigabit with half duplex.
8425 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008426 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8427 (6 << TX_LENGTHS_IPG_SHIFT) |
8428 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8429
8430 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8431 val |= tr32(MAC_TX_LENGTHS) &
8432 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8433 TX_LENGTHS_CNT_DWN_VAL_MSK);
8434
8435 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008436
8437 /* Receive rules. */
8438 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8439 tw32(RCVLPC_CONFIG, 0x0181);
8440
8441 /* Calculate RDMAC_MODE setting early, we need it to determine
8442 * the RCVLPC_STATE_ENABLE mask.
8443 */
8444 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8445 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8446 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8447 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8448 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008449
Matt Carlsondeabaac2010-11-24 08:31:50 +00008450 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008451 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8452
Matt Carlson57e69832008-05-25 23:48:31 -07008453 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008454 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8455 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008456 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8457 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8458 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8459
Matt Carlsonc5908932011-03-09 16:58:25 +00008460 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8461 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008462 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008463 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008464 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8465 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008466 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008467 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8468 }
8469 }
8470
Joe Perches63c3a662011-04-26 08:12:10 +00008471 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008472 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8473
Joe Perches63c3a662011-04-26 08:12:10 +00008474 if (tg3_flag(tp, HW_TSO_1) ||
8475 tg3_flag(tp, HW_TSO_2) ||
8476 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008477 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8478
Matt Carlson108a6c12011-05-19 12:12:47 +00008479 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008480 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008481 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8482 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008483
Matt Carlsonf2096f92011-04-05 14:22:48 +00008484 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8485 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8486
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008487 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8488 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8489 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8490 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008491 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008492 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008493 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8494 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008495 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8496 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8497 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8498 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8499 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8500 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008501 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008502 tw32(TG3_RDMA_RSRVCTRL_REG,
8503 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8504 }
8505
Matt Carlsond78b59f2011-04-05 14:22:46 +00008506 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8507 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008508 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8509 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8510 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8511 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8512 }
8513
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008515 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008516 val = tr32(RCVLPC_STATS_ENABLE);
8517 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8518 tw32(RCVLPC_STATS_ENABLE, val);
8519 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008520 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008521 val = tr32(RCVLPC_STATS_ENABLE);
8522 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8523 tw32(RCVLPC_STATS_ENABLE, val);
8524 } else {
8525 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8526 }
8527 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8528 tw32(SNDDATAI_STATSENAB, 0xffffff);
8529 tw32(SNDDATAI_STATSCTRL,
8530 (SNDDATAI_SCTRL_ENABLE |
8531 SNDDATAI_SCTRL_FASTUPD));
8532
8533 /* Setup host coalescing engine. */
8534 tw32(HOSTCC_MODE, 0);
8535 for (i = 0; i < 2000; i++) {
8536 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8537 break;
8538 udelay(10);
8539 }
8540
Michael Chand244c892005-07-05 14:42:33 -07008541 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008542
Joe Perches63c3a662011-04-26 08:12:10 +00008543 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008544 /* Status/statistics block address. See tg3_timer,
8545 * the tg3_periodic_fetch_stats call there, and
8546 * tg3_get_stats to see how this works for 5705/5750 chips.
8547 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008548 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8549 ((u64) tp->stats_mapping >> 32));
8550 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8551 ((u64) tp->stats_mapping & 0xffffffff));
8552 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008553
Linus Torvalds1da177e2005-04-16 15:20:36 -07008554 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008555
8556 /* Clear statistics and status block memory areas */
8557 for (i = NIC_SRAM_STATS_BLK;
8558 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8559 i += sizeof(u32)) {
8560 tg3_write_mem(tp, i, 0);
8561 udelay(40);
8562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008563 }
8564
8565 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8566
8567 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8568 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008569 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008570 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8571
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008572 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8573 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008574 /* reset to prevent losing 1st rx packet intermittently */
8575 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8576 udelay(10);
8577 }
8578
Matt Carlson3bda1252008-08-15 14:08:22 -07008579 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00008580 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8581 MAC_MODE_FHDE_ENABLE;
8582 if (tg3_flag(tp, ENABLE_APE))
8583 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008584 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008585 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008586 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8587 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008588 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8589 udelay(40);
8590
Michael Chan314fba32005-04-21 17:07:04 -07008591 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008592 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008593 * register to preserve the GPIO settings for LOMs. The GPIOs,
8594 * whether used as inputs or outputs, are set by boot code after
8595 * reset.
8596 */
Joe Perches63c3a662011-04-26 08:12:10 +00008597 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008598 u32 gpio_mask;
8599
Michael Chan9d26e212006-12-07 00:21:14 -08008600 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8601 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8602 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008603
8604 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8605 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8606 GRC_LCLCTRL_GPIO_OUTPUT3;
8607
Michael Chanaf36e6b2006-03-23 01:28:06 -08008608 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8609 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8610
Gary Zambranoaaf84462007-05-05 11:51:45 -07008611 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008612 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8613
8614 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008615 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008616 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8617 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008619 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8620 udelay(100);
8621
Joe Perches63c3a662011-04-26 08:12:10 +00008622 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008623 val = tr32(MSGINT_MODE);
8624 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8625 tw32(MSGINT_MODE, val);
8626 }
8627
Joe Perches63c3a662011-04-26 08:12:10 +00008628 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008629 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8630 udelay(40);
8631 }
8632
8633 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8634 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8635 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8636 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8637 WDMAC_MODE_LNGREAD_ENAB);
8638
Matt Carlsonc5908932011-03-09 16:58:25 +00008639 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8640 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008641 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008642 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8643 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8644 /* nothing */
8645 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008646 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647 val |= WDMAC_MODE_RX_ACCEL;
8648 }
8649 }
8650
Michael Chand9ab5ad2006-03-20 22:27:35 -08008651 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008652 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008653 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008654
Matt Carlson788a0352009-11-02 14:26:03 +00008655 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8656 val |= WDMAC_MODE_BURST_ALL_DATA;
8657
Linus Torvalds1da177e2005-04-16 15:20:36 -07008658 tw32_f(WDMAC_MODE, val);
8659 udelay(40);
8660
Joe Perches63c3a662011-04-26 08:12:10 +00008661 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008662 u16 pcix_cmd;
8663
8664 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8665 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008666 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008667 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8668 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008669 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008670 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8671 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008672 }
Matt Carlson9974a352007-10-07 23:27:28 -07008673 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8674 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008675 }
8676
8677 tw32_f(RDMAC_MODE, rdmac_mode);
8678 udelay(40);
8679
8680 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008681 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008682 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008683
8684 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8685 tw32(SNDDATAC_MODE,
8686 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8687 else
8688 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8689
Linus Torvalds1da177e2005-04-16 15:20:36 -07008690 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8691 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008692 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008693 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008694 val |= RCVDBDI_MODE_LRG_RING_SZ;
8695 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008696 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008697 if (tg3_flag(tp, HW_TSO_1) ||
8698 tg3_flag(tp, HW_TSO_2) ||
8699 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008700 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008701 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008702 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008703 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8704 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008705 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8706
8707 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8708 err = tg3_load_5701_a0_firmware_fix(tp);
8709 if (err)
8710 return err;
8711 }
8712
Joe Perches63c3a662011-04-26 08:12:10 +00008713 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008714 err = tg3_load_tso_firmware(tp);
8715 if (err)
8716 return err;
8717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718
8719 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008720
Joe Perches63c3a662011-04-26 08:12:10 +00008721 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008722 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8723 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008724
8725 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8726 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8727 tp->tx_mode &= ~val;
8728 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8729 }
8730
Linus Torvalds1da177e2005-04-16 15:20:36 -07008731 tw32_f(MAC_TX_MODE, tp->tx_mode);
8732 udelay(100);
8733
Joe Perches63c3a662011-04-26 08:12:10 +00008734 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson9d53fa12011-07-20 10:20:54 +00008735 int i = 0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008736 u32 reg = MAC_RSS_INDIR_TBL_0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008737
Matt Carlson9d53fa12011-07-20 10:20:54 +00008738 if (tp->irq_cnt == 2) {
8739 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i += 8) {
8740 tw32(reg, 0x0);
8741 reg += 4;
8742 }
8743 } else {
8744 u32 val;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008745
Matt Carlson9d53fa12011-07-20 10:20:54 +00008746 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8747 val = i % (tp->irq_cnt - 1);
8748 i++;
8749 for (; i % 8; i++) {
8750 val <<= 4;
8751 val |= (i % (tp->irq_cnt - 1));
8752 }
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008753 tw32(reg, val);
8754 reg += 4;
8755 }
8756 }
8757
8758 /* Setup the "secret" hash key. */
8759 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8760 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8761 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8762 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8763 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8764 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8765 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8766 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8767 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8768 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8769 }
8770
Linus Torvalds1da177e2005-04-16 15:20:36 -07008771 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008772 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008773 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8774
Joe Perches63c3a662011-04-26 08:12:10 +00008775 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008776 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8777 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8778 RX_MODE_RSS_IPV6_HASH_EN |
8779 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8780 RX_MODE_RSS_IPV4_HASH_EN |
8781 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8782
Linus Torvalds1da177e2005-04-16 15:20:36 -07008783 tw32_f(MAC_RX_MODE, tp->rx_mode);
8784 udelay(10);
8785
Linus Torvalds1da177e2005-04-16 15:20:36 -07008786 tw32(MAC_LED_CTRL, tp->led_ctrl);
8787
8788 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008789 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008790 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8791 udelay(10);
8792 }
8793 tw32_f(MAC_RX_MODE, tp->rx_mode);
8794 udelay(10);
8795
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008796 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008797 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008798 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008799 /* Set drive transmission level to 1.2V */
8800 /* only if the signal pre-emphasis bit is not set */
8801 val = tr32(MAC_SERDES_CFG);
8802 val &= 0xfffff000;
8803 val |= 0x880;
8804 tw32(MAC_SERDES_CFG, val);
8805 }
8806 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8807 tw32(MAC_SERDES_CFG, 0x616000);
8808 }
8809
8810 /* Prevent chip from dropping frames when flow control
8811 * is enabled.
8812 */
Matt Carlson666bc832010-01-20 16:58:03 +00008813 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8814 val = 1;
8815 else
8816 val = 2;
8817 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008818
8819 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008820 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008821 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00008822 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008823 }
8824
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008825 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00008826 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08008827 u32 tmp;
8828
8829 tmp = tr32(SERDES_RX_CTRL);
8830 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8831 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8832 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8833 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8834 }
8835
Joe Perches63c3a662011-04-26 08:12:10 +00008836 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008837 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8838 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008839 tp->link_config.speed = tp->link_config.orig_speed;
8840 tp->link_config.duplex = tp->link_config.orig_duplex;
8841 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008843
Matt Carlsondd477002008-05-25 23:45:58 -07008844 err = tg3_setup_phy(tp, 0);
8845 if (err)
8846 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008847
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008848 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8849 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008850 u32 tmp;
8851
8852 /* Clear CRC stats. */
8853 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8854 tg3_writephy(tp, MII_TG3_TEST1,
8855 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008856 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008858 }
8859 }
8860
8861 __tg3_set_rx_mode(tp->dev);
8862
8863 /* Initialize receive rules. */
8864 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8865 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8866 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8867 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8868
Joe Perches63c3a662011-04-26 08:12:10 +00008869 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008870 limit = 8;
8871 else
8872 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008873 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008874 limit -= 4;
8875 switch (limit) {
8876 case 16:
8877 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8878 case 15:
8879 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8880 case 14:
8881 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8882 case 13:
8883 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8884 case 12:
8885 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8886 case 11:
8887 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8888 case 10:
8889 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8890 case 9:
8891 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8892 case 8:
8893 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8894 case 7:
8895 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8896 case 6:
8897 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8898 case 5:
8899 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8900 case 4:
8901 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8902 case 3:
8903 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8904 case 2:
8905 case 1:
8906
8907 default:
8908 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008910
Joe Perches63c3a662011-04-26 08:12:10 +00008911 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07008912 /* Write our heartbeat update interval to APE. */
8913 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8914 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008915
Linus Torvalds1da177e2005-04-16 15:20:36 -07008916 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8917
Linus Torvalds1da177e2005-04-16 15:20:36 -07008918 return 0;
8919}
8920
8921/* Called at device open time to get the chip ready for
8922 * packet processing. Invoked with tp->lock held.
8923 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008924static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008926 tg3_switch_clocks(tp);
8927
8928 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8929
Matt Carlson2f751b62008-08-04 23:17:34 -07008930 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931}
8932
8933#define TG3_STAT_ADD32(PSTAT, REG) \
8934do { u32 __val = tr32(REG); \
8935 (PSTAT)->low += __val; \
8936 if ((PSTAT)->low < __val) \
8937 (PSTAT)->high += 1; \
8938} while (0)
8939
8940static void tg3_periodic_fetch_stats(struct tg3 *tp)
8941{
8942 struct tg3_hw_stats *sp = tp->hw_stats;
8943
8944 if (!netif_carrier_ok(tp->dev))
8945 return;
8946
8947 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8948 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8949 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8950 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8951 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8952 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8953 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8954 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8955 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8956 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8957 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8958 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8959 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8960
8961 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8962 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8963 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8964 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8965 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8966 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8967 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8968 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8969 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8970 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8971 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8972 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8973 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8974 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008975
8976 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00008977 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8978 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
8979 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00008980 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8981 } else {
8982 u32 val = tr32(HOSTCC_FLOW_ATTN);
8983 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
8984 if (val) {
8985 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
8986 sp->rx_discards.low += val;
8987 if (sp->rx_discards.low < val)
8988 sp->rx_discards.high += 1;
8989 }
8990 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
8991 }
Michael Chan463d3052006-05-22 16:36:27 -07008992 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008993}
8994
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008995static void tg3_chk_missed_msi(struct tg3 *tp)
8996{
8997 u32 i;
8998
8999 for (i = 0; i < tp->irq_cnt; i++) {
9000 struct tg3_napi *tnapi = &tp->napi[i];
9001
9002 if (tg3_has_work(tnapi)) {
9003 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9004 tnapi->last_tx_cons == tnapi->tx_cons) {
9005 if (tnapi->chk_msi_cnt < 1) {
9006 tnapi->chk_msi_cnt++;
9007 return;
9008 }
9009 tw32_mailbox(tnapi->int_mbox,
9010 tnapi->last_tag << 24);
9011 }
9012 }
9013 tnapi->chk_msi_cnt = 0;
9014 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9015 tnapi->last_tx_cons = tnapi->tx_cons;
9016 }
9017}
9018
Linus Torvalds1da177e2005-04-16 15:20:36 -07009019static void tg3_timer(unsigned long __opaque)
9020{
9021 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009022
Michael Chanf475f162006-03-27 23:20:14 -08009023 if (tp->irq_sync)
9024 goto restart_timer;
9025
David S. Millerf47c11e2005-06-24 20:18:35 -07009026 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009027
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009028 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
9029 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
9030 tg3_chk_missed_msi(tp);
9031
Joe Perches63c3a662011-04-26 08:12:10 +00009032 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009033 /* All of this garbage is because when using non-tagged
9034 * IRQ status the mailbox/status_block protocol the chip
9035 * uses with the cpu is race prone.
9036 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009037 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009038 tw32(GRC_LOCAL_CTRL,
9039 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9040 } else {
9041 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009042 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009044
David S. Millerfac9b832005-05-18 22:46:34 -07009045 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +00009046 tg3_flag_set(tp, RESTART_TIMER);
David S. Millerf47c11e2005-06-24 20:18:35 -07009047 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07009048 schedule_work(&tp->reset_task);
9049 return;
9050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009051 }
9052
Linus Torvalds1da177e2005-04-16 15:20:36 -07009053 /* This part only runs once per second. */
9054 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009055 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009056 tg3_periodic_fetch_stats(tp);
9057
Matt Carlsonb0c59432011-05-19 12:12:48 +00009058 if (tp->setlpicnt && !--tp->setlpicnt)
9059 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009060
Joe Perches63c3a662011-04-26 08:12:10 +00009061 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009062 u32 mac_stat;
9063 int phy_event;
9064
9065 mac_stat = tr32(MAC_STATUS);
9066
9067 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009068 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009069 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9070 phy_event = 1;
9071 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9072 phy_event = 1;
9073
9074 if (phy_event)
9075 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009076 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009077 u32 mac_stat = tr32(MAC_STATUS);
9078 int need_setup = 0;
9079
9080 if (netif_carrier_ok(tp->dev) &&
9081 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9082 need_setup = 1;
9083 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009084 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009085 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9086 MAC_STATUS_SIGNAL_DET))) {
9087 need_setup = 1;
9088 }
9089 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009090 if (!tp->serdes_counter) {
9091 tw32_f(MAC_MODE,
9092 (tp->mac_mode &
9093 ~MAC_MODE_PORT_MODE_MASK));
9094 udelay(40);
9095 tw32_f(MAC_MODE, tp->mac_mode);
9096 udelay(40);
9097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009098 tg3_setup_phy(tp, 0);
9099 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009100 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009101 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009102 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104
9105 tp->timer_counter = tp->timer_multiplier;
9106 }
9107
Michael Chan130b8e42006-09-27 16:00:40 -07009108 /* Heartbeat is only sent once every 2 seconds.
9109 *
9110 * The heartbeat is to tell the ASF firmware that the host
9111 * driver is still alive. In the event that the OS crashes,
9112 * ASF needs to reset the hardware to free up the FIFO space
9113 * that may be filled with rx packets destined for the host.
9114 * If the FIFO is full, ASF will no longer function properly.
9115 *
9116 * Unintended resets have been reported on real time kernels
9117 * where the timer doesn't run on time. Netpoll will also have
9118 * same problem.
9119 *
9120 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9121 * to check the ring condition when the heartbeat is expiring
9122 * before doing the reset. This will prevent most unintended
9123 * resets.
9124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009125 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009126 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009127 tg3_wait_for_event_ack(tp);
9128
Michael Chanbbadf502006-04-06 21:46:34 -07009129 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009130 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009131 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009132 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9133 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009134
9135 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009136 }
9137 tp->asf_counter = tp->asf_multiplier;
9138 }
9139
David S. Millerf47c11e2005-06-24 20:18:35 -07009140 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009141
Michael Chanf475f162006-03-27 23:20:14 -08009142restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009143 tp->timer.expires = jiffies + tp->timer_offset;
9144 add_timer(&tp->timer);
9145}
9146
Matt Carlson4f125f42009-09-01 12:55:02 +00009147static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009148{
David Howells7d12e782006-10-05 14:55:46 +01009149 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009150 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009151 char *name;
9152 struct tg3_napi *tnapi = &tp->napi[irq_num];
9153
9154 if (tp->irq_cnt == 1)
9155 name = tp->dev->name;
9156 else {
9157 name = &tnapi->irq_lbl[0];
9158 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9159 name[IFNAMSIZ-1] = 0;
9160 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009161
Joe Perches63c3a662011-04-26 08:12:10 +00009162 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009163 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009164 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009165 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009166 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009167 } else {
9168 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009169 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009170 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009171 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009172 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009173
9174 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009175}
9176
Michael Chan79381092005-04-21 17:13:59 -07009177static int tg3_test_interrupt(struct tg3 *tp)
9178{
Matt Carlson09943a12009-08-28 14:01:57 +00009179 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009180 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009181 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009182 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009183
Michael Chand4bc3922005-05-29 14:59:20 -07009184 if (!netif_running(dev))
9185 return -ENODEV;
9186
Michael Chan79381092005-04-21 17:13:59 -07009187 tg3_disable_ints(tp);
9188
Matt Carlson4f125f42009-09-01 12:55:02 +00009189 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009190
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009191 /*
9192 * Turn off MSI one shot mode. Otherwise this test has no
9193 * observable way to know whether the interrupt was delivered.
9194 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009195 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009196 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9197 tw32(MSGINT_MODE, val);
9198 }
9199
Matt Carlson4f125f42009-09-01 12:55:02 +00009200 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009201 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009202 if (err)
9203 return err;
9204
Matt Carlson898a56f2009-08-28 14:02:40 +00009205 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009206 tg3_enable_ints(tp);
9207
9208 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009209 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009210
9211 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009212 u32 int_mbox, misc_host_ctrl;
9213
Matt Carlson898a56f2009-08-28 14:02:40 +00009214 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009215 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9216
9217 if ((int_mbox != 0) ||
9218 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9219 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009220 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009221 }
9222
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009223 if (tg3_flag(tp, 57765_PLUS) &&
9224 tnapi->hw_status->status_tag != tnapi->last_tag)
9225 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9226
Michael Chan79381092005-04-21 17:13:59 -07009227 msleep(10);
9228 }
9229
9230 tg3_disable_ints(tp);
9231
Matt Carlson4f125f42009-09-01 12:55:02 +00009232 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009233
Matt Carlson4f125f42009-09-01 12:55:02 +00009234 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009235
9236 if (err)
9237 return err;
9238
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009239 if (intr_ok) {
9240 /* Reenable MSI one shot mode. */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009241 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009242 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9243 tw32(MSGINT_MODE, val);
9244 }
Michael Chan79381092005-04-21 17:13:59 -07009245 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009246 }
Michael Chan79381092005-04-21 17:13:59 -07009247
9248 return -EIO;
9249}
9250
9251/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9252 * successfully restored
9253 */
9254static int tg3_test_msi(struct tg3 *tp)
9255{
Michael Chan79381092005-04-21 17:13:59 -07009256 int err;
9257 u16 pci_cmd;
9258
Joe Perches63c3a662011-04-26 08:12:10 +00009259 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009260 return 0;
9261
9262 /* Turn off SERR reporting in case MSI terminates with Master
9263 * Abort.
9264 */
9265 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9266 pci_write_config_word(tp->pdev, PCI_COMMAND,
9267 pci_cmd & ~PCI_COMMAND_SERR);
9268
9269 err = tg3_test_interrupt(tp);
9270
9271 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9272
9273 if (!err)
9274 return 0;
9275
9276 /* other failures */
9277 if (err != -EIO)
9278 return err;
9279
9280 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009281 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9282 "to INTx mode. Please report this failure to the PCI "
9283 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009284
Matt Carlson4f125f42009-09-01 12:55:02 +00009285 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009286
Michael Chan79381092005-04-21 17:13:59 -07009287 pci_disable_msi(tp->pdev);
9288
Joe Perches63c3a662011-04-26 08:12:10 +00009289 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009290 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009291
Matt Carlson4f125f42009-09-01 12:55:02 +00009292 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009293 if (err)
9294 return err;
9295
9296 /* Need to reset the chip because the MSI cycle may have terminated
9297 * with Master Abort.
9298 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009299 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009300
Michael Chan944d9802005-05-29 14:57:48 -07009301 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009302 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009303
David S. Millerf47c11e2005-06-24 20:18:35 -07009304 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009305
9306 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009307 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009308
9309 return err;
9310}
9311
Matt Carlson9e9fd122009-01-19 16:57:45 -08009312static int tg3_request_firmware(struct tg3 *tp)
9313{
9314 const __be32 *fw_data;
9315
9316 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009317 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9318 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009319 return -ENOENT;
9320 }
9321
9322 fw_data = (void *)tp->fw->data;
9323
9324 /* Firmware blob starts with version numbers, followed by
9325 * start address and _full_ length including BSS sections
9326 * (which must be longer than the actual data, of course
9327 */
9328
9329 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9330 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009331 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9332 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009333 release_firmware(tp->fw);
9334 tp->fw = NULL;
9335 return -EINVAL;
9336 }
9337
9338 /* We no longer need firmware; we have it. */
9339 tp->fw_needed = NULL;
9340 return 0;
9341}
9342
Matt Carlson679563f2009-09-01 12:55:46 +00009343static bool tg3_enable_msix(struct tg3 *tp)
9344{
9345 int i, rc, cpus = num_online_cpus();
9346 struct msix_entry msix_ent[tp->irq_max];
9347
9348 if (cpus == 1)
9349 /* Just fallback to the simpler MSI mode. */
9350 return false;
9351
9352 /*
9353 * We want as many rx rings enabled as there are cpus.
9354 * The first MSIX vector only deals with link interrupts, etc,
9355 * so we add one to the number of vectors we are requesting.
9356 */
9357 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9358
9359 for (i = 0; i < tp->irq_max; i++) {
9360 msix_ent[i].entry = i;
9361 msix_ent[i].vector = 0;
9362 }
9363
9364 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009365 if (rc < 0) {
9366 return false;
9367 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009368 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9369 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009370 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9371 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009372 tp->irq_cnt = rc;
9373 }
9374
9375 for (i = 0; i < tp->irq_max; i++)
9376 tp->napi[i].irq_vec = msix_ent[i].vector;
9377
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009378 netif_set_real_num_tx_queues(tp->dev, 1);
9379 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9380 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9381 pci_disable_msix(tp->pdev);
9382 return false;
9383 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009384
9385 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009386 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009387
9388 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9389 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009390 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009391 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9392 }
9393 }
Matt Carlson2430b032010-06-05 17:24:34 +00009394
Matt Carlson679563f2009-09-01 12:55:46 +00009395 return true;
9396}
9397
Matt Carlson07b01732009-08-28 14:01:15 +00009398static void tg3_ints_init(struct tg3 *tp)
9399{
Joe Perches63c3a662011-04-26 08:12:10 +00009400 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9401 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009402 /* All MSI supporting chips should support tagged
9403 * status. Assert that this is the case.
9404 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009405 netdev_warn(tp->dev,
9406 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009407 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009408 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009409
Joe Perches63c3a662011-04-26 08:12:10 +00009410 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9411 tg3_flag_set(tp, USING_MSIX);
9412 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9413 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009414
Joe Perches63c3a662011-04-26 08:12:10 +00009415 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009416 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009417 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009418 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009419 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9420 }
9421defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009422 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009423 tp->irq_cnt = 1;
9424 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009425 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009426 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009427 }
Matt Carlson07b01732009-08-28 14:01:15 +00009428}
9429
9430static void tg3_ints_fini(struct tg3 *tp)
9431{
Joe Perches63c3a662011-04-26 08:12:10 +00009432 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009433 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009434 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009435 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009436 tg3_flag_clear(tp, USING_MSI);
9437 tg3_flag_clear(tp, USING_MSIX);
9438 tg3_flag_clear(tp, ENABLE_RSS);
9439 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009440}
9441
Linus Torvalds1da177e2005-04-16 15:20:36 -07009442static int tg3_open(struct net_device *dev)
9443{
9444 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009445 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446
Matt Carlson9e9fd122009-01-19 16:57:45 -08009447 if (tp->fw_needed) {
9448 err = tg3_request_firmware(tp);
9449 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9450 if (err)
9451 return err;
9452 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009453 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009454 tg3_flag_clear(tp, TSO_CAPABLE);
9455 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009456 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009457 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009458 }
9459 }
9460
Michael Chanc49a1562006-12-17 17:07:29 -08009461 netif_carrier_off(tp->dev);
9462
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009463 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009464 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009465 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009466
9467 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009468
Linus Torvalds1da177e2005-04-16 15:20:36 -07009469 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009470 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009471
David S. Millerf47c11e2005-06-24 20:18:35 -07009472 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009473
Matt Carlson679563f2009-09-01 12:55:46 +00009474 /*
9475 * Setup interrupts first so we know how
9476 * many NAPI resources to allocate
9477 */
9478 tg3_ints_init(tp);
9479
Linus Torvalds1da177e2005-04-16 15:20:36 -07009480 /* The placement of this call is tied
9481 * to the setup and use of Host TX descriptors.
9482 */
9483 err = tg3_alloc_consistent(tp);
9484 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009485 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009486
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009487 tg3_napi_init(tp);
9488
Matt Carlsonfed97812009-09-01 13:10:19 +00009489 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009490
Matt Carlson4f125f42009-09-01 12:55:02 +00009491 for (i = 0; i < tp->irq_cnt; i++) {
9492 struct tg3_napi *tnapi = &tp->napi[i];
9493 err = tg3_request_irq(tp, i);
9494 if (err) {
9495 for (i--; i >= 0; i--)
9496 free_irq(tnapi->irq_vec, tnapi);
9497 break;
9498 }
9499 }
Matt Carlson07b01732009-08-28 14:01:15 +00009500
9501 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009502 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009503
David S. Millerf47c11e2005-06-24 20:18:35 -07009504 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009505
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009506 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009507 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009508 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009509 tg3_free_rings(tp);
9510 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009511 if (tg3_flag(tp, TAGGED_STATUS) &&
9512 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9513 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765)
David S. Millerfac9b832005-05-18 22:46:34 -07009514 tp->timer_offset = HZ;
9515 else
9516 tp->timer_offset = HZ / 10;
9517
9518 BUG_ON(tp->timer_offset > HZ);
9519 tp->timer_counter = tp->timer_multiplier =
9520 (HZ / tp->timer_offset);
9521 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009522 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009523
9524 init_timer(&tp->timer);
9525 tp->timer.expires = jiffies + tp->timer_offset;
9526 tp->timer.data = (unsigned long) tp;
9527 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009528 }
9529
David S. Millerf47c11e2005-06-24 20:18:35 -07009530 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009531
Matt Carlson07b01732009-08-28 14:01:15 +00009532 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009533 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009534
Joe Perches63c3a662011-04-26 08:12:10 +00009535 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009536 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009537
Michael Chan79381092005-04-21 17:13:59 -07009538 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009539 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009540 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009541 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009542 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009543
Matt Carlson679563f2009-09-01 12:55:46 +00009544 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009545 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009546
Joe Perches63c3a662011-04-26 08:12:10 +00009547 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009548 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009549
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009550 tw32(PCIE_TRANSACTION_CFG,
9551 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009552 }
Michael Chan79381092005-04-21 17:13:59 -07009553 }
9554
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009555 tg3_phy_start(tp);
9556
David S. Millerf47c11e2005-06-24 20:18:35 -07009557 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009558
Michael Chan79381092005-04-21 17:13:59 -07009559 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009560 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009561 tg3_enable_ints(tp);
9562
David S. Millerf47c11e2005-06-24 20:18:35 -07009563 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009564
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009565 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009566
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009567 /*
9568 * Reset loopback feature if it was turned on while the device was down
9569 * make sure that it's installed properly now.
9570 */
9571 if (dev->features & NETIF_F_LOOPBACK)
9572 tg3_set_loopback(dev, dev->features);
9573
Linus Torvalds1da177e2005-04-16 15:20:36 -07009574 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009575
Matt Carlson679563f2009-09-01 12:55:46 +00009576err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009577 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9578 struct tg3_napi *tnapi = &tp->napi[i];
9579 free_irq(tnapi->irq_vec, tnapi);
9580 }
Matt Carlson07b01732009-08-28 14:01:15 +00009581
Matt Carlson679563f2009-09-01 12:55:46 +00009582err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009583 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009584 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009585 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009586
9587err_out1:
9588 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +00009589 tg3_frob_aux_power(tp, false);
9590 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +00009591 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009592}
9593
Eric Dumazet511d2222010-07-07 20:44:24 +00009594static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9595 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009596static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9597
9598static int tg3_close(struct net_device *dev)
9599{
Matt Carlson4f125f42009-09-01 12:55:02 +00009600 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009601 struct tg3 *tp = netdev_priv(dev);
9602
Matt Carlsonfed97812009-09-01 13:10:19 +00009603 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009604 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009605
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009606 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009607
9608 del_timer_sync(&tp->timer);
9609
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009610 tg3_phy_stop(tp);
9611
David S. Millerf47c11e2005-06-24 20:18:35 -07009612 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009613
9614 tg3_disable_ints(tp);
9615
Michael Chan944d9802005-05-29 14:57:48 -07009616 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009617 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009618 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009619
David S. Millerf47c11e2005-06-24 20:18:35 -07009620 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009621
Matt Carlson4f125f42009-09-01 12:55:02 +00009622 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9623 struct tg3_napi *tnapi = &tp->napi[i];
9624 free_irq(tnapi->irq_vec, tnapi);
9625 }
Matt Carlson07b01732009-08-28 14:01:15 +00009626
9627 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009628
Eric Dumazet511d2222010-07-07 20:44:24 +00009629 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9630
Linus Torvalds1da177e2005-04-16 15:20:36 -07009631 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9632 sizeof(tp->estats_prev));
9633
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009634 tg3_napi_fini(tp);
9635
Linus Torvalds1da177e2005-04-16 15:20:36 -07009636 tg3_free_consistent(tp);
9637
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009638 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009639
9640 netif_carrier_off(tp->dev);
9641
Linus Torvalds1da177e2005-04-16 15:20:36 -07009642 return 0;
9643}
9644
Eric Dumazet511d2222010-07-07 20:44:24 +00009645static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009646{
9647 return ((u64)val->high << 32) | ((u64)val->low);
9648}
9649
Eric Dumazet511d2222010-07-07 20:44:24 +00009650static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009651{
9652 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9653
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009654 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009655 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9656 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009657 u32 val;
9658
David S. Millerf47c11e2005-06-24 20:18:35 -07009659 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009660 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9661 tg3_writephy(tp, MII_TG3_TEST1,
9662 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009663 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009664 } else
9665 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009666 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009667
9668 tp->phy_crc_errors += val;
9669
9670 return tp->phy_crc_errors;
9671 }
9672
9673 return get_stat64(&hw_stats->rx_fcs_errors);
9674}
9675
9676#define ESTAT_ADD(member) \
9677 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009678 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009679
9680static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9681{
9682 struct tg3_ethtool_stats *estats = &tp->estats;
9683 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9684 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9685
9686 if (!hw_stats)
9687 return old_estats;
9688
9689 ESTAT_ADD(rx_octets);
9690 ESTAT_ADD(rx_fragments);
9691 ESTAT_ADD(rx_ucast_packets);
9692 ESTAT_ADD(rx_mcast_packets);
9693 ESTAT_ADD(rx_bcast_packets);
9694 ESTAT_ADD(rx_fcs_errors);
9695 ESTAT_ADD(rx_align_errors);
9696 ESTAT_ADD(rx_xon_pause_rcvd);
9697 ESTAT_ADD(rx_xoff_pause_rcvd);
9698 ESTAT_ADD(rx_mac_ctrl_rcvd);
9699 ESTAT_ADD(rx_xoff_entered);
9700 ESTAT_ADD(rx_frame_too_long_errors);
9701 ESTAT_ADD(rx_jabbers);
9702 ESTAT_ADD(rx_undersize_packets);
9703 ESTAT_ADD(rx_in_length_errors);
9704 ESTAT_ADD(rx_out_length_errors);
9705 ESTAT_ADD(rx_64_or_less_octet_packets);
9706 ESTAT_ADD(rx_65_to_127_octet_packets);
9707 ESTAT_ADD(rx_128_to_255_octet_packets);
9708 ESTAT_ADD(rx_256_to_511_octet_packets);
9709 ESTAT_ADD(rx_512_to_1023_octet_packets);
9710 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9711 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9712 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9713 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9714 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9715
9716 ESTAT_ADD(tx_octets);
9717 ESTAT_ADD(tx_collisions);
9718 ESTAT_ADD(tx_xon_sent);
9719 ESTAT_ADD(tx_xoff_sent);
9720 ESTAT_ADD(tx_flow_control);
9721 ESTAT_ADD(tx_mac_errors);
9722 ESTAT_ADD(tx_single_collisions);
9723 ESTAT_ADD(tx_mult_collisions);
9724 ESTAT_ADD(tx_deferred);
9725 ESTAT_ADD(tx_excessive_collisions);
9726 ESTAT_ADD(tx_late_collisions);
9727 ESTAT_ADD(tx_collide_2times);
9728 ESTAT_ADD(tx_collide_3times);
9729 ESTAT_ADD(tx_collide_4times);
9730 ESTAT_ADD(tx_collide_5times);
9731 ESTAT_ADD(tx_collide_6times);
9732 ESTAT_ADD(tx_collide_7times);
9733 ESTAT_ADD(tx_collide_8times);
9734 ESTAT_ADD(tx_collide_9times);
9735 ESTAT_ADD(tx_collide_10times);
9736 ESTAT_ADD(tx_collide_11times);
9737 ESTAT_ADD(tx_collide_12times);
9738 ESTAT_ADD(tx_collide_13times);
9739 ESTAT_ADD(tx_collide_14times);
9740 ESTAT_ADD(tx_collide_15times);
9741 ESTAT_ADD(tx_ucast_packets);
9742 ESTAT_ADD(tx_mcast_packets);
9743 ESTAT_ADD(tx_bcast_packets);
9744 ESTAT_ADD(tx_carrier_sense_errors);
9745 ESTAT_ADD(tx_discards);
9746 ESTAT_ADD(tx_errors);
9747
9748 ESTAT_ADD(dma_writeq_full);
9749 ESTAT_ADD(dma_write_prioq_full);
9750 ESTAT_ADD(rxbds_empty);
9751 ESTAT_ADD(rx_discards);
9752 ESTAT_ADD(rx_errors);
9753 ESTAT_ADD(rx_threshold_hit);
9754
9755 ESTAT_ADD(dma_readq_full);
9756 ESTAT_ADD(dma_read_prioq_full);
9757 ESTAT_ADD(tx_comp_queue_full);
9758
9759 ESTAT_ADD(ring_set_send_prod_index);
9760 ESTAT_ADD(ring_status_update);
9761 ESTAT_ADD(nic_irqs);
9762 ESTAT_ADD(nic_avoided_irqs);
9763 ESTAT_ADD(nic_tx_threshold_hit);
9764
Matt Carlson4452d092011-05-19 12:12:51 +00009765 ESTAT_ADD(mbuf_lwm_thresh_hit);
9766
Linus Torvalds1da177e2005-04-16 15:20:36 -07009767 return estats;
9768}
9769
Eric Dumazet511d2222010-07-07 20:44:24 +00009770static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9771 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009772{
9773 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009774 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009775 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9776
9777 if (!hw_stats)
9778 return old_stats;
9779
9780 stats->rx_packets = old_stats->rx_packets +
9781 get_stat64(&hw_stats->rx_ucast_packets) +
9782 get_stat64(&hw_stats->rx_mcast_packets) +
9783 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009784
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785 stats->tx_packets = old_stats->tx_packets +
9786 get_stat64(&hw_stats->tx_ucast_packets) +
9787 get_stat64(&hw_stats->tx_mcast_packets) +
9788 get_stat64(&hw_stats->tx_bcast_packets);
9789
9790 stats->rx_bytes = old_stats->rx_bytes +
9791 get_stat64(&hw_stats->rx_octets);
9792 stats->tx_bytes = old_stats->tx_bytes +
9793 get_stat64(&hw_stats->tx_octets);
9794
9795 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009796 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009797 stats->tx_errors = old_stats->tx_errors +
9798 get_stat64(&hw_stats->tx_errors) +
9799 get_stat64(&hw_stats->tx_mac_errors) +
9800 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9801 get_stat64(&hw_stats->tx_discards);
9802
9803 stats->multicast = old_stats->multicast +
9804 get_stat64(&hw_stats->rx_mcast_packets);
9805 stats->collisions = old_stats->collisions +
9806 get_stat64(&hw_stats->tx_collisions);
9807
9808 stats->rx_length_errors = old_stats->rx_length_errors +
9809 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9810 get_stat64(&hw_stats->rx_undersize_packets);
9811
9812 stats->rx_over_errors = old_stats->rx_over_errors +
9813 get_stat64(&hw_stats->rxbds_empty);
9814 stats->rx_frame_errors = old_stats->rx_frame_errors +
9815 get_stat64(&hw_stats->rx_align_errors);
9816 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9817 get_stat64(&hw_stats->tx_discards);
9818 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9819 get_stat64(&hw_stats->tx_carrier_sense_errors);
9820
9821 stats->rx_crc_errors = old_stats->rx_crc_errors +
9822 calc_crc_errors(tp);
9823
John W. Linville4f63b872005-09-12 14:43:18 -07009824 stats->rx_missed_errors = old_stats->rx_missed_errors +
9825 get_stat64(&hw_stats->rx_discards);
9826
Eric Dumazetb0057c52010-10-10 19:55:52 +00009827 stats->rx_dropped = tp->rx_dropped;
9828
Linus Torvalds1da177e2005-04-16 15:20:36 -07009829 return stats;
9830}
9831
9832static inline u32 calc_crc(unsigned char *buf, int len)
9833{
9834 u32 reg;
9835 u32 tmp;
9836 int j, k;
9837
9838 reg = 0xffffffff;
9839
9840 for (j = 0; j < len; j++) {
9841 reg ^= buf[j];
9842
9843 for (k = 0; k < 8; k++) {
9844 tmp = reg & 0x01;
9845
9846 reg >>= 1;
9847
Matt Carlson859a5882010-04-05 10:19:28 +00009848 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009849 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850 }
9851 }
9852
9853 return ~reg;
9854}
9855
9856static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9857{
9858 /* accept or reject all multicast frames */
9859 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9860 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9861 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9862 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9863}
9864
9865static void __tg3_set_rx_mode(struct net_device *dev)
9866{
9867 struct tg3 *tp = netdev_priv(dev);
9868 u32 rx_mode;
9869
9870 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9871 RX_MODE_KEEP_VLAN_TAG);
9872
Matt Carlsonbf933c82011-01-25 15:58:49 +00009873#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009874 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9875 * flag clear.
9876 */
Joe Perches63c3a662011-04-26 08:12:10 +00009877 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009878 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9879#endif
9880
9881 if (dev->flags & IFF_PROMISC) {
9882 /* Promiscuous mode. */
9883 rx_mode |= RX_MODE_PROMISC;
9884 } else if (dev->flags & IFF_ALLMULTI) {
9885 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009886 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009887 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009888 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009889 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009890 } else {
9891 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +00009892 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009893 u32 mc_filter[4] = { 0, };
9894 u32 regidx;
9895 u32 bit;
9896 u32 crc;
9897
Jiri Pirko22bedad2010-04-01 21:22:57 +00009898 netdev_for_each_mc_addr(ha, dev) {
9899 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009900 bit = ~crc & 0x7f;
9901 regidx = (bit & 0x60) >> 5;
9902 bit &= 0x1f;
9903 mc_filter[regidx] |= (1 << bit);
9904 }
9905
9906 tw32(MAC_HASH_REG_0, mc_filter[0]);
9907 tw32(MAC_HASH_REG_1, mc_filter[1]);
9908 tw32(MAC_HASH_REG_2, mc_filter[2]);
9909 tw32(MAC_HASH_REG_3, mc_filter[3]);
9910 }
9911
9912 if (rx_mode != tp->rx_mode) {
9913 tp->rx_mode = rx_mode;
9914 tw32_f(MAC_RX_MODE, rx_mode);
9915 udelay(10);
9916 }
9917}
9918
9919static void tg3_set_rx_mode(struct net_device *dev)
9920{
9921 struct tg3 *tp = netdev_priv(dev);
9922
Michael Chane75f7c92006-03-20 21:33:26 -08009923 if (!netif_running(dev))
9924 return;
9925
David S. Millerf47c11e2005-06-24 20:18:35 -07009926 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009927 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009928 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009929}
9930
Linus Torvalds1da177e2005-04-16 15:20:36 -07009931static int tg3_get_regs_len(struct net_device *dev)
9932{
Matt Carlson97bd8e42011-04-13 11:05:04 +00009933 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009934}
9935
9936static void tg3_get_regs(struct net_device *dev,
9937 struct ethtool_regs *regs, void *_p)
9938{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009939 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009940
9941 regs->version = 0;
9942
Matt Carlson97bd8e42011-04-13 11:05:04 +00009943 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009944
Matt Carlson80096062010-08-02 11:26:06 +00009945 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009946 return;
9947
David S. Millerf47c11e2005-06-24 20:18:35 -07009948 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009949
Matt Carlson97bd8e42011-04-13 11:05:04 +00009950 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009951
David S. Millerf47c11e2005-06-24 20:18:35 -07009952 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009953}
9954
9955static int tg3_get_eeprom_len(struct net_device *dev)
9956{
9957 struct tg3 *tp = netdev_priv(dev);
9958
9959 return tp->nvram_size;
9960}
9961
Linus Torvalds1da177e2005-04-16 15:20:36 -07009962static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9963{
9964 struct tg3 *tp = netdev_priv(dev);
9965 int ret;
9966 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009967 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009968 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009969
Joe Perches63c3a662011-04-26 08:12:10 +00009970 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +00009971 return -EINVAL;
9972
Matt Carlson80096062010-08-02 11:26:06 +00009973 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009974 return -EAGAIN;
9975
Linus Torvalds1da177e2005-04-16 15:20:36 -07009976 offset = eeprom->offset;
9977 len = eeprom->len;
9978 eeprom->len = 0;
9979
9980 eeprom->magic = TG3_EEPROM_MAGIC;
9981
9982 if (offset & 3) {
9983 /* adjustments to start on required 4 byte boundary */
9984 b_offset = offset & 3;
9985 b_count = 4 - b_offset;
9986 if (b_count > len) {
9987 /* i.e. offset=1 len=2 */
9988 b_count = len;
9989 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009990 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009991 if (ret)
9992 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009993 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009994 len -= b_count;
9995 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009996 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009997 }
9998
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009999 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010000 pd = &data[eeprom->len];
10001 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010002 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010003 if (ret) {
10004 eeprom->len += i;
10005 return ret;
10006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010007 memcpy(pd + i, &val, 4);
10008 }
10009 eeprom->len += i;
10010
10011 if (len & 3) {
10012 /* read last bytes not ending on 4 byte boundary */
10013 pd = &data[eeprom->len];
10014 b_count = len & 3;
10015 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010016 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010017 if (ret)
10018 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010019 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010020 eeprom->len += b_count;
10021 }
10022 return 0;
10023}
10024
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010025static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010026
10027static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10028{
10029 struct tg3 *tp = netdev_priv(dev);
10030 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010031 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010032 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010033 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010034
Matt Carlson80096062010-08-02 11:26:06 +000010035 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010036 return -EAGAIN;
10037
Joe Perches63c3a662011-04-26 08:12:10 +000010038 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010039 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010040 return -EINVAL;
10041
10042 offset = eeprom->offset;
10043 len = eeprom->len;
10044
10045 if ((b_offset = (offset & 3))) {
10046 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010047 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010048 if (ret)
10049 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010050 len += b_offset;
10051 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010052 if (len < 4)
10053 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010054 }
10055
10056 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010057 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010058 /* adjustments to end on required 4 byte boundary */
10059 odd_len = 1;
10060 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010061 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010062 if (ret)
10063 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010064 }
10065
10066 buf = data;
10067 if (b_offset || odd_len) {
10068 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010069 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010070 return -ENOMEM;
10071 if (b_offset)
10072 memcpy(buf, &start, 4);
10073 if (odd_len)
10074 memcpy(buf+len-4, &end, 4);
10075 memcpy(buf + b_offset, data, eeprom->len);
10076 }
10077
10078 ret = tg3_nvram_write_block(tp, offset, len, buf);
10079
10080 if (buf != data)
10081 kfree(buf);
10082
10083 return ret;
10084}
10085
10086static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10087{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010088 struct tg3 *tp = netdev_priv(dev);
10089
Joe Perches63c3a662011-04-26 08:12:10 +000010090 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010091 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010092 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010093 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010094 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10095 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010096 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010097
Linus Torvalds1da177e2005-04-16 15:20:36 -070010098 cmd->supported = (SUPPORTED_Autoneg);
10099
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010100 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010101 cmd->supported |= (SUPPORTED_1000baseT_Half |
10102 SUPPORTED_1000baseT_Full);
10103
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010104 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010105 cmd->supported |= (SUPPORTED_100baseT_Half |
10106 SUPPORTED_100baseT_Full |
10107 SUPPORTED_10baseT_Half |
10108 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010109 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010110 cmd->port = PORT_TP;
10111 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010112 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010113 cmd->port = PORT_FIBRE;
10114 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010115
Linus Torvalds1da177e2005-04-16 15:20:36 -070010116 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010117 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10118 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10119 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10120 cmd->advertising |= ADVERTISED_Pause;
10121 } else {
10122 cmd->advertising |= ADVERTISED_Pause |
10123 ADVERTISED_Asym_Pause;
10124 }
10125 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10126 cmd->advertising |= ADVERTISED_Asym_Pause;
10127 }
10128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010129 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010130 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010131 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +000010132 } else {
David Decotigny70739492011-04-27 18:32:40 +000010133 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010134 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010135 }
Matt Carlson882e9792009-09-01 13:21:36 +000010136 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010137 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010138 cmd->autoneg = tp->link_config.autoneg;
10139 cmd->maxtxpkt = 0;
10140 cmd->maxrxpkt = 0;
10141 return 0;
10142}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010143
Linus Torvalds1da177e2005-04-16 15:20:36 -070010144static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10145{
10146 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010147 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010148
Joe Perches63c3a662011-04-26 08:12:10 +000010149 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010150 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010151 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010152 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010153 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10154 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010155 }
10156
Matt Carlson7e5856b2009-02-25 14:23:01 +000010157 if (cmd->autoneg != AUTONEG_ENABLE &&
10158 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010159 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010160
10161 if (cmd->autoneg == AUTONEG_DISABLE &&
10162 cmd->duplex != DUPLEX_FULL &&
10163 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010164 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010165
Matt Carlson7e5856b2009-02-25 14:23:01 +000010166 if (cmd->autoneg == AUTONEG_ENABLE) {
10167 u32 mask = ADVERTISED_Autoneg |
10168 ADVERTISED_Pause |
10169 ADVERTISED_Asym_Pause;
10170
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010171 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010172 mask |= ADVERTISED_1000baseT_Half |
10173 ADVERTISED_1000baseT_Full;
10174
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010175 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010176 mask |= ADVERTISED_100baseT_Half |
10177 ADVERTISED_100baseT_Full |
10178 ADVERTISED_10baseT_Half |
10179 ADVERTISED_10baseT_Full |
10180 ADVERTISED_TP;
10181 else
10182 mask |= ADVERTISED_FIBRE;
10183
10184 if (cmd->advertising & ~mask)
10185 return -EINVAL;
10186
10187 mask &= (ADVERTISED_1000baseT_Half |
10188 ADVERTISED_1000baseT_Full |
10189 ADVERTISED_100baseT_Half |
10190 ADVERTISED_100baseT_Full |
10191 ADVERTISED_10baseT_Half |
10192 ADVERTISED_10baseT_Full);
10193
10194 cmd->advertising &= mask;
10195 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010196 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010197 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010198 return -EINVAL;
10199
10200 if (cmd->duplex != DUPLEX_FULL)
10201 return -EINVAL;
10202 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010203 if (speed != SPEED_100 &&
10204 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010205 return -EINVAL;
10206 }
10207 }
10208
David S. Millerf47c11e2005-06-24 20:18:35 -070010209 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010210
10211 tp->link_config.autoneg = cmd->autoneg;
10212 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010213 tp->link_config.advertising = (cmd->advertising |
10214 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010215 tp->link_config.speed = SPEED_INVALID;
10216 tp->link_config.duplex = DUPLEX_INVALID;
10217 } else {
10218 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010219 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010220 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010221 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010222
Michael Chan24fcad62006-12-17 17:06:46 -080010223 tp->link_config.orig_speed = tp->link_config.speed;
10224 tp->link_config.orig_duplex = tp->link_config.duplex;
10225 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10226
Linus Torvalds1da177e2005-04-16 15:20:36 -070010227 if (netif_running(dev))
10228 tg3_setup_phy(tp, 1);
10229
David S. Millerf47c11e2005-06-24 20:18:35 -070010230 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010231
Linus Torvalds1da177e2005-04-16 15:20:36 -070010232 return 0;
10233}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010234
Linus Torvalds1da177e2005-04-16 15:20:36 -070010235static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10236{
10237 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010238
Linus Torvalds1da177e2005-04-16 15:20:36 -070010239 strcpy(info->driver, DRV_MODULE_NAME);
10240 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010241 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010242 strcpy(info->bus_info, pci_name(tp->pdev));
10243}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010244
Linus Torvalds1da177e2005-04-16 15:20:36 -070010245static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10246{
10247 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010248
Joe Perches63c3a662011-04-26 08:12:10 +000010249 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010250 wol->supported = WAKE_MAGIC;
10251 else
10252 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010253 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010254 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010255 wol->wolopts = WAKE_MAGIC;
10256 memset(&wol->sopass, 0, sizeof(wol->sopass));
10257}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010258
Linus Torvalds1da177e2005-04-16 15:20:36 -070010259static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10260{
10261 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010262 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010263
Linus Torvalds1da177e2005-04-16 15:20:36 -070010264 if (wol->wolopts & ~WAKE_MAGIC)
10265 return -EINVAL;
10266 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010267 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010268 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010269
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010270 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10271
David S. Millerf47c11e2005-06-24 20:18:35 -070010272 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010273 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010274 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010275 else
Joe Perches63c3a662011-04-26 08:12:10 +000010276 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010277 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010278
Linus Torvalds1da177e2005-04-16 15:20:36 -070010279 return 0;
10280}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010281
Linus Torvalds1da177e2005-04-16 15:20:36 -070010282static u32 tg3_get_msglevel(struct net_device *dev)
10283{
10284 struct tg3 *tp = netdev_priv(dev);
10285 return tp->msg_enable;
10286}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010287
Linus Torvalds1da177e2005-04-16 15:20:36 -070010288static void tg3_set_msglevel(struct net_device *dev, u32 value)
10289{
10290 struct tg3 *tp = netdev_priv(dev);
10291 tp->msg_enable = value;
10292}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010293
Linus Torvalds1da177e2005-04-16 15:20:36 -070010294static int tg3_nway_reset(struct net_device *dev)
10295{
10296 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010297 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010298
Linus Torvalds1da177e2005-04-16 15:20:36 -070010299 if (!netif_running(dev))
10300 return -EAGAIN;
10301
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010302 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010303 return -EINVAL;
10304
Joe Perches63c3a662011-04-26 08:12:10 +000010305 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010306 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010307 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010308 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010309 } else {
10310 u32 bmcr;
10311
10312 spin_lock_bh(&tp->lock);
10313 r = -EINVAL;
10314 tg3_readphy(tp, MII_BMCR, &bmcr);
10315 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10316 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010317 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010318 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10319 BMCR_ANENABLE);
10320 r = 0;
10321 }
10322 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010323 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010324
Linus Torvalds1da177e2005-04-16 15:20:36 -070010325 return r;
10326}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010327
Linus Torvalds1da177e2005-04-16 15:20:36 -070010328static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10329{
10330 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010331
Matt Carlson2c49a442010-09-30 10:34:35 +000010332 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010333 ering->rx_mini_max_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010334 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010335 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010336 else
10337 ering->rx_jumbo_max_pending = 0;
10338
10339 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010340
10341 ering->rx_pending = tp->rx_pending;
10342 ering->rx_mini_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010343 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010344 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10345 else
10346 ering->rx_jumbo_pending = 0;
10347
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010348 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010349}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010350
Linus Torvalds1da177e2005-04-16 15:20:36 -070010351static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10352{
10353 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010354 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010355
Matt Carlson2c49a442010-09-30 10:34:35 +000010356 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10357 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010358 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10359 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010360 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010361 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010362 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010363
Michael Chanbbe832c2005-06-24 20:20:04 -070010364 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010365 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010366 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010367 irq_sync = 1;
10368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010369
Michael Chanbbe832c2005-06-24 20:20:04 -070010370 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010371
Linus Torvalds1da177e2005-04-16 15:20:36 -070010372 tp->rx_pending = ering->rx_pending;
10373
Joe Perches63c3a662011-04-26 08:12:10 +000010374 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010375 tp->rx_pending > 63)
10376 tp->rx_pending = 63;
10377 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010378
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010379 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010380 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010381
10382 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010383 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010384 err = tg3_restart_hw(tp, 1);
10385 if (!err)
10386 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010387 }
10388
David S. Millerf47c11e2005-06-24 20:18:35 -070010389 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010390
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010391 if (irq_sync && !err)
10392 tg3_phy_start(tp);
10393
Michael Chanb9ec6c12006-07-25 16:37:27 -070010394 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010395}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010396
Linus Torvalds1da177e2005-04-16 15:20:36 -070010397static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10398{
10399 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010400
Joe Perches63c3a662011-04-26 08:12:10 +000010401 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010402
Steve Glendinninge18ce342008-12-16 02:00:00 -080010403 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010404 epause->rx_pause = 1;
10405 else
10406 epause->rx_pause = 0;
10407
Steve Glendinninge18ce342008-12-16 02:00:00 -080010408 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010409 epause->tx_pause = 1;
10410 else
10411 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010412}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010413
Linus Torvalds1da177e2005-04-16 15:20:36 -070010414static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10415{
10416 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010417 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010418
Joe Perches63c3a662011-04-26 08:12:10 +000010419 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010420 u32 newadv;
10421 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010422
Matt Carlson27121682010-02-17 15:16:57 +000010423 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010424
Matt Carlson27121682010-02-17 15:16:57 +000010425 if (!(phydev->supported & SUPPORTED_Pause) ||
10426 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010427 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010428 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010429
Matt Carlson27121682010-02-17 15:16:57 +000010430 tp->link_config.flowctrl = 0;
10431 if (epause->rx_pause) {
10432 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010433
Matt Carlson27121682010-02-17 15:16:57 +000010434 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010435 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010436 newadv = ADVERTISED_Pause;
10437 } else
10438 newadv = ADVERTISED_Pause |
10439 ADVERTISED_Asym_Pause;
10440 } else if (epause->tx_pause) {
10441 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10442 newadv = ADVERTISED_Asym_Pause;
10443 } else
10444 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010445
Matt Carlson27121682010-02-17 15:16:57 +000010446 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010447 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010448 else
Joe Perches63c3a662011-04-26 08:12:10 +000010449 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010450
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010451 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010452 u32 oldadv = phydev->advertising &
10453 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10454 if (oldadv != newadv) {
10455 phydev->advertising &=
10456 ~(ADVERTISED_Pause |
10457 ADVERTISED_Asym_Pause);
10458 phydev->advertising |= newadv;
10459 if (phydev->autoneg) {
10460 /*
10461 * Always renegotiate the link to
10462 * inform our link partner of our
10463 * flow control settings, even if the
10464 * flow control is forced. Let
10465 * tg3_adjust_link() do the final
10466 * flow control setup.
10467 */
10468 return phy_start_aneg(phydev);
10469 }
10470 }
10471
10472 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010473 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010474 } else {
10475 tp->link_config.orig_advertising &=
10476 ~(ADVERTISED_Pause |
10477 ADVERTISED_Asym_Pause);
10478 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010479 }
10480 } else {
10481 int irq_sync = 0;
10482
10483 if (netif_running(dev)) {
10484 tg3_netif_stop(tp);
10485 irq_sync = 1;
10486 }
10487
10488 tg3_full_lock(tp, irq_sync);
10489
10490 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010491 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010492 else
Joe Perches63c3a662011-04-26 08:12:10 +000010493 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010494 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010495 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010496 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010497 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010498 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010499 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010500 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010501 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010502
10503 if (netif_running(dev)) {
10504 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10505 err = tg3_restart_hw(tp, 1);
10506 if (!err)
10507 tg3_netif_start(tp);
10508 }
10509
10510 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010512
Michael Chanb9ec6c12006-07-25 16:37:27 -070010513 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010514}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010515
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010516static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010517{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010518 switch (sset) {
10519 case ETH_SS_TEST:
10520 return TG3_NUM_TEST;
10521 case ETH_SS_STATS:
10522 return TG3_NUM_STATS;
10523 default:
10524 return -EOPNOTSUPP;
10525 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010526}
10527
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010528static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010529{
10530 switch (stringset) {
10531 case ETH_SS_STATS:
10532 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10533 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010534 case ETH_SS_TEST:
10535 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10536 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010537 default:
10538 WARN_ON(1); /* we need a WARN() */
10539 break;
10540 }
10541}
10542
stephen hemminger81b87092011-04-04 08:43:50 +000010543static int tg3_set_phys_id(struct net_device *dev,
10544 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010545{
10546 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010547
10548 if (!netif_running(tp->dev))
10549 return -EAGAIN;
10550
stephen hemminger81b87092011-04-04 08:43:50 +000010551 switch (state) {
10552 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010553 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010554
stephen hemminger81b87092011-04-04 08:43:50 +000010555 case ETHTOOL_ID_ON:
10556 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10557 LED_CTRL_1000MBPS_ON |
10558 LED_CTRL_100MBPS_ON |
10559 LED_CTRL_10MBPS_ON |
10560 LED_CTRL_TRAFFIC_OVERRIDE |
10561 LED_CTRL_TRAFFIC_BLINK |
10562 LED_CTRL_TRAFFIC_LED);
10563 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010564
stephen hemminger81b87092011-04-04 08:43:50 +000010565 case ETHTOOL_ID_OFF:
10566 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10567 LED_CTRL_TRAFFIC_OVERRIDE);
10568 break;
Michael Chan4009a932005-09-05 17:52:54 -070010569
stephen hemminger81b87092011-04-04 08:43:50 +000010570 case ETHTOOL_ID_INACTIVE:
10571 tw32(MAC_LED_CTRL, tp->led_ctrl);
10572 break;
Michael Chan4009a932005-09-05 17:52:54 -070010573 }
stephen hemminger81b87092011-04-04 08:43:50 +000010574
Michael Chan4009a932005-09-05 17:52:54 -070010575 return 0;
10576}
10577
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010578static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010579 struct ethtool_stats *estats, u64 *tmp_stats)
10580{
10581 struct tg3 *tp = netdev_priv(dev);
10582 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10583}
10584
Matt Carlson535a4902011-07-20 10:20:56 +000010585static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000010586{
10587 int i;
10588 __be32 *buf;
10589 u32 offset = 0, len = 0;
10590 u32 magic, val;
10591
Joe Perches63c3a662011-04-26 08:12:10 +000010592 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010593 return NULL;
10594
10595 if (magic == TG3_EEPROM_MAGIC) {
10596 for (offset = TG3_NVM_DIR_START;
10597 offset < TG3_NVM_DIR_END;
10598 offset += TG3_NVM_DIRENT_SIZE) {
10599 if (tg3_nvram_read(tp, offset, &val))
10600 return NULL;
10601
10602 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10603 TG3_NVM_DIRTYPE_EXTVPD)
10604 break;
10605 }
10606
10607 if (offset != TG3_NVM_DIR_END) {
10608 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10609 if (tg3_nvram_read(tp, offset + 4, &offset))
10610 return NULL;
10611
10612 offset = tg3_nvram_logical_addr(tp, offset);
10613 }
10614 }
10615
10616 if (!offset || !len) {
10617 offset = TG3_NVM_VPD_OFF;
10618 len = TG3_NVM_VPD_LEN;
10619 }
10620
10621 buf = kmalloc(len, GFP_KERNEL);
10622 if (buf == NULL)
10623 return NULL;
10624
10625 if (magic == TG3_EEPROM_MAGIC) {
10626 for (i = 0; i < len; i += 4) {
10627 /* The data is in little-endian format in NVRAM.
10628 * Use the big-endian read routines to preserve
10629 * the byte order as it exists in NVRAM.
10630 */
10631 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10632 goto error;
10633 }
10634 } else {
10635 u8 *ptr;
10636 ssize_t cnt;
10637 unsigned int pos = 0;
10638
10639 ptr = (u8 *)&buf[0];
10640 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10641 cnt = pci_read_vpd(tp->pdev, pos,
10642 len - pos, ptr);
10643 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10644 cnt = 0;
10645 else if (cnt < 0)
10646 goto error;
10647 }
10648 if (pos != len)
10649 goto error;
10650 }
10651
Matt Carlson535a4902011-07-20 10:20:56 +000010652 *vpdlen = len;
10653
Matt Carlsonc3e94502011-04-13 11:05:08 +000010654 return buf;
10655
10656error:
10657 kfree(buf);
10658 return NULL;
10659}
10660
Michael Chan566f86a2005-05-29 14:56:58 -070010661#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010662#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10663#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10664#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010665#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10666#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000010667#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070010668#define NVRAM_SELFBOOT_HW_SIZE 0x20
10669#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010670
10671static int tg3_test_nvram(struct tg3 *tp)
10672{
Matt Carlson535a4902011-07-20 10:20:56 +000010673 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010674 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010675 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010676
Joe Perches63c3a662011-04-26 08:12:10 +000010677 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010678 return 0;
10679
Matt Carlsone4f34112009-02-25 14:25:00 +000010680 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010681 return -EIO;
10682
Michael Chan1b277772006-03-20 22:27:48 -080010683 if (magic == TG3_EEPROM_MAGIC)
10684 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010685 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010686 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10687 TG3_EEPROM_SB_FORMAT_1) {
10688 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10689 case TG3_EEPROM_SB_REVISION_0:
10690 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10691 break;
10692 case TG3_EEPROM_SB_REVISION_2:
10693 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10694 break;
10695 case TG3_EEPROM_SB_REVISION_3:
10696 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10697 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010698 case TG3_EEPROM_SB_REVISION_4:
10699 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10700 break;
10701 case TG3_EEPROM_SB_REVISION_5:
10702 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10703 break;
10704 case TG3_EEPROM_SB_REVISION_6:
10705 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10706 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010707 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010708 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010709 }
10710 } else
Michael Chan1b277772006-03-20 22:27:48 -080010711 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010712 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10713 size = NVRAM_SELFBOOT_HW_SIZE;
10714 else
Michael Chan1b277772006-03-20 22:27:48 -080010715 return -EIO;
10716
10717 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010718 if (buf == NULL)
10719 return -ENOMEM;
10720
Michael Chan1b277772006-03-20 22:27:48 -080010721 err = -EIO;
10722 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010723 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10724 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010725 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010726 }
Michael Chan1b277772006-03-20 22:27:48 -080010727 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010728 goto out;
10729
Michael Chan1b277772006-03-20 22:27:48 -080010730 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010731 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010732 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010733 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010734 u8 *buf8 = (u8 *) buf, csum8 = 0;
10735
Al Virob9fc7dc2007-12-17 22:59:57 -080010736 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010737 TG3_EEPROM_SB_REVISION_2) {
10738 /* For rev 2, the csum doesn't include the MBA. */
10739 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10740 csum8 += buf8[i];
10741 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10742 csum8 += buf8[i];
10743 } else {
10744 for (i = 0; i < size; i++)
10745 csum8 += buf8[i];
10746 }
Michael Chan1b277772006-03-20 22:27:48 -080010747
Adrian Bunkad96b482006-04-05 22:21:04 -070010748 if (csum8 == 0) {
10749 err = 0;
10750 goto out;
10751 }
10752
10753 err = -EIO;
10754 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010755 }
Michael Chan566f86a2005-05-29 14:56:58 -070010756
Al Virob9fc7dc2007-12-17 22:59:57 -080010757 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010758 TG3_EEPROM_MAGIC_HW) {
10759 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010760 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010761 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010762
10763 /* Separate the parity bits and the data bytes. */
10764 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10765 if ((i == 0) || (i == 8)) {
10766 int l;
10767 u8 msk;
10768
10769 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10770 parity[k++] = buf8[i] & msk;
10771 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010772 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010773 int l;
10774 u8 msk;
10775
10776 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10777 parity[k++] = buf8[i] & msk;
10778 i++;
10779
10780 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10781 parity[k++] = buf8[i] & msk;
10782 i++;
10783 }
10784 data[j++] = buf8[i];
10785 }
10786
10787 err = -EIO;
10788 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10789 u8 hw8 = hweight8(data[i]);
10790
10791 if ((hw8 & 0x1) && parity[i])
10792 goto out;
10793 else if (!(hw8 & 0x1) && !parity[i])
10794 goto out;
10795 }
10796 err = 0;
10797 goto out;
10798 }
10799
Matt Carlson01c3a392011-03-09 16:58:20 +000010800 err = -EIO;
10801
Michael Chan566f86a2005-05-29 14:56:58 -070010802 /* Bootstrap checksum at offset 0x10 */
10803 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010804 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010805 goto out;
10806
10807 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10808 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010809 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010810 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010811
Matt Carlsonc3e94502011-04-13 11:05:08 +000010812 kfree(buf);
10813
Matt Carlson535a4902011-07-20 10:20:56 +000010814 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000010815 if (!buf)
10816 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010817
Matt Carlson535a4902011-07-20 10:20:56 +000010818 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000010819 if (i > 0) {
10820 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10821 if (j < 0)
10822 goto out;
10823
Matt Carlson535a4902011-07-20 10:20:56 +000010824 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000010825 goto out;
10826
10827 i += PCI_VPD_LRDT_TAG_SIZE;
10828 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10829 PCI_VPD_RO_KEYWORD_CHKSUM);
10830 if (j > 0) {
10831 u8 csum8 = 0;
10832
10833 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10834
10835 for (i = 0; i <= j; i++)
10836 csum8 += ((u8 *)buf)[i];
10837
10838 if (csum8)
10839 goto out;
10840 }
10841 }
10842
Michael Chan566f86a2005-05-29 14:56:58 -070010843 err = 0;
10844
10845out:
10846 kfree(buf);
10847 return err;
10848}
10849
Michael Chanca430072005-05-29 14:57:23 -070010850#define TG3_SERDES_TIMEOUT_SEC 2
10851#define TG3_COPPER_TIMEOUT_SEC 6
10852
10853static int tg3_test_link(struct tg3 *tp)
10854{
10855 int i, max;
10856
10857 if (!netif_running(tp->dev))
10858 return -ENODEV;
10859
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010860 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010861 max = TG3_SERDES_TIMEOUT_SEC;
10862 else
10863 max = TG3_COPPER_TIMEOUT_SEC;
10864
10865 for (i = 0; i < max; i++) {
10866 if (netif_carrier_ok(tp->dev))
10867 return 0;
10868
10869 if (msleep_interruptible(1000))
10870 break;
10871 }
10872
10873 return -EIO;
10874}
10875
Michael Chana71116d2005-05-29 14:58:11 -070010876/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010877static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010878{
Michael Chanb16250e2006-09-27 16:10:14 -070010879 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010880 u32 offset, read_mask, write_mask, val, save_val, read_val;
10881 static struct {
10882 u16 offset;
10883 u16 flags;
10884#define TG3_FL_5705 0x1
10885#define TG3_FL_NOT_5705 0x2
10886#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010887#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010888 u32 read_mask;
10889 u32 write_mask;
10890 } reg_tbl[] = {
10891 /* MAC Control Registers */
10892 { MAC_MODE, TG3_FL_NOT_5705,
10893 0x00000000, 0x00ef6f8c },
10894 { MAC_MODE, TG3_FL_5705,
10895 0x00000000, 0x01ef6b8c },
10896 { MAC_STATUS, TG3_FL_NOT_5705,
10897 0x03800107, 0x00000000 },
10898 { MAC_STATUS, TG3_FL_5705,
10899 0x03800100, 0x00000000 },
10900 { MAC_ADDR_0_HIGH, 0x0000,
10901 0x00000000, 0x0000ffff },
10902 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010903 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010904 { MAC_RX_MTU_SIZE, 0x0000,
10905 0x00000000, 0x0000ffff },
10906 { MAC_TX_MODE, 0x0000,
10907 0x00000000, 0x00000070 },
10908 { MAC_TX_LENGTHS, 0x0000,
10909 0x00000000, 0x00003fff },
10910 { MAC_RX_MODE, TG3_FL_NOT_5705,
10911 0x00000000, 0x000007fc },
10912 { MAC_RX_MODE, TG3_FL_5705,
10913 0x00000000, 0x000007dc },
10914 { MAC_HASH_REG_0, 0x0000,
10915 0x00000000, 0xffffffff },
10916 { MAC_HASH_REG_1, 0x0000,
10917 0x00000000, 0xffffffff },
10918 { MAC_HASH_REG_2, 0x0000,
10919 0x00000000, 0xffffffff },
10920 { MAC_HASH_REG_3, 0x0000,
10921 0x00000000, 0xffffffff },
10922
10923 /* Receive Data and Receive BD Initiator Control Registers. */
10924 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10925 0x00000000, 0xffffffff },
10926 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10927 0x00000000, 0xffffffff },
10928 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10929 0x00000000, 0x00000003 },
10930 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10931 0x00000000, 0xffffffff },
10932 { RCVDBDI_STD_BD+0, 0x0000,
10933 0x00000000, 0xffffffff },
10934 { RCVDBDI_STD_BD+4, 0x0000,
10935 0x00000000, 0xffffffff },
10936 { RCVDBDI_STD_BD+8, 0x0000,
10937 0x00000000, 0xffff0002 },
10938 { RCVDBDI_STD_BD+0xc, 0x0000,
10939 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010940
Michael Chana71116d2005-05-29 14:58:11 -070010941 /* Receive BD Initiator Control Registers. */
10942 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10943 0x00000000, 0xffffffff },
10944 { RCVBDI_STD_THRESH, TG3_FL_5705,
10945 0x00000000, 0x000003ff },
10946 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10947 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010948
Michael Chana71116d2005-05-29 14:58:11 -070010949 /* Host Coalescing Control Registers. */
10950 { HOSTCC_MODE, TG3_FL_NOT_5705,
10951 0x00000000, 0x00000004 },
10952 { HOSTCC_MODE, TG3_FL_5705,
10953 0x00000000, 0x000000f6 },
10954 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10955 0x00000000, 0xffffffff },
10956 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10957 0x00000000, 0x000003ff },
10958 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10959 0x00000000, 0xffffffff },
10960 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10961 0x00000000, 0x000003ff },
10962 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10963 0x00000000, 0xffffffff },
10964 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10965 0x00000000, 0x000000ff },
10966 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10967 0x00000000, 0xffffffff },
10968 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10969 0x00000000, 0x000000ff },
10970 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10971 0x00000000, 0xffffffff },
10972 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10973 0x00000000, 0xffffffff },
10974 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10975 0x00000000, 0xffffffff },
10976 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10977 0x00000000, 0x000000ff },
10978 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10979 0x00000000, 0xffffffff },
10980 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10981 0x00000000, 0x000000ff },
10982 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10983 0x00000000, 0xffffffff },
10984 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10985 0x00000000, 0xffffffff },
10986 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10987 0x00000000, 0xffffffff },
10988 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10989 0x00000000, 0xffffffff },
10990 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10991 0x00000000, 0xffffffff },
10992 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10993 0xffffffff, 0x00000000 },
10994 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10995 0xffffffff, 0x00000000 },
10996
10997 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010998 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010999 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011000 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011001 0x00000000, 0x007fffff },
11002 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11003 0x00000000, 0x0000003f },
11004 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11005 0x00000000, 0x000001ff },
11006 { BUFMGR_MB_HIGH_WATER, 0x0000,
11007 0x00000000, 0x000001ff },
11008 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11009 0xffffffff, 0x00000000 },
11010 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11011 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011012
Michael Chana71116d2005-05-29 14:58:11 -070011013 /* Mailbox Registers */
11014 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11015 0x00000000, 0x000001ff },
11016 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11017 0x00000000, 0x000001ff },
11018 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11019 0x00000000, 0x000007ff },
11020 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11021 0x00000000, 0x000001ff },
11022
11023 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11024 };
11025
Michael Chanb16250e2006-09-27 16:10:14 -070011026 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011027 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011028 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011029 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011030 is_5750 = 1;
11031 }
Michael Chana71116d2005-05-29 14:58:11 -070011032
11033 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11034 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11035 continue;
11036
11037 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11038 continue;
11039
Joe Perches63c3a662011-04-26 08:12:10 +000011040 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011041 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11042 continue;
11043
Michael Chanb16250e2006-09-27 16:10:14 -070011044 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11045 continue;
11046
Michael Chana71116d2005-05-29 14:58:11 -070011047 offset = (u32) reg_tbl[i].offset;
11048 read_mask = reg_tbl[i].read_mask;
11049 write_mask = reg_tbl[i].write_mask;
11050
11051 /* Save the original register content */
11052 save_val = tr32(offset);
11053
11054 /* Determine the read-only value. */
11055 read_val = save_val & read_mask;
11056
11057 /* Write zero to the register, then make sure the read-only bits
11058 * are not changed and the read/write bits are all zeros.
11059 */
11060 tw32(offset, 0);
11061
11062 val = tr32(offset);
11063
11064 /* Test the read-only and read/write bits. */
11065 if (((val & read_mask) != read_val) || (val & write_mask))
11066 goto out;
11067
11068 /* Write ones to all the bits defined by RdMask and WrMask, then
11069 * make sure the read-only bits are not changed and the
11070 * read/write bits are all ones.
11071 */
11072 tw32(offset, read_mask | write_mask);
11073
11074 val = tr32(offset);
11075
11076 /* Test the read-only bits. */
11077 if ((val & read_mask) != read_val)
11078 goto out;
11079
11080 /* Test the read/write bits. */
11081 if ((val & write_mask) != write_mask)
11082 goto out;
11083
11084 tw32(offset, save_val);
11085 }
11086
11087 return 0;
11088
11089out:
Michael Chan9f88f292006-12-07 00:22:54 -080011090 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011091 netdev_err(tp->dev,
11092 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011093 tw32(offset, save_val);
11094 return -EIO;
11095}
11096
Michael Chan7942e1d2005-05-29 14:58:36 -070011097static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11098{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011099 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011100 int i;
11101 u32 j;
11102
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011103 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011104 for (j = 0; j < len; j += 4) {
11105 u32 val;
11106
11107 tg3_write_mem(tp, offset + j, test_pattern[i]);
11108 tg3_read_mem(tp, offset + j, &val);
11109 if (val != test_pattern[i])
11110 return -EIO;
11111 }
11112 }
11113 return 0;
11114}
11115
11116static int tg3_test_memory(struct tg3 *tp)
11117{
11118 static struct mem_entry {
11119 u32 offset;
11120 u32 len;
11121 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011122 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011123 { 0x00002000, 0x1c000},
11124 { 0xffffffff, 0x00000}
11125 }, mem_tbl_5705[] = {
11126 { 0x00000100, 0x0000c},
11127 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011128 { 0x00004000, 0x00800},
11129 { 0x00006000, 0x01000},
11130 { 0x00008000, 0x02000},
11131 { 0x00010000, 0x0e000},
11132 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011133 }, mem_tbl_5755[] = {
11134 { 0x00000200, 0x00008},
11135 { 0x00004000, 0x00800},
11136 { 0x00006000, 0x00800},
11137 { 0x00008000, 0x02000},
11138 { 0x00010000, 0x0c000},
11139 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011140 }, mem_tbl_5906[] = {
11141 { 0x00000200, 0x00008},
11142 { 0x00004000, 0x00400},
11143 { 0x00006000, 0x00400},
11144 { 0x00008000, 0x01000},
11145 { 0x00010000, 0x01000},
11146 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011147 }, mem_tbl_5717[] = {
11148 { 0x00000200, 0x00008},
11149 { 0x00010000, 0x0a000},
11150 { 0x00020000, 0x13c00},
11151 { 0xffffffff, 0x00000}
11152 }, mem_tbl_57765[] = {
11153 { 0x00000200, 0x00008},
11154 { 0x00004000, 0x00800},
11155 { 0x00006000, 0x09800},
11156 { 0x00010000, 0x0a000},
11157 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011158 };
11159 struct mem_entry *mem_tbl;
11160 int err = 0;
11161 int i;
11162
Joe Perches63c3a662011-04-26 08:12:10 +000011163 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011164 mem_tbl = mem_tbl_5717;
11165 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11166 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011167 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011168 mem_tbl = mem_tbl_5755;
11169 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11170 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011171 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011172 mem_tbl = mem_tbl_5705;
11173 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011174 mem_tbl = mem_tbl_570x;
11175
11176 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011177 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11178 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011179 break;
11180 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011181
Michael Chan7942e1d2005-05-29 14:58:36 -070011182 return err;
11183}
11184
Michael Chan9f40dea2005-09-05 17:53:06 -070011185#define TG3_MAC_LOOPBACK 0
11186#define TG3_PHY_LOOPBACK 1
Matt Carlsonbb158d62011-04-25 12:42:47 +000011187#define TG3_TSO_LOOPBACK 2
11188
11189#define TG3_TSO_MSS 500
11190
11191#define TG3_TSO_IP_HDR_LEN 20
11192#define TG3_TSO_TCP_HDR_LEN 20
11193#define TG3_TSO_TCP_OPT_LEN 12
11194
11195static const u8 tg3_tso_header[] = {
111960x08, 0x00,
111970x45, 0x00, 0x00, 0x00,
111980x00, 0x00, 0x40, 0x00,
111990x40, 0x06, 0x00, 0x00,
112000x0a, 0x00, 0x00, 0x01,
112010x0a, 0x00, 0x00, 0x02,
112020x0d, 0x00, 0xe0, 0x00,
112030x00, 0x00, 0x01, 0x00,
112040x00, 0x00, 0x02, 0x00,
112050x80, 0x10, 0x10, 0x00,
112060x14, 0x09, 0x00, 0x00,
112070x01, 0x01, 0x08, 0x0a,
112080x11, 0x11, 0x11, 0x11,
112090x11, 0x11, 0x11, 0x11,
11210};
Michael Chan9f40dea2005-09-05 17:53:06 -070011211
Matt Carlson4852a862011-04-13 11:05:07 +000011212static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070011213{
Michael Chan9f40dea2005-09-05 17:53:06 -070011214 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011215 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011216 u32 budget;
Michael Chanc76949a2005-05-29 14:58:59 -070011217 struct sk_buff *skb, *rx_skb;
11218 u8 *tx_data;
11219 dma_addr_t map;
11220 int num_pkts, tx_len, rx_len, i, err;
11221 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011222 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011223 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011224
Matt Carlsonc8873402010-02-12 14:47:11 +000011225 tnapi = &tp->napi[0];
11226 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011227 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011228 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011229 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011230 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011231 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011232 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011233 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011234
Michael Chan9f40dea2005-09-05 17:53:06 -070011235 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070011236 /* HW errata - mac loopback fails in some cases on 5780.
11237 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000011238 * errata. Also, the MAC loopback test is deprecated for
11239 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070011240 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000011241 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011242 tg3_flag(tp, CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070011243 return 0;
11244
Matt Carlson49692ca2011-01-25 15:58:52 +000011245 mac_mode = tp->mac_mode &
11246 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
11247 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Joe Perches63c3a662011-04-26 08:12:10 +000011248 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011249 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011250 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070011251 mac_mode |= MAC_MODE_PORT_MODE_MII;
11252 else
11253 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070011254 tw32(MAC_MODE, mac_mode);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011255 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011256 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000011257 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080011258 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
11259 } else
11260 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070011261
Matt Carlson9ef8ca92007-07-11 19:48:29 -070011262 tg3_phy_toggle_automdix(tp, 0);
11263
Michael Chan3f7045c2006-09-27 16:02:29 -070011264 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070011265 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080011266
Matt Carlson49692ca2011-01-25 15:58:52 +000011267 mac_mode = tp->mac_mode &
11268 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011269 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000011270 tg3_writephy(tp, MII_TG3_FET_PTEST,
11271 MII_TG3_FET_PTEST_FRC_TX_LINK |
11272 MII_TG3_FET_PTEST_FRC_TX_LOCK);
11273 /* The write needs to be flushed for the AC131 */
11274 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11275 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080011276 mac_mode |= MAC_MODE_PORT_MODE_MII;
11277 } else
11278 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070011279
Michael Chanc94e3942005-09-27 12:12:42 -070011280 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011281 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070011282 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
11283 udelay(10);
11284 tw32_f(MAC_RX_MODE, tp->rx_mode);
11285 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011286 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000011287 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
11288 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011289 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000011290 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011291 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080011292 tg3_writephy(tp, MII_TG3_EXT_CTRL,
11293 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
11294 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011295 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000011296
11297 /* Wait for link */
11298 for (i = 0; i < 100; i++) {
11299 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11300 break;
11301 mdelay(1);
11302 }
Matt Carlson859a5882010-04-05 10:19:28 +000011303 }
Michael Chanc76949a2005-05-29 14:58:59 -070011304
11305 err = -EIO;
11306
Matt Carlson4852a862011-04-13 11:05:07 +000011307 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011308 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011309 if (!skb)
11310 return -ENOMEM;
11311
Michael Chanc76949a2005-05-29 14:58:59 -070011312 tx_data = skb_put(skb, tx_len);
11313 memcpy(tx_data, tp->dev->dev_addr, 6);
11314 memset(tx_data + 6, 0x0, 8);
11315
Matt Carlson4852a862011-04-13 11:05:07 +000011316 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011317
Matt Carlsonbb158d62011-04-25 12:42:47 +000011318 if (loopback_mode == TG3_TSO_LOOPBACK) {
11319 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11320
11321 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11322 TG3_TSO_TCP_OPT_LEN;
11323
11324 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11325 sizeof(tg3_tso_header));
11326 mss = TG3_TSO_MSS;
11327
11328 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11329 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11330
11331 /* Set the total length field in the IP header */
11332 iph->tot_len = htons((u16)(mss + hdr_len));
11333
11334 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11335 TXD_FLAG_CPU_POST_DMA);
11336
Joe Perches63c3a662011-04-26 08:12:10 +000011337 if (tg3_flag(tp, HW_TSO_1) ||
11338 tg3_flag(tp, HW_TSO_2) ||
11339 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011340 struct tcphdr *th;
11341 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11342 th = (struct tcphdr *)&tx_data[val];
11343 th->check = 0;
11344 } else
11345 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11346
Joe Perches63c3a662011-04-26 08:12:10 +000011347 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011348 mss |= (hdr_len & 0xc) << 12;
11349 if (hdr_len & 0x10)
11350 base_flags |= 0x00000010;
11351 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011352 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011353 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011354 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011355 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11356 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11357 } else {
11358 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11359 }
11360
11361 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11362 } else {
11363 num_pkts = 1;
11364 data_off = ETH_HLEN;
11365 }
11366
11367 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011368 tx_data[i] = (u8) (i & 0xff);
11369
Alexander Duyckf4188d82009-12-02 16:48:38 +000011370 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11371 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011372 dev_kfree_skb(skb);
11373 return -EIO;
11374 }
Michael Chanc76949a2005-05-29 14:58:59 -070011375
Matt Carlson0d681b22011-07-27 14:20:49 +000011376 val = tnapi->tx_prod;
11377 tnapi->tx_buffers[val].skb = skb;
11378 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11379
Michael Chanc76949a2005-05-29 14:58:59 -070011380 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011381 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011382
11383 udelay(10);
11384
Matt Carlson898a56f2009-08-28 14:02:40 +000011385 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011386
Matt Carlson84b67b22011-07-27 14:20:52 +000011387 budget = tg3_tx_avail(tnapi);
11388 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011389 base_flags | TXD_FLAG_END, mss, 0)) {
11390 tnapi->tx_buffers[val].skb = NULL;
11391 dev_kfree_skb(skb);
11392 return -EIO;
11393 }
Michael Chanc76949a2005-05-29 14:58:59 -070011394
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011395 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011396
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011397 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11398 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011399
11400 udelay(10);
11401
Matt Carlson303fc922009-11-02 14:27:34 +000011402 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11403 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011404 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011405 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011406
11407 udelay(10);
11408
Matt Carlson898a56f2009-08-28 14:02:40 +000011409 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11410 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011411 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011412 (rx_idx == (rx_start_idx + num_pkts)))
11413 break;
11414 }
11415
Matt Carlson0d681b22011-07-27 14:20:49 +000011416 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, 0);
Michael Chanc76949a2005-05-29 14:58:59 -070011417 dev_kfree_skb(skb);
11418
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011419 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011420 goto out;
11421
11422 if (rx_idx != rx_start_idx + num_pkts)
11423 goto out;
11424
Matt Carlsonbb158d62011-04-25 12:42:47 +000011425 val = data_off;
11426 while (rx_idx != rx_start_idx) {
11427 desc = &rnapi->rx_rcb[rx_start_idx++];
11428 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11429 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011430
Matt Carlsonbb158d62011-04-25 12:42:47 +000011431 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11432 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011433 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011434
Matt Carlsonbb158d62011-04-25 12:42:47 +000011435 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11436 - ETH_FCS_LEN;
11437
11438 if (loopback_mode != TG3_TSO_LOOPBACK) {
11439 if (rx_len != tx_len)
11440 goto out;
11441
11442 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11443 if (opaque_key != RXD_OPAQUE_RING_STD)
11444 goto out;
11445 } else {
11446 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11447 goto out;
11448 }
11449 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11450 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011451 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011452 goto out;
11453 }
11454
11455 if (opaque_key == RXD_OPAQUE_RING_STD) {
11456 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11457 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11458 mapping);
11459 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
11460 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11461 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11462 mapping);
11463 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011464 goto out;
11465
Matt Carlsonbb158d62011-04-25 12:42:47 +000011466 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11467 PCI_DMA_FROMDEVICE);
11468
11469 for (i = data_off; i < rx_len; i++, val++) {
11470 if (*(rx_skb->data + i) != (u8) (val & 0xff))
11471 goto out;
11472 }
Matt Carlson4852a862011-04-13 11:05:07 +000011473 }
11474
Michael Chanc76949a2005-05-29 14:58:59 -070011475 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011476
Michael Chanc76949a2005-05-29 14:58:59 -070011477 /* tg3_free_rings will unmap and free the rx_skb */
11478out:
11479 return err;
11480}
11481
Matt Carlson00c266b2011-04-25 12:42:46 +000011482#define TG3_STD_LOOPBACK_FAILED 1
11483#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011484#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson00c266b2011-04-25 12:42:46 +000011485
11486#define TG3_MAC_LOOPBACK_SHIFT 0
11487#define TG3_PHY_LOOPBACK_SHIFT 4
Matt Carlsonbb158d62011-04-25 12:42:47 +000011488#define TG3_LOOPBACK_FAILED 0x00000077
Michael Chan9f40dea2005-09-05 17:53:06 -070011489
11490static int tg3_test_loopback(struct tg3 *tp)
11491{
11492 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011493 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011494
11495 if (!netif_running(tp->dev))
11496 return TG3_LOOPBACK_FAILED;
11497
Matt Carlsonab789042011-01-25 15:58:54 +000011498 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11499 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11500
Michael Chanb9ec6c12006-07-25 16:37:27 -070011501 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011502 if (err) {
11503 err = TG3_LOOPBACK_FAILED;
11504 goto done;
11505 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011506
Joe Perches63c3a662011-04-26 08:12:10 +000011507 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011508 int i;
11509
11510 /* Reroute all rx packets to the 1st queue */
11511 for (i = MAC_RSS_INDIR_TBL_0;
11512 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11513 tw32(i, 0x0);
11514 }
11515
Matt Carlson6833c042008-11-21 17:18:59 -080011516 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011517 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011518 tg3_phy_toggle_apd(tp, false);
11519
Joe Perches63c3a662011-04-26 08:12:10 +000011520 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011521 int i;
11522 u32 status;
11523
11524 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11525
11526 /* Wait for up to 40 microseconds to acquire lock. */
11527 for (i = 0; i < 4; i++) {
11528 status = tr32(TG3_CPMU_MUTEX_GNT);
11529 if (status == CPMU_MUTEX_GNT_DRIVER)
11530 break;
11531 udelay(10);
11532 }
11533
Matt Carlsonab789042011-01-25 15:58:54 +000011534 if (status != CPMU_MUTEX_GNT_DRIVER) {
11535 err = TG3_LOOPBACK_FAILED;
11536 goto done;
11537 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011538
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011539 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011540 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011541 tw32(TG3_CPMU_CTRL,
11542 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11543 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011544 }
11545
Matt Carlson4852a862011-04-13 11:05:07 +000011546 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011547 err |= TG3_STD_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011548
Joe Perches63c3a662011-04-26 08:12:10 +000011549 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011550 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011551 err |= TG3_JMB_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson4852a862011-04-13 11:05:07 +000011552
Joe Perches63c3a662011-04-26 08:12:10 +000011553 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011554 tw32(TG3_CPMU_CTRL, cpmuctrl);
11555
11556 /* Release the mutex */
11557 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11558 }
11559
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011560 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011561 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson4852a862011-04-13 11:05:07 +000011562 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011563 err |= TG3_STD_LOOPBACK_FAILED <<
11564 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011565 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonbb158d62011-04-25 12:42:47 +000011566 tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_TSO_LOOPBACK))
11567 err |= TG3_TSO_LOOPBACK_FAILED <<
11568 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011569 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011570 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011571 err |= TG3_JMB_LOOPBACK_FAILED <<
11572 TG3_PHY_LOOPBACK_SHIFT;
Michael Chan9f40dea2005-09-05 17:53:06 -070011573 }
11574
Matt Carlson6833c042008-11-21 17:18:59 -080011575 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011576 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011577 tg3_phy_toggle_apd(tp, true);
11578
Matt Carlsonab789042011-01-25 15:58:54 +000011579done:
11580 tp->phy_flags |= eee_cap;
11581
Michael Chan9f40dea2005-09-05 17:53:06 -070011582 return err;
11583}
11584
Michael Chan4cafd3f2005-05-29 14:56:34 -070011585static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11586 u64 *data)
11587{
Michael Chan566f86a2005-05-29 14:56:58 -070011588 struct tg3 *tp = netdev_priv(dev);
11589
Matt Carlsonbed98292011-07-13 09:27:29 +000011590 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11591 tg3_power_up(tp)) {
11592 etest->flags |= ETH_TEST_FL_FAILED;
11593 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11594 return;
11595 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011596
Michael Chan566f86a2005-05-29 14:56:58 -070011597 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11598
11599 if (tg3_test_nvram(tp) != 0) {
11600 etest->flags |= ETH_TEST_FL_FAILED;
11601 data[0] = 1;
11602 }
Michael Chanca430072005-05-29 14:57:23 -070011603 if (tg3_test_link(tp) != 0) {
11604 etest->flags |= ETH_TEST_FL_FAILED;
11605 data[1] = 1;
11606 }
Michael Chana71116d2005-05-29 14:58:11 -070011607 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011608 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011609
Michael Chanbbe832c2005-06-24 20:20:04 -070011610 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011611 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011612 tg3_netif_stop(tp);
11613 irq_sync = 1;
11614 }
11615
11616 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011617
11618 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011619 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011620 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011621 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011622 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011623 if (!err)
11624 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011625
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011626 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011627 tg3_phy_reset(tp);
11628
Michael Chana71116d2005-05-29 14:58:11 -070011629 if (tg3_test_registers(tp) != 0) {
11630 etest->flags |= ETH_TEST_FL_FAILED;
11631 data[2] = 1;
11632 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011633 if (tg3_test_memory(tp) != 0) {
11634 etest->flags |= ETH_TEST_FL_FAILED;
11635 data[3] = 1;
11636 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011637 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011638 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011639
David S. Millerf47c11e2005-06-24 20:18:35 -070011640 tg3_full_unlock(tp);
11641
Michael Chand4bc3922005-05-29 14:59:20 -070011642 if (tg3_test_interrupt(tp) != 0) {
11643 etest->flags |= ETH_TEST_FL_FAILED;
11644 data[5] = 1;
11645 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011646
11647 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011648
Michael Chana71116d2005-05-29 14:58:11 -070011649 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11650 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011651 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011652 err2 = tg3_restart_hw(tp, 1);
11653 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011654 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011655 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011656
11657 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011658
11659 if (irq_sync && !err2)
11660 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011661 }
Matt Carlson80096062010-08-02 11:26:06 +000011662 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011663 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011664
Michael Chan4cafd3f2005-05-29 14:56:34 -070011665}
11666
Linus Torvalds1da177e2005-04-16 15:20:36 -070011667static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11668{
11669 struct mii_ioctl_data *data = if_mii(ifr);
11670 struct tg3 *tp = netdev_priv(dev);
11671 int err;
11672
Joe Perches63c3a662011-04-26 08:12:10 +000011673 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011674 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011675 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011676 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011677 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011678 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011679 }
11680
Matt Carlson33f401a2010-04-05 10:19:27 +000011681 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011682 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011683 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011684
11685 /* fallthru */
11686 case SIOCGMIIREG: {
11687 u32 mii_regval;
11688
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011689 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011690 break; /* We have no PHY */
11691
Matt Carlson34eea5a2011-04-20 07:57:38 +000011692 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011693 return -EAGAIN;
11694
David S. Millerf47c11e2005-06-24 20:18:35 -070011695 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011696 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011697 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011698
11699 data->val_out = mii_regval;
11700
11701 return err;
11702 }
11703
11704 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011705 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011706 break; /* We have no PHY */
11707
Matt Carlson34eea5a2011-04-20 07:57:38 +000011708 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011709 return -EAGAIN;
11710
David S. Millerf47c11e2005-06-24 20:18:35 -070011711 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011712 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011713 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011714
11715 return err;
11716
11717 default:
11718 /* do nothing */
11719 break;
11720 }
11721 return -EOPNOTSUPP;
11722}
11723
David S. Miller15f98502005-05-18 22:49:26 -070011724static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11725{
11726 struct tg3 *tp = netdev_priv(dev);
11727
11728 memcpy(ec, &tp->coal, sizeof(*ec));
11729 return 0;
11730}
11731
Michael Chand244c892005-07-05 14:42:33 -070011732static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11733{
11734 struct tg3 *tp = netdev_priv(dev);
11735 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11736 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11737
Joe Perches63c3a662011-04-26 08:12:10 +000011738 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011739 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11740 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11741 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11742 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11743 }
11744
11745 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11746 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11747 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11748 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11749 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11750 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11751 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11752 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11753 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11754 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11755 return -EINVAL;
11756
11757 /* No rx interrupts will be generated if both are zero */
11758 if ((ec->rx_coalesce_usecs == 0) &&
11759 (ec->rx_max_coalesced_frames == 0))
11760 return -EINVAL;
11761
11762 /* No tx interrupts will be generated if both are zero */
11763 if ((ec->tx_coalesce_usecs == 0) &&
11764 (ec->tx_max_coalesced_frames == 0))
11765 return -EINVAL;
11766
11767 /* Only copy relevant parameters, ignore all others. */
11768 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11769 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11770 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11771 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11772 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11773 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11774 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11775 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11776 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11777
11778 if (netif_running(dev)) {
11779 tg3_full_lock(tp, 0);
11780 __tg3_set_coalesce(tp, &tp->coal);
11781 tg3_full_unlock(tp);
11782 }
11783 return 0;
11784}
11785
Jeff Garzik7282d492006-09-13 14:30:00 -040011786static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011787 .get_settings = tg3_get_settings,
11788 .set_settings = tg3_set_settings,
11789 .get_drvinfo = tg3_get_drvinfo,
11790 .get_regs_len = tg3_get_regs_len,
11791 .get_regs = tg3_get_regs,
11792 .get_wol = tg3_get_wol,
11793 .set_wol = tg3_set_wol,
11794 .get_msglevel = tg3_get_msglevel,
11795 .set_msglevel = tg3_set_msglevel,
11796 .nway_reset = tg3_nway_reset,
11797 .get_link = ethtool_op_get_link,
11798 .get_eeprom_len = tg3_get_eeprom_len,
11799 .get_eeprom = tg3_get_eeprom,
11800 .set_eeprom = tg3_set_eeprom,
11801 .get_ringparam = tg3_get_ringparam,
11802 .set_ringparam = tg3_set_ringparam,
11803 .get_pauseparam = tg3_get_pauseparam,
11804 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011805 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011806 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011807 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011808 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011809 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011810 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011811 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011812};
11813
11814static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11815{
Michael Chan1b277772006-03-20 22:27:48 -080011816 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011817
11818 tp->nvram_size = EEPROM_CHIP_SIZE;
11819
Matt Carlsone4f34112009-02-25 14:25:00 +000011820 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011821 return;
11822
Michael Chanb16250e2006-09-27 16:10:14 -070011823 if ((magic != TG3_EEPROM_MAGIC) &&
11824 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11825 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011826 return;
11827
11828 /*
11829 * Size the chip by reading offsets at increasing powers of two.
11830 * When we encounter our validation signature, we know the addressing
11831 * has wrapped around, and thus have our chip size.
11832 */
Michael Chan1b277772006-03-20 22:27:48 -080011833 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011834
11835 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011836 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011837 return;
11838
Michael Chan18201802006-03-20 22:29:15 -080011839 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011840 break;
11841
11842 cursize <<= 1;
11843 }
11844
11845 tp->nvram_size = cursize;
11846}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011847
Linus Torvalds1da177e2005-04-16 15:20:36 -070011848static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11849{
11850 u32 val;
11851
Joe Perches63c3a662011-04-26 08:12:10 +000011852 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011853 return;
11854
11855 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011856 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011857 tg3_get_eeprom_size(tp);
11858 return;
11859 }
11860
Matt Carlson6d348f22009-02-25 14:25:52 +000011861 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011862 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011863 /* This is confusing. We want to operate on the
11864 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11865 * call will read from NVRAM and byteswap the data
11866 * according to the byteswapping settings for all
11867 * other register accesses. This ensures the data we
11868 * want will always reside in the lower 16-bits.
11869 * However, the data in NVRAM is in LE format, which
11870 * means the data from the NVRAM read will always be
11871 * opposite the endianness of the CPU. The 16-bit
11872 * byteswap then brings the data to CPU endianness.
11873 */
11874 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011875 return;
11876 }
11877 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011878 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011879}
11880
11881static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11882{
11883 u32 nvcfg1;
11884
11885 nvcfg1 = tr32(NVRAM_CFG1);
11886 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000011887 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011888 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011889 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11890 tw32(NVRAM_CFG1, nvcfg1);
11891 }
11892
Matt Carlson6ff6f812011-05-19 12:12:54 +000011893 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011894 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011895 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011896 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11897 tp->nvram_jedecnum = JEDEC_ATMEL;
11898 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011899 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011900 break;
11901 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11902 tp->nvram_jedecnum = JEDEC_ATMEL;
11903 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11904 break;
11905 case FLASH_VENDOR_ATMEL_EEPROM:
11906 tp->nvram_jedecnum = JEDEC_ATMEL;
11907 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011908 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011909 break;
11910 case FLASH_VENDOR_ST:
11911 tp->nvram_jedecnum = JEDEC_ST;
11912 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011913 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011914 break;
11915 case FLASH_VENDOR_SAIFUN:
11916 tp->nvram_jedecnum = JEDEC_SAIFUN;
11917 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11918 break;
11919 case FLASH_VENDOR_SST_SMALL:
11920 case FLASH_VENDOR_SST_LARGE:
11921 tp->nvram_jedecnum = JEDEC_SST;
11922 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11923 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011924 }
Matt Carlson8590a602009-08-28 12:29:16 +000011925 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011926 tp->nvram_jedecnum = JEDEC_ATMEL;
11927 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011928 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011929 }
11930}
11931
Matt Carlsona1b950d2009-09-01 13:20:17 +000011932static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11933{
11934 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11935 case FLASH_5752PAGE_SIZE_256:
11936 tp->nvram_pagesize = 256;
11937 break;
11938 case FLASH_5752PAGE_SIZE_512:
11939 tp->nvram_pagesize = 512;
11940 break;
11941 case FLASH_5752PAGE_SIZE_1K:
11942 tp->nvram_pagesize = 1024;
11943 break;
11944 case FLASH_5752PAGE_SIZE_2K:
11945 tp->nvram_pagesize = 2048;
11946 break;
11947 case FLASH_5752PAGE_SIZE_4K:
11948 tp->nvram_pagesize = 4096;
11949 break;
11950 case FLASH_5752PAGE_SIZE_264:
11951 tp->nvram_pagesize = 264;
11952 break;
11953 case FLASH_5752PAGE_SIZE_528:
11954 tp->nvram_pagesize = 528;
11955 break;
11956 }
11957}
11958
Michael Chan361b4ac2005-04-21 17:11:21 -070011959static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11960{
11961 u32 nvcfg1;
11962
11963 nvcfg1 = tr32(NVRAM_CFG1);
11964
Michael Chane6af3012005-04-21 17:12:05 -070011965 /* NVRAM protection for TPM */
11966 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000011967 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070011968
Michael Chan361b4ac2005-04-21 17:11:21 -070011969 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011970 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11971 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11972 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011973 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011974 break;
11975 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11976 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011977 tg3_flag_set(tp, NVRAM_BUFFERED);
11978 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011979 break;
11980 case FLASH_5752VENDOR_ST_M45PE10:
11981 case FLASH_5752VENDOR_ST_M45PE20:
11982 case FLASH_5752VENDOR_ST_M45PE40:
11983 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011984 tg3_flag_set(tp, NVRAM_BUFFERED);
11985 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011986 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011987 }
11988
Joe Perches63c3a662011-04-26 08:12:10 +000011989 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011990 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011991 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011992 /* For eeprom, set pagesize to maximum eeprom size */
11993 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11994
11995 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11996 tw32(NVRAM_CFG1, nvcfg1);
11997 }
11998}
11999
Michael Chand3c7b882006-03-23 01:28:25 -080012000static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12001{
Matt Carlson989a9d22007-05-05 11:51:05 -070012002 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012003
12004 nvcfg1 = tr32(NVRAM_CFG1);
12005
12006 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012007 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012008 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012009 protect = 1;
12010 }
Michael Chand3c7b882006-03-23 01:28:25 -080012011
Matt Carlson989a9d22007-05-05 11:51:05 -070012012 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12013 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012014 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12015 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12016 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12017 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12018 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012019 tg3_flag_set(tp, NVRAM_BUFFERED);
12020 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012021 tp->nvram_pagesize = 264;
12022 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12023 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12024 tp->nvram_size = (protect ? 0x3e200 :
12025 TG3_NVRAM_SIZE_512KB);
12026 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12027 tp->nvram_size = (protect ? 0x1f200 :
12028 TG3_NVRAM_SIZE_256KB);
12029 else
12030 tp->nvram_size = (protect ? 0x1f200 :
12031 TG3_NVRAM_SIZE_128KB);
12032 break;
12033 case FLASH_5752VENDOR_ST_M45PE10:
12034 case FLASH_5752VENDOR_ST_M45PE20:
12035 case FLASH_5752VENDOR_ST_M45PE40:
12036 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012037 tg3_flag_set(tp, NVRAM_BUFFERED);
12038 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012039 tp->nvram_pagesize = 256;
12040 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12041 tp->nvram_size = (protect ?
12042 TG3_NVRAM_SIZE_64KB :
12043 TG3_NVRAM_SIZE_128KB);
12044 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12045 tp->nvram_size = (protect ?
12046 TG3_NVRAM_SIZE_64KB :
12047 TG3_NVRAM_SIZE_256KB);
12048 else
12049 tp->nvram_size = (protect ?
12050 TG3_NVRAM_SIZE_128KB :
12051 TG3_NVRAM_SIZE_512KB);
12052 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012053 }
12054}
12055
Michael Chan1b277772006-03-20 22:27:48 -080012056static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12057{
12058 u32 nvcfg1;
12059
12060 nvcfg1 = tr32(NVRAM_CFG1);
12061
12062 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012063 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12064 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12065 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12066 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12067 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012068 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012069 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012070
Matt Carlson8590a602009-08-28 12:29:16 +000012071 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12072 tw32(NVRAM_CFG1, nvcfg1);
12073 break;
12074 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12075 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12076 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12077 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12078 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012079 tg3_flag_set(tp, NVRAM_BUFFERED);
12080 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012081 tp->nvram_pagesize = 264;
12082 break;
12083 case FLASH_5752VENDOR_ST_M45PE10:
12084 case FLASH_5752VENDOR_ST_M45PE20:
12085 case FLASH_5752VENDOR_ST_M45PE40:
12086 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012087 tg3_flag_set(tp, NVRAM_BUFFERED);
12088 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012089 tp->nvram_pagesize = 256;
12090 break;
Michael Chan1b277772006-03-20 22:27:48 -080012091 }
12092}
12093
Matt Carlson6b91fa02007-10-10 18:01:09 -070012094static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12095{
12096 u32 nvcfg1, protect = 0;
12097
12098 nvcfg1 = tr32(NVRAM_CFG1);
12099
12100 /* NVRAM protection for TPM */
12101 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012102 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012103 protect = 1;
12104 }
12105
12106 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12107 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012108 case FLASH_5761VENDOR_ATMEL_ADB021D:
12109 case FLASH_5761VENDOR_ATMEL_ADB041D:
12110 case FLASH_5761VENDOR_ATMEL_ADB081D:
12111 case FLASH_5761VENDOR_ATMEL_ADB161D:
12112 case FLASH_5761VENDOR_ATMEL_MDB021D:
12113 case FLASH_5761VENDOR_ATMEL_MDB041D:
12114 case FLASH_5761VENDOR_ATMEL_MDB081D:
12115 case FLASH_5761VENDOR_ATMEL_MDB161D:
12116 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012117 tg3_flag_set(tp, NVRAM_BUFFERED);
12118 tg3_flag_set(tp, FLASH);
12119 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012120 tp->nvram_pagesize = 256;
12121 break;
12122 case FLASH_5761VENDOR_ST_A_M45PE20:
12123 case FLASH_5761VENDOR_ST_A_M45PE40:
12124 case FLASH_5761VENDOR_ST_A_M45PE80:
12125 case FLASH_5761VENDOR_ST_A_M45PE16:
12126 case FLASH_5761VENDOR_ST_M_M45PE20:
12127 case FLASH_5761VENDOR_ST_M_M45PE40:
12128 case FLASH_5761VENDOR_ST_M_M45PE80:
12129 case FLASH_5761VENDOR_ST_M_M45PE16:
12130 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012131 tg3_flag_set(tp, NVRAM_BUFFERED);
12132 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012133 tp->nvram_pagesize = 256;
12134 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012135 }
12136
12137 if (protect) {
12138 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12139 } else {
12140 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012141 case FLASH_5761VENDOR_ATMEL_ADB161D:
12142 case FLASH_5761VENDOR_ATMEL_MDB161D:
12143 case FLASH_5761VENDOR_ST_A_M45PE16:
12144 case FLASH_5761VENDOR_ST_M_M45PE16:
12145 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12146 break;
12147 case FLASH_5761VENDOR_ATMEL_ADB081D:
12148 case FLASH_5761VENDOR_ATMEL_MDB081D:
12149 case FLASH_5761VENDOR_ST_A_M45PE80:
12150 case FLASH_5761VENDOR_ST_M_M45PE80:
12151 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12152 break;
12153 case FLASH_5761VENDOR_ATMEL_ADB041D:
12154 case FLASH_5761VENDOR_ATMEL_MDB041D:
12155 case FLASH_5761VENDOR_ST_A_M45PE40:
12156 case FLASH_5761VENDOR_ST_M_M45PE40:
12157 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12158 break;
12159 case FLASH_5761VENDOR_ATMEL_ADB021D:
12160 case FLASH_5761VENDOR_ATMEL_MDB021D:
12161 case FLASH_5761VENDOR_ST_A_M45PE20:
12162 case FLASH_5761VENDOR_ST_M_M45PE20:
12163 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12164 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012165 }
12166 }
12167}
12168
Michael Chanb5d37722006-09-27 16:06:21 -070012169static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12170{
12171 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012172 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012173 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12174}
12175
Matt Carlson321d32a2008-11-21 17:22:19 -080012176static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12177{
12178 u32 nvcfg1;
12179
12180 nvcfg1 = tr32(NVRAM_CFG1);
12181
12182 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12183 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12184 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12185 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012186 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012187 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12188
12189 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12190 tw32(NVRAM_CFG1, nvcfg1);
12191 return;
12192 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12193 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12194 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12195 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12196 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12197 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12198 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12199 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012200 tg3_flag_set(tp, NVRAM_BUFFERED);
12201 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012202
12203 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12204 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12205 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12206 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12207 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12208 break;
12209 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12210 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12211 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12212 break;
12213 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12214 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12215 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12216 break;
12217 }
12218 break;
12219 case FLASH_5752VENDOR_ST_M45PE10:
12220 case FLASH_5752VENDOR_ST_M45PE20:
12221 case FLASH_5752VENDOR_ST_M45PE40:
12222 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012223 tg3_flag_set(tp, NVRAM_BUFFERED);
12224 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012225
12226 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12227 case FLASH_5752VENDOR_ST_M45PE10:
12228 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12229 break;
12230 case FLASH_5752VENDOR_ST_M45PE20:
12231 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12232 break;
12233 case FLASH_5752VENDOR_ST_M45PE40:
12234 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12235 break;
12236 }
12237 break;
12238 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012239 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012240 return;
12241 }
12242
Matt Carlsona1b950d2009-09-01 13:20:17 +000012243 tg3_nvram_get_pagesize(tp, nvcfg1);
12244 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012245 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012246}
12247
12248
12249static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12250{
12251 u32 nvcfg1;
12252
12253 nvcfg1 = tr32(NVRAM_CFG1);
12254
12255 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12256 case FLASH_5717VENDOR_ATMEL_EEPROM:
12257 case FLASH_5717VENDOR_MICRO_EEPROM:
12258 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012259 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012260 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12261
12262 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12263 tw32(NVRAM_CFG1, nvcfg1);
12264 return;
12265 case FLASH_5717VENDOR_ATMEL_MDB011D:
12266 case FLASH_5717VENDOR_ATMEL_ADB011B:
12267 case FLASH_5717VENDOR_ATMEL_ADB011D:
12268 case FLASH_5717VENDOR_ATMEL_MDB021D:
12269 case FLASH_5717VENDOR_ATMEL_ADB021B:
12270 case FLASH_5717VENDOR_ATMEL_ADB021D:
12271 case FLASH_5717VENDOR_ATMEL_45USPT:
12272 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012273 tg3_flag_set(tp, NVRAM_BUFFERED);
12274 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012275
12276 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12277 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012278 /* Detect size with tg3_nvram_get_size() */
12279 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012280 case FLASH_5717VENDOR_ATMEL_ADB021B:
12281 case FLASH_5717VENDOR_ATMEL_ADB021D:
12282 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12283 break;
12284 default:
12285 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12286 break;
12287 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012288 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012289 case FLASH_5717VENDOR_ST_M_M25PE10:
12290 case FLASH_5717VENDOR_ST_A_M25PE10:
12291 case FLASH_5717VENDOR_ST_M_M45PE10:
12292 case FLASH_5717VENDOR_ST_A_M45PE10:
12293 case FLASH_5717VENDOR_ST_M_M25PE20:
12294 case FLASH_5717VENDOR_ST_A_M25PE20:
12295 case FLASH_5717VENDOR_ST_M_M45PE20:
12296 case FLASH_5717VENDOR_ST_A_M45PE20:
12297 case FLASH_5717VENDOR_ST_25USPT:
12298 case FLASH_5717VENDOR_ST_45USPT:
12299 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012300 tg3_flag_set(tp, NVRAM_BUFFERED);
12301 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012302
12303 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12304 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012305 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012306 /* Detect size with tg3_nvram_get_size() */
12307 break;
12308 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012309 case FLASH_5717VENDOR_ST_A_M45PE20:
12310 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12311 break;
12312 default:
12313 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12314 break;
12315 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012316 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012317 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012318 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012319 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012320 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012321
12322 tg3_nvram_get_pagesize(tp, nvcfg1);
12323 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012324 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012325}
12326
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012327static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12328{
12329 u32 nvcfg1, nvmpinstrp;
12330
12331 nvcfg1 = tr32(NVRAM_CFG1);
12332 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12333
12334 switch (nvmpinstrp) {
12335 case FLASH_5720_EEPROM_HD:
12336 case FLASH_5720_EEPROM_LD:
12337 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012338 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012339
12340 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12341 tw32(NVRAM_CFG1, nvcfg1);
12342 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12343 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12344 else
12345 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12346 return;
12347 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12348 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12349 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12350 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12351 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12352 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12353 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12354 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12355 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12356 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12357 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12358 case FLASH_5720VENDOR_ATMEL_45USPT:
12359 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012360 tg3_flag_set(tp, NVRAM_BUFFERED);
12361 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012362
12363 switch (nvmpinstrp) {
12364 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12365 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12366 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12367 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12368 break;
12369 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12370 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12371 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12372 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12373 break;
12374 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12375 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12376 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12377 break;
12378 default:
12379 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12380 break;
12381 }
12382 break;
12383 case FLASH_5720VENDOR_M_ST_M25PE10:
12384 case FLASH_5720VENDOR_M_ST_M45PE10:
12385 case FLASH_5720VENDOR_A_ST_M25PE10:
12386 case FLASH_5720VENDOR_A_ST_M45PE10:
12387 case FLASH_5720VENDOR_M_ST_M25PE20:
12388 case FLASH_5720VENDOR_M_ST_M45PE20:
12389 case FLASH_5720VENDOR_A_ST_M25PE20:
12390 case FLASH_5720VENDOR_A_ST_M45PE20:
12391 case FLASH_5720VENDOR_M_ST_M25PE40:
12392 case FLASH_5720VENDOR_M_ST_M45PE40:
12393 case FLASH_5720VENDOR_A_ST_M25PE40:
12394 case FLASH_5720VENDOR_A_ST_M45PE40:
12395 case FLASH_5720VENDOR_M_ST_M25PE80:
12396 case FLASH_5720VENDOR_M_ST_M45PE80:
12397 case FLASH_5720VENDOR_A_ST_M25PE80:
12398 case FLASH_5720VENDOR_A_ST_M45PE80:
12399 case FLASH_5720VENDOR_ST_25USPT:
12400 case FLASH_5720VENDOR_ST_45USPT:
12401 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012402 tg3_flag_set(tp, NVRAM_BUFFERED);
12403 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012404
12405 switch (nvmpinstrp) {
12406 case FLASH_5720VENDOR_M_ST_M25PE20:
12407 case FLASH_5720VENDOR_M_ST_M45PE20:
12408 case FLASH_5720VENDOR_A_ST_M25PE20:
12409 case FLASH_5720VENDOR_A_ST_M45PE20:
12410 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12411 break;
12412 case FLASH_5720VENDOR_M_ST_M25PE40:
12413 case FLASH_5720VENDOR_M_ST_M45PE40:
12414 case FLASH_5720VENDOR_A_ST_M25PE40:
12415 case FLASH_5720VENDOR_A_ST_M45PE40:
12416 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12417 break;
12418 case FLASH_5720VENDOR_M_ST_M25PE80:
12419 case FLASH_5720VENDOR_M_ST_M45PE80:
12420 case FLASH_5720VENDOR_A_ST_M25PE80:
12421 case FLASH_5720VENDOR_A_ST_M45PE80:
12422 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12423 break;
12424 default:
12425 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12426 break;
12427 }
12428 break;
12429 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012430 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012431 return;
12432 }
12433
12434 tg3_nvram_get_pagesize(tp, nvcfg1);
12435 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012436 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012437}
12438
Linus Torvalds1da177e2005-04-16 15:20:36 -070012439/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12440static void __devinit tg3_nvram_init(struct tg3 *tp)
12441{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012442 tw32_f(GRC_EEPROM_ADDR,
12443 (EEPROM_ADDR_FSM_RESET |
12444 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12445 EEPROM_ADDR_CLKPERD_SHIFT)));
12446
Michael Chan9d57f012006-12-07 00:23:25 -080012447 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012448
12449 /* Enable seeprom accesses. */
12450 tw32_f(GRC_LOCAL_CTRL,
12451 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12452 udelay(100);
12453
12454 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12455 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012456 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012457
Michael Chanec41c7d2006-01-17 02:40:55 -080012458 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012459 netdev_warn(tp->dev,
12460 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012461 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012462 return;
12463 }
Michael Chane6af3012005-04-21 17:12:05 -070012464 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012465
Matt Carlson989a9d22007-05-05 11:51:05 -070012466 tp->nvram_size = 0;
12467
Michael Chan361b4ac2005-04-21 17:11:21 -070012468 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12469 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012470 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12471 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012472 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012473 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12474 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012475 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012476 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12477 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012478 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12479 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012480 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12481 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012482 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012483 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12484 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012485 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012486 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12487 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012488 else
12489 tg3_get_nvram_info(tp);
12490
Matt Carlson989a9d22007-05-05 11:51:05 -070012491 if (tp->nvram_size == 0)
12492 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012493
Michael Chane6af3012005-04-21 17:12:05 -070012494 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012495 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012496
12497 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012498 tg3_flag_clear(tp, NVRAM);
12499 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012500
12501 tg3_get_eeprom_size(tp);
12502 }
12503}
12504
Linus Torvalds1da177e2005-04-16 15:20:36 -070012505static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12506 u32 offset, u32 len, u8 *buf)
12507{
12508 int i, j, rc = 0;
12509 u32 val;
12510
12511 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012512 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012513 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012514
12515 addr = offset + i;
12516
12517 memcpy(&data, buf + i, 4);
12518
Matt Carlson62cedd12009-04-20 14:52:29 -070012519 /*
12520 * The SEEPROM interface expects the data to always be opposite
12521 * the native endian format. We accomplish this by reversing
12522 * all the operations that would have been performed on the
12523 * data from a call to tg3_nvram_read_be32().
12524 */
12525 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012526
12527 val = tr32(GRC_EEPROM_ADDR);
12528 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12529
12530 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12531 EEPROM_ADDR_READ);
12532 tw32(GRC_EEPROM_ADDR, val |
12533 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12534 (addr & EEPROM_ADDR_ADDR_MASK) |
12535 EEPROM_ADDR_START |
12536 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012537
Michael Chan9d57f012006-12-07 00:23:25 -080012538 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012539 val = tr32(GRC_EEPROM_ADDR);
12540
12541 if (val & EEPROM_ADDR_COMPLETE)
12542 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012543 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012544 }
12545 if (!(val & EEPROM_ADDR_COMPLETE)) {
12546 rc = -EBUSY;
12547 break;
12548 }
12549 }
12550
12551 return rc;
12552}
12553
12554/* offset and length are dword aligned */
12555static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12556 u8 *buf)
12557{
12558 int ret = 0;
12559 u32 pagesize = tp->nvram_pagesize;
12560 u32 pagemask = pagesize - 1;
12561 u32 nvram_cmd;
12562 u8 *tmp;
12563
12564 tmp = kmalloc(pagesize, GFP_KERNEL);
12565 if (tmp == NULL)
12566 return -ENOMEM;
12567
12568 while (len) {
12569 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012570 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012571
12572 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012573
Linus Torvalds1da177e2005-04-16 15:20:36 -070012574 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012575 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12576 (__be32 *) (tmp + j));
12577 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012578 break;
12579 }
12580 if (ret)
12581 break;
12582
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012583 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012584 size = pagesize;
12585 if (len < size)
12586 size = len;
12587
12588 len -= size;
12589
12590 memcpy(tmp + page_off, buf, size);
12591
12592 offset = offset + (pagesize - page_off);
12593
Michael Chane6af3012005-04-21 17:12:05 -070012594 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012595
12596 /*
12597 * Before we can erase the flash page, we need
12598 * to issue a special "write enable" command.
12599 */
12600 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12601
12602 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12603 break;
12604
12605 /* Erase the target page */
12606 tw32(NVRAM_ADDR, phy_addr);
12607
12608 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12609 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12610
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012611 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012612 break;
12613
12614 /* Issue another write enable to start the write. */
12615 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12616
12617 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12618 break;
12619
12620 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012621 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012622
Al Virob9fc7dc2007-12-17 22:59:57 -080012623 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012624
Al Virob9fc7dc2007-12-17 22:59:57 -080012625 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012626
12627 tw32(NVRAM_ADDR, phy_addr + j);
12628
12629 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12630 NVRAM_CMD_WR;
12631
12632 if (j == 0)
12633 nvram_cmd |= NVRAM_CMD_FIRST;
12634 else if (j == (pagesize - 4))
12635 nvram_cmd |= NVRAM_CMD_LAST;
12636
12637 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12638 break;
12639 }
12640 if (ret)
12641 break;
12642 }
12643
12644 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12645 tg3_nvram_exec_cmd(tp, nvram_cmd);
12646
12647 kfree(tmp);
12648
12649 return ret;
12650}
12651
12652/* offset and length are dword aligned */
12653static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12654 u8 *buf)
12655{
12656 int i, ret = 0;
12657
12658 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012659 u32 page_off, phy_addr, nvram_cmd;
12660 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012661
12662 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012663 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012664
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012665 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012666
Michael Chan18201802006-03-20 22:29:15 -080012667 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012668
12669 tw32(NVRAM_ADDR, phy_addr);
12670
12671 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12672
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012673 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012674 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012675 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012676 nvram_cmd |= NVRAM_CMD_LAST;
12677
12678 if (i == (len - 4))
12679 nvram_cmd |= NVRAM_CMD_LAST;
12680
Matt Carlson321d32a2008-11-21 17:22:19 -080012681 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012682 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012683 (tp->nvram_jedecnum == JEDEC_ST) &&
12684 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012685
12686 if ((ret = tg3_nvram_exec_cmd(tp,
12687 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12688 NVRAM_CMD_DONE)))
12689
12690 break;
12691 }
Joe Perches63c3a662011-04-26 08:12:10 +000012692 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012693 /* We always do complete word writes to eeprom. */
12694 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12695 }
12696
12697 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12698 break;
12699 }
12700 return ret;
12701}
12702
12703/* offset and length are dword aligned */
12704static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12705{
12706 int ret;
12707
Joe Perches63c3a662011-04-26 08:12:10 +000012708 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012709 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12710 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012711 udelay(40);
12712 }
12713
Joe Perches63c3a662011-04-26 08:12:10 +000012714 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012715 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012716 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012717 u32 grc_mode;
12718
Michael Chanec41c7d2006-01-17 02:40:55 -080012719 ret = tg3_nvram_lock(tp);
12720 if (ret)
12721 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012722
Michael Chane6af3012005-04-21 17:12:05 -070012723 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012724 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012725 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012726
12727 grc_mode = tr32(GRC_MODE);
12728 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12729
Joe Perches63c3a662011-04-26 08:12:10 +000012730 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012731 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12732 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012733 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012734 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12735 buf);
12736 }
12737
12738 grc_mode = tr32(GRC_MODE);
12739 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12740
Michael Chane6af3012005-04-21 17:12:05 -070012741 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012742 tg3_nvram_unlock(tp);
12743 }
12744
Joe Perches63c3a662011-04-26 08:12:10 +000012745 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012746 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012747 udelay(40);
12748 }
12749
12750 return ret;
12751}
12752
12753struct subsys_tbl_ent {
12754 u16 subsys_vendor, subsys_devid;
12755 u32 phy_id;
12756};
12757
Matt Carlson24daf2b2010-02-17 15:17:02 +000012758static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012759 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012760 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012761 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012762 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012763 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012764 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012765 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012766 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12767 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12768 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012769 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012770 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012771 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012772 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12773 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12774 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012775 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012776 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012777 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012778 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012779 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012780 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012781 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012782
12783 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012784 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012785 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012786 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012787 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012788 { TG3PCI_SUBVENDOR_ID_3COM,
12789 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12790 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012791 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012792 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012793 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012794
12795 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012796 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012797 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012798 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012799 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012800 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012801 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012802 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012803 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012804
12805 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012806 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012807 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012808 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012809 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012810 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12811 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12812 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012813 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012814 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012815 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012816
12817 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012818 { TG3PCI_SUBVENDOR_ID_IBM,
12819 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012820};
12821
Matt Carlson24daf2b2010-02-17 15:17:02 +000012822static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012823{
12824 int i;
12825
12826 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12827 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12828 tp->pdev->subsystem_vendor) &&
12829 (subsys_id_to_phy_id[i].subsys_devid ==
12830 tp->pdev->subsystem_device))
12831 return &subsys_id_to_phy_id[i];
12832 }
12833 return NULL;
12834}
12835
Michael Chan7d0c41e2005-04-21 17:06:20 -070012836static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012837{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012838 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070012839
Matt Carlson79eb6902010-02-17 15:17:03 +000012840 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012841 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12842
Gary Zambranoa85feb82007-05-05 11:52:19 -070012843 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012844 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12845 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012846
Michael Chanb5d37722006-09-27 16:06:21 -070012847 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012848 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012849 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12850 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012851 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012852 val = tr32(VCPU_CFGSHDW);
12853 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012854 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012855 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012856 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012857 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012858 device_set_wakeup_enable(&tp->pdev->dev, true);
12859 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012860 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012861 }
12862
Linus Torvalds1da177e2005-04-16 15:20:36 -070012863 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12864 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12865 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012866 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012867 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012868
12869 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12870 tp->nic_sram_data_cfg = nic_cfg;
12871
12872 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12873 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000012874 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12875 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
12876 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012877 (ver > 0) && (ver < 0x100))
12878 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12879
Matt Carlsona9daf362008-05-25 23:49:44 -070012880 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12881 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12882
Linus Torvalds1da177e2005-04-16 15:20:36 -070012883 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12884 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12885 eeprom_phy_serdes = 1;
12886
12887 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12888 if (nic_phy_id != 0) {
12889 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12890 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12891
12892 eeprom_phy_id = (id1 >> 16) << 10;
12893 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12894 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12895 } else
12896 eeprom_phy_id = 0;
12897
Michael Chan7d0c41e2005-04-21 17:06:20 -070012898 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012899 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000012900 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012901 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012902 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012903 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012904 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012905
Joe Perches63c3a662011-04-26 08:12:10 +000012906 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012907 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12908 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012909 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012910 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12911
12912 switch (led_cfg) {
12913 default:
12914 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12915 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12916 break;
12917
12918 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12919 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12920 break;
12921
12922 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12923 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012924
12925 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12926 * read on some older 5700/5701 bootcode.
12927 */
12928 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12929 ASIC_REV_5700 ||
12930 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12931 ASIC_REV_5701)
12932 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12933
Linus Torvalds1da177e2005-04-16 15:20:36 -070012934 break;
12935
12936 case SHASTA_EXT_LED_SHARED:
12937 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12938 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12939 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12940 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12941 LED_CTRL_MODE_PHY_2);
12942 break;
12943
12944 case SHASTA_EXT_LED_MAC:
12945 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12946 break;
12947
12948 case SHASTA_EXT_LED_COMBO:
12949 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12950 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12951 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12952 LED_CTRL_MODE_PHY_2);
12953 break;
12954
Stephen Hemminger855e1112008-04-16 16:37:28 -070012955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012956
12957 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12958 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12959 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12960 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12961
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012962 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12963 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012964
Michael Chan9d26e212006-12-07 00:21:14 -080012965 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000012966 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012967 if ((tp->pdev->subsystem_vendor ==
12968 PCI_VENDOR_ID_ARIMA) &&
12969 (tp->pdev->subsystem_device == 0x205a ||
12970 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000012971 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012972 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012973 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12974 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012976
12977 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000012978 tg3_flag_set(tp, ENABLE_ASF);
12979 if (tg3_flag(tp, 5750_PLUS))
12980 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012981 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012982
12983 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012984 tg3_flag(tp, 5750_PLUS))
12985 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012986
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012987 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012988 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000012989 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012990
Joe Perches63c3a662011-04-26 08:12:10 +000012991 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012992 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012993 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012994 device_set_wakeup_enable(&tp->pdev->dev, true);
12995 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012996
Linus Torvalds1da177e2005-04-16 15:20:36 -070012997 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012998 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012999
13000 /* serdes signal pre-emphasis in register 0x590 set by */
13001 /* bootcode if bit 18 is set */
13002 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013003 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013004
Joe Perches63c3a662011-04-26 08:12:10 +000013005 if ((tg3_flag(tp, 57765_PLUS) ||
13006 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13007 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013008 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013009 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013010
Joe Perches63c3a662011-04-26 08:12:10 +000013011 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013012 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013013 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013014 u32 cfg3;
13015
13016 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13017 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013018 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013019 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013020
Matt Carlson14417062010-02-17 15:16:59 +000013021 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013022 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013023 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013024 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013025 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013026 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013027 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013028done:
Joe Perches63c3a662011-04-26 08:12:10 +000013029 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013030 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013031 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013032 else
13033 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013034}
13035
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013036static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13037{
13038 int i;
13039 u32 val;
13040
13041 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13042 tw32(OTP_CTRL, cmd);
13043
13044 /* Wait for up to 1 ms for command to execute. */
13045 for (i = 0; i < 100; i++) {
13046 val = tr32(OTP_STATUS);
13047 if (val & OTP_STATUS_CMD_DONE)
13048 break;
13049 udelay(10);
13050 }
13051
13052 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13053}
13054
13055/* Read the gphy configuration from the OTP region of the chip. The gphy
13056 * configuration is a 32-bit value that straddles the alignment boundary.
13057 * We do two 32-bit reads and then shift and merge the results.
13058 */
13059static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13060{
13061 u32 bhalf_otp, thalf_otp;
13062
13063 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13064
13065 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13066 return 0;
13067
13068 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13069
13070 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13071 return 0;
13072
13073 thalf_otp = tr32(OTP_READ_DATA);
13074
13075 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13076
13077 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13078 return 0;
13079
13080 bhalf_otp = tr32(OTP_READ_DATA);
13081
13082 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13083}
13084
Matt Carlsone256f8a2011-03-09 16:58:24 +000013085static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13086{
13087 u32 adv = ADVERTISED_Autoneg |
13088 ADVERTISED_Pause;
13089
13090 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13091 adv |= ADVERTISED_1000baseT_Half |
13092 ADVERTISED_1000baseT_Full;
13093
13094 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13095 adv |= ADVERTISED_100baseT_Half |
13096 ADVERTISED_100baseT_Full |
13097 ADVERTISED_10baseT_Half |
13098 ADVERTISED_10baseT_Full |
13099 ADVERTISED_TP;
13100 else
13101 adv |= ADVERTISED_FIBRE;
13102
13103 tp->link_config.advertising = adv;
13104 tp->link_config.speed = SPEED_INVALID;
13105 tp->link_config.duplex = DUPLEX_INVALID;
13106 tp->link_config.autoneg = AUTONEG_ENABLE;
13107 tp->link_config.active_speed = SPEED_INVALID;
13108 tp->link_config.active_duplex = DUPLEX_INVALID;
13109 tp->link_config.orig_speed = SPEED_INVALID;
13110 tp->link_config.orig_duplex = DUPLEX_INVALID;
13111 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13112}
13113
Michael Chan7d0c41e2005-04-21 17:06:20 -070013114static int __devinit tg3_phy_probe(struct tg3 *tp)
13115{
13116 u32 hw_phy_id_1, hw_phy_id_2;
13117 u32 hw_phy_id, hw_phy_id_masked;
13118 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013119
Matt Carlsone256f8a2011-03-09 16:58:24 +000013120 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013121 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013122 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13123
Joe Perches63c3a662011-04-26 08:12:10 +000013124 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013125 return tg3_phy_init(tp);
13126
Linus Torvalds1da177e2005-04-16 15:20:36 -070013127 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013128 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013129 */
13130 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013131 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013132 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013133 } else {
13134 /* Now read the physical PHY_ID from the chip and verify
13135 * that it is sane. If it doesn't look good, we fall back
13136 * to either the hard-coded table based PHY_ID and failing
13137 * that the value found in the eeprom area.
13138 */
13139 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13140 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13141
13142 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13143 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13144 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13145
Matt Carlson79eb6902010-02-17 15:17:03 +000013146 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013147 }
13148
Matt Carlson79eb6902010-02-17 15:17:03 +000013149 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013150 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013151 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013152 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013153 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013154 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013155 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013156 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013157 /* Do nothing, phy ID already set up in
13158 * tg3_get_eeprom_hw_cfg().
13159 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013160 } else {
13161 struct subsys_tbl_ent *p;
13162
13163 /* No eeprom signature? Try the hardcoded
13164 * subsys device table.
13165 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013166 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013167 if (!p)
13168 return -ENODEV;
13169
13170 tp->phy_id = p->phy_id;
13171 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013172 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013173 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013174 }
13175 }
13176
Matt Carlsona6b68da2010-12-06 08:28:52 +000013177 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013178 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13179 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13180 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013181 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13182 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13183 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013184 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13185
Matt Carlsone256f8a2011-03-09 16:58:24 +000013186 tg3_phy_init_link_config(tp);
13187
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013188 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013189 !tg3_flag(tp, ENABLE_APE) &&
13190 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013191 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013192
13193 tg3_readphy(tp, MII_BMSR, &bmsr);
13194 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13195 (bmsr & BMSR_LSTATUS))
13196 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013197
Linus Torvalds1da177e2005-04-16 15:20:36 -070013198 err = tg3_phy_reset(tp);
13199 if (err)
13200 return err;
13201
Matt Carlson42b64a42011-05-19 12:12:49 +000013202 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013203
Michael Chan3600d912006-12-07 00:21:48 -080013204 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13205 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13206 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13207 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013208 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13209 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013210
13211 tg3_writephy(tp, MII_BMCR,
13212 BMCR_ANENABLE | BMCR_ANRESTART);
13213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013214 }
13215
13216skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013217 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013218 err = tg3_init_5401phy_dsp(tp);
13219 if (err)
13220 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013221
Linus Torvalds1da177e2005-04-16 15:20:36 -070013222 err = tg3_init_5401phy_dsp(tp);
13223 }
13224
Linus Torvalds1da177e2005-04-16 15:20:36 -070013225 return err;
13226}
13227
Matt Carlson184b8902010-04-05 10:19:25 +000013228static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013229{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013230 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013231 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013232 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013233 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013234
Matt Carlson535a4902011-07-20 10:20:56 +000013235 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013236 if (!vpd_data)
13237 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013238
Matt Carlson535a4902011-07-20 10:20:56 +000013239 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013240 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013241 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013242
13243 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13244 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13245 i += PCI_VPD_LRDT_TAG_SIZE;
13246
Matt Carlson535a4902011-07-20 10:20:56 +000013247 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013248 goto out_not_found;
13249
Matt Carlson184b8902010-04-05 10:19:25 +000013250 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13251 PCI_VPD_RO_KEYWORD_MFR_ID);
13252 if (j > 0) {
13253 len = pci_vpd_info_field_size(&vpd_data[j]);
13254
13255 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13256 if (j + len > block_end || len != 4 ||
13257 memcmp(&vpd_data[j], "1028", 4))
13258 goto partno;
13259
13260 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13261 PCI_VPD_RO_KEYWORD_VENDOR0);
13262 if (j < 0)
13263 goto partno;
13264
13265 len = pci_vpd_info_field_size(&vpd_data[j]);
13266
13267 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13268 if (j + len > block_end)
13269 goto partno;
13270
13271 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013272 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013273 }
13274
13275partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013276 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13277 PCI_VPD_RO_KEYWORD_PARTNO);
13278 if (i < 0)
13279 goto out_not_found;
13280
13281 len = pci_vpd_info_field_size(&vpd_data[i]);
13282
13283 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13284 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013285 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013286 goto out_not_found;
13287
13288 memcpy(tp->board_part_number, &vpd_data[i], len);
13289
Linus Torvalds1da177e2005-04-16 15:20:36 -070013290out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013291 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013292 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013293 return;
13294
13295out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013296 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13297 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13298 strcpy(tp->board_part_number, "BCM5717");
13299 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13300 strcpy(tp->board_part_number, "BCM5718");
13301 else
13302 goto nomatch;
13303 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13304 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13305 strcpy(tp->board_part_number, "BCM57780");
13306 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13307 strcpy(tp->board_part_number, "BCM57760");
13308 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13309 strcpy(tp->board_part_number, "BCM57790");
13310 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13311 strcpy(tp->board_part_number, "BCM57788");
13312 else
13313 goto nomatch;
13314 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13315 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13316 strcpy(tp->board_part_number, "BCM57761");
13317 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13318 strcpy(tp->board_part_number, "BCM57765");
13319 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13320 strcpy(tp->board_part_number, "BCM57781");
13321 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13322 strcpy(tp->board_part_number, "BCM57785");
13323 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13324 strcpy(tp->board_part_number, "BCM57791");
13325 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13326 strcpy(tp->board_part_number, "BCM57795");
13327 else
13328 goto nomatch;
13329 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013330 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013331 } else {
13332nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013333 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013335}
13336
Matt Carlson9c8a6202007-10-21 16:16:08 -070013337static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13338{
13339 u32 val;
13340
Matt Carlsone4f34112009-02-25 14:25:00 +000013341 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013342 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013343 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013344 val != 0)
13345 return 0;
13346
13347 return 1;
13348}
13349
Matt Carlsonacd9c112009-02-25 14:26:33 +000013350static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13351{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013352 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013353 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013354 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013355
13356 if (tg3_nvram_read(tp, 0xc, &offset) ||
13357 tg3_nvram_read(tp, 0x4, &start))
13358 return;
13359
13360 offset = tg3_nvram_logical_addr(tp, offset);
13361
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013362 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013363 return;
13364
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013365 if ((val & 0xfc000000) == 0x0c000000) {
13366 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013367 return;
13368
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013369 if (val == 0)
13370 newver = true;
13371 }
13372
Matt Carlson75f99362010-04-05 10:19:24 +000013373 dst_off = strlen(tp->fw_ver);
13374
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013375 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013376 if (TG3_VER_SIZE - dst_off < 16 ||
13377 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013378 return;
13379
13380 offset = offset + ver_offset - start;
13381 for (i = 0; i < 16; i += 4) {
13382 __be32 v;
13383 if (tg3_nvram_read_be32(tp, offset + i, &v))
13384 return;
13385
Matt Carlson75f99362010-04-05 10:19:24 +000013386 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013387 }
13388 } else {
13389 u32 major, minor;
13390
13391 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13392 return;
13393
13394 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13395 TG3_NVM_BCVER_MAJSFT;
13396 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013397 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13398 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013399 }
13400}
13401
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013402static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13403{
13404 u32 val, major, minor;
13405
13406 /* Use native endian representation */
13407 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13408 return;
13409
13410 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13411 TG3_NVM_HWSB_CFG1_MAJSFT;
13412 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13413 TG3_NVM_HWSB_CFG1_MINSFT;
13414
13415 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13416}
13417
Matt Carlsondfe00d72008-11-21 17:19:41 -080013418static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13419{
13420 u32 offset, major, minor, build;
13421
Matt Carlson75f99362010-04-05 10:19:24 +000013422 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013423
13424 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13425 return;
13426
13427 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13428 case TG3_EEPROM_SB_REVISION_0:
13429 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13430 break;
13431 case TG3_EEPROM_SB_REVISION_2:
13432 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13433 break;
13434 case TG3_EEPROM_SB_REVISION_3:
13435 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13436 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013437 case TG3_EEPROM_SB_REVISION_4:
13438 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13439 break;
13440 case TG3_EEPROM_SB_REVISION_5:
13441 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13442 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013443 case TG3_EEPROM_SB_REVISION_6:
13444 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13445 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013446 default:
13447 return;
13448 }
13449
Matt Carlsone4f34112009-02-25 14:25:00 +000013450 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013451 return;
13452
13453 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13454 TG3_EEPROM_SB_EDH_BLD_SHFT;
13455 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13456 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13457 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13458
13459 if (minor > 99 || build > 26)
13460 return;
13461
Matt Carlson75f99362010-04-05 10:19:24 +000013462 offset = strlen(tp->fw_ver);
13463 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13464 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013465
13466 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013467 offset = strlen(tp->fw_ver);
13468 if (offset < TG3_VER_SIZE - 1)
13469 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013470 }
13471}
13472
Matt Carlsonacd9c112009-02-25 14:26:33 +000013473static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013474{
13475 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013476 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013477
13478 for (offset = TG3_NVM_DIR_START;
13479 offset < TG3_NVM_DIR_END;
13480 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013481 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013482 return;
13483
13484 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13485 break;
13486 }
13487
13488 if (offset == TG3_NVM_DIR_END)
13489 return;
13490
Joe Perches63c3a662011-04-26 08:12:10 +000013491 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013492 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013493 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013494 return;
13495
Matt Carlsone4f34112009-02-25 14:25:00 +000013496 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013497 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013498 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013499 return;
13500
13501 offset += val - start;
13502
Matt Carlsonacd9c112009-02-25 14:26:33 +000013503 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013504
Matt Carlsonacd9c112009-02-25 14:26:33 +000013505 tp->fw_ver[vlen++] = ',';
13506 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013507
13508 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013509 __be32 v;
13510 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013511 return;
13512
Al Virob9fc7dc2007-12-17 22:59:57 -080013513 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013514
Matt Carlsonacd9c112009-02-25 14:26:33 +000013515 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13516 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013517 break;
13518 }
13519
Matt Carlsonacd9c112009-02-25 14:26:33 +000013520 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13521 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013522 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013523}
13524
Matt Carlson7fd76442009-02-25 14:27:20 +000013525static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13526{
13527 int vlen;
13528 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013529 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013530
Joe Perches63c3a662011-04-26 08:12:10 +000013531 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013532 return;
13533
13534 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13535 if (apedata != APE_SEG_SIG_MAGIC)
13536 return;
13537
13538 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13539 if (!(apedata & APE_FW_STATUS_READY))
13540 return;
13541
13542 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13543
Matt Carlsondc6d0742010-09-15 08:59:55 +000013544 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013545 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013546 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013547 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013548 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013549 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013550
Matt Carlson7fd76442009-02-25 14:27:20 +000013551 vlen = strlen(tp->fw_ver);
13552
Matt Carlsonecc79642010-08-02 11:26:01 +000013553 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13554 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013555 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13556 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13557 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13558 (apedata & APE_FW_VERSION_BLDMSK));
13559}
13560
Matt Carlsonacd9c112009-02-25 14:26:33 +000013561static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13562{
13563 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013564 bool vpd_vers = false;
13565
13566 if (tp->fw_ver[0] != 0)
13567 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013568
Joe Perches63c3a662011-04-26 08:12:10 +000013569 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013570 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013571 return;
13572 }
13573
Matt Carlsonacd9c112009-02-25 14:26:33 +000013574 if (tg3_nvram_read(tp, 0, &val))
13575 return;
13576
13577 if (val == TG3_EEPROM_MAGIC)
13578 tg3_read_bc_ver(tp);
13579 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13580 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013581 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13582 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013583 else
13584 return;
13585
Matt Carlsonc9cab242011-07-13 09:27:27 +000013586 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013587 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013588
Matt Carlsonc9cab242011-07-13 09:27:27 +000013589 if (tg3_flag(tp, ENABLE_APE)) {
13590 if (tg3_flag(tp, ENABLE_ASF))
13591 tg3_read_dash_ver(tp);
13592 } else if (tg3_flag(tp, ENABLE_ASF)) {
13593 tg3_read_mgmtfw_ver(tp);
13594 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013595
Matt Carlson75f99362010-04-05 10:19:24 +000013596done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013597 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013598}
13599
Michael Chan7544b092007-05-05 13:08:32 -070013600static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13601
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013602static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13603{
Joe Perches63c3a662011-04-26 08:12:10 +000013604 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013605 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013606 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013607 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013608 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013609 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013610}
13611
Matt Carlson41434702011-03-09 16:58:22 +000013612static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013613 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13614 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13615 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13616 { },
13617};
13618
Linus Torvalds1da177e2005-04-16 15:20:36 -070013619static int __devinit tg3_get_invariants(struct tg3 *tp)
13620{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013621 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013622 u32 pci_state_reg, grc_misc_cfg;
13623 u32 val;
13624 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013625 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013626
Linus Torvalds1da177e2005-04-16 15:20:36 -070013627 /* Force memory write invalidate off. If we leave it on,
13628 * then on 5700_BX chips we have to enable a workaround.
13629 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13630 * to match the cacheline size. The Broadcom driver have this
13631 * workaround but turns MWI off all the times so never uses
13632 * it. This seems to suggest that the workaround is insufficient.
13633 */
13634 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13635 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13636 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13637
Matt Carlson16821282011-07-13 09:27:28 +000013638 /* Important! -- Make sure register accesses are byteswapped
13639 * correctly. Also, for those chips that require it, make
13640 * sure that indirect register accesses are enabled before
13641 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013642 */
13643 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13644 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013645 tp->misc_host_ctrl |= (misc_ctrl_reg &
13646 MISC_HOST_CTRL_CHIPREV);
13647 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13648 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013649
13650 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13651 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013652 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13653 u32 prod_id_asic_rev;
13654
Matt Carlson5001e2f2009-11-13 13:03:51 +000013655 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13656 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013657 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13658 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013659 pci_read_config_dword(tp->pdev,
13660 TG3PCI_GEN2_PRODID_ASICREV,
13661 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013662 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13663 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13664 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13665 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13666 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13667 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13668 pci_read_config_dword(tp->pdev,
13669 TG3PCI_GEN15_PRODID_ASICREV,
13670 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013671 else
13672 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13673 &prod_id_asic_rev);
13674
Matt Carlson321d32a2008-11-21 17:22:19 -080013675 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013677
Michael Chanff645be2005-04-21 17:09:53 -070013678 /* Wrong chip ID in 5752 A0. This code can be removed later
13679 * as A0 is not in production.
13680 */
13681 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13682 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13683
Michael Chan68929142005-08-09 20:17:14 -070013684 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13685 * we need to disable memory and use config. cycles
13686 * only to access all registers. The 5702/03 chips
13687 * can mistakenly decode the special cycles from the
13688 * ICH chipsets as memory write cycles, causing corruption
13689 * of register and memory space. Only certain ICH bridges
13690 * will drive special cycles with non-zero data during the
13691 * address phase which can fall within the 5703's address
13692 * range. This is not an ICH bug as the PCI spec allows
13693 * non-zero address during special cycles. However, only
13694 * these ICH bridges are known to drive non-zero addresses
13695 * during special cycles.
13696 *
13697 * Since special cycles do not cross PCI bridges, we only
13698 * enable this workaround if the 5703 is on the secondary
13699 * bus of these ICH bridges.
13700 */
13701 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13702 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13703 static struct tg3_dev_id {
13704 u32 vendor;
13705 u32 device;
13706 u32 rev;
13707 } ich_chipsets[] = {
13708 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13709 PCI_ANY_ID },
13710 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13711 PCI_ANY_ID },
13712 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13713 0xa },
13714 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13715 PCI_ANY_ID },
13716 { },
13717 };
13718 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13719 struct pci_dev *bridge = NULL;
13720
13721 while (pci_id->vendor != 0) {
13722 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13723 bridge);
13724 if (!bridge) {
13725 pci_id++;
13726 continue;
13727 }
13728 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013729 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013730 continue;
13731 }
13732 if (bridge->subordinate &&
13733 (bridge->subordinate->number ==
13734 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013735 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013736 pci_dev_put(bridge);
13737 break;
13738 }
13739 }
13740 }
13741
Matt Carlson6ff6f812011-05-19 12:12:54 +000013742 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013743 static struct tg3_dev_id {
13744 u32 vendor;
13745 u32 device;
13746 } bridge_chipsets[] = {
13747 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13748 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13749 { },
13750 };
13751 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13752 struct pci_dev *bridge = NULL;
13753
13754 while (pci_id->vendor != 0) {
13755 bridge = pci_get_device(pci_id->vendor,
13756 pci_id->device,
13757 bridge);
13758 if (!bridge) {
13759 pci_id++;
13760 continue;
13761 }
13762 if (bridge->subordinate &&
13763 (bridge->subordinate->number <=
13764 tp->pdev->bus->number) &&
13765 (bridge->subordinate->subordinate >=
13766 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013767 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013768 pci_dev_put(bridge);
13769 break;
13770 }
13771 }
13772 }
13773
Michael Chan4a29cc22006-03-19 13:21:12 -080013774 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13775 * DMA addresses > 40-bit. This bridge may have other additional
13776 * 57xx devices behind it in some 4-port NIC designs for example.
13777 * Any tg3 device found behind the bridge will also need the 40-bit
13778 * DMA workaround.
13779 */
Michael Chana4e2b342005-10-26 15:46:52 -070013780 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13781 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013782 tg3_flag_set(tp, 5780_CLASS);
13783 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013784 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013785 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013786 struct pci_dev *bridge = NULL;
13787
13788 do {
13789 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13790 PCI_DEVICE_ID_SERVERWORKS_EPB,
13791 bridge);
13792 if (bridge && bridge->subordinate &&
13793 (bridge->subordinate->number <=
13794 tp->pdev->bus->number) &&
13795 (bridge->subordinate->subordinate >=
13796 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013797 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013798 pci_dev_put(bridge);
13799 break;
13800 }
13801 } while (bridge);
13802 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013803
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013804 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000013805 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070013806 tp->pdev_peer = tg3_find_peer(tp);
13807
Matt Carlsonc885e822010-08-02 11:25:57 +000013808 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013809 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13810 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013811 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013812
13813 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013814 tg3_flag(tp, 5717_PLUS))
13815 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013816
Matt Carlson321d32a2008-11-21 17:22:19 -080013817 /* Intentionally exclude ASIC_REV_5906 */
13818 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013819 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013820 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013821 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013822 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013823 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013824 tg3_flag(tp, 57765_PLUS))
13825 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013826
13827 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13828 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013829 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013830 tg3_flag(tp, 5755_PLUS) ||
13831 tg3_flag(tp, 5780_CLASS))
13832 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013833
Matt Carlson6ff6f812011-05-19 12:12:54 +000013834 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013835 tg3_flag(tp, 5750_PLUS))
13836 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013837
Matt Carlson507399f2009-11-13 13:03:37 +000013838 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013839 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013840 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013841 else if (tg3_flag(tp, 57765_PLUS))
13842 tg3_flag_set(tp, HW_TSO_3);
13843 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013844 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013845 tg3_flag_set(tp, HW_TSO_2);
13846 else if (tg3_flag(tp, 5750_PLUS)) {
13847 tg3_flag_set(tp, HW_TSO_1);
13848 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013849 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13850 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013851 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013852 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13853 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13854 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013855 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013856 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13857 tp->fw_needed = FIRMWARE_TG3TSO5;
13858 else
13859 tp->fw_needed = FIRMWARE_TG3TSO;
13860 }
13861
Matt Carlsondabc5c62011-05-19 12:12:52 +000013862 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013863 if (tg3_flag(tp, HW_TSO_1) ||
13864 tg3_flag(tp, HW_TSO_2) ||
13865 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsondabc5c62011-05-19 12:12:52 +000013866 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13867 tg3_flag_set(tp, TSO_CAPABLE);
13868 else {
13869 tg3_flag_clear(tp, TSO_CAPABLE);
13870 tg3_flag_clear(tp, TSO_BUG);
13871 tp->fw_needed = NULL;
13872 }
13873
13874 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
13875 tp->fw_needed = FIRMWARE_TG3;
13876
Matt Carlson507399f2009-11-13 13:03:37 +000013877 tp->irq_max = 1;
13878
Joe Perches63c3a662011-04-26 08:12:10 +000013879 if (tg3_flag(tp, 5750_PLUS)) {
13880 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013881 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13882 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13883 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13884 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13885 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000013886 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013887
Joe Perches63c3a662011-04-26 08:12:10 +000013888 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013889 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013890 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070013891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013892
Joe Perches63c3a662011-04-26 08:12:10 +000013893 if (tg3_flag(tp, 57765_PLUS)) {
13894 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000013895 tp->irq_max = TG3_IRQ_MAX_VECS;
13896 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013897 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013898
Matt Carlson2ffcc982011-05-19 12:12:44 +000013899 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000013900 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013901
Joe Perches63c3a662011-04-26 08:12:10 +000013902 if (tg3_flag(tp, 5717_PLUS))
13903 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000013904
Joe Perches63c3a662011-04-26 08:12:10 +000013905 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013906 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Joe Perches63c3a662011-04-26 08:12:10 +000013907 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000013908
Joe Perches63c3a662011-04-26 08:12:10 +000013909 if (!tg3_flag(tp, 5705_PLUS) ||
13910 tg3_flag(tp, 5780_CLASS) ||
13911 tg3_flag(tp, USE_JUMBO_BDFLAG))
13912 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070013913
Matt Carlson52f44902008-11-21 17:17:04 -080013914 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13915 &pci_state_reg);
13916
Jon Mason708ebb32011-06-27 12:56:50 +000013917 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013918 u16 lnkctl;
13919
Joe Perches63c3a662011-04-26 08:12:10 +000013920 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013921
Matt Carlsoncf790032010-11-24 08:31:48 +000013922 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013923 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13924 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013925 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013926
13927 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013928
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013929 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +000013930 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013931 &lnkctl);
13932 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000013933 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13934 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013935 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000013936 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000013937 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013938 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013939 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013940 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13941 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000013942 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b05902010-01-20 16:58:02 +000013943 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013944 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080013945 }
Matt Carlson52f44902008-11-21 17:17:04 -080013946 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000013947 /* BCM5785 devices are effectively PCIe devices, and should
13948 * follow PCIe codepaths, but do not have a PCIe capabilities
13949 * section.
13950 */
Joe Perches63c3a662011-04-26 08:12:10 +000013951 tg3_flag_set(tp, PCI_EXPRESS);
13952 } else if (!tg3_flag(tp, 5705_PLUS) ||
13953 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080013954 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13955 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013956 dev_err(&tp->pdev->dev,
13957 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013958 return -EIO;
13959 }
13960
13961 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000013962 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080013963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013964
Michael Chan399de502005-10-03 14:02:39 -070013965 /* If we have an AMD 762 or VIA K8T800 chipset, write
13966 * reordering to the mailbox registers done by the host
13967 * controller can cause major troubles. We read back from
13968 * every mailbox register write to force the writes to be
13969 * posted to the chip in order.
13970 */
Matt Carlson41434702011-03-09 16:58:22 +000013971 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013972 !tg3_flag(tp, PCI_EXPRESS))
13973 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070013974
Matt Carlson69fc4052008-12-21 20:19:57 -080013975 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13976 &tp->pci_cacheline_sz);
13977 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13978 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013979 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13980 tp->pci_lat_timer < 64) {
13981 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013982 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13983 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013984 }
13985
Matt Carlson16821282011-07-13 09:27:28 +000013986 /* Important! -- It is critical that the PCI-X hw workaround
13987 * situation is decided before the first MMIO register access.
13988 */
Matt Carlson52f44902008-11-21 17:17:04 -080013989 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13990 /* 5700 BX chips need to have their TX producer index
13991 * mailboxes written twice to workaround a bug.
13992 */
Joe Perches63c3a662011-04-26 08:12:10 +000013993 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070013994
Matt Carlson52f44902008-11-21 17:17:04 -080013995 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013996 *
13997 * The workaround is to use indirect register accesses
13998 * for all chip writes not to mailbox registers.
13999 */
Joe Perches63c3a662011-04-26 08:12:10 +000014000 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014001 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014002
Joe Perches63c3a662011-04-26 08:12:10 +000014003 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014004
14005 /* The chip can have it's power management PCI config
14006 * space registers clobbered due to this bug.
14007 * So explicitly force the chip into D0 here.
14008 */
Matt Carlson9974a352007-10-07 23:27:28 -070014009 pci_read_config_dword(tp->pdev,
14010 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014011 &pm_reg);
14012 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14013 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014014 pci_write_config_dword(tp->pdev,
14015 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014016 pm_reg);
14017
14018 /* Also, force SERR#/PERR# in PCI command. */
14019 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14020 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14021 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14022 }
14023 }
14024
Linus Torvalds1da177e2005-04-16 15:20:36 -070014025 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014026 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014027 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014028 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014029
14030 /* Chip-specific fixup from Broadcom driver */
14031 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14032 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14033 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14034 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14035 }
14036
Michael Chan1ee582d2005-08-09 20:16:46 -070014037 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014038 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014039 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014040 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014041 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014042 tp->write32_tx_mbox = tg3_write32;
14043 tp->write32_rx_mbox = tg3_write32;
14044
14045 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014046 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014047 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014048 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014049 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014050 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14051 /*
14052 * Back to back register writes can cause problems on these
14053 * chips, the workaround is to read back all reg writes
14054 * except those to mailbox regs.
14055 *
14056 * See tg3_write_indirect_reg32().
14057 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014058 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014059 }
14060
Joe Perches63c3a662011-04-26 08:12:10 +000014061 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014062 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014063 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014064 tp->write32_rx_mbox = tg3_write_flush_reg32;
14065 }
Michael Chan20094932005-08-09 20:16:32 -070014066
Joe Perches63c3a662011-04-26 08:12:10 +000014067 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014068 tp->read32 = tg3_read_indirect_reg32;
14069 tp->write32 = tg3_write_indirect_reg32;
14070 tp->read32_mbox = tg3_read_indirect_mbox;
14071 tp->write32_mbox = tg3_write_indirect_mbox;
14072 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14073 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14074
14075 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014076 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014077
14078 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14079 pci_cmd &= ~PCI_COMMAND_MEMORY;
14080 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14081 }
Michael Chanb5d37722006-09-27 16:06:21 -070014082 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14083 tp->read32_mbox = tg3_read32_mbox_5906;
14084 tp->write32_mbox = tg3_write32_mbox_5906;
14085 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14086 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14087 }
Michael Chan68929142005-08-09 20:17:14 -070014088
Michael Chanbbadf502006-04-06 21:46:34 -070014089 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014090 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014091 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014092 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014093 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014094
Matt Carlson16821282011-07-13 09:27:28 +000014095 /* The memory arbiter has to be enabled in order for SRAM accesses
14096 * to succeed. Normally on powerup the tg3 chip firmware will make
14097 * sure it is enabled, but other entities such as system netboot
14098 * code might disable it.
14099 */
14100 val = tr32(MEMARB_MODE);
14101 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14102
Matt Carlson69f11c92011-07-13 09:27:30 +000014103 if (tg3_flag(tp, PCIX_MODE)) {
14104 pci_read_config_dword(tp->pdev,
14105 tp->pcix_cap + PCI_X_STATUS, &val);
14106 tp->pci_fn = val & 0x7;
14107 } else {
14108 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14109 }
14110
Michael Chan7d0c41e2005-04-21 17:06:20 -070014111 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014112 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014113 * determined before calling tg3_set_power_state() so that
14114 * we know whether or not to switch out of Vaux power.
14115 * When the flag is set, it means that GPIO1 is used for eeprom
14116 * write protect and also implies that it is a LOM where GPIOs
14117 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014118 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014119 tg3_get_eeprom_hw_cfg(tp);
14120
Joe Perches63c3a662011-04-26 08:12:10 +000014121 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014122 /* Allow reads and writes to the
14123 * APE register and memory space.
14124 */
14125 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +000014126 PCISTATE_ALLOW_APE_SHMEM_WR |
14127 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014128 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14129 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014130
14131 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014132 }
14133
Matt Carlson9936bcf2007-10-10 18:03:07 -070014134 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014135 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014136 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014137 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014138 tg3_flag(tp, 57765_PLUS))
14139 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014140
Matt Carlson16821282011-07-13 09:27:28 +000014141 /* Set up tp->grc_local_ctrl before calling
14142 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14143 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014144 * It is also used as eeprom write protect on LOMs.
14145 */
14146 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014147 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014148 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014149 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14150 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014151 /* Unused GPIO3 must be driven as output on 5752 because there
14152 * are no pull-up resistors on unused GPIO pins.
14153 */
14154 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14155 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014156
Matt Carlson321d32a2008-11-21 17:22:19 -080014157 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014158 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14159 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080014160 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14161
Matt Carlson8d519ab2009-04-20 06:58:01 +000014162 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14163 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014164 /* Turn off the debug UART. */
14165 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014166 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014167 /* Keep VMain power. */
14168 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14169 GRC_LCLCTRL_GPIO_OUTPUT0;
14170 }
14171
Matt Carlson16821282011-07-13 09:27:28 +000014172 /* Switch out of Vaux if it is a NIC */
14173 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014174
Linus Torvalds1da177e2005-04-16 15:20:36 -070014175 /* Derive initial jumbo mode from MTU assigned in
14176 * ether_setup() via the alloc_etherdev() call
14177 */
Joe Perches63c3a662011-04-26 08:12:10 +000014178 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14179 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014180
14181 /* Determine WakeOnLan speed to use. */
14182 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14183 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14184 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14185 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014186 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014187 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014188 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014189 }
14190
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014191 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014192 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014193
Linus Torvalds1da177e2005-04-16 15:20:36 -070014194 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014195 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14196 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014197 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014198 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014199 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14200 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14201 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014202
14203 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14204 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014205 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014206 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014207 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014208
Joe Perches63c3a662011-04-26 08:12:10 +000014209 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014210 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014211 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014212 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014213 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014214 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014215 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014216 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14217 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014218 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14219 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014220 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014221 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014222 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014223 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014224 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014226
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014227 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14228 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14229 tp->phy_otp = tg3_read_otp_phycfg(tp);
14230 if (tp->phy_otp == 0)
14231 tp->phy_otp = TG3_OTP_DEFAULT;
14232 }
14233
Joe Perches63c3a662011-04-26 08:12:10 +000014234 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014235 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14236 else
14237 tp->mi_mode = MAC_MI_MODE_BASE;
14238
Linus Torvalds1da177e2005-04-16 15:20:36 -070014239 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014240 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14241 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14242 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14243
Matt Carlson4d958472011-04-20 07:57:35 +000014244 /* Set these bits to enable statistics workaround. */
14245 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14246 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14247 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14248 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14249 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14250 }
14251
Matt Carlson321d32a2008-11-21 17:22:19 -080014252 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14253 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014254 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014255
Matt Carlson158d7ab2008-05-29 01:37:54 -070014256 err = tg3_mdio_init(tp);
14257 if (err)
14258 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014259
14260 /* Initialize data/descriptor byte/word swapping. */
14261 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014262 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14263 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14264 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14265 GRC_MODE_B2HRX_ENABLE |
14266 GRC_MODE_HTX2B_ENABLE |
14267 GRC_MODE_HOST_STACKUP);
14268 else
14269 val &= GRC_MODE_HOST_STACKUP;
14270
Linus Torvalds1da177e2005-04-16 15:20:36 -070014271 tw32(GRC_MODE, val | tp->grc_mode);
14272
14273 tg3_switch_clocks(tp);
14274
14275 /* Clear this out for sanity. */
14276 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14277
14278 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14279 &pci_state_reg);
14280 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014281 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014282 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14283
14284 if (chiprevid == CHIPREV_ID_5701_A0 ||
14285 chiprevid == CHIPREV_ID_5701_B0 ||
14286 chiprevid == CHIPREV_ID_5701_B2 ||
14287 chiprevid == CHIPREV_ID_5701_B5) {
14288 void __iomem *sram_base;
14289
14290 /* Write some dummy words into the SRAM status block
14291 * area, see if it reads back correctly. If the return
14292 * value is bad, force enable the PCIX workaround.
14293 */
14294 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14295
14296 writel(0x00000000, sram_base);
14297 writel(0x00000000, sram_base + 4);
14298 writel(0xffffffff, sram_base + 4);
14299 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014300 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014301 }
14302 }
14303
14304 udelay(50);
14305 tg3_nvram_init(tp);
14306
14307 grc_misc_cfg = tr32(GRC_MISC_CFG);
14308 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14309
Linus Torvalds1da177e2005-04-16 15:20:36 -070014310 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14311 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14312 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014313 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014314
Joe Perches63c3a662011-04-26 08:12:10 +000014315 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014316 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014317 tg3_flag_set(tp, TAGGED_STATUS);
14318 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014319 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14320 HOSTCC_MODE_CLRTICK_TXBD);
14321
14322 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14323 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14324 tp->misc_host_ctrl);
14325 }
14326
Matt Carlson3bda1252008-08-15 14:08:22 -070014327 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014328 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014329 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014330 else
14331 tp->mac_mode = TG3_DEF_MAC_MODE;
14332
Linus Torvalds1da177e2005-04-16 15:20:36 -070014333 /* these are limited to 10/100 only */
14334 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14335 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14336 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14337 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14338 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14339 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14340 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14341 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14342 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014343 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14344 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014345 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014346 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14347 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014348 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14349 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014350
14351 err = tg3_phy_probe(tp);
14352 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014353 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014354 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014355 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014356 }
14357
Matt Carlson184b8902010-04-05 10:19:25 +000014358 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014359 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014360
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014361 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14362 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014363 } else {
14364 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014365 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014366 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014367 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014368 }
14369
14370 /* 5700 {AX,BX} chips have a broken status block link
14371 * change bit implementation, so we must use the
14372 * status register in those cases.
14373 */
14374 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014375 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014376 else
Joe Perches63c3a662011-04-26 08:12:10 +000014377 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014378
14379 /* The led_ctrl is set during tg3_phy_probe, here we might
14380 * have to force the link status polling mechanism based
14381 * upon subsystem IDs.
14382 */
14383 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014384 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014385 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14386 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014387 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014388 }
14389
14390 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014391 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014392 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014393 else
Joe Perches63c3a662011-04-26 08:12:10 +000014394 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014395
Matt Carlsonbf933c82011-01-25 15:58:49 +000014396 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014397 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014398 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014399 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014400 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014401#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014402 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014403#endif
14404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014405
Matt Carlson2c49a442010-09-30 10:34:35 +000014406 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14407 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014408 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14409
Matt Carlson2c49a442010-09-30 10:34:35 +000014410 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014411
14412 /* Increment the rx prod index on the rx std ring by at most
14413 * 8 for these chips to workaround hw errata.
14414 */
14415 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14416 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14417 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14418 tp->rx_std_max_post = 8;
14419
Joe Perches63c3a662011-04-26 08:12:10 +000014420 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014421 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14422 PCIE_PWR_MGMT_L1_THRESH_MSK;
14423
Linus Torvalds1da177e2005-04-16 15:20:36 -070014424 return err;
14425}
14426
David S. Miller49b6e95f2007-03-29 01:38:42 -070014427#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014428static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14429{
14430 struct net_device *dev = tp->dev;
14431 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014432 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014433 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014434 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014435
David S. Miller49b6e95f2007-03-29 01:38:42 -070014436 addr = of_get_property(dp, "local-mac-address", &len);
14437 if (addr && len == 6) {
14438 memcpy(dev->dev_addr, addr, 6);
14439 memcpy(dev->perm_addr, dev->dev_addr, 6);
14440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014441 }
14442 return -ENODEV;
14443}
14444
14445static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14446{
14447 struct net_device *dev = tp->dev;
14448
14449 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014450 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014451 return 0;
14452}
14453#endif
14454
14455static int __devinit tg3_get_device_address(struct tg3 *tp)
14456{
14457 struct net_device *dev = tp->dev;
14458 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014459 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014460
David S. Miller49b6e95f2007-03-29 01:38:42 -070014461#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014462 if (!tg3_get_macaddr_sparc(tp))
14463 return 0;
14464#endif
14465
14466 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014467 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014468 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014469 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14470 mac_offset = 0xcc;
14471 if (tg3_nvram_lock(tp))
14472 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14473 else
14474 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014475 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014476 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014477 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014478 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014479 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014480 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014481 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014482
14483 /* First try to get it from MAC address mailbox. */
14484 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14485 if ((hi >> 16) == 0x484b) {
14486 dev->dev_addr[0] = (hi >> 8) & 0xff;
14487 dev->dev_addr[1] = (hi >> 0) & 0xff;
14488
14489 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14490 dev->dev_addr[2] = (lo >> 24) & 0xff;
14491 dev->dev_addr[3] = (lo >> 16) & 0xff;
14492 dev->dev_addr[4] = (lo >> 8) & 0xff;
14493 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014494
Michael Chan008652b2006-03-27 23:14:53 -080014495 /* Some old bootcode may report a 0 MAC address in SRAM */
14496 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14497 }
14498 if (!addr_ok) {
14499 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014500 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014501 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014502 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014503 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14504 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014505 }
14506 /* Finally just fetch it out of the MAC control regs. */
14507 else {
14508 hi = tr32(MAC_ADDR_0_HIGH);
14509 lo = tr32(MAC_ADDR_0_LOW);
14510
14511 dev->dev_addr[5] = lo & 0xff;
14512 dev->dev_addr[4] = (lo >> 8) & 0xff;
14513 dev->dev_addr[3] = (lo >> 16) & 0xff;
14514 dev->dev_addr[2] = (lo >> 24) & 0xff;
14515 dev->dev_addr[1] = hi & 0xff;
14516 dev->dev_addr[0] = (hi >> 8) & 0xff;
14517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014518 }
14519
14520 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014521#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014522 if (!tg3_get_default_macaddr_sparc(tp))
14523 return 0;
14524#endif
14525 return -EINVAL;
14526 }
John W. Linville2ff43692005-09-12 14:44:20 -070014527 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014528 return 0;
14529}
14530
David S. Miller59e6b432005-05-18 22:50:10 -070014531#define BOUNDARY_SINGLE_CACHELINE 1
14532#define BOUNDARY_MULTI_CACHELINE 2
14533
14534static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14535{
14536 int cacheline_size;
14537 u8 byte;
14538 int goal;
14539
14540 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14541 if (byte == 0)
14542 cacheline_size = 1024;
14543 else
14544 cacheline_size = (int) byte * 4;
14545
14546 /* On 5703 and later chips, the boundary bits have no
14547 * effect.
14548 */
14549 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14550 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014551 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014552 goto out;
14553
14554#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14555 goal = BOUNDARY_MULTI_CACHELINE;
14556#else
14557#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14558 goal = BOUNDARY_SINGLE_CACHELINE;
14559#else
14560 goal = 0;
14561#endif
14562#endif
14563
Joe Perches63c3a662011-04-26 08:12:10 +000014564 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014565 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14566 goto out;
14567 }
14568
David S. Miller59e6b432005-05-18 22:50:10 -070014569 if (!goal)
14570 goto out;
14571
14572 /* PCI controllers on most RISC systems tend to disconnect
14573 * when a device tries to burst across a cache-line boundary.
14574 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14575 *
14576 * Unfortunately, for PCI-E there are only limited
14577 * write-side controls for this, and thus for reads
14578 * we will still get the disconnects. We'll also waste
14579 * these PCI cycles for both read and write for chips
14580 * other than 5700 and 5701 which do not implement the
14581 * boundary bits.
14582 */
Joe Perches63c3a662011-04-26 08:12:10 +000014583 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014584 switch (cacheline_size) {
14585 case 16:
14586 case 32:
14587 case 64:
14588 case 128:
14589 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14590 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14591 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14592 } else {
14593 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14594 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14595 }
14596 break;
14597
14598 case 256:
14599 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14600 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14601 break;
14602
14603 default:
14604 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14605 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14606 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014607 }
Joe Perches63c3a662011-04-26 08:12:10 +000014608 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014609 switch (cacheline_size) {
14610 case 16:
14611 case 32:
14612 case 64:
14613 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14614 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14615 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14616 break;
14617 }
14618 /* fallthrough */
14619 case 128:
14620 default:
14621 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14622 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14623 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014624 }
David S. Miller59e6b432005-05-18 22:50:10 -070014625 } else {
14626 switch (cacheline_size) {
14627 case 16:
14628 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14629 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14630 DMA_RWCTRL_WRITE_BNDRY_16);
14631 break;
14632 }
14633 /* fallthrough */
14634 case 32:
14635 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14636 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14637 DMA_RWCTRL_WRITE_BNDRY_32);
14638 break;
14639 }
14640 /* fallthrough */
14641 case 64:
14642 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14643 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14644 DMA_RWCTRL_WRITE_BNDRY_64);
14645 break;
14646 }
14647 /* fallthrough */
14648 case 128:
14649 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14650 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14651 DMA_RWCTRL_WRITE_BNDRY_128);
14652 break;
14653 }
14654 /* fallthrough */
14655 case 256:
14656 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14657 DMA_RWCTRL_WRITE_BNDRY_256);
14658 break;
14659 case 512:
14660 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14661 DMA_RWCTRL_WRITE_BNDRY_512);
14662 break;
14663 case 1024:
14664 default:
14665 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14666 DMA_RWCTRL_WRITE_BNDRY_1024);
14667 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014668 }
David S. Miller59e6b432005-05-18 22:50:10 -070014669 }
14670
14671out:
14672 return val;
14673}
14674
Linus Torvalds1da177e2005-04-16 15:20:36 -070014675static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14676{
14677 struct tg3_internal_buffer_desc test_desc;
14678 u32 sram_dma_descs;
14679 int i, ret;
14680
14681 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14682
14683 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14684 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14685 tw32(RDMAC_STATUS, 0);
14686 tw32(WDMAC_STATUS, 0);
14687
14688 tw32(BUFMGR_MODE, 0);
14689 tw32(FTQ_RESET, 0);
14690
14691 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14692 test_desc.addr_lo = buf_dma & 0xffffffff;
14693 test_desc.nic_mbuf = 0x00002100;
14694 test_desc.len = size;
14695
14696 /*
14697 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14698 * the *second* time the tg3 driver was getting loaded after an
14699 * initial scan.
14700 *
14701 * Broadcom tells me:
14702 * ...the DMA engine is connected to the GRC block and a DMA
14703 * reset may affect the GRC block in some unpredictable way...
14704 * The behavior of resets to individual blocks has not been tested.
14705 *
14706 * Broadcom noted the GRC reset will also reset all sub-components.
14707 */
14708 if (to_device) {
14709 test_desc.cqid_sqid = (13 << 8) | 2;
14710
14711 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14712 udelay(40);
14713 } else {
14714 test_desc.cqid_sqid = (16 << 8) | 7;
14715
14716 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14717 udelay(40);
14718 }
14719 test_desc.flags = 0x00000005;
14720
14721 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14722 u32 val;
14723
14724 val = *(((u32 *)&test_desc) + i);
14725 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14726 sram_dma_descs + (i * sizeof(u32)));
14727 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14728 }
14729 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14730
Matt Carlson859a5882010-04-05 10:19:28 +000014731 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014732 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014733 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014734 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014735
14736 ret = -ENODEV;
14737 for (i = 0; i < 40; i++) {
14738 u32 val;
14739
14740 if (to_device)
14741 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14742 else
14743 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14744 if ((val & 0xffff) == sram_dma_descs) {
14745 ret = 0;
14746 break;
14747 }
14748
14749 udelay(100);
14750 }
14751
14752 return ret;
14753}
14754
David S. Millerded73402005-05-23 13:59:47 -070014755#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014756
Matt Carlson41434702011-03-09 16:58:22 +000014757static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014758 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14759 { },
14760};
14761
Linus Torvalds1da177e2005-04-16 15:20:36 -070014762static int __devinit tg3_test_dma(struct tg3 *tp)
14763{
14764 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014765 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014766 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014767
Matt Carlson4bae65c2010-11-24 08:31:52 +000014768 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14769 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014770 if (!buf) {
14771 ret = -ENOMEM;
14772 goto out_nofree;
14773 }
14774
14775 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14776 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14777
David S. Miller59e6b432005-05-18 22:50:10 -070014778 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014779
Joe Perches63c3a662011-04-26 08:12:10 +000014780 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014781 goto out;
14782
Joe Perches63c3a662011-04-26 08:12:10 +000014783 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014784 /* DMA read watermark not used on PCIE */
14785 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014786 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014787 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14788 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014789 tp->dma_rwctrl |= 0x003f0000;
14790 else
14791 tp->dma_rwctrl |= 0x003f000f;
14792 } else {
14793 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14794 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14795 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014796 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014797
Michael Chan4a29cc22006-03-19 13:21:12 -080014798 /* If the 5704 is behind the EPB bridge, we can
14799 * do the less restrictive ONE_DMA workaround for
14800 * better performance.
14801 */
Joe Perches63c3a662011-04-26 08:12:10 +000014802 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014803 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14804 tp->dma_rwctrl |= 0x8000;
14805 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014806 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14807
Michael Chan49afdeb2007-02-13 12:17:03 -080014808 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14809 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014810 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014811 tp->dma_rwctrl |=
14812 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14813 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14814 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014815 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14816 /* 5780 always in PCIX mode */
14817 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014818 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14819 /* 5714 always in PCIX mode */
14820 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014821 } else {
14822 tp->dma_rwctrl |= 0x001b000f;
14823 }
14824 }
14825
14826 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14827 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14828 tp->dma_rwctrl &= 0xfffffff0;
14829
14830 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14831 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14832 /* Remove this if it causes problems for some boards. */
14833 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14834
14835 /* On 5700/5701 chips, we need to set this bit.
14836 * Otherwise the chip will issue cacheline transactions
14837 * to streamable DMA memory with not all the byte
14838 * enables turned on. This is an error on several
14839 * RISC PCI controllers, in particular sparc64.
14840 *
14841 * On 5703/5704 chips, this bit has been reassigned
14842 * a different meaning. In particular, it is used
14843 * on those chips to enable a PCI-X workaround.
14844 */
14845 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14846 }
14847
14848 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14849
14850#if 0
14851 /* Unneeded, already done by tg3_get_invariants. */
14852 tg3_switch_clocks(tp);
14853#endif
14854
Linus Torvalds1da177e2005-04-16 15:20:36 -070014855 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14856 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14857 goto out;
14858
David S. Miller59e6b432005-05-18 22:50:10 -070014859 /* It is best to perform DMA test with maximum write burst size
14860 * to expose the 5700/5701 write DMA bug.
14861 */
14862 saved_dma_rwctrl = tp->dma_rwctrl;
14863 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14864 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14865
Linus Torvalds1da177e2005-04-16 15:20:36 -070014866 while (1) {
14867 u32 *p = buf, i;
14868
14869 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14870 p[i] = i;
14871
14872 /* Send the buffer to the chip. */
14873 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14874 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014875 dev_err(&tp->pdev->dev,
14876 "%s: Buffer write failed. err = %d\n",
14877 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014878 break;
14879 }
14880
14881#if 0
14882 /* validate data reached card RAM correctly. */
14883 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14884 u32 val;
14885 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14886 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014887 dev_err(&tp->pdev->dev,
14888 "%s: Buffer corrupted on device! "
14889 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014890 /* ret = -ENODEV here? */
14891 }
14892 p[i] = 0;
14893 }
14894#endif
14895 /* Now read it back. */
14896 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14897 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014898 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14899 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014900 break;
14901 }
14902
14903 /* Verify it. */
14904 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14905 if (p[i] == i)
14906 continue;
14907
David S. Miller59e6b432005-05-18 22:50:10 -070014908 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14909 DMA_RWCTRL_WRITE_BNDRY_16) {
14910 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014911 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14912 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14913 break;
14914 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014915 dev_err(&tp->pdev->dev,
14916 "%s: Buffer corrupted on read back! "
14917 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014918 ret = -ENODEV;
14919 goto out;
14920 }
14921 }
14922
14923 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14924 /* Success. */
14925 ret = 0;
14926 break;
14927 }
14928 }
David S. Miller59e6b432005-05-18 22:50:10 -070014929 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14930 DMA_RWCTRL_WRITE_BNDRY_16) {
14931 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014932 * now look for chipsets that are known to expose the
14933 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014934 */
Matt Carlson41434702011-03-09 16:58:22 +000014935 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014936 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14937 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000014938 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014939 /* Safe to use the calculated DMA boundary. */
14940 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000014941 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014942
David S. Miller59e6b432005-05-18 22:50:10 -070014943 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014945
14946out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014947 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014948out_nofree:
14949 return ret;
14950}
14951
Linus Torvalds1da177e2005-04-16 15:20:36 -070014952static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14953{
Joe Perches63c3a662011-04-26 08:12:10 +000014954 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000014955 tp->bufmgr_config.mbuf_read_dma_low_water =
14956 DEFAULT_MB_RDMA_LOW_WATER_5705;
14957 tp->bufmgr_config.mbuf_mac_rx_low_water =
14958 DEFAULT_MB_MACRX_LOW_WATER_57765;
14959 tp->bufmgr_config.mbuf_high_water =
14960 DEFAULT_MB_HIGH_WATER_57765;
14961
14962 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14963 DEFAULT_MB_RDMA_LOW_WATER_5705;
14964 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14965 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14966 tp->bufmgr_config.mbuf_high_water_jumbo =
14967 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000014968 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec172005-07-25 12:31:48 -070014969 tp->bufmgr_config.mbuf_read_dma_low_water =
14970 DEFAULT_MB_RDMA_LOW_WATER_5705;
14971 tp->bufmgr_config.mbuf_mac_rx_low_water =
14972 DEFAULT_MB_MACRX_LOW_WATER_5705;
14973 tp->bufmgr_config.mbuf_high_water =
14974 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014975 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14976 tp->bufmgr_config.mbuf_mac_rx_low_water =
14977 DEFAULT_MB_MACRX_LOW_WATER_5906;
14978 tp->bufmgr_config.mbuf_high_water =
14979 DEFAULT_MB_HIGH_WATER_5906;
14980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014981
Michael Chanfdfec172005-07-25 12:31:48 -070014982 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14983 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14984 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14985 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14986 tp->bufmgr_config.mbuf_high_water_jumbo =
14987 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14988 } else {
14989 tp->bufmgr_config.mbuf_read_dma_low_water =
14990 DEFAULT_MB_RDMA_LOW_WATER;
14991 tp->bufmgr_config.mbuf_mac_rx_low_water =
14992 DEFAULT_MB_MACRX_LOW_WATER;
14993 tp->bufmgr_config.mbuf_high_water =
14994 DEFAULT_MB_HIGH_WATER;
14995
14996 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14997 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14998 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14999 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15000 tp->bufmgr_config.mbuf_high_water_jumbo =
15001 DEFAULT_MB_HIGH_WATER_JUMBO;
15002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015003
15004 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15005 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15006}
15007
15008static char * __devinit tg3_phy_string(struct tg3 *tp)
15009{
Matt Carlson79eb6902010-02-17 15:17:03 +000015010 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15011 case TG3_PHY_ID_BCM5400: return "5400";
15012 case TG3_PHY_ID_BCM5401: return "5401";
15013 case TG3_PHY_ID_BCM5411: return "5411";
15014 case TG3_PHY_ID_BCM5701: return "5701";
15015 case TG3_PHY_ID_BCM5703: return "5703";
15016 case TG3_PHY_ID_BCM5704: return "5704";
15017 case TG3_PHY_ID_BCM5705: return "5705";
15018 case TG3_PHY_ID_BCM5750: return "5750";
15019 case TG3_PHY_ID_BCM5752: return "5752";
15020 case TG3_PHY_ID_BCM5714: return "5714";
15021 case TG3_PHY_ID_BCM5780: return "5780";
15022 case TG3_PHY_ID_BCM5755: return "5755";
15023 case TG3_PHY_ID_BCM5787: return "5787";
15024 case TG3_PHY_ID_BCM5784: return "5784";
15025 case TG3_PHY_ID_BCM5756: return "5722/5756";
15026 case TG3_PHY_ID_BCM5906: return "5906";
15027 case TG3_PHY_ID_BCM5761: return "5761";
15028 case TG3_PHY_ID_BCM5718C: return "5718C";
15029 case TG3_PHY_ID_BCM5718S: return "5718S";
15030 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015031 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015032 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015033 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015034 case 0: return "serdes";
15035 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015037}
15038
Michael Chanf9804dd2005-09-27 12:13:10 -070015039static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15040{
Joe Perches63c3a662011-04-26 08:12:10 +000015041 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015042 strcpy(str, "PCI Express");
15043 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015044 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015045 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15046
15047 strcpy(str, "PCIX:");
15048
15049 if ((clock_ctrl == 7) ||
15050 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15051 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15052 strcat(str, "133MHz");
15053 else if (clock_ctrl == 0)
15054 strcat(str, "33MHz");
15055 else if (clock_ctrl == 2)
15056 strcat(str, "50MHz");
15057 else if (clock_ctrl == 4)
15058 strcat(str, "66MHz");
15059 else if (clock_ctrl == 6)
15060 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015061 } else {
15062 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015063 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015064 strcat(str, "66MHz");
15065 else
15066 strcat(str, "33MHz");
15067 }
Joe Perches63c3a662011-04-26 08:12:10 +000015068 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015069 strcat(str, ":32-bit");
15070 else
15071 strcat(str, ":64-bit");
15072 return str;
15073}
15074
Michael Chan8c2dc7e2005-12-19 16:26:02 -080015075static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015076{
15077 struct pci_dev *peer;
15078 unsigned int func, devnr = tp->pdev->devfn & ~7;
15079
15080 for (func = 0; func < 8; func++) {
15081 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15082 if (peer && peer != tp->pdev)
15083 break;
15084 pci_dev_put(peer);
15085 }
Michael Chan16fe9d72005-12-13 21:09:54 -080015086 /* 5704 can be configured in single-port mode, set peer to
15087 * tp->pdev in that case.
15088 */
15089 if (!peer) {
15090 peer = tp->pdev;
15091 return peer;
15092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015093
15094 /*
15095 * We don't need to keep the refcount elevated; there's no way
15096 * to remove one half of this device without removing the other
15097 */
15098 pci_dev_put(peer);
15099
15100 return peer;
15101}
15102
David S. Miller15f98502005-05-18 22:49:26 -070015103static void __devinit tg3_init_coal(struct tg3 *tp)
15104{
15105 struct ethtool_coalesce *ec = &tp->coal;
15106
15107 memset(ec, 0, sizeof(*ec));
15108 ec->cmd = ETHTOOL_GCOALESCE;
15109 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15110 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15111 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15112 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15113 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15114 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15115 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15116 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15117 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15118
15119 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15120 HOSTCC_MODE_CLRTICK_TXBD)) {
15121 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15122 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15123 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15124 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15125 }
Michael Chand244c892005-07-05 14:42:33 -070015126
Joe Perches63c3a662011-04-26 08:12:10 +000015127 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015128 ec->rx_coalesce_usecs_irq = 0;
15129 ec->tx_coalesce_usecs_irq = 0;
15130 ec->stats_block_coalesce_usecs = 0;
15131 }
David S. Miller15f98502005-05-18 22:49:26 -070015132}
15133
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015134static const struct net_device_ops tg3_netdev_ops = {
15135 .ndo_open = tg3_open,
15136 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080015137 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000015138 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080015139 .ndo_validate_addr = eth_validate_addr,
15140 .ndo_set_multicast_list = tg3_set_rx_mode,
15141 .ndo_set_mac_address = tg3_set_mac_addr,
15142 .ndo_do_ioctl = tg3_ioctl,
15143 .ndo_tx_timeout = tg3_tx_timeout,
15144 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000015145 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015146 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080015147#ifdef CONFIG_NET_POLL_CONTROLLER
15148 .ndo_poll_controller = tg3_poll_controller,
15149#endif
15150};
15151
Linus Torvalds1da177e2005-04-16 15:20:36 -070015152static int __devinit tg3_init_one(struct pci_dev *pdev,
15153 const struct pci_device_id *ent)
15154{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015155 struct net_device *dev;
15156 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015157 int i, err, pm_cap;
15158 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015159 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015160 u64 dma_mask, persist_dma_mask;
Matt Carlson0da06062011-05-19 12:12:53 +000015161 u32 features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015162
Joe Perches05dbe002010-02-17 19:44:19 +000015163 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015164
15165 err = pci_enable_device(pdev);
15166 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015167 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015168 return err;
15169 }
15170
Linus Torvalds1da177e2005-04-16 15:20:36 -070015171 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15172 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015173 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015174 goto err_out_disable_pdev;
15175 }
15176
15177 pci_set_master(pdev);
15178
15179 /* Find power-management capability. */
15180 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15181 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015182 dev_err(&pdev->dev,
15183 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015184 err = -EIO;
15185 goto err_out_free_res;
15186 }
15187
Matt Carlson16821282011-07-13 09:27:28 +000015188 err = pci_set_power_state(pdev, PCI_D0);
15189 if (err) {
15190 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15191 goto err_out_free_res;
15192 }
15193
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015194 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015195 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015196 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015197 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015198 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015199 }
15200
Linus Torvalds1da177e2005-04-16 15:20:36 -070015201 SET_NETDEV_DEV(dev, &pdev->dev);
15202
Linus Torvalds1da177e2005-04-16 15:20:36 -070015203 tp = netdev_priv(dev);
15204 tp->pdev = pdev;
15205 tp->dev = dev;
15206 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015207 tp->rx_mode = TG3_DEF_RX_MODE;
15208 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015209
Linus Torvalds1da177e2005-04-16 15:20:36 -070015210 if (tg3_debug > 0)
15211 tp->msg_enable = tg3_debug;
15212 else
15213 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15214
15215 /* The word/byte swap controls here control register access byte
15216 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15217 * setting below.
15218 */
15219 tp->misc_host_ctrl =
15220 MISC_HOST_CTRL_MASK_PCI_INT |
15221 MISC_HOST_CTRL_WORD_SWAP |
15222 MISC_HOST_CTRL_INDIR_ACCESS |
15223 MISC_HOST_CTRL_PCISTATE_RW;
15224
15225 /* The NONFRM (non-frame) byte/word swap controls take effect
15226 * on descriptor entries, anything which isn't packet data.
15227 *
15228 * The StrongARM chips on the board (one for tx, one for rx)
15229 * are running in big-endian mode.
15230 */
15231 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15232 GRC_MODE_WSWAP_NONFRM_DATA);
15233#ifdef __BIG_ENDIAN
15234 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15235#endif
15236 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015237 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015238 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015239
Matt Carlsond5fe4882008-11-21 17:20:32 -080015240 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015241 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015242 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015243 err = -ENOMEM;
15244 goto err_out_free_dev;
15245 }
15246
Matt Carlsonc9cab242011-07-13 09:27:27 +000015247 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15248 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15249 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15250 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15251 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15252 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15253 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15254 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15255 tg3_flag_set(tp, ENABLE_APE);
15256 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15257 if (!tp->aperegs) {
15258 dev_err(&pdev->dev,
15259 "Cannot map APE registers, aborting\n");
15260 err = -ENOMEM;
15261 goto err_out_iounmap;
15262 }
15263 }
15264
Linus Torvalds1da177e2005-04-16 15:20:36 -070015265 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15266 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015267
Linus Torvalds1da177e2005-04-16 15:20:36 -070015268 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015269 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015270 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015271 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015272
15273 err = tg3_get_invariants(tp);
15274 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015275 dev_err(&pdev->dev,
15276 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015277 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015278 }
15279
Michael Chan4a29cc22006-03-19 13:21:12 -080015280 /* The EPB bridge inside 5714, 5715, and 5780 and any
15281 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015282 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15283 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15284 * do DMA address check in tg3_start_xmit().
15285 */
Joe Perches63c3a662011-04-26 08:12:10 +000015286 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015287 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015288 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015289 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015290#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015291 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015292#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015293 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015294 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015295
15296 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015297 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015298 err = pci_set_dma_mask(pdev, dma_mask);
15299 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015300 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015301 err = pci_set_consistent_dma_mask(pdev,
15302 persist_dma_mask);
15303 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015304 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15305 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015306 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015307 }
15308 }
15309 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015310 if (err || dma_mask == DMA_BIT_MASK(32)) {
15311 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015312 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015313 dev_err(&pdev->dev,
15314 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015315 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015316 }
15317 }
15318
Michael Chanfdfec172005-07-25 12:31:48 -070015319 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015320
Matt Carlson0da06062011-05-19 12:12:53 +000015321 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15322
15323 /* 5700 B0 chips do not support checksumming correctly due
15324 * to hardware bugs.
15325 */
15326 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15327 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15328
15329 if (tg3_flag(tp, 5755_PLUS))
15330 features |= NETIF_F_IPV6_CSUM;
15331 }
15332
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015333 /* TSO is on by default on chips that support hardware TSO.
15334 * Firmware TSO on older chips gives lower performance, so it
15335 * is off by default, but can be enabled using ethtool.
15336 */
Joe Perches63c3a662011-04-26 08:12:10 +000015337 if ((tg3_flag(tp, HW_TSO_1) ||
15338 tg3_flag(tp, HW_TSO_2) ||
15339 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015340 (features & NETIF_F_IP_CSUM))
15341 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015342 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015343 if (features & NETIF_F_IPV6_CSUM)
15344 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015345 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015346 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015347 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15348 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015349 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015350 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015351 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015353
Matt Carlsond542fe22011-05-19 16:02:43 +000015354 dev->features |= features;
15355 dev->vlan_features |= features;
15356
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015357 /*
15358 * Add loopback capability only for a subset of devices that support
15359 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15360 * loopback for the remaining devices.
15361 */
15362 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15363 !tg3_flag(tp, CPMU_PRESENT))
15364 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015365 features |= NETIF_F_LOOPBACK;
15366
Matt Carlson0da06062011-05-19 12:12:53 +000015367 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015368
Linus Torvalds1da177e2005-04-16 15:20:36 -070015369 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015370 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015371 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015372 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015373 tp->rx_pending = 63;
15374 }
15375
Linus Torvalds1da177e2005-04-16 15:20:36 -070015376 err = tg3_get_device_address(tp);
15377 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015378 dev_err(&pdev->dev,
15379 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015380 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015381 }
15382
Matt Carlsonc88864d2007-11-12 21:07:01 -080015383 /*
15384 * Reset chip in case UNDI or EFI driver did not shutdown
15385 * DMA self test will enable WDMAC and we'll see (spurious)
15386 * pending DMA on the PCI bus at that point.
15387 */
15388 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15389 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15390 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15391 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15392 }
15393
15394 err = tg3_test_dma(tp);
15395 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015396 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015397 goto err_out_apeunmap;
15398 }
15399
Matt Carlson78f90dc2009-11-13 13:03:42 +000015400 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15401 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15402 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015403 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015404 struct tg3_napi *tnapi = &tp->napi[i];
15405
15406 tnapi->tp = tp;
15407 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15408
15409 tnapi->int_mbox = intmbx;
15410 if (i < 4)
15411 intmbx += 0x8;
15412 else
15413 intmbx += 0x4;
15414
15415 tnapi->consmbox = rcvmbx;
15416 tnapi->prodmbox = sndmbx;
15417
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015418 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015419 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015420 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015421 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015422
Joe Perches63c3a662011-04-26 08:12:10 +000015423 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015424 break;
15425
15426 /*
15427 * If we support MSIX, we'll be using RSS. If we're using
15428 * RSS, the first vector only handles link interrupts and the
15429 * remaining vectors handle rx and tx interrupts. Reuse the
15430 * mailbox values for the next iteration. The values we setup
15431 * above are still useful for the single vectored mode.
15432 */
15433 if (!i)
15434 continue;
15435
15436 rcvmbx += 0x8;
15437
15438 if (sndmbx & 0x4)
15439 sndmbx -= 0x4;
15440 else
15441 sndmbx += 0xc;
15442 }
15443
Matt Carlsonc88864d2007-11-12 21:07:01 -080015444 tg3_init_coal(tp);
15445
Michael Chanc49a1562006-12-17 17:07:29 -080015446 pci_set_drvdata(pdev, dev);
15447
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015448 if (tg3_flag(tp, 5717_PLUS)) {
15449 /* Resume a low-power mode */
15450 tg3_frob_aux_power(tp, false);
15451 }
15452
Linus Torvalds1da177e2005-04-16 15:20:36 -070015453 err = register_netdev(dev);
15454 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015455 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015456 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015457 }
15458
Joe Perches05dbe002010-02-17 19:44:19 +000015459 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15460 tp->board_part_number,
15461 tp->pci_chip_rev_id,
15462 tg3_bus_string(tp, str),
15463 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015464
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015465 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015466 struct phy_device *phydev;
15467 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015468 netdev_info(dev,
15469 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015470 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015471 } else {
15472 char *ethtype;
15473
15474 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15475 ethtype = "10/100Base-TX";
15476 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15477 ethtype = "1000Base-SX";
15478 else
15479 ethtype = "10/100/1000Base-T";
15480
Matt Carlson5129c3a2010-04-05 10:19:23 +000015481 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015482 "(WireSpeed[%d], EEE[%d])\n",
15483 tg3_phy_string(tp), ethtype,
15484 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15485 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015486 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015487
Joe Perches05dbe002010-02-17 19:44:19 +000015488 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015489 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015490 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015491 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015492 tg3_flag(tp, ENABLE_ASF) != 0,
15493 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015494 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15495 tp->dma_rwctrl,
15496 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15497 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015498
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015499 pci_save_state(pdev);
15500
Linus Torvalds1da177e2005-04-16 15:20:36 -070015501 return 0;
15502
Matt Carlson0d3031d2007-10-10 18:02:43 -070015503err_out_apeunmap:
15504 if (tp->aperegs) {
15505 iounmap(tp->aperegs);
15506 tp->aperegs = NULL;
15507 }
15508
Linus Torvalds1da177e2005-04-16 15:20:36 -070015509err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015510 if (tp->regs) {
15511 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015512 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015514
15515err_out_free_dev:
15516 free_netdev(dev);
15517
Matt Carlson16821282011-07-13 09:27:28 +000015518err_out_power_down:
15519 pci_set_power_state(pdev, PCI_D3hot);
15520
Linus Torvalds1da177e2005-04-16 15:20:36 -070015521err_out_free_res:
15522 pci_release_regions(pdev);
15523
15524err_out_disable_pdev:
15525 pci_disable_device(pdev);
15526 pci_set_drvdata(pdev, NULL);
15527 return err;
15528}
15529
15530static void __devexit tg3_remove_one(struct pci_dev *pdev)
15531{
15532 struct net_device *dev = pci_get_drvdata(pdev);
15533
15534 if (dev) {
15535 struct tg3 *tp = netdev_priv(dev);
15536
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015537 if (tp->fw)
15538 release_firmware(tp->fw);
15539
Tejun Heo23f333a2010-12-12 16:45:14 +010015540 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015541
Joe Perches63c3a662011-04-26 08:12:10 +000015542 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015543 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015544 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015545 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015546
Linus Torvalds1da177e2005-04-16 15:20:36 -070015547 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015548 if (tp->aperegs) {
15549 iounmap(tp->aperegs);
15550 tp->aperegs = NULL;
15551 }
Michael Chan68929142005-08-09 20:17:14 -070015552 if (tp->regs) {
15553 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015554 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015556 free_netdev(dev);
15557 pci_release_regions(pdev);
15558 pci_disable_device(pdev);
15559 pci_set_drvdata(pdev, NULL);
15560 }
15561}
15562
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015563#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015564static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015565{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015566 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015567 struct net_device *dev = pci_get_drvdata(pdev);
15568 struct tg3 *tp = netdev_priv(dev);
15569 int err;
15570
15571 if (!netif_running(dev))
15572 return 0;
15573
Tejun Heo23f333a2010-12-12 16:45:14 +010015574 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015575 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015576 tg3_netif_stop(tp);
15577
15578 del_timer_sync(&tp->timer);
15579
David S. Millerf47c11e2005-06-24 20:18:35 -070015580 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015581 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015582 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015583
15584 netif_device_detach(dev);
15585
David S. Millerf47c11e2005-06-24 20:18:35 -070015586 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015587 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015588 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015589 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015590
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015591 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015592 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015593 int err2;
15594
David S. Millerf47c11e2005-06-24 20:18:35 -070015595 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015596
Joe Perches63c3a662011-04-26 08:12:10 +000015597 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015598 err2 = tg3_restart_hw(tp, 1);
15599 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015600 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015601
15602 tp->timer.expires = jiffies + tp->timer_offset;
15603 add_timer(&tp->timer);
15604
15605 netif_device_attach(dev);
15606 tg3_netif_start(tp);
15607
Michael Chanb9ec6c12006-07-25 16:37:27 -070015608out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015609 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015610
15611 if (!err2)
15612 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015613 }
15614
15615 return err;
15616}
15617
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015618static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015619{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015620 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015621 struct net_device *dev = pci_get_drvdata(pdev);
15622 struct tg3 *tp = netdev_priv(dev);
15623 int err;
15624
15625 if (!netif_running(dev))
15626 return 0;
15627
Linus Torvalds1da177e2005-04-16 15:20:36 -070015628 netif_device_attach(dev);
15629
David S. Millerf47c11e2005-06-24 20:18:35 -070015630 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015631
Joe Perches63c3a662011-04-26 08:12:10 +000015632 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015633 err = tg3_restart_hw(tp, 1);
15634 if (err)
15635 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015636
15637 tp->timer.expires = jiffies + tp->timer_offset;
15638 add_timer(&tp->timer);
15639
Linus Torvalds1da177e2005-04-16 15:20:36 -070015640 tg3_netif_start(tp);
15641
Michael Chanb9ec6c12006-07-25 16:37:27 -070015642out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015643 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015644
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015645 if (!err)
15646 tg3_phy_start(tp);
15647
Michael Chanb9ec6c12006-07-25 16:37:27 -070015648 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015649}
15650
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015651static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015652#define TG3_PM_OPS (&tg3_pm_ops)
15653
15654#else
15655
15656#define TG3_PM_OPS NULL
15657
15658#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015659
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015660/**
15661 * tg3_io_error_detected - called when PCI error is detected
15662 * @pdev: Pointer to PCI device
15663 * @state: The current pci connection state
15664 *
15665 * This function is called after a PCI bus error affecting
15666 * this device has been detected.
15667 */
15668static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15669 pci_channel_state_t state)
15670{
15671 struct net_device *netdev = pci_get_drvdata(pdev);
15672 struct tg3 *tp = netdev_priv(netdev);
15673 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15674
15675 netdev_info(netdev, "PCI I/O error detected\n");
15676
15677 rtnl_lock();
15678
15679 if (!netif_running(netdev))
15680 goto done;
15681
15682 tg3_phy_stop(tp);
15683
15684 tg3_netif_stop(tp);
15685
15686 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015687 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015688
15689 /* Want to make sure that the reset task doesn't run */
15690 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015691 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15692 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015693
15694 netif_device_detach(netdev);
15695
15696 /* Clean up software state, even if MMIO is blocked */
15697 tg3_full_lock(tp, 0);
15698 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15699 tg3_full_unlock(tp);
15700
15701done:
15702 if (state == pci_channel_io_perm_failure)
15703 err = PCI_ERS_RESULT_DISCONNECT;
15704 else
15705 pci_disable_device(pdev);
15706
15707 rtnl_unlock();
15708
15709 return err;
15710}
15711
15712/**
15713 * tg3_io_slot_reset - called after the pci bus has been reset.
15714 * @pdev: Pointer to PCI device
15715 *
15716 * Restart the card from scratch, as if from a cold-boot.
15717 * At this point, the card has exprienced a hard reset,
15718 * followed by fixups by BIOS, and has its config space
15719 * set up identically to what it was at cold boot.
15720 */
15721static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15722{
15723 struct net_device *netdev = pci_get_drvdata(pdev);
15724 struct tg3 *tp = netdev_priv(netdev);
15725 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15726 int err;
15727
15728 rtnl_lock();
15729
15730 if (pci_enable_device(pdev)) {
15731 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15732 goto done;
15733 }
15734
15735 pci_set_master(pdev);
15736 pci_restore_state(pdev);
15737 pci_save_state(pdev);
15738
15739 if (!netif_running(netdev)) {
15740 rc = PCI_ERS_RESULT_RECOVERED;
15741 goto done;
15742 }
15743
15744 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000015745 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015746 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015747
15748 rc = PCI_ERS_RESULT_RECOVERED;
15749
15750done:
15751 rtnl_unlock();
15752
15753 return rc;
15754}
15755
15756/**
15757 * tg3_io_resume - called when traffic can start flowing again.
15758 * @pdev: Pointer to PCI device
15759 *
15760 * This callback is called when the error recovery driver tells
15761 * us that its OK to resume normal operation.
15762 */
15763static void tg3_io_resume(struct pci_dev *pdev)
15764{
15765 struct net_device *netdev = pci_get_drvdata(pdev);
15766 struct tg3 *tp = netdev_priv(netdev);
15767 int err;
15768
15769 rtnl_lock();
15770
15771 if (!netif_running(netdev))
15772 goto done;
15773
15774 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015775 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015776 err = tg3_restart_hw(tp, 1);
15777 tg3_full_unlock(tp);
15778 if (err) {
15779 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15780 goto done;
15781 }
15782
15783 netif_device_attach(netdev);
15784
15785 tp->timer.expires = jiffies + tp->timer_offset;
15786 add_timer(&tp->timer);
15787
15788 tg3_netif_start(tp);
15789
15790 tg3_phy_start(tp);
15791
15792done:
15793 rtnl_unlock();
15794}
15795
15796static struct pci_error_handlers tg3_err_handler = {
15797 .error_detected = tg3_io_error_detected,
15798 .slot_reset = tg3_io_slot_reset,
15799 .resume = tg3_io_resume
15800};
15801
Linus Torvalds1da177e2005-04-16 15:20:36 -070015802static struct pci_driver tg3_driver = {
15803 .name = DRV_MODULE_NAME,
15804 .id_table = tg3_pci_tbl,
15805 .probe = tg3_init_one,
15806 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015807 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015808 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015809};
15810
15811static int __init tg3_init(void)
15812{
Jeff Garzik29917622006-08-19 17:48:59 -040015813 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015814}
15815
15816static void __exit tg3_cleanup(void)
15817{
15818 pci_unregister_driver(&tg3_driver);
15819}
15820
15821module_init(tg3_init);
15822module_exit(tg3_cleanup);