blob: 4137e4e027a3440bd0b0aebcd049c3feaa4aa195 [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) {
1861 val = tr32(TG3_CPMU_EEE_MODE);
1862 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1863 }
1864}
1865
Matt Carlsonb0c59432011-05-19 12:12:48 +00001866static void tg3_phy_eee_enable(struct tg3 *tp)
1867{
1868 u32 val;
1869
1870 if (tp->link_config.active_speed == SPEED_1000 &&
1871 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1872 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
1873 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
1874 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
1875 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0003);
1876 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1877 }
1878
1879 val = tr32(TG3_CPMU_EEE_MODE);
1880 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
1881}
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883static int tg3_wait_macro_done(struct tg3 *tp)
1884{
1885 int limit = 100;
1886
1887 while (limit--) {
1888 u32 tmp32;
1889
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001890 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if ((tmp32 & 0x1000) == 0)
1892 break;
1893 }
1894 }
Roel Kluind4675b52009-02-12 16:33:27 -08001895 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return -EBUSY;
1897
1898 return 0;
1899}
1900
1901static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1902{
1903 static const u32 test_pat[4][6] = {
1904 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1905 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1906 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1907 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1908 };
1909 int chan;
1910
1911 for (chan = 0; chan < 4; chan++) {
1912 int i;
1913
1914 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1915 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001916 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 for (i = 0; i < 6; i++)
1919 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1920 test_pat[chan][i]);
1921
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001922 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (tg3_wait_macro_done(tp)) {
1924 *resetp = 1;
1925 return -EBUSY;
1926 }
1927
1928 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1929 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001930 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (tg3_wait_macro_done(tp)) {
1932 *resetp = 1;
1933 return -EBUSY;
1934 }
1935
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001936 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if (tg3_wait_macro_done(tp)) {
1938 *resetp = 1;
1939 return -EBUSY;
1940 }
1941
1942 for (i = 0; i < 6; i += 2) {
1943 u32 low, high;
1944
1945 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1946 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1947 tg3_wait_macro_done(tp)) {
1948 *resetp = 1;
1949 return -EBUSY;
1950 }
1951 low &= 0x7fff;
1952 high &= 0x000f;
1953 if (low != test_pat[chan][i] ||
1954 high != test_pat[chan][i+1]) {
1955 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1956 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1957 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1958
1959 return -EBUSY;
1960 }
1961 }
1962 }
1963
1964 return 0;
1965}
1966
1967static int tg3_phy_reset_chanpat(struct tg3 *tp)
1968{
1969 int chan;
1970
1971 for (chan = 0; chan < 4; chan++) {
1972 int i;
1973
1974 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1975 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001976 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 for (i = 0; i < 6; i++)
1978 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001979 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (tg3_wait_macro_done(tp))
1981 return -EBUSY;
1982 }
1983
1984 return 0;
1985}
1986
1987static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1988{
1989 u32 reg32, phy9_orig;
1990 int retries, do_phy_reset, err;
1991
1992 retries = 10;
1993 do_phy_reset = 1;
1994 do {
1995 if (do_phy_reset) {
1996 err = tg3_bmcr_reset(tp);
1997 if (err)
1998 return err;
1999 do_phy_reset = 0;
2000 }
2001
2002 /* Disable transmitter and interrupt. */
2003 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2004 continue;
2005
2006 reg32 |= 0x3000;
2007 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2008
2009 /* Set full-duplex, 1000 mbps. */
2010 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002011 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002014 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 continue;
2016
Matt Carlson221c5632011-06-13 13:39:01 +00002017 tg3_writephy(tp, MII_CTRL1000,
2018 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Matt Carlson1d36ba42011-04-20 07:57:42 +00002020 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2021 if (err)
2022 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002025 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2028 if (!err)
2029 break;
2030 } while (--retries);
2031
2032 err = tg3_phy_reset_chanpat(tp);
2033 if (err)
2034 return err;
2035
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002036 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002039 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Matt Carlson1d36ba42011-04-20 07:57:42 +00002041 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Matt Carlson221c5632011-06-13 13:39:01 +00002043 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2046 reg32 &= ~0x3000;
2047 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2048 } else if (!err)
2049 err = -EBUSY;
2050
2051 return err;
2052}
2053
2054/* This will reset the tigon3 PHY if there is no valid
2055 * link unless the FORCE argument is non-zero.
2056 */
2057static int tg3_phy_reset(struct tg3 *tp)
2058{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002059 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 int err;
2061
Michael Chan60189dd2006-12-17 17:08:07 -08002062 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002063 val = tr32(GRC_MISC_CFG);
2064 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2065 udelay(40);
2066 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002067 err = tg3_readphy(tp, MII_BMSR, &val);
2068 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (err != 0)
2070 return -EBUSY;
2071
Michael Chanc8e1e822006-04-29 18:55:17 -07002072 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2073 netif_carrier_off(tp->dev);
2074 tg3_link_report(tp);
2075 }
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2078 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2079 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2080 err = tg3_phy_reset_5703_4_5(tp);
2081 if (err)
2082 return err;
2083 goto out;
2084 }
2085
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002086 cpmuctrl = 0;
2087 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2088 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2089 cpmuctrl = tr32(TG3_CPMU_CTRL);
2090 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2091 tw32(TG3_CPMU_CTRL,
2092 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2093 }
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 err = tg3_bmcr_reset(tp);
2096 if (err)
2097 return err;
2098
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002099 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002100 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2101 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002102
2103 tw32(TG3_CPMU_CTRL, cpmuctrl);
2104 }
2105
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002106 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2107 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002108 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2109 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2110 CPMU_LSPD_1000MB_MACCLK_12_5) {
2111 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2112 udelay(40);
2113 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2114 }
2115 }
2116
Joe Perches63c3a662011-04-26 08:12:10 +00002117 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002118 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002119 return 0;
2120
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002121 tg3_phy_apply_otp(tp);
2122
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002123 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002124 tg3_phy_toggle_apd(tp, true);
2125 else
2126 tg3_phy_toggle_apd(tp, false);
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002129 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2130 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002131 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2132 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002133 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002135
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002136 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002137 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2138 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002140
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002141 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002142 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2143 tg3_phydsp_write(tp, 0x000a, 0x310b);
2144 tg3_phydsp_write(tp, 0x201f, 0x9506);
2145 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2146 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2147 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002148 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002149 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2150 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2151 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2152 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2153 tg3_writephy(tp, MII_TG3_TEST1,
2154 MII_TG3_TEST1_TRIM_EN | 0x4);
2155 } else
2156 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2157
2158 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2159 }
Michael Chanc424cb22006-04-29 18:56:34 -07002160 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 /* Set Extended packet length bit (bit 14) on all chips that */
2163 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002164 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002166 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002167 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002169 err = tg3_phy_auxctl_read(tp,
2170 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2171 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002172 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2173 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
2175
2176 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2177 * jumbo frames transmission.
2178 */
Joe Perches63c3a662011-04-26 08:12:10 +00002179 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002180 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002181 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002182 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
2184
Michael Chan715116a2006-09-27 16:09:25 -07002185 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002186 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002187 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002188 }
2189
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002190 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 tg3_phy_set_wirespeed(tp);
2192 return 0;
2193}
2194
Matt Carlson520b2752011-06-13 13:39:02 +00002195static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2196{
2197 if (!tg3_flag(tp, IS_NIC))
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002198 return;
2199
2200 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
Matt Carlson520b2752011-06-13 13:39:02 +00002201 return 0;
2202
2203 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2204 TG3_GRC_LCLCTL_PWRSW_DELAY);
2205
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002206 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2207
Matt Carlson520b2752011-06-13 13:39:02 +00002208 return 0;
2209}
2210
2211static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2212{
2213 u32 grc_local_ctrl;
2214
2215 if (!tg3_flag(tp, IS_NIC) ||
2216 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2217 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2218 return;
2219
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002220
2221 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2222 return;
2223
Matt Carlson520b2752011-06-13 13:39:02 +00002224 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2225
2226 tw32_wait_f(GRC_LOCAL_CTRL,
2227 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2228 TG3_GRC_LCLCTL_PWRSW_DELAY);
2229
2230 tw32_wait_f(GRC_LOCAL_CTRL,
2231 grc_local_ctrl,
2232 TG3_GRC_LCLCTL_PWRSW_DELAY);
2233
2234 tw32_wait_f(GRC_LOCAL_CTRL,
2235 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2236 TG3_GRC_LCLCTL_PWRSW_DELAY);
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002237
2238 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002239}
2240
2241static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2242{
2243 if (!tg3_flag(tp, IS_NIC))
2244 return;
2245
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002246 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2247 return;
2248
Matt Carlson520b2752011-06-13 13:39:02 +00002249 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2250 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2251 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2252 (GRC_LCLCTRL_GPIO_OE0 |
2253 GRC_LCLCTRL_GPIO_OE1 |
2254 GRC_LCLCTRL_GPIO_OE2 |
2255 GRC_LCLCTRL_GPIO_OUTPUT0 |
2256 GRC_LCLCTRL_GPIO_OUTPUT1),
2257 TG3_GRC_LCLCTL_PWRSW_DELAY);
2258 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2259 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2260 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2261 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2262 GRC_LCLCTRL_GPIO_OE1 |
2263 GRC_LCLCTRL_GPIO_OE2 |
2264 GRC_LCLCTRL_GPIO_OUTPUT0 |
2265 GRC_LCLCTRL_GPIO_OUTPUT1 |
2266 tp->grc_local_ctrl;
2267 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2268 TG3_GRC_LCLCTL_PWRSW_DELAY);
2269
2270 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2271 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2272 TG3_GRC_LCLCTL_PWRSW_DELAY);
2273
2274 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2275 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2276 TG3_GRC_LCLCTL_PWRSW_DELAY);
2277 } else {
2278 u32 no_gpio2;
2279 u32 grc_local_ctrl = 0;
2280
2281 /* Workaround to prevent overdrawing Amps. */
2282 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2283 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2284 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2285 grc_local_ctrl,
2286 TG3_GRC_LCLCTL_PWRSW_DELAY);
2287 }
2288
2289 /* On 5753 and variants, GPIO2 cannot be used. */
2290 no_gpio2 = tp->nic_sram_data_cfg &
2291 NIC_SRAM_DATA_CFG_NO_GPIO2;
2292
2293 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2294 GRC_LCLCTRL_GPIO_OE1 |
2295 GRC_LCLCTRL_GPIO_OE2 |
2296 GRC_LCLCTRL_GPIO_OUTPUT1 |
2297 GRC_LCLCTRL_GPIO_OUTPUT2;
2298 if (no_gpio2) {
2299 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2300 GRC_LCLCTRL_GPIO_OUTPUT2);
2301 }
2302 tw32_wait_f(GRC_LOCAL_CTRL,
2303 tp->grc_local_ctrl | grc_local_ctrl,
2304 TG3_GRC_LCLCTL_PWRSW_DELAY);
2305
2306 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2307
2308 tw32_wait_f(GRC_LOCAL_CTRL,
2309 tp->grc_local_ctrl | grc_local_ctrl,
2310 TG3_GRC_LCLCTL_PWRSW_DELAY);
2311
2312 if (!no_gpio2) {
2313 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2314 tw32_wait_f(GRC_LOCAL_CTRL,
2315 tp->grc_local_ctrl | grc_local_ctrl,
2316 TG3_GRC_LCLCTL_PWRSW_DELAY);
2317 }
2318 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002319
2320 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002321}
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323static void tg3_frob_aux_power(struct tg3 *tp)
2324{
Matt Carlson683644b2011-03-09 16:58:23 +00002325 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Matt Carlson334355a2010-01-20 16:58:10 +00002327 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002328 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002329 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002330 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return;
2332
Matt Carlson683644b2011-03-09 16:58:23 +00002333 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2334 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +00002335 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2336 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
Matt Carlson683644b2011-03-09 16:58:23 +00002337 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002338 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002340 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002341
Michael Chanbc1c7562006-03-20 17:48:03 -08002342 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002343 if (dev_peer) {
2344 struct tg3 *tp_peer = netdev_priv(dev_peer);
2345
Joe Perches63c3a662011-04-26 08:12:10 +00002346 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002347 return;
2348
Joe Perches63c3a662011-04-26 08:12:10 +00002349 if (tg3_flag(tp_peer, WOL_ENABLE) ||
2350 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002351 need_vaux = true;
2352 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Joe Perches63c3a662011-04-26 08:12:10 +00002355 if (tg3_flag(tp, WOL_ENABLE) || tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002356 need_vaux = true;
2357
Matt Carlson520b2752011-06-13 13:39:02 +00002358 if (need_vaux)
2359 tg3_pwrsrc_switch_to_vaux(tp);
2360 else
2361 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362}
2363
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002364static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2365{
2366 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2367 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002368 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002369 if (speed != SPEED_10)
2370 return 1;
2371 } else if (speed == SPEED_10)
2372 return 1;
2373
2374 return 0;
2375}
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377static int tg3_setup_phy(struct tg3 *, int);
2378
2379#define RESET_KIND_SHUTDOWN 0
2380#define RESET_KIND_INIT 1
2381#define RESET_KIND_SUSPEND 2
2382
2383static void tg3_write_sig_post_reset(struct tg3 *, int);
2384static int tg3_halt_cpu(struct tg3 *, u32);
2385
Matt Carlson0a459aa2008-11-03 16:54:15 -08002386static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002387{
Matt Carlsonce057f02007-11-12 21:08:03 -08002388 u32 val;
2389
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002390 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002391 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2392 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2393 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2394
2395 sg_dig_ctrl |=
2396 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2397 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2398 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2399 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002400 return;
Michael Chan51297242007-02-13 12:17:57 -08002401 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002402
Michael Chan60189dd2006-12-17 17:08:07 -08002403 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002404 tg3_bmcr_reset(tp);
2405 val = tr32(GRC_MISC_CFG);
2406 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2407 udelay(40);
2408 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002409 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002410 u32 phytest;
2411 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2412 u32 phy;
2413
2414 tg3_writephy(tp, MII_ADVERTISE, 0);
2415 tg3_writephy(tp, MII_BMCR,
2416 BMCR_ANENABLE | BMCR_ANRESTART);
2417
2418 tg3_writephy(tp, MII_TG3_FET_TEST,
2419 phytest | MII_TG3_FET_SHADOW_EN);
2420 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2421 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2422 tg3_writephy(tp,
2423 MII_TG3_FET_SHDW_AUXMODE4,
2424 phy);
2425 }
2426 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2427 }
2428 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002429 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002430 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2431 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002432
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002433 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2434 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2435 MII_TG3_AUXCTL_PCTL_VREG_11V;
2436 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002437 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002438
Michael Chan15c3b692006-03-22 01:06:52 -08002439 /* The PHY should not be powered down on some chips because
2440 * of bugs.
2441 */
2442 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2443 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2444 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002445 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002446 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002447
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002448 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2449 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002450 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2451 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2452 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2453 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2454 }
2455
Michael Chan15c3b692006-03-22 01:06:52 -08002456 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2457}
2458
Matt Carlson3f007892008-11-03 16:51:36 -08002459/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002460static int tg3_nvram_lock(struct tg3 *tp)
2461{
Joe Perches63c3a662011-04-26 08:12:10 +00002462 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002463 int i;
2464
2465 if (tp->nvram_lock_cnt == 0) {
2466 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2467 for (i = 0; i < 8000; i++) {
2468 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2469 break;
2470 udelay(20);
2471 }
2472 if (i == 8000) {
2473 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2474 return -ENODEV;
2475 }
2476 }
2477 tp->nvram_lock_cnt++;
2478 }
2479 return 0;
2480}
2481
2482/* tp->lock is held. */
2483static void tg3_nvram_unlock(struct tg3 *tp)
2484{
Joe Perches63c3a662011-04-26 08:12:10 +00002485 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002486 if (tp->nvram_lock_cnt > 0)
2487 tp->nvram_lock_cnt--;
2488 if (tp->nvram_lock_cnt == 0)
2489 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2490 }
2491}
2492
2493/* tp->lock is held. */
2494static void tg3_enable_nvram_access(struct tg3 *tp)
2495{
Joe Perches63c3a662011-04-26 08:12:10 +00002496 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002497 u32 nvaccess = tr32(NVRAM_ACCESS);
2498
2499 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2500 }
2501}
2502
2503/* tp->lock is held. */
2504static void tg3_disable_nvram_access(struct tg3 *tp)
2505{
Joe Perches63c3a662011-04-26 08:12:10 +00002506 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002507 u32 nvaccess = tr32(NVRAM_ACCESS);
2508
2509 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2510 }
2511}
2512
2513static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2514 u32 offset, u32 *val)
2515{
2516 u32 tmp;
2517 int i;
2518
2519 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2520 return -EINVAL;
2521
2522 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2523 EEPROM_ADDR_DEVID_MASK |
2524 EEPROM_ADDR_READ);
2525 tw32(GRC_EEPROM_ADDR,
2526 tmp |
2527 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2528 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2529 EEPROM_ADDR_ADDR_MASK) |
2530 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2531
2532 for (i = 0; i < 1000; i++) {
2533 tmp = tr32(GRC_EEPROM_ADDR);
2534
2535 if (tmp & EEPROM_ADDR_COMPLETE)
2536 break;
2537 msleep(1);
2538 }
2539 if (!(tmp & EEPROM_ADDR_COMPLETE))
2540 return -EBUSY;
2541
Matt Carlson62cedd12009-04-20 14:52:29 -07002542 tmp = tr32(GRC_EEPROM_DATA);
2543
2544 /*
2545 * The data will always be opposite the native endian
2546 * format. Perform a blind byteswap to compensate.
2547 */
2548 *val = swab32(tmp);
2549
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002550 return 0;
2551}
2552
2553#define NVRAM_CMD_TIMEOUT 10000
2554
2555static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2556{
2557 int i;
2558
2559 tw32(NVRAM_CMD, nvram_cmd);
2560 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2561 udelay(10);
2562 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2563 udelay(10);
2564 break;
2565 }
2566 }
2567
2568 if (i == NVRAM_CMD_TIMEOUT)
2569 return -EBUSY;
2570
2571 return 0;
2572}
2573
2574static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2575{
Joe Perches63c3a662011-04-26 08:12:10 +00002576 if (tg3_flag(tp, NVRAM) &&
2577 tg3_flag(tp, NVRAM_BUFFERED) &&
2578 tg3_flag(tp, FLASH) &&
2579 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002580 (tp->nvram_jedecnum == JEDEC_ATMEL))
2581
2582 addr = ((addr / tp->nvram_pagesize) <<
2583 ATMEL_AT45DB0X1B_PAGE_POS) +
2584 (addr % tp->nvram_pagesize);
2585
2586 return addr;
2587}
2588
2589static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2590{
Joe Perches63c3a662011-04-26 08:12:10 +00002591 if (tg3_flag(tp, NVRAM) &&
2592 tg3_flag(tp, NVRAM_BUFFERED) &&
2593 tg3_flag(tp, FLASH) &&
2594 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002595 (tp->nvram_jedecnum == JEDEC_ATMEL))
2596
2597 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2598 tp->nvram_pagesize) +
2599 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2600
2601 return addr;
2602}
2603
Matt Carlsone4f34112009-02-25 14:25:00 +00002604/* NOTE: Data read in from NVRAM is byteswapped according to
2605 * the byteswapping settings for all other register accesses.
2606 * tg3 devices are BE devices, so on a BE machine, the data
2607 * returned will be exactly as it is seen in NVRAM. On a LE
2608 * machine, the 32-bit value will be byteswapped.
2609 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002610static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2611{
2612 int ret;
2613
Joe Perches63c3a662011-04-26 08:12:10 +00002614 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002615 return tg3_nvram_read_using_eeprom(tp, offset, val);
2616
2617 offset = tg3_nvram_phys_addr(tp, offset);
2618
2619 if (offset > NVRAM_ADDR_MSK)
2620 return -EINVAL;
2621
2622 ret = tg3_nvram_lock(tp);
2623 if (ret)
2624 return ret;
2625
2626 tg3_enable_nvram_access(tp);
2627
2628 tw32(NVRAM_ADDR, offset);
2629 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2630 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2631
2632 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002633 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002634
2635 tg3_disable_nvram_access(tp);
2636
2637 tg3_nvram_unlock(tp);
2638
2639 return ret;
2640}
2641
Matt Carlsona9dc5292009-02-25 14:25:30 +00002642/* Ensures NVRAM data is in bytestream format. */
2643static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002644{
2645 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002646 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002647 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002648 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002649 return res;
2650}
2651
2652/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002653static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2654{
2655 u32 addr_high, addr_low;
2656 int i;
2657
2658 addr_high = ((tp->dev->dev_addr[0] << 8) |
2659 tp->dev->dev_addr[1]);
2660 addr_low = ((tp->dev->dev_addr[2] << 24) |
2661 (tp->dev->dev_addr[3] << 16) |
2662 (tp->dev->dev_addr[4] << 8) |
2663 (tp->dev->dev_addr[5] << 0));
2664 for (i = 0; i < 4; i++) {
2665 if (i == 1 && skip_mac_1)
2666 continue;
2667 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2668 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2669 }
2670
2671 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2672 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2673 for (i = 0; i < 12; i++) {
2674 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2675 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2676 }
2677 }
2678
2679 addr_high = (tp->dev->dev_addr[0] +
2680 tp->dev->dev_addr[1] +
2681 tp->dev->dev_addr[2] +
2682 tp->dev->dev_addr[3] +
2683 tp->dev->dev_addr[4] +
2684 tp->dev->dev_addr[5]) &
2685 TX_BACKOFF_SEED_MASK;
2686 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2687}
2688
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002689static void tg3_enable_register_access(struct tg3 *tp)
2690{
2691 /*
2692 * Make sure register accesses (indirect or otherwise) will function
2693 * correctly.
2694 */
2695 pci_write_config_dword(tp->pdev,
2696 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2697}
2698
2699static int tg3_power_up(struct tg3 *tp)
2700{
Matt Carlsonbed98292011-07-13 09:27:29 +00002701 int err;
2702
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002703 tg3_enable_register_access(tp);
2704
Matt Carlsonbed98292011-07-13 09:27:29 +00002705 err = pci_set_power_state(tp->pdev, PCI_D0);
2706 if (!err) {
2707 /* Switch out of Vaux if it is a NIC */
2708 tg3_pwrsrc_switch_to_vmain(tp);
2709 } else {
2710 netdev_err(tp->dev, "Transition to D0 failed\n");
2711 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002712
Matt Carlsonbed98292011-07-13 09:27:29 +00002713 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002714}
2715
2716static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002719 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002721 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002722
2723 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00002724 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002725 u16 lnkctl;
2726
2727 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00002728 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002729 &lnkctl);
2730 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2731 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00002732 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002733 lnkctl);
2734 }
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2737 tw32(TG3PCI_MISC_HOST_CTRL,
2738 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2739
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002740 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00002741 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002742
Joe Perches63c3a662011-04-26 08:12:10 +00002743 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002744 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002745 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002746 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002747 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002748 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002749
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002750 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002751
Matt Carlson80096062010-08-02 11:26:06 +00002752 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002753
2754 tp->link_config.orig_speed = phydev->speed;
2755 tp->link_config.orig_duplex = phydev->duplex;
2756 tp->link_config.orig_autoneg = phydev->autoneg;
2757 tp->link_config.orig_advertising = phydev->advertising;
2758
2759 advertising = ADVERTISED_TP |
2760 ADVERTISED_Pause |
2761 ADVERTISED_Autoneg |
2762 ADVERTISED_10baseT_Half;
2763
Joe Perches63c3a662011-04-26 08:12:10 +00002764 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
2765 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002766 advertising |=
2767 ADVERTISED_100baseT_Half |
2768 ADVERTISED_100baseT_Full |
2769 ADVERTISED_10baseT_Full;
2770 else
2771 advertising |= ADVERTISED_10baseT_Full;
2772 }
2773
2774 phydev->advertising = advertising;
2775
2776 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002777
2778 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002779 if (phyid != PHY_ID_BCMAC131) {
2780 phyid &= PHY_BCM_OUI_MASK;
2781 if (phyid == PHY_BCM_OUI_1 ||
2782 phyid == PHY_BCM_OUI_2 ||
2783 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002784 do_low_power = true;
2785 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002786 }
Matt Carlsondd477002008-05-25 23:45:58 -07002787 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002788 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002789
Matt Carlson80096062010-08-02 11:26:06 +00002790 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2791 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002792 tp->link_config.orig_speed = tp->link_config.speed;
2793 tp->link_config.orig_duplex = tp->link_config.duplex;
2794 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002797 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002798 tp->link_config.speed = SPEED_10;
2799 tp->link_config.duplex = DUPLEX_HALF;
2800 tp->link_config.autoneg = AUTONEG_ENABLE;
2801 tg3_setup_phy(tp, 0);
2802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
2804
Michael Chanb5d37722006-09-27 16:06:21 -07002805 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2806 u32 val;
2807
2808 val = tr32(GRC_VCPU_EXT_CTRL);
2809 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00002810 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002811 int i;
2812 u32 val;
2813
2814 for (i = 0; i < 200; i++) {
2815 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2816 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2817 break;
2818 msleep(1);
2819 }
2820 }
Joe Perches63c3a662011-04-26 08:12:10 +00002821 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07002822 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2823 WOL_DRV_STATE_SHUTDOWN |
2824 WOL_DRV_WOL |
2825 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002826
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002827 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 u32 mac_mode;
2829
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002830 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002831 if (do_low_power &&
2832 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
2833 tg3_phy_auxctl_write(tp,
2834 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
2835 MII_TG3_AUXCTL_PCTL_WOL_EN |
2836 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2837 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07002838 udelay(40);
2839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002841 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002842 mac_mode = MAC_MODE_PORT_MODE_GMII;
2843 else
2844 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002846 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2847 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2848 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00002849 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002850 SPEED_100 : SPEED_10;
2851 if (tg3_5700_link_polarity(tp, speed))
2852 mac_mode |= MAC_MODE_LINK_POLARITY;
2853 else
2854 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 } else {
2857 mac_mode = MAC_MODE_PORT_MODE_TBI;
2858 }
2859
Joe Perches63c3a662011-04-26 08:12:10 +00002860 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 tw32(MAC_LED_CTRL, tp->led_ctrl);
2862
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002863 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00002864 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
2865 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002866 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Joe Perches63c3a662011-04-26 08:12:10 +00002868 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002869 mac_mode |= MAC_MODE_APE_TX_EN |
2870 MAC_MODE_APE_RX_EN |
2871 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 tw32_f(MAC_MODE, mac_mode);
2874 udelay(100);
2875
2876 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2877 udelay(10);
2878 }
2879
Joe Perches63c3a662011-04-26 08:12:10 +00002880 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2882 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2883 u32 base_val;
2884
2885 base_val = tp->pci_clock_ctrl;
2886 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2887 CLOCK_CTRL_TXCLK_DISABLE);
2888
Michael Chanb401e9e2005-12-19 16:27:04 -08002889 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2890 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00002891 } else if (tg3_flag(tp, 5780_CLASS) ||
2892 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00002893 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002894 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00002895 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 u32 newbits1, newbits2;
2897
2898 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2899 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2900 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2901 CLOCK_CTRL_TXCLK_DISABLE |
2902 CLOCK_CTRL_ALTCLK);
2903 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00002904 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 newbits1 = CLOCK_CTRL_625_CORE;
2906 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2907 } else {
2908 newbits1 = CLOCK_CTRL_ALTCLK;
2909 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2910 }
2911
Michael Chanb401e9e2005-12-19 16:27:04 -08002912 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2913 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Michael Chanb401e9e2005-12-19 16:27:04 -08002915 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2916 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Joe Perches63c3a662011-04-26 08:12:10 +00002918 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 u32 newbits3;
2920
2921 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2922 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2923 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2924 CLOCK_CTRL_TXCLK_DISABLE |
2925 CLOCK_CTRL_44MHZ_CORE);
2926 } else {
2927 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2928 }
2929
Michael Chanb401e9e2005-12-19 16:27:04 -08002930 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2931 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 }
2933 }
2934
Joe Perches63c3a662011-04-26 08:12:10 +00002935 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002936 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 tg3_frob_aux_power(tp);
2939
2940 /* Workaround for unstable PLL clock */
2941 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2942 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2943 u32 val = tr32(0x7d00);
2944
2945 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2946 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00002947 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002948 int err;
2949
2950 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002952 if (!err)
2953 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
2956
Michael Chanbbadf502006-04-06 21:46:34 -07002957 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 return 0;
2960}
2961
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002962static void tg3_power_down(struct tg3 *tp)
2963{
2964 tg3_power_down_prepare(tp);
2965
Joe Perches63c3a662011-04-26 08:12:10 +00002966 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002967 pci_set_power_state(tp->pdev, PCI_D3hot);
2968}
2969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2971{
2972 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2973 case MII_TG3_AUX_STAT_10HALF:
2974 *speed = SPEED_10;
2975 *duplex = DUPLEX_HALF;
2976 break;
2977
2978 case MII_TG3_AUX_STAT_10FULL:
2979 *speed = SPEED_10;
2980 *duplex = DUPLEX_FULL;
2981 break;
2982
2983 case MII_TG3_AUX_STAT_100HALF:
2984 *speed = SPEED_100;
2985 *duplex = DUPLEX_HALF;
2986 break;
2987
2988 case MII_TG3_AUX_STAT_100FULL:
2989 *speed = SPEED_100;
2990 *duplex = DUPLEX_FULL;
2991 break;
2992
2993 case MII_TG3_AUX_STAT_1000HALF:
2994 *speed = SPEED_1000;
2995 *duplex = DUPLEX_HALF;
2996 break;
2997
2998 case MII_TG3_AUX_STAT_1000FULL:
2999 *speed = SPEED_1000;
3000 *duplex = DUPLEX_FULL;
3001 break;
3002
3003 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003004 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003005 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3006 SPEED_10;
3007 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3008 DUPLEX_HALF;
3009 break;
3010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 *speed = SPEED_INVALID;
3012 *duplex = DUPLEX_INVALID;
3013 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
Matt Carlson42b64a42011-05-19 12:12:49 +00003017static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
Matt Carlson42b64a42011-05-19 12:12:49 +00003019 int err = 0;
3020 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Matt Carlson42b64a42011-05-19 12:12:49 +00003022 new_adv = ADVERTISE_CSMA;
3023 if (advertise & ADVERTISED_10baseT_Half)
3024 new_adv |= ADVERTISE_10HALF;
3025 if (advertise & ADVERTISED_10baseT_Full)
3026 new_adv |= ADVERTISE_10FULL;
3027 if (advertise & ADVERTISED_100baseT_Half)
3028 new_adv |= ADVERTISE_100HALF;
3029 if (advertise & ADVERTISED_100baseT_Full)
3030 new_adv |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Matt Carlson42b64a42011-05-19 12:12:49 +00003032 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Matt Carlson42b64a42011-05-19 12:12:49 +00003034 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3035 if (err)
3036 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Matt Carlson42b64a42011-05-19 12:12:49 +00003038 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3039 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003040
Matt Carlson42b64a42011-05-19 12:12:49 +00003041 new_adv = 0;
3042 if (advertise & ADVERTISED_1000baseT_Half)
Matt Carlson221c5632011-06-13 13:39:01 +00003043 new_adv |= ADVERTISE_1000HALF;
Matt Carlson42b64a42011-05-19 12:12:49 +00003044 if (advertise & ADVERTISED_1000baseT_Full)
Matt Carlson221c5632011-06-13 13:39:01 +00003045 new_adv |= ADVERTISE_1000FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003046
Matt Carlson42b64a42011-05-19 12:12:49 +00003047 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3048 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
Matt Carlson221c5632011-06-13 13:39:01 +00003049 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Matt Carlson221c5632011-06-13 13:39:01 +00003051 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
Matt Carlson42b64a42011-05-19 12:12:49 +00003052 if (err)
3053 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003054
Matt Carlson42b64a42011-05-19 12:12:49 +00003055 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3056 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Matt Carlson42b64a42011-05-19 12:12:49 +00003058 tw32(TG3_CPMU_EEE_MODE,
3059 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003060
Matt Carlson42b64a42011-05-19 12:12:49 +00003061 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3062 if (!err) {
3063 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003064
Matt Carlson21a00ab2011-01-25 15:58:55 +00003065 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3066 case ASIC_REV_5717:
3067 case ASIC_REV_57765:
3068 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3069 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3070 MII_TG3_DSP_CH34TP2_HIBW01);
3071 /* Fall through */
3072 case ASIC_REV_5719:
3073 val = MII_TG3_DSP_TAP26_ALNOKO |
3074 MII_TG3_DSP_TAP26_RMRXSTO |
3075 MII_TG3_DSP_TAP26_OPCSINPT;
3076 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3077 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003078
Matt Carlsona6b68da2010-12-06 08:28:52 +00003079 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003080 /* Advertise 100-BaseTX EEE ability */
3081 if (advertise & ADVERTISED_100baseT_Full)
3082 val |= MDIO_AN_EEE_ADV_100TX;
3083 /* Advertise 1000-BaseT EEE ability */
3084 if (advertise & ADVERTISED_1000baseT_Full)
3085 val |= MDIO_AN_EEE_ADV_1000T;
3086 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003087
Matt Carlson42b64a42011-05-19 12:12:49 +00003088 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3089 if (!err)
3090 err = err2;
3091 }
3092
3093done:
3094 return err;
3095}
3096
3097static void tg3_phy_copper_begin(struct tg3 *tp)
3098{
3099 u32 new_adv;
3100 int i;
3101
3102 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3103 new_adv = ADVERTISED_10baseT_Half |
3104 ADVERTISED_10baseT_Full;
3105 if (tg3_flag(tp, WOL_SPEED_100MB))
3106 new_adv |= ADVERTISED_100baseT_Half |
3107 ADVERTISED_100baseT_Full;
3108
3109 tg3_phy_autoneg_cfg(tp, new_adv,
3110 FLOW_CTRL_TX | FLOW_CTRL_RX);
3111 } else if (tp->link_config.speed == SPEED_INVALID) {
3112 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3113 tp->link_config.advertising &=
3114 ~(ADVERTISED_1000baseT_Half |
3115 ADVERTISED_1000baseT_Full);
3116
3117 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3118 tp->link_config.flowctrl);
3119 } else {
3120 /* Asking for a specific link mode. */
3121 if (tp->link_config.speed == SPEED_1000) {
3122 if (tp->link_config.duplex == DUPLEX_FULL)
3123 new_adv = ADVERTISED_1000baseT_Full;
3124 else
3125 new_adv = ADVERTISED_1000baseT_Half;
3126 } else if (tp->link_config.speed == SPEED_100) {
3127 if (tp->link_config.duplex == DUPLEX_FULL)
3128 new_adv = ADVERTISED_100baseT_Full;
3129 else
3130 new_adv = ADVERTISED_100baseT_Half;
3131 } else {
3132 if (tp->link_config.duplex == DUPLEX_FULL)
3133 new_adv = ADVERTISED_10baseT_Full;
3134 else
3135 new_adv = ADVERTISED_10baseT_Half;
3136 }
3137
3138 tg3_phy_autoneg_cfg(tp, new_adv,
3139 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003140 }
3141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3143 tp->link_config.speed != SPEED_INVALID) {
3144 u32 bmcr, orig_bmcr;
3145
3146 tp->link_config.active_speed = tp->link_config.speed;
3147 tp->link_config.active_duplex = tp->link_config.duplex;
3148
3149 bmcr = 0;
3150 switch (tp->link_config.speed) {
3151 default:
3152 case SPEED_10:
3153 break;
3154
3155 case SPEED_100:
3156 bmcr |= BMCR_SPEED100;
3157 break;
3158
3159 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003160 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
3164 if (tp->link_config.duplex == DUPLEX_FULL)
3165 bmcr |= BMCR_FULLDPLX;
3166
3167 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3168 (bmcr != orig_bmcr)) {
3169 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3170 for (i = 0; i < 1500; i++) {
3171 u32 tmp;
3172
3173 udelay(10);
3174 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3175 tg3_readphy(tp, MII_BMSR, &tmp))
3176 continue;
3177 if (!(tmp & BMSR_LSTATUS)) {
3178 udelay(40);
3179 break;
3180 }
3181 }
3182 tg3_writephy(tp, MII_BMCR, bmcr);
3183 udelay(40);
3184 }
3185 } else {
3186 tg3_writephy(tp, MII_BMCR,
3187 BMCR_ANENABLE | BMCR_ANRESTART);
3188 }
3189}
3190
3191static int tg3_init_5401phy_dsp(struct tg3 *tp)
3192{
3193 int err;
3194
3195 /* Turn off tap power management. */
3196 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003197 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003199 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3200 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3201 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3202 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3203 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
3205 udelay(40);
3206
3207 return err;
3208}
3209
Michael Chan3600d912006-12-07 00:21:48 -08003210static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Michael Chan3600d912006-12-07 00:21:48 -08003212 u32 adv_reg, all_mask = 0;
3213
3214 if (mask & ADVERTISED_10baseT_Half)
3215 all_mask |= ADVERTISE_10HALF;
3216 if (mask & ADVERTISED_10baseT_Full)
3217 all_mask |= ADVERTISE_10FULL;
3218 if (mask & ADVERTISED_100baseT_Half)
3219 all_mask |= ADVERTISE_100HALF;
3220 if (mask & ADVERTISED_100baseT_Full)
3221 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
3223 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3224 return 0;
3225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if ((adv_reg & all_mask) != all_mask)
3227 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003228 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 u32 tg3_ctrl;
3230
Michael Chan3600d912006-12-07 00:21:48 -08003231 all_mask = 0;
3232 if (mask & ADVERTISED_1000baseT_Half)
3233 all_mask |= ADVERTISE_1000HALF;
3234 if (mask & ADVERTISED_1000baseT_Full)
3235 all_mask |= ADVERTISE_1000FULL;
3236
Matt Carlson221c5632011-06-13 13:39:01 +00003237 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 return 0;
3239
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 if ((tg3_ctrl & all_mask) != all_mask)
3241 return 0;
3242 }
3243 return 1;
3244}
3245
Matt Carlsonef167e22007-12-20 20:10:01 -08003246static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3247{
3248 u32 curadv, reqadv;
3249
3250 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3251 return 1;
3252
3253 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3254 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3255
3256 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3257 if (curadv != reqadv)
3258 return 0;
3259
Joe Perches63c3a662011-04-26 08:12:10 +00003260 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003261 tg3_readphy(tp, MII_LPA, rmtadv);
3262 } else {
3263 /* Reprogram the advertisement register, even if it
3264 * does not affect the current link. If the link
3265 * gets renegotiated in the future, we can save an
3266 * additional renegotiation cycle by advertising
3267 * it correctly in the first place.
3268 */
3269 if (curadv != reqadv) {
3270 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3271 ADVERTISE_PAUSE_ASYM);
3272 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3273 }
3274 }
3275
3276 return 1;
3277}
3278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3280{
3281 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003282 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003283 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 u16 current_speed;
3285 u8 current_duplex;
3286 int i, err;
3287
3288 tw32(MAC_EVENT, 0);
3289
3290 tw32_f(MAC_STATUS,
3291 (MAC_STATUS_SYNC_CHANGED |
3292 MAC_STATUS_CFG_CHANGED |
3293 MAC_STATUS_MI_COMPLETION |
3294 MAC_STATUS_LNKSTATE_CHANGED));
3295 udelay(40);
3296
Matt Carlson8ef21422008-05-02 16:47:53 -07003297 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3298 tw32_f(MAC_MI_MODE,
3299 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3300 udelay(80);
3301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003303 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
3305 /* Some third-party PHYs need to be reset on link going
3306 * down.
3307 */
3308 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3309 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3310 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3311 netif_carrier_ok(tp->dev)) {
3312 tg3_readphy(tp, MII_BMSR, &bmsr);
3313 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3314 !(bmsr & BMSR_LSTATUS))
3315 force_reset = 1;
3316 }
3317 if (force_reset)
3318 tg3_phy_reset(tp);
3319
Matt Carlson79eb6902010-02-17 15:17:03 +00003320 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 tg3_readphy(tp, MII_BMSR, &bmsr);
3322 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003323 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 bmsr = 0;
3325
3326 if (!(bmsr & BMSR_LSTATUS)) {
3327 err = tg3_init_5401phy_dsp(tp);
3328 if (err)
3329 return err;
3330
3331 tg3_readphy(tp, MII_BMSR, &bmsr);
3332 for (i = 0; i < 1000; i++) {
3333 udelay(10);
3334 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3335 (bmsr & BMSR_LSTATUS)) {
3336 udelay(40);
3337 break;
3338 }
3339 }
3340
Matt Carlson79eb6902010-02-17 15:17:03 +00003341 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3342 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 !(bmsr & BMSR_LSTATUS) &&
3344 tp->link_config.active_speed == SPEED_1000) {
3345 err = tg3_phy_reset(tp);
3346 if (!err)
3347 err = tg3_init_5401phy_dsp(tp);
3348 if (err)
3349 return err;
3350 }
3351 }
3352 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3353 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3354 /* 5701 {A0,B0} CRC bug workaround */
3355 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003356 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3357 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3358 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 }
3360
3361 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003362 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3363 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003365 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003367 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3369
3370 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3371 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3372 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3373 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3374 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3375 else
3376 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3377 }
3378
3379 current_link_up = 0;
3380 current_speed = SPEED_INVALID;
3381 current_duplex = DUPLEX_INVALID;
3382
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003383 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003384 err = tg3_phy_auxctl_read(tp,
3385 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3386 &val);
3387 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003388 tg3_phy_auxctl_write(tp,
3389 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3390 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 goto relink;
3392 }
3393 }
3394
3395 bmsr = 0;
3396 for (i = 0; i < 100; i++) {
3397 tg3_readphy(tp, MII_BMSR, &bmsr);
3398 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3399 (bmsr & BMSR_LSTATUS))
3400 break;
3401 udelay(40);
3402 }
3403
3404 if (bmsr & BMSR_LSTATUS) {
3405 u32 aux_stat, bmcr;
3406
3407 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3408 for (i = 0; i < 2000; i++) {
3409 udelay(10);
3410 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3411 aux_stat)
3412 break;
3413 }
3414
3415 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3416 &current_speed,
3417 &current_duplex);
3418
3419 bmcr = 0;
3420 for (i = 0; i < 200; i++) {
3421 tg3_readphy(tp, MII_BMCR, &bmcr);
3422 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3423 continue;
3424 if (bmcr && bmcr != 0x7fff)
3425 break;
3426 udelay(10);
3427 }
3428
Matt Carlsonef167e22007-12-20 20:10:01 -08003429 lcl_adv = 0;
3430 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
Matt Carlsonef167e22007-12-20 20:10:01 -08003432 tp->link_config.active_speed = current_speed;
3433 tp->link_config.active_duplex = current_duplex;
3434
3435 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3436 if ((bmcr & BMCR_ANENABLE) &&
3437 tg3_copper_is_advertising_all(tp,
3438 tp->link_config.advertising)) {
3439 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3440 &rmt_adv))
3441 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 }
3443 } else {
3444 if (!(bmcr & BMCR_ANENABLE) &&
3445 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003446 tp->link_config.duplex == current_duplex &&
3447 tp->link_config.flowctrl ==
3448 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 }
3451 }
3452
Matt Carlsonef167e22007-12-20 20:10:01 -08003453 if (current_link_up == 1 &&
3454 tp->link_config.active_duplex == DUPLEX_FULL)
3455 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 }
3457
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458relink:
Matt Carlson80096062010-08-02 11:26:06 +00003459 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 tg3_phy_copper_begin(tp);
3461
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003462 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00003463 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
3464 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 current_link_up = 1;
3466 }
3467
3468 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3469 if (current_link_up == 1) {
3470 if (tp->link_config.active_speed == SPEED_100 ||
3471 tp->link_config.active_speed == SPEED_10)
3472 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3473 else
3474 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003475 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003476 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3477 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3479
3480 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3481 if (tp->link_config.active_duplex == DUPLEX_HALF)
3482 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003485 if (current_link_up == 1 &&
3486 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003488 else
3489 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 }
3491
3492 /* ??? Without this setting Netgear GA302T PHY does not
3493 * ??? send/receive packets...
3494 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003495 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3497 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3498 tw32_f(MAC_MI_MODE, tp->mi_mode);
3499 udelay(80);
3500 }
3501
3502 tw32_f(MAC_MODE, tp->mac_mode);
3503 udelay(40);
3504
Matt Carlson52b02d02010-10-14 10:37:41 +00003505 tg3_phy_eee_adjust(tp, current_link_up);
3506
Joe Perches63c3a662011-04-26 08:12:10 +00003507 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 /* Polled via timer. */
3509 tw32_f(MAC_EVENT, 0);
3510 } else {
3511 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3512 }
3513 udelay(40);
3514
3515 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3516 current_link_up == 1 &&
3517 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00003518 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 udelay(120);
3520 tw32_f(MAC_STATUS,
3521 (MAC_STATUS_SYNC_CHANGED |
3522 MAC_STATUS_CFG_CHANGED));
3523 udelay(40);
3524 tg3_write_mem(tp,
3525 NIC_SRAM_FIRMWARE_MBOX,
3526 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3527 }
3528
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003529 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00003530 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003531 u16 oldlnkctl, newlnkctl;
3532
3533 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003534 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003535 &oldlnkctl);
3536 if (tp->link_config.active_speed == SPEED_100 ||
3537 tp->link_config.active_speed == SPEED_10)
3538 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3539 else
3540 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3541 if (newlnkctl != oldlnkctl)
3542 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003543 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003544 newlnkctl);
3545 }
3546
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 if (current_link_up != netif_carrier_ok(tp->dev)) {
3548 if (current_link_up)
3549 netif_carrier_on(tp->dev);
3550 else
3551 netif_carrier_off(tp->dev);
3552 tg3_link_report(tp);
3553 }
3554
3555 return 0;
3556}
3557
3558struct tg3_fiber_aneginfo {
3559 int state;
3560#define ANEG_STATE_UNKNOWN 0
3561#define ANEG_STATE_AN_ENABLE 1
3562#define ANEG_STATE_RESTART_INIT 2
3563#define ANEG_STATE_RESTART 3
3564#define ANEG_STATE_DISABLE_LINK_OK 4
3565#define ANEG_STATE_ABILITY_DETECT_INIT 5
3566#define ANEG_STATE_ABILITY_DETECT 6
3567#define ANEG_STATE_ACK_DETECT_INIT 7
3568#define ANEG_STATE_ACK_DETECT 8
3569#define ANEG_STATE_COMPLETE_ACK_INIT 9
3570#define ANEG_STATE_COMPLETE_ACK 10
3571#define ANEG_STATE_IDLE_DETECT_INIT 11
3572#define ANEG_STATE_IDLE_DETECT 12
3573#define ANEG_STATE_LINK_OK 13
3574#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3575#define ANEG_STATE_NEXT_PAGE_WAIT 15
3576
3577 u32 flags;
3578#define MR_AN_ENABLE 0x00000001
3579#define MR_RESTART_AN 0x00000002
3580#define MR_AN_COMPLETE 0x00000004
3581#define MR_PAGE_RX 0x00000008
3582#define MR_NP_LOADED 0x00000010
3583#define MR_TOGGLE_TX 0x00000020
3584#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3585#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3586#define MR_LP_ADV_SYM_PAUSE 0x00000100
3587#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3588#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3589#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3590#define MR_LP_ADV_NEXT_PAGE 0x00001000
3591#define MR_TOGGLE_RX 0x00002000
3592#define MR_NP_RX 0x00004000
3593
3594#define MR_LINK_OK 0x80000000
3595
3596 unsigned long link_time, cur_time;
3597
3598 u32 ability_match_cfg;
3599 int ability_match_count;
3600
3601 char ability_match, idle_match, ack_match;
3602
3603 u32 txconfig, rxconfig;
3604#define ANEG_CFG_NP 0x00000080
3605#define ANEG_CFG_ACK 0x00000040
3606#define ANEG_CFG_RF2 0x00000020
3607#define ANEG_CFG_RF1 0x00000010
3608#define ANEG_CFG_PS2 0x00000001
3609#define ANEG_CFG_PS1 0x00008000
3610#define ANEG_CFG_HD 0x00004000
3611#define ANEG_CFG_FD 0x00002000
3612#define ANEG_CFG_INVAL 0x00001f06
3613
3614};
3615#define ANEG_OK 0
3616#define ANEG_DONE 1
3617#define ANEG_TIMER_ENAB 2
3618#define ANEG_FAILED -1
3619
3620#define ANEG_STATE_SETTLE_TIME 10000
3621
3622static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3623 struct tg3_fiber_aneginfo *ap)
3624{
Matt Carlson5be73b42007-12-20 20:09:29 -08003625 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 unsigned long delta;
3627 u32 rx_cfg_reg;
3628 int ret;
3629
3630 if (ap->state == ANEG_STATE_UNKNOWN) {
3631 ap->rxconfig = 0;
3632 ap->link_time = 0;
3633 ap->cur_time = 0;
3634 ap->ability_match_cfg = 0;
3635 ap->ability_match_count = 0;
3636 ap->ability_match = 0;
3637 ap->idle_match = 0;
3638 ap->ack_match = 0;
3639 }
3640 ap->cur_time++;
3641
3642 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3643 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3644
3645 if (rx_cfg_reg != ap->ability_match_cfg) {
3646 ap->ability_match_cfg = rx_cfg_reg;
3647 ap->ability_match = 0;
3648 ap->ability_match_count = 0;
3649 } else {
3650 if (++ap->ability_match_count > 1) {
3651 ap->ability_match = 1;
3652 ap->ability_match_cfg = rx_cfg_reg;
3653 }
3654 }
3655 if (rx_cfg_reg & ANEG_CFG_ACK)
3656 ap->ack_match = 1;
3657 else
3658 ap->ack_match = 0;
3659
3660 ap->idle_match = 0;
3661 } else {
3662 ap->idle_match = 1;
3663 ap->ability_match_cfg = 0;
3664 ap->ability_match_count = 0;
3665 ap->ability_match = 0;
3666 ap->ack_match = 0;
3667
3668 rx_cfg_reg = 0;
3669 }
3670
3671 ap->rxconfig = rx_cfg_reg;
3672 ret = ANEG_OK;
3673
Matt Carlson33f401a2010-04-05 10:19:27 +00003674 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 case ANEG_STATE_UNKNOWN:
3676 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3677 ap->state = ANEG_STATE_AN_ENABLE;
3678
3679 /* fallthru */
3680 case ANEG_STATE_AN_ENABLE:
3681 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3682 if (ap->flags & MR_AN_ENABLE) {
3683 ap->link_time = 0;
3684 ap->cur_time = 0;
3685 ap->ability_match_cfg = 0;
3686 ap->ability_match_count = 0;
3687 ap->ability_match = 0;
3688 ap->idle_match = 0;
3689 ap->ack_match = 0;
3690
3691 ap->state = ANEG_STATE_RESTART_INIT;
3692 } else {
3693 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3694 }
3695 break;
3696
3697 case ANEG_STATE_RESTART_INIT:
3698 ap->link_time = ap->cur_time;
3699 ap->flags &= ~(MR_NP_LOADED);
3700 ap->txconfig = 0;
3701 tw32(MAC_TX_AUTO_NEG, 0);
3702 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3703 tw32_f(MAC_MODE, tp->mac_mode);
3704 udelay(40);
3705
3706 ret = ANEG_TIMER_ENAB;
3707 ap->state = ANEG_STATE_RESTART;
3708
3709 /* fallthru */
3710 case ANEG_STATE_RESTART:
3711 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00003712 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00003714 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 break;
3717
3718 case ANEG_STATE_DISABLE_LINK_OK:
3719 ret = ANEG_DONE;
3720 break;
3721
3722 case ANEG_STATE_ABILITY_DETECT_INIT:
3723 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003724 ap->txconfig = ANEG_CFG_FD;
3725 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3726 if (flowctrl & ADVERTISE_1000XPAUSE)
3727 ap->txconfig |= ANEG_CFG_PS1;
3728 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3729 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3731 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3732 tw32_f(MAC_MODE, tp->mac_mode);
3733 udelay(40);
3734
3735 ap->state = ANEG_STATE_ABILITY_DETECT;
3736 break;
3737
3738 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00003739 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 break;
3742
3743 case ANEG_STATE_ACK_DETECT_INIT:
3744 ap->txconfig |= ANEG_CFG_ACK;
3745 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3746 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3747 tw32_f(MAC_MODE, tp->mac_mode);
3748 udelay(40);
3749
3750 ap->state = ANEG_STATE_ACK_DETECT;
3751
3752 /* fallthru */
3753 case ANEG_STATE_ACK_DETECT:
3754 if (ap->ack_match != 0) {
3755 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3756 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3757 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3758 } else {
3759 ap->state = ANEG_STATE_AN_ENABLE;
3760 }
3761 } else if (ap->ability_match != 0 &&
3762 ap->rxconfig == 0) {
3763 ap->state = ANEG_STATE_AN_ENABLE;
3764 }
3765 break;
3766
3767 case ANEG_STATE_COMPLETE_ACK_INIT:
3768 if (ap->rxconfig & ANEG_CFG_INVAL) {
3769 ret = ANEG_FAILED;
3770 break;
3771 }
3772 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3773 MR_LP_ADV_HALF_DUPLEX |
3774 MR_LP_ADV_SYM_PAUSE |
3775 MR_LP_ADV_ASYM_PAUSE |
3776 MR_LP_ADV_REMOTE_FAULT1 |
3777 MR_LP_ADV_REMOTE_FAULT2 |
3778 MR_LP_ADV_NEXT_PAGE |
3779 MR_TOGGLE_RX |
3780 MR_NP_RX);
3781 if (ap->rxconfig & ANEG_CFG_FD)
3782 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3783 if (ap->rxconfig & ANEG_CFG_HD)
3784 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3785 if (ap->rxconfig & ANEG_CFG_PS1)
3786 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3787 if (ap->rxconfig & ANEG_CFG_PS2)
3788 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3789 if (ap->rxconfig & ANEG_CFG_RF1)
3790 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3791 if (ap->rxconfig & ANEG_CFG_RF2)
3792 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3793 if (ap->rxconfig & ANEG_CFG_NP)
3794 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3795
3796 ap->link_time = ap->cur_time;
3797
3798 ap->flags ^= (MR_TOGGLE_TX);
3799 if (ap->rxconfig & 0x0008)
3800 ap->flags |= MR_TOGGLE_RX;
3801 if (ap->rxconfig & ANEG_CFG_NP)
3802 ap->flags |= MR_NP_RX;
3803 ap->flags |= MR_PAGE_RX;
3804
3805 ap->state = ANEG_STATE_COMPLETE_ACK;
3806 ret = ANEG_TIMER_ENAB;
3807 break;
3808
3809 case ANEG_STATE_COMPLETE_ACK:
3810 if (ap->ability_match != 0 &&
3811 ap->rxconfig == 0) {
3812 ap->state = ANEG_STATE_AN_ENABLE;
3813 break;
3814 }
3815 delta = ap->cur_time - ap->link_time;
3816 if (delta > ANEG_STATE_SETTLE_TIME) {
3817 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3818 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3819 } else {
3820 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3821 !(ap->flags & MR_NP_RX)) {
3822 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3823 } else {
3824 ret = ANEG_FAILED;
3825 }
3826 }
3827 }
3828 break;
3829
3830 case ANEG_STATE_IDLE_DETECT_INIT:
3831 ap->link_time = ap->cur_time;
3832 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3833 tw32_f(MAC_MODE, tp->mac_mode);
3834 udelay(40);
3835
3836 ap->state = ANEG_STATE_IDLE_DETECT;
3837 ret = ANEG_TIMER_ENAB;
3838 break;
3839
3840 case ANEG_STATE_IDLE_DETECT:
3841 if (ap->ability_match != 0 &&
3842 ap->rxconfig == 0) {
3843 ap->state = ANEG_STATE_AN_ENABLE;
3844 break;
3845 }
3846 delta = ap->cur_time - ap->link_time;
3847 if (delta > ANEG_STATE_SETTLE_TIME) {
3848 /* XXX another gem from the Broadcom driver :( */
3849 ap->state = ANEG_STATE_LINK_OK;
3850 }
3851 break;
3852
3853 case ANEG_STATE_LINK_OK:
3854 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3855 ret = ANEG_DONE;
3856 break;
3857
3858 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3859 /* ??? unimplemented */
3860 break;
3861
3862 case ANEG_STATE_NEXT_PAGE_WAIT:
3863 /* ??? unimplemented */
3864 break;
3865
3866 default:
3867 ret = ANEG_FAILED;
3868 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
3871 return ret;
3872}
3873
Matt Carlson5be73b42007-12-20 20:09:29 -08003874static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875{
3876 int res = 0;
3877 struct tg3_fiber_aneginfo aninfo;
3878 int status = ANEG_FAILED;
3879 unsigned int tick;
3880 u32 tmp;
3881
3882 tw32_f(MAC_TX_AUTO_NEG, 0);
3883
3884 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3885 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3886 udelay(40);
3887
3888 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3889 udelay(40);
3890
3891 memset(&aninfo, 0, sizeof(aninfo));
3892 aninfo.flags |= MR_AN_ENABLE;
3893 aninfo.state = ANEG_STATE_UNKNOWN;
3894 aninfo.cur_time = 0;
3895 tick = 0;
3896 while (++tick < 195000) {
3897 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3898 if (status == ANEG_DONE || status == ANEG_FAILED)
3899 break;
3900
3901 udelay(1);
3902 }
3903
3904 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3905 tw32_f(MAC_MODE, tp->mac_mode);
3906 udelay(40);
3907
Matt Carlson5be73b42007-12-20 20:09:29 -08003908 *txflags = aninfo.txconfig;
3909 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
3911 if (status == ANEG_DONE &&
3912 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3913 MR_LP_ADV_FULL_DUPLEX)))
3914 res = 1;
3915
3916 return res;
3917}
3918
3919static void tg3_init_bcm8002(struct tg3 *tp)
3920{
3921 u32 mac_status = tr32(MAC_STATUS);
3922 int i;
3923
3924 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00003925 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 !(mac_status & MAC_STATUS_PCS_SYNCED))
3927 return;
3928
3929 /* Set PLL lock range. */
3930 tg3_writephy(tp, 0x16, 0x8007);
3931
3932 /* SW reset */
3933 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3934
3935 /* Wait for reset to complete. */
3936 /* XXX schedule_timeout() ... */
3937 for (i = 0; i < 500; i++)
3938 udelay(10);
3939
3940 /* Config mode; select PMA/Ch 1 regs. */
3941 tg3_writephy(tp, 0x10, 0x8411);
3942
3943 /* Enable auto-lock and comdet, select txclk for tx. */
3944 tg3_writephy(tp, 0x11, 0x0a10);
3945
3946 tg3_writephy(tp, 0x18, 0x00a0);
3947 tg3_writephy(tp, 0x16, 0x41ff);
3948
3949 /* Assert and deassert POR. */
3950 tg3_writephy(tp, 0x13, 0x0400);
3951 udelay(40);
3952 tg3_writephy(tp, 0x13, 0x0000);
3953
3954 tg3_writephy(tp, 0x11, 0x0a50);
3955 udelay(40);
3956 tg3_writephy(tp, 0x11, 0x0a10);
3957
3958 /* Wait for signal to stabilize */
3959 /* XXX schedule_timeout() ... */
3960 for (i = 0; i < 15000; i++)
3961 udelay(10);
3962
3963 /* Deselect the channel register so we can read the PHYID
3964 * later.
3965 */
3966 tg3_writephy(tp, 0x10, 0x8011);
3967}
3968
3969static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3970{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003971 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 u32 sg_dig_ctrl, sg_dig_status;
3973 u32 serdes_cfg, expected_sg_dig_ctrl;
3974 int workaround, port_a;
3975 int current_link_up;
3976
3977 serdes_cfg = 0;
3978 expected_sg_dig_ctrl = 0;
3979 workaround = 0;
3980 port_a = 1;
3981 current_link_up = 0;
3982
3983 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3984 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3985 workaround = 1;
3986 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3987 port_a = 0;
3988
3989 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3990 /* preserve bits 20-23 for voltage regulator */
3991 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3992 }
3993
3994 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3995
3996 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003997 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 if (workaround) {
3999 u32 val = serdes_cfg;
4000
4001 if (port_a)
4002 val |= 0xc010000;
4003 else
4004 val |= 0x4010000;
4005 tw32_f(MAC_SERDES_CFG, val);
4006 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004007
4008 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 }
4010 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4011 tg3_setup_flow_control(tp, 0, 0);
4012 current_link_up = 1;
4013 }
4014 goto out;
4015 }
4016
4017 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004018 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
Matt Carlson82cd3d12007-12-20 20:09:00 -08004020 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4021 if (flowctrl & ADVERTISE_1000XPAUSE)
4022 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4023 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4024 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025
4026 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004027 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004028 tp->serdes_counter &&
4029 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4030 MAC_STATUS_RCVD_CFG)) ==
4031 MAC_STATUS_PCS_SYNCED)) {
4032 tp->serdes_counter--;
4033 current_link_up = 1;
4034 goto out;
4035 }
4036restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 if (workaround)
4038 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004039 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 udelay(5);
4041 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4042
Michael Chan3d3ebe72006-09-27 15:59:15 -07004043 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004044 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4046 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004047 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 mac_status = tr32(MAC_STATUS);
4049
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004050 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004052 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053
Matt Carlson82cd3d12007-12-20 20:09:00 -08004054 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4055 local_adv |= ADVERTISE_1000XPAUSE;
4056 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4057 local_adv |= ADVERTISE_1000XPSE_ASYM;
4058
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004059 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004060 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004061 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004062 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
4064 tg3_setup_flow_control(tp, local_adv, remote_adv);
4065 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004066 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004067 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004068 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004069 if (tp->serdes_counter)
4070 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 else {
4072 if (workaround) {
4073 u32 val = serdes_cfg;
4074
4075 if (port_a)
4076 val |= 0xc010000;
4077 else
4078 val |= 0x4010000;
4079
4080 tw32_f(MAC_SERDES_CFG, val);
4081 }
4082
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004083 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 udelay(40);
4085
4086 /* Link parallel detection - link is up */
4087 /* only if we have PCS_SYNC and not */
4088 /* receiving config code words */
4089 mac_status = tr32(MAC_STATUS);
4090 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4091 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4092 tg3_setup_flow_control(tp, 0, 0);
4093 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004094 tp->phy_flags |=
4095 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004096 tp->serdes_counter =
4097 SERDES_PARALLEL_DET_TIMEOUT;
4098 } else
4099 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 }
4101 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004102 } else {
4103 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004104 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 }
4106
4107out:
4108 return current_link_up;
4109}
4110
4111static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4112{
4113 int current_link_up = 0;
4114
Michael Chan5cf64b82007-05-05 12:11:21 -07004115 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
4118 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004119 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004121
Matt Carlson5be73b42007-12-20 20:09:29 -08004122 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4123 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
Matt Carlson5be73b42007-12-20 20:09:29 -08004125 if (txflags & ANEG_CFG_PS1)
4126 local_adv |= ADVERTISE_1000XPAUSE;
4127 if (txflags & ANEG_CFG_PS2)
4128 local_adv |= ADVERTISE_1000XPSE_ASYM;
4129
4130 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4131 remote_adv |= LPA_1000XPAUSE;
4132 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4133 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
4135 tg3_setup_flow_control(tp, local_adv, remote_adv);
4136
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 current_link_up = 1;
4138 }
4139 for (i = 0; i < 30; i++) {
4140 udelay(20);
4141 tw32_f(MAC_STATUS,
4142 (MAC_STATUS_SYNC_CHANGED |
4143 MAC_STATUS_CFG_CHANGED));
4144 udelay(40);
4145 if ((tr32(MAC_STATUS) &
4146 (MAC_STATUS_SYNC_CHANGED |
4147 MAC_STATUS_CFG_CHANGED)) == 0)
4148 break;
4149 }
4150
4151 mac_status = tr32(MAC_STATUS);
4152 if (current_link_up == 0 &&
4153 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4154 !(mac_status & MAC_STATUS_RCVD_CFG))
4155 current_link_up = 1;
4156 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004157 tg3_setup_flow_control(tp, 0, 0);
4158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 /* Forcing 1000FD link up. */
4160 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
4162 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4163 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004164
4165 tw32_f(MAC_MODE, tp->mac_mode);
4166 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 }
4168
4169out:
4170 return current_link_up;
4171}
4172
4173static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4174{
4175 u32 orig_pause_cfg;
4176 u16 orig_active_speed;
4177 u8 orig_active_duplex;
4178 u32 mac_status;
4179 int current_link_up;
4180 int i;
4181
Matt Carlson8d018622007-12-20 20:05:44 -08004182 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 orig_active_speed = tp->link_config.active_speed;
4184 orig_active_duplex = tp->link_config.active_duplex;
4185
Joe Perches63c3a662011-04-26 08:12:10 +00004186 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004188 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 mac_status = tr32(MAC_STATUS);
4190 mac_status &= (MAC_STATUS_PCS_SYNCED |
4191 MAC_STATUS_SIGNAL_DET |
4192 MAC_STATUS_CFG_CHANGED |
4193 MAC_STATUS_RCVD_CFG);
4194 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4195 MAC_STATUS_SIGNAL_DET)) {
4196 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4197 MAC_STATUS_CFG_CHANGED));
4198 return 0;
4199 }
4200 }
4201
4202 tw32_f(MAC_TX_AUTO_NEG, 0);
4203
4204 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4205 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4206 tw32_f(MAC_MODE, tp->mac_mode);
4207 udelay(40);
4208
Matt Carlson79eb6902010-02-17 15:17:03 +00004209 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 tg3_init_bcm8002(tp);
4211
4212 /* Enable link change event even when serdes polling. */
4213 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4214 udelay(40);
4215
4216 current_link_up = 0;
4217 mac_status = tr32(MAC_STATUS);
4218
Joe Perches63c3a662011-04-26 08:12:10 +00004219 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4221 else
4222 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4223
Matt Carlson898a56f2009-08-28 14:02:40 +00004224 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004226 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
4228 for (i = 0; i < 100; i++) {
4229 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4230 MAC_STATUS_CFG_CHANGED));
4231 udelay(5);
4232 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004233 MAC_STATUS_CFG_CHANGED |
4234 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 break;
4236 }
4237
4238 mac_status = tr32(MAC_STATUS);
4239 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4240 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004241 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4242 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 tw32_f(MAC_MODE, (tp->mac_mode |
4244 MAC_MODE_SEND_CONFIGS));
4245 udelay(1);
4246 tw32_f(MAC_MODE, tp->mac_mode);
4247 }
4248 }
4249
4250 if (current_link_up == 1) {
4251 tp->link_config.active_speed = SPEED_1000;
4252 tp->link_config.active_duplex = DUPLEX_FULL;
4253 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4254 LED_CTRL_LNKLED_OVERRIDE |
4255 LED_CTRL_1000MBPS_ON));
4256 } else {
4257 tp->link_config.active_speed = SPEED_INVALID;
4258 tp->link_config.active_duplex = DUPLEX_INVALID;
4259 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4260 LED_CTRL_LNKLED_OVERRIDE |
4261 LED_CTRL_TRAFFIC_OVERRIDE));
4262 }
4263
4264 if (current_link_up != netif_carrier_ok(tp->dev)) {
4265 if (current_link_up)
4266 netif_carrier_on(tp->dev);
4267 else
4268 netif_carrier_off(tp->dev);
4269 tg3_link_report(tp);
4270 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004271 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 if (orig_pause_cfg != now_pause_cfg ||
4273 orig_active_speed != tp->link_config.active_speed ||
4274 orig_active_duplex != tp->link_config.active_duplex)
4275 tg3_link_report(tp);
4276 }
4277
4278 return 0;
4279}
4280
Michael Chan747e8f82005-07-25 12:33:22 -07004281static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4282{
4283 int current_link_up, err = 0;
4284 u32 bmsr, bmcr;
4285 u16 current_speed;
4286 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004287 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004288
4289 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4290 tw32_f(MAC_MODE, tp->mac_mode);
4291 udelay(40);
4292
4293 tw32(MAC_EVENT, 0);
4294
4295 tw32_f(MAC_STATUS,
4296 (MAC_STATUS_SYNC_CHANGED |
4297 MAC_STATUS_CFG_CHANGED |
4298 MAC_STATUS_MI_COMPLETION |
4299 MAC_STATUS_LNKSTATE_CHANGED));
4300 udelay(40);
4301
4302 if (force_reset)
4303 tg3_phy_reset(tp);
4304
4305 current_link_up = 0;
4306 current_speed = SPEED_INVALID;
4307 current_duplex = DUPLEX_INVALID;
4308
4309 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4310 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004311 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4312 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4313 bmsr |= BMSR_LSTATUS;
4314 else
4315 bmsr &= ~BMSR_LSTATUS;
4316 }
Michael Chan747e8f82005-07-25 12:33:22 -07004317
4318 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4319
4320 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004321 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004322 /* do nothing, just check for link up at the end */
4323 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4324 u32 adv, new_adv;
4325
4326 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4327 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4328 ADVERTISE_1000XPAUSE |
4329 ADVERTISE_1000XPSE_ASYM |
4330 ADVERTISE_SLCT);
4331
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004332 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004333
4334 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4335 new_adv |= ADVERTISE_1000XHALF;
4336 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4337 new_adv |= ADVERTISE_1000XFULL;
4338
4339 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4340 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4341 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4342 tg3_writephy(tp, MII_BMCR, bmcr);
4343
4344 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004345 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004346 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004347
4348 return err;
4349 }
4350 } else {
4351 u32 new_bmcr;
4352
4353 bmcr &= ~BMCR_SPEED1000;
4354 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4355
4356 if (tp->link_config.duplex == DUPLEX_FULL)
4357 new_bmcr |= BMCR_FULLDPLX;
4358
4359 if (new_bmcr != bmcr) {
4360 /* BMCR_SPEED1000 is a reserved bit that needs
4361 * to be set on write.
4362 */
4363 new_bmcr |= BMCR_SPEED1000;
4364
4365 /* Force a linkdown */
4366 if (netif_carrier_ok(tp->dev)) {
4367 u32 adv;
4368
4369 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4370 adv &= ~(ADVERTISE_1000XFULL |
4371 ADVERTISE_1000XHALF |
4372 ADVERTISE_SLCT);
4373 tg3_writephy(tp, MII_ADVERTISE, adv);
4374 tg3_writephy(tp, MII_BMCR, bmcr |
4375 BMCR_ANRESTART |
4376 BMCR_ANENABLE);
4377 udelay(10);
4378 netif_carrier_off(tp->dev);
4379 }
4380 tg3_writephy(tp, MII_BMCR, new_bmcr);
4381 bmcr = new_bmcr;
4382 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4383 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004384 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4385 ASIC_REV_5714) {
4386 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4387 bmsr |= BMSR_LSTATUS;
4388 else
4389 bmsr &= ~BMSR_LSTATUS;
4390 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004391 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004392 }
4393 }
4394
4395 if (bmsr & BMSR_LSTATUS) {
4396 current_speed = SPEED_1000;
4397 current_link_up = 1;
4398 if (bmcr & BMCR_FULLDPLX)
4399 current_duplex = DUPLEX_FULL;
4400 else
4401 current_duplex = DUPLEX_HALF;
4402
Matt Carlsonef167e22007-12-20 20:10:01 -08004403 local_adv = 0;
4404 remote_adv = 0;
4405
Michael Chan747e8f82005-07-25 12:33:22 -07004406 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004407 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004408
4409 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4410 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4411 common = local_adv & remote_adv;
4412 if (common & (ADVERTISE_1000XHALF |
4413 ADVERTISE_1000XFULL)) {
4414 if (common & ADVERTISE_1000XFULL)
4415 current_duplex = DUPLEX_FULL;
4416 else
4417 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004418 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004419 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004420 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004421 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004422 }
Michael Chan747e8f82005-07-25 12:33:22 -07004423 }
4424 }
4425
Matt Carlsonef167e22007-12-20 20:10:01 -08004426 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4427 tg3_setup_flow_control(tp, local_adv, remote_adv);
4428
Michael Chan747e8f82005-07-25 12:33:22 -07004429 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4430 if (tp->link_config.active_duplex == DUPLEX_HALF)
4431 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4432
4433 tw32_f(MAC_MODE, tp->mac_mode);
4434 udelay(40);
4435
4436 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4437
4438 tp->link_config.active_speed = current_speed;
4439 tp->link_config.active_duplex = current_duplex;
4440
4441 if (current_link_up != netif_carrier_ok(tp->dev)) {
4442 if (current_link_up)
4443 netif_carrier_on(tp->dev);
4444 else {
4445 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004446 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004447 }
4448 tg3_link_report(tp);
4449 }
4450 return err;
4451}
4452
4453static void tg3_serdes_parallel_detect(struct tg3 *tp)
4454{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004455 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004456 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004457 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004458 return;
4459 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004460
Michael Chan747e8f82005-07-25 12:33:22 -07004461 if (!netif_carrier_ok(tp->dev) &&
4462 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4463 u32 bmcr;
4464
4465 tg3_readphy(tp, MII_BMCR, &bmcr);
4466 if (bmcr & BMCR_ANENABLE) {
4467 u32 phy1, phy2;
4468
4469 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004470 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4471 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004472
4473 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004474 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4475 MII_TG3_DSP_EXP1_INT_STAT);
4476 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4477 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004478
4479 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4480 /* We have signal detect and not receiving
4481 * config code words, link is up by parallel
4482 * detection.
4483 */
4484
4485 bmcr &= ~BMCR_ANENABLE;
4486 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4487 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004488 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004489 }
4490 }
Matt Carlson859a5882010-04-05 10:19:28 +00004491 } else if (netif_carrier_ok(tp->dev) &&
4492 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004493 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004494 u32 phy2;
4495
4496 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004497 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4498 MII_TG3_DSP_EXP1_INT_STAT);
4499 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004500 if (phy2 & 0x20) {
4501 u32 bmcr;
4502
4503 /* Config code words received, turn on autoneg. */
4504 tg3_readphy(tp, MII_BMCR, &bmcr);
4505 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4506
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004507 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004508
4509 }
4510 }
4511}
4512
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4514{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004515 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 int err;
4517
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004518 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004520 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004521 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00004522 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004525 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004526 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004527
4528 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4529 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4530 scale = 65;
4531 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4532 scale = 6;
4533 else
4534 scale = 12;
4535
4536 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4537 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4538 tw32(GRC_MISC_CFG, val);
4539 }
4540
Matt Carlsonf2096f92011-04-05 14:22:48 +00004541 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4542 (6 << TX_LENGTHS_IPG_SHIFT);
4543 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4544 val |= tr32(MAC_TX_LENGTHS) &
4545 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4546 TX_LENGTHS_CNT_DWN_VAL_MSK);
4547
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 if (tp->link_config.active_speed == SPEED_1000 &&
4549 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004550 tw32(MAC_TX_LENGTHS, val |
4551 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004553 tw32(MAC_TX_LENGTHS, val |
4554 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Joe Perches63c3a662011-04-26 08:12:10 +00004556 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 if (netif_carrier_ok(tp->dev)) {
4558 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004559 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 } else {
4561 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4562 }
4563 }
4564
Joe Perches63c3a662011-04-26 08:12:10 +00004565 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004566 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004567 if (!netif_carrier_ok(tp->dev))
4568 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4569 tp->pwrmgmt_thresh;
4570 else
4571 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4572 tw32(PCIE_PWR_MGMT_THRESH, val);
4573 }
4574
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 return err;
4576}
4577
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004578static inline int tg3_irq_sync(struct tg3 *tp)
4579{
4580 return tp->irq_sync;
4581}
4582
Matt Carlson97bd8e42011-04-13 11:05:04 +00004583static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4584{
4585 int i;
4586
4587 dst = (u32 *)((u8 *)dst + off);
4588 for (i = 0; i < len; i += sizeof(u32))
4589 *dst++ = tr32(off + i);
4590}
4591
4592static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4593{
4594 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4595 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4596 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4597 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4598 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4599 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4600 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4601 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4602 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4603 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4604 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4605 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4606 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4607 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4608 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4609 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4610 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4611 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4612 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4613
Joe Perches63c3a662011-04-26 08:12:10 +00004614 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004615 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4616
4617 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4618 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4619 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4620 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4621 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4622 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4623 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4624 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4625
Joe Perches63c3a662011-04-26 08:12:10 +00004626 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004627 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4628 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4629 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4630 }
4631
4632 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4633 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4634 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4635 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4636 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4637
Joe Perches63c3a662011-04-26 08:12:10 +00004638 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004639 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4640}
4641
4642static void tg3_dump_state(struct tg3 *tp)
4643{
4644 int i;
4645 u32 *regs;
4646
4647 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4648 if (!regs) {
4649 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4650 return;
4651 }
4652
Joe Perches63c3a662011-04-26 08:12:10 +00004653 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004654 /* Read up to but not including private PCI registers */
4655 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4656 regs[i / sizeof(u32)] = tr32(i);
4657 } else
4658 tg3_dump_legacy_regs(tp, regs);
4659
4660 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4661 if (!regs[i + 0] && !regs[i + 1] &&
4662 !regs[i + 2] && !regs[i + 3])
4663 continue;
4664
4665 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4666 i * 4,
4667 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4668 }
4669
4670 kfree(regs);
4671
4672 for (i = 0; i < tp->irq_cnt; i++) {
4673 struct tg3_napi *tnapi = &tp->napi[i];
4674
4675 /* SW status block */
4676 netdev_err(tp->dev,
4677 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4678 i,
4679 tnapi->hw_status->status,
4680 tnapi->hw_status->status_tag,
4681 tnapi->hw_status->rx_jumbo_consumer,
4682 tnapi->hw_status->rx_consumer,
4683 tnapi->hw_status->rx_mini_consumer,
4684 tnapi->hw_status->idx[0].rx_producer,
4685 tnapi->hw_status->idx[0].tx_consumer);
4686
4687 netdev_err(tp->dev,
4688 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4689 i,
4690 tnapi->last_tag, tnapi->last_irq_tag,
4691 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4692 tnapi->rx_rcb_ptr,
4693 tnapi->prodring.rx_std_prod_idx,
4694 tnapi->prodring.rx_std_cons_idx,
4695 tnapi->prodring.rx_jmb_prod_idx,
4696 tnapi->prodring.rx_jmb_cons_idx);
4697 }
4698}
4699
Michael Chandf3e6542006-05-26 17:48:07 -07004700/* This is called whenever we suspect that the system chipset is re-
4701 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4702 * is bogus tx completions. We try to recover by setting the
4703 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4704 * in the workqueue.
4705 */
4706static void tg3_tx_recover(struct tg3 *tp)
4707{
Joe Perches63c3a662011-04-26 08:12:10 +00004708 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07004709 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4710
Matt Carlson5129c3a2010-04-05 10:19:23 +00004711 netdev_warn(tp->dev,
4712 "The system may be re-ordering memory-mapped I/O "
4713 "cycles to the network device, attempting to recover. "
4714 "Please report the problem to the driver maintainer "
4715 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004716
4717 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00004718 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07004719 spin_unlock(&tp->lock);
4720}
4721
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004722static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004723{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004724 /* Tell compiler to fetch tx indices from memory. */
4725 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004726 return tnapi->tx_pending -
4727 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004728}
4729
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730/* Tigon3 never reports partial packet sends. So we do not
4731 * need special logic to handle SKBs that have not had all
4732 * of their frags sent yet, like SunGEM does.
4733 */
Matt Carlson17375d22009-08-28 14:02:18 +00004734static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735{
Matt Carlson17375d22009-08-28 14:02:18 +00004736 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004737 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004738 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004739 struct netdev_queue *txq;
4740 int index = tnapi - tp->napi;
4741
Joe Perches63c3a662011-04-26 08:12:10 +00004742 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004743 index--;
4744
4745 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
4747 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004748 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004750 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
Michael Chandf3e6542006-05-26 17:48:07 -07004752 if (unlikely(skb == NULL)) {
4753 tg3_tx_recover(tp);
4754 return;
4755 }
4756
Alexander Duyckf4188d82009-12-02 16:48:38 +00004757 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004758 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004759 skb_headlen(skb),
4760 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
4762 ri->skb = NULL;
4763
4764 sw_idx = NEXT_TX(sw_idx);
4765
4766 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004767 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004768 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4769 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004770
4771 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004772 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004773 skb_shinfo(skb)->frags[i].size,
4774 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 sw_idx = NEXT_TX(sw_idx);
4776 }
4777
David S. Millerf47c11e2005-06-24 20:18:35 -07004778 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004779
4780 if (unlikely(tx_bug)) {
4781 tg3_tx_recover(tp);
4782 return;
4783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 }
4785
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004786 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
Michael Chan1b2a7202006-08-07 21:46:02 -07004788 /* Need to make the tx_cons update visible to tg3_start_xmit()
4789 * before checking for netif_queue_stopped(). Without the
4790 * memory barrier, there is a small possibility that tg3_start_xmit()
4791 * will miss it and cause the queue to be stopped forever.
4792 */
4793 smp_mb();
4794
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004795 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004796 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004797 __netif_tx_lock(txq, smp_processor_id());
4798 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004799 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004800 netif_tx_wake_queue(txq);
4801 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803}
4804
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004805static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4806{
4807 if (!ri->skb)
4808 return;
4809
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004810 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004811 map_sz, PCI_DMA_FROMDEVICE);
4812 dev_kfree_skb_any(ri->skb);
4813 ri->skb = NULL;
4814}
4815
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816/* Returns size of skb allocated or < 0 on error.
4817 *
4818 * We only need to fill in the address because the other members
4819 * of the RX descriptor are invariant, see tg3_init_rings.
4820 *
4821 * Note the purposeful assymetry of cpu vs. chip accesses. For
4822 * posting buffers we only dirty the first cache line of the RX
4823 * descriptor (containing the address). Whereas for the RX status
4824 * buffers the cpu only reads the last cacheline of the RX descriptor
4825 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4826 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004827static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004828 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829{
4830 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004831 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 struct sk_buff *skb;
4833 dma_addr_t mapping;
4834 int skb_size, dest_idx;
4835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 switch (opaque_key) {
4837 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004838 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004839 desc = &tpr->rx_std[dest_idx];
4840 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004841 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 break;
4843
4844 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004845 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004846 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004847 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004848 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 break;
4850
4851 default:
4852 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
4855 /* Do not overwrite any of the map or rp information
4856 * until we are sure we can commit to a new buffer.
4857 *
4858 * Callers depend upon this behavior and assume that
4859 * we leave everything unchanged if we fail.
4860 */
Matt Carlson287be122009-08-28 13:58:46 +00004861 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 if (skb == NULL)
4863 return -ENOMEM;
4864
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 skb_reserve(skb, tp->rx_offset);
4866
Matt Carlson287be122009-08-28 13:58:46 +00004867 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004869 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4870 dev_kfree_skb(skb);
4871 return -EIO;
4872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
4874 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004875 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 desc->addr_hi = ((u64)mapping >> 32);
4878 desc->addr_lo = ((u64)mapping & 0xffffffff);
4879
4880 return skb_size;
4881}
4882
4883/* We only need to move over in the address because the other
4884 * members of the RX descriptor are invariant. See notes above
4885 * tg3_alloc_rx_skb for full details.
4886 */
Matt Carlsona3896162009-11-13 13:03:44 +00004887static void tg3_recycle_rx(struct tg3_napi *tnapi,
4888 struct tg3_rx_prodring_set *dpr,
4889 u32 opaque_key, int src_idx,
4890 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891{
Matt Carlson17375d22009-08-28 14:02:18 +00004892 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4894 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004895 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004896 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
4898 switch (opaque_key) {
4899 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004900 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004901 dest_desc = &dpr->rx_std[dest_idx];
4902 dest_map = &dpr->rx_std_buffers[dest_idx];
4903 src_desc = &spr->rx_std[src_idx];
4904 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 break;
4906
4907 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004908 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004909 dest_desc = &dpr->rx_jmb[dest_idx].std;
4910 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4911 src_desc = &spr->rx_jmb[src_idx].std;
4912 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 break;
4914
4915 default:
4916 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918
4919 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004920 dma_unmap_addr_set(dest_map, mapping,
4921 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 dest_desc->addr_hi = src_desc->addr_hi;
4923 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004924
4925 /* Ensure that the update to the skb happens after the physical
4926 * addresses have been transferred to the new BD location.
4927 */
4928 smp_wmb();
4929
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 src_map->skb = NULL;
4931}
4932
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933/* The RX ring scheme is composed of multiple rings which post fresh
4934 * buffers to the chip, and one special ring the chip uses to report
4935 * status back to the host.
4936 *
4937 * The special ring reports the status of received packets to the
4938 * host. The chip does not write into the original descriptor the
4939 * RX buffer was obtained from. The chip simply takes the original
4940 * descriptor as provided by the host, updates the status and length
4941 * field, then writes this into the next status ring entry.
4942 *
4943 * Each ring the host uses to post buffers to the chip is described
4944 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4945 * it is first placed into the on-chip ram. When the packet's length
4946 * is known, it walks down the TG3_BDINFO entries to select the ring.
4947 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4948 * which is within the range of the new packet's length is chosen.
4949 *
4950 * The "separate ring for rx status" scheme may sound queer, but it makes
4951 * sense from a cache coherency perspective. If only the host writes
4952 * to the buffer post rings, and only the chip writes to the rx status
4953 * rings, then cache lines never move beyond shared-modified state.
4954 * If both the host and chip were to write into the same ring, cache line
4955 * eviction could occur since both entities want it in an exclusive state.
4956 */
Matt Carlson17375d22009-08-28 14:02:18 +00004957static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958{
Matt Carlson17375d22009-08-28 14:02:18 +00004959 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004960 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004961 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004962 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004963 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004965 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004967 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 /*
4969 * We need to order the read of hw_idx and the read of
4970 * the opaque cookie.
4971 */
4972 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 work_mask = 0;
4974 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004975 std_prod_idx = tpr->rx_std_prod_idx;
4976 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004978 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004979 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 unsigned int len;
4981 struct sk_buff *skb;
4982 dma_addr_t dma_addr;
4983 u32 opaque_key, desc_idx, *post_ptr;
4984
4985 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4986 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4987 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004988 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004989 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004990 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004991 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004992 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004994 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004995 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004996 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004997 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004998 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000
5001 work_mask |= opaque_key;
5002
5003 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5004 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5005 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005006 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 desc_idx, *post_ptr);
5008 drop_it_no_recycle:
5009 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005010 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 goto next_pkt;
5012 }
5013
Matt Carlsonad829262008-11-21 17:16:16 -08005014 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5015 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
Matt Carlsond2757fc2010-04-12 06:58:27 +00005017 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 int skb_size;
5019
Matt Carlson86b21e52009-11-13 13:03:45 +00005020 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005021 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 if (skb_size < 0)
5023 goto drop_it;
5024
Matt Carlson287be122009-08-28 13:58:46 +00005025 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 PCI_DMA_FROMDEVICE);
5027
Matt Carlson61e800c2010-02-17 15:16:54 +00005028 /* Ensure that the update to the skb happens
5029 * after the usage of the old DMA mapping.
5030 */
5031 smp_wmb();
5032
5033 ri->skb = NULL;
5034
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 skb_put(skb, len);
5036 } else {
5037 struct sk_buff *copy_skb;
5038
Matt Carlsona3896162009-11-13 13:03:44 +00005039 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 desc_idx, *post_ptr);
5041
Matt Carlsonbf933c82011-01-25 15:58:49 +00005042 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00005043 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 if (copy_skb == NULL)
5045 goto drop_it_no_recycle;
5046
Matt Carlsonbf933c82011-01-25 15:58:49 +00005047 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 skb_put(copy_skb, len);
5049 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03005050 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
5052
5053 /* We'll reuse the original ring buffer. */
5054 skb = copy_skb;
5055 }
5056
Michał Mirosławdc668912011-04-07 03:35:07 +00005057 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5059 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5060 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5061 skb->ip_summed = CHECKSUM_UNNECESSARY;
5062 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005063 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064
5065 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005066
5067 if (len > (tp->dev->mtu + ETH_HLEN) &&
5068 skb->protocol != htons(ETH_P_8021Q)) {
5069 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005070 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005071 }
5072
Matt Carlson9dc7a112010-04-12 06:58:28 +00005073 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005074 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5075 __vlan_hwaccel_put_tag(skb,
5076 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005077
Matt Carlsonbf933c82011-01-25 15:58:49 +00005078 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 received++;
5081 budget--;
5082
5083next_pkt:
5084 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005085
5086 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005087 tpr->rx_std_prod_idx = std_prod_idx &
5088 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005089 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5090 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005091 work_mask &= ~RXD_OPAQUE_RING_STD;
5092 rx_std_posted = 0;
5093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005095 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005096 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005097
5098 /* Refresh hw_idx to see if there is new work */
5099 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005100 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005101 rmb();
5102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 }
5104
5105 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005106 tnapi->rx_rcb_ptr = sw_idx;
5107 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
5109 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005110 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005111 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005112 tpr->rx_std_prod_idx = std_prod_idx &
5113 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005114 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5115 tpr->rx_std_prod_idx);
5116 }
5117 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005118 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5119 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005120 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5121 tpr->rx_jmb_prod_idx);
5122 }
5123 mmiowb();
5124 } else if (work_mask) {
5125 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5126 * updated before the producer indices can be updated.
5127 */
5128 smp_wmb();
5129
Matt Carlson2c49a442010-09-30 10:34:35 +00005130 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5131 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005132
Matt Carlsone4af1af2010-02-12 14:47:05 +00005133 if (tnapi != &tp->napi[1])
5134 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
5137 return received;
5138}
5139
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005140static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005143 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005144 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5145
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 if (sblk->status & SD_STATUS_LINK_CHG) {
5147 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005148 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005149 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005150 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005151 tw32_f(MAC_STATUS,
5152 (MAC_STATUS_SYNC_CHANGED |
5153 MAC_STATUS_CFG_CHANGED |
5154 MAC_STATUS_MI_COMPLETION |
5155 MAC_STATUS_LNKSTATE_CHANGED));
5156 udelay(40);
5157 } else
5158 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005159 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 }
5161 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005162}
5163
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005164static int tg3_rx_prodring_xfer(struct tg3 *tp,
5165 struct tg3_rx_prodring_set *dpr,
5166 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005167{
5168 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005169 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005170
5171 while (1) {
5172 src_prod_idx = spr->rx_std_prod_idx;
5173
5174 /* Make sure updates to the rx_std_buffers[] entries and the
5175 * standard producer index are seen in the correct order.
5176 */
5177 smp_rmb();
5178
5179 if (spr->rx_std_cons_idx == src_prod_idx)
5180 break;
5181
5182 if (spr->rx_std_cons_idx < src_prod_idx)
5183 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5184 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005185 cpycnt = tp->rx_std_ring_mask + 1 -
5186 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005187
Matt Carlson2c49a442010-09-30 10:34:35 +00005188 cpycnt = min(cpycnt,
5189 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005190
5191 si = spr->rx_std_cons_idx;
5192 di = dpr->rx_std_prod_idx;
5193
Matt Carlsone92967b2010-02-12 14:47:06 +00005194 for (i = di; i < di + cpycnt; i++) {
5195 if (dpr->rx_std_buffers[i].skb) {
5196 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005197 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005198 break;
5199 }
5200 }
5201
5202 if (!cpycnt)
5203 break;
5204
5205 /* Ensure that updates to the rx_std_buffers ring and the
5206 * shadowed hardware producer ring from tg3_recycle_skb() are
5207 * ordered correctly WRT the skb check above.
5208 */
5209 smp_rmb();
5210
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005211 memcpy(&dpr->rx_std_buffers[di],
5212 &spr->rx_std_buffers[si],
5213 cpycnt * sizeof(struct ring_info));
5214
5215 for (i = 0; i < cpycnt; i++, di++, si++) {
5216 struct tg3_rx_buffer_desc *sbd, *dbd;
5217 sbd = &spr->rx_std[si];
5218 dbd = &dpr->rx_std[di];
5219 dbd->addr_hi = sbd->addr_hi;
5220 dbd->addr_lo = sbd->addr_lo;
5221 }
5222
Matt Carlson2c49a442010-09-30 10:34:35 +00005223 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5224 tp->rx_std_ring_mask;
5225 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5226 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005227 }
5228
5229 while (1) {
5230 src_prod_idx = spr->rx_jmb_prod_idx;
5231
5232 /* Make sure updates to the rx_jmb_buffers[] entries and
5233 * the jumbo producer index are seen in the correct order.
5234 */
5235 smp_rmb();
5236
5237 if (spr->rx_jmb_cons_idx == src_prod_idx)
5238 break;
5239
5240 if (spr->rx_jmb_cons_idx < src_prod_idx)
5241 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5242 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005243 cpycnt = tp->rx_jmb_ring_mask + 1 -
5244 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005245
5246 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005247 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005248
5249 si = spr->rx_jmb_cons_idx;
5250 di = dpr->rx_jmb_prod_idx;
5251
Matt Carlsone92967b2010-02-12 14:47:06 +00005252 for (i = di; i < di + cpycnt; i++) {
5253 if (dpr->rx_jmb_buffers[i].skb) {
5254 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005255 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005256 break;
5257 }
5258 }
5259
5260 if (!cpycnt)
5261 break;
5262
5263 /* Ensure that updates to the rx_jmb_buffers ring and the
5264 * shadowed hardware producer ring from tg3_recycle_skb() are
5265 * ordered correctly WRT the skb check above.
5266 */
5267 smp_rmb();
5268
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005269 memcpy(&dpr->rx_jmb_buffers[di],
5270 &spr->rx_jmb_buffers[si],
5271 cpycnt * sizeof(struct ring_info));
5272
5273 for (i = 0; i < cpycnt; i++, di++, si++) {
5274 struct tg3_rx_buffer_desc *sbd, *dbd;
5275 sbd = &spr->rx_jmb[si].std;
5276 dbd = &dpr->rx_jmb[di].std;
5277 dbd->addr_hi = sbd->addr_hi;
5278 dbd->addr_lo = sbd->addr_lo;
5279 }
5280
Matt Carlson2c49a442010-09-30 10:34:35 +00005281 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5282 tp->rx_jmb_ring_mask;
5283 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5284 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005285 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005286
5287 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005288}
5289
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005290static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5291{
5292 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293
5294 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005295 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005296 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005297 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005298 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 }
5300
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 /* run RX thread, within the bounds set by NAPI.
5302 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005303 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005305 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005306 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
Joe Perches63c3a662011-04-26 08:12:10 +00005308 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005309 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005310 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005311 u32 std_prod_idx = dpr->rx_std_prod_idx;
5312 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005313
Matt Carlsone4af1af2010-02-12 14:47:05 +00005314 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005315 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005316 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005317
5318 wmb();
5319
Matt Carlsone4af1af2010-02-12 14:47:05 +00005320 if (std_prod_idx != dpr->rx_std_prod_idx)
5321 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5322 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005323
Matt Carlsone4af1af2010-02-12 14:47:05 +00005324 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5325 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5326 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005327
5328 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005329
5330 if (err)
5331 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005332 }
5333
David S. Miller6f535762007-10-11 18:08:29 -07005334 return work_done;
5335}
David S. Millerf7383c22005-05-18 22:50:53 -07005336
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005337static int tg3_poll_msix(struct napi_struct *napi, int budget)
5338{
5339 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5340 struct tg3 *tp = tnapi->tp;
5341 int work_done = 0;
5342 struct tg3_hw_status *sblk = tnapi->hw_status;
5343
5344 while (1) {
5345 work_done = tg3_poll_work(tnapi, work_done, budget);
5346
Joe Perches63c3a662011-04-26 08:12:10 +00005347 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005348 goto tx_recovery;
5349
5350 if (unlikely(work_done >= budget))
5351 break;
5352
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005353 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005354 * to tell the hw how much work has been processed,
5355 * so we must read it before checking for more work.
5356 */
5357 tnapi->last_tag = sblk->status_tag;
5358 tnapi->last_irq_tag = tnapi->last_tag;
5359 rmb();
5360
5361 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005362 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5363 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005364 napi_complete(napi);
5365 /* Reenable interrupts. */
5366 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5367 mmiowb();
5368 break;
5369 }
5370 }
5371
5372 return work_done;
5373
5374tx_recovery:
5375 /* work_done is guaranteed to be less than budget. */
5376 napi_complete(napi);
5377 schedule_work(&tp->reset_task);
5378 return work_done;
5379}
5380
Matt Carlsone64de4e2011-04-13 11:05:05 +00005381static void tg3_process_error(struct tg3 *tp)
5382{
5383 u32 val;
5384 bool real_error = false;
5385
Joe Perches63c3a662011-04-26 08:12:10 +00005386 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005387 return;
5388
5389 /* Check Flow Attention register */
5390 val = tr32(HOSTCC_FLOW_ATTN);
5391 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5392 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5393 real_error = true;
5394 }
5395
5396 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5397 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5398 real_error = true;
5399 }
5400
5401 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5402 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5403 real_error = true;
5404 }
5405
5406 if (!real_error)
5407 return;
5408
5409 tg3_dump_state(tp);
5410
Joe Perches63c3a662011-04-26 08:12:10 +00005411 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005412 schedule_work(&tp->reset_task);
5413}
5414
David S. Miller6f535762007-10-11 18:08:29 -07005415static int tg3_poll(struct napi_struct *napi, int budget)
5416{
Matt Carlson8ef04422009-08-28 14:01:37 +00005417 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5418 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005419 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005420 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005421
5422 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005423 if (sblk->status & SD_STATUS_ERROR)
5424 tg3_process_error(tp);
5425
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005426 tg3_poll_link(tp);
5427
Matt Carlson17375d22009-08-28 14:02:18 +00005428 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005429
Joe Perches63c3a662011-04-26 08:12:10 +00005430 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07005431 goto tx_recovery;
5432
5433 if (unlikely(work_done >= budget))
5434 break;
5435
Joe Perches63c3a662011-04-26 08:12:10 +00005436 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00005437 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005438 * to tell the hw how much work has been processed,
5439 * so we must read it before checking for more work.
5440 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005441 tnapi->last_tag = sblk->status_tag;
5442 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005443 rmb();
5444 } else
5445 sblk->status &= ~SD_STATUS_UPDATED;
5446
Matt Carlson17375d22009-08-28 14:02:18 +00005447 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005448 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005449 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005450 break;
5451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 }
5453
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005454 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005455
5456tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005457 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005458 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005459 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005460 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461}
5462
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005463static void tg3_napi_disable(struct tg3 *tp)
5464{
5465 int i;
5466
5467 for (i = tp->irq_cnt - 1; i >= 0; i--)
5468 napi_disable(&tp->napi[i].napi);
5469}
5470
5471static void tg3_napi_enable(struct tg3 *tp)
5472{
5473 int i;
5474
5475 for (i = 0; i < tp->irq_cnt; i++)
5476 napi_enable(&tp->napi[i].napi);
5477}
5478
5479static void tg3_napi_init(struct tg3 *tp)
5480{
5481 int i;
5482
5483 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5484 for (i = 1; i < tp->irq_cnt; i++)
5485 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5486}
5487
5488static void tg3_napi_fini(struct tg3 *tp)
5489{
5490 int i;
5491
5492 for (i = 0; i < tp->irq_cnt; i++)
5493 netif_napi_del(&tp->napi[i].napi);
5494}
5495
5496static inline void tg3_netif_stop(struct tg3 *tp)
5497{
5498 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5499 tg3_napi_disable(tp);
5500 netif_tx_disable(tp->dev);
5501}
5502
5503static inline void tg3_netif_start(struct tg3 *tp)
5504{
5505 /* NOTE: unconditional netif_tx_wake_all_queues is only
5506 * appropriate so long as all callers are assured to
5507 * have free tx slots (such as after tg3_init_hw)
5508 */
5509 netif_tx_wake_all_queues(tp->dev);
5510
5511 tg3_napi_enable(tp);
5512 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5513 tg3_enable_ints(tp);
5514}
5515
David S. Millerf47c11e2005-06-24 20:18:35 -07005516static void tg3_irq_quiesce(struct tg3 *tp)
5517{
Matt Carlson4f125f42009-09-01 12:55:02 +00005518 int i;
5519
David S. Millerf47c11e2005-06-24 20:18:35 -07005520 BUG_ON(tp->irq_sync);
5521
5522 tp->irq_sync = 1;
5523 smp_mb();
5524
Matt Carlson4f125f42009-09-01 12:55:02 +00005525 for (i = 0; i < tp->irq_cnt; i++)
5526 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005527}
5528
David S. Millerf47c11e2005-06-24 20:18:35 -07005529/* Fully shutdown all tg3 driver activity elsewhere in the system.
5530 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5531 * with as well. Most of the time, this is not necessary except when
5532 * shutting down the device.
5533 */
5534static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5535{
Michael Chan46966542007-07-11 19:47:19 -07005536 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005537 if (irq_sync)
5538 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005539}
5540
5541static inline void tg3_full_unlock(struct tg3 *tp)
5542{
David S. Millerf47c11e2005-06-24 20:18:35 -07005543 spin_unlock_bh(&tp->lock);
5544}
5545
Michael Chanfcfa0a32006-03-20 22:28:41 -08005546/* One-shot MSI handler - Chip automatically disables interrupt
5547 * after sending MSI so driver doesn't have to do it.
5548 */
David Howells7d12e782006-10-05 14:55:46 +01005549static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005550{
Matt Carlson09943a12009-08-28 14:01:57 +00005551 struct tg3_napi *tnapi = dev_id;
5552 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005553
Matt Carlson898a56f2009-08-28 14:02:40 +00005554 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005555 if (tnapi->rx_rcb)
5556 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005557
5558 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005559 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005560
5561 return IRQ_HANDLED;
5562}
5563
Michael Chan88b06bc2005-04-21 17:13:25 -07005564/* MSI ISR - No need to check for interrupt sharing and no need to
5565 * flush status block and interrupt mailbox. PCI ordering rules
5566 * guarantee that MSI will arrive after the status block.
5567 */
David Howells7d12e782006-10-05 14:55:46 +01005568static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07005569{
Matt Carlson09943a12009-08-28 14:01:57 +00005570 struct tg3_napi *tnapi = dev_id;
5571 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07005572
Matt Carlson898a56f2009-08-28 14:02:40 +00005573 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005574 if (tnapi->rx_rcb)
5575 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07005576 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005577 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07005578 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005579 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07005580 * NIC to stop sending us irqs, engaging "in-intr-handler"
5581 * event coalescing.
5582 */
5583 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005584 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005585 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005586
Michael Chan88b06bc2005-04-21 17:13:25 -07005587 return IRQ_RETVAL(1);
5588}
5589
David Howells7d12e782006-10-05 14:55:46 +01005590static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591{
Matt Carlson09943a12009-08-28 14:01:57 +00005592 struct tg3_napi *tnapi = dev_id;
5593 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005594 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 unsigned int handled = 1;
5596
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 /* In INTx mode, it is possible for the interrupt to arrive at
5598 * the CPU before the status block posted prior to the interrupt.
5599 * Reading the PCI State register will confirm whether the
5600 * interrupt is ours and will flush the status block.
5601 */
Michael Chand18edcb2007-03-24 20:57:11 -07005602 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00005603 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005604 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5605 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005606 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005607 }
Michael Chand18edcb2007-03-24 20:57:11 -07005608 }
5609
5610 /*
5611 * Writing any value to intr-mbox-0 clears PCI INTA# and
5612 * chip-internal interrupt pending events.
5613 * Writing non-zero to intr-mbox-0 additional tells the
5614 * NIC to stop sending us irqs, engaging "in-intr-handler"
5615 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005616 *
5617 * Flush the mailbox to de-assert the IRQ immediately to prevent
5618 * spurious interrupts. The flush impacts performance but
5619 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005620 */
Michael Chanc04cb342007-05-07 00:26:15 -07005621 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005622 if (tg3_irq_sync(tp))
5623 goto out;
5624 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005625 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005626 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005627 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005628 } else {
5629 /* No work, shared interrupt perhaps? re-enable
5630 * interrupts, and flush that PCI write
5631 */
5632 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5633 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005634 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005635out:
David S. Millerfac9b832005-05-18 22:46:34 -07005636 return IRQ_RETVAL(handled);
5637}
5638
David Howells7d12e782006-10-05 14:55:46 +01005639static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005640{
Matt Carlson09943a12009-08-28 14:01:57 +00005641 struct tg3_napi *tnapi = dev_id;
5642 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005643 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005644 unsigned int handled = 1;
5645
David S. Millerfac9b832005-05-18 22:46:34 -07005646 /* In INTx mode, it is possible for the interrupt to arrive at
5647 * the CPU before the status block posted prior to the interrupt.
5648 * Reading the PCI State register will confirm whether the
5649 * interrupt is ours and will flush the status block.
5650 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005651 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00005652 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005653 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5654 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005655 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 }
Michael Chand18edcb2007-03-24 20:57:11 -07005657 }
5658
5659 /*
5660 * writing any value to intr-mbox-0 clears PCI INTA# and
5661 * chip-internal interrupt pending events.
5662 * writing non-zero to intr-mbox-0 additional tells the
5663 * NIC to stop sending us irqs, engaging "in-intr-handler"
5664 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005665 *
5666 * Flush the mailbox to de-assert the IRQ immediately to prevent
5667 * spurious interrupts. The flush impacts performance but
5668 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005669 */
Michael Chanc04cb342007-05-07 00:26:15 -07005670 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005671
5672 /*
5673 * In a shared interrupt configuration, sometimes other devices'
5674 * interrupts will scream. We record the current status tag here
5675 * so that the above check can report that the screaming interrupts
5676 * are unhandled. Eventually they will be silenced.
5677 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005678 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005679
Michael Chand18edcb2007-03-24 20:57:11 -07005680 if (tg3_irq_sync(tp))
5681 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005682
Matt Carlson72334482009-08-28 14:03:01 +00005683 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005684
Matt Carlson09943a12009-08-28 14:01:57 +00005685 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005686
David S. Millerf47c11e2005-06-24 20:18:35 -07005687out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 return IRQ_RETVAL(handled);
5689}
5690
Michael Chan79381092005-04-21 17:13:59 -07005691/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005692static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005693{
Matt Carlson09943a12009-08-28 14:01:57 +00005694 struct tg3_napi *tnapi = dev_id;
5695 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005696 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005697
Michael Chanf9804dd2005-09-27 12:13:10 -07005698 if ((sblk->status & SD_STATUS_UPDATED) ||
5699 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005700 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005701 return IRQ_RETVAL(1);
5702 }
5703 return IRQ_RETVAL(0);
5704}
5705
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005706static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005707static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708
Michael Chanb9ec6c12006-07-25 16:37:27 -07005709/* Restart hardware after configuration changes, self-test, etc.
5710 * Invoked with tp->lock held.
5711 */
5712static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005713 __releases(tp->lock)
5714 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005715{
5716 int err;
5717
5718 err = tg3_init_hw(tp, reset_phy);
5719 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005720 netdev_err(tp->dev,
5721 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005722 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5723 tg3_full_unlock(tp);
5724 del_timer_sync(&tp->timer);
5725 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005726 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005727 dev_close(tp->dev);
5728 tg3_full_lock(tp, 0);
5729 }
5730 return err;
5731}
5732
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733#ifdef CONFIG_NET_POLL_CONTROLLER
5734static void tg3_poll_controller(struct net_device *dev)
5735{
Matt Carlson4f125f42009-09-01 12:55:02 +00005736 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07005737 struct tg3 *tp = netdev_priv(dev);
5738
Matt Carlson4f125f42009-09-01 12:55:02 +00005739 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005740 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741}
5742#endif
5743
David Howellsc4028952006-11-22 14:57:56 +00005744static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745{
David Howellsc4028952006-11-22 14:57:56 +00005746 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005747 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 unsigned int restart_timer;
5749
Michael Chan7faa0062006-02-02 17:29:28 -08005750 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005751
5752 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005753 tg3_full_unlock(tp);
5754 return;
5755 }
5756
5757 tg3_full_unlock(tp);
5758
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005759 tg3_phy_stop(tp);
5760
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 tg3_netif_stop(tp);
5762
David S. Millerf47c11e2005-06-24 20:18:35 -07005763 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764
Joe Perches63c3a662011-04-26 08:12:10 +00005765 restart_timer = tg3_flag(tp, RESTART_TIMER);
5766 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767
Joe Perches63c3a662011-04-26 08:12:10 +00005768 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07005769 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5770 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00005771 tg3_flag_set(tp, MBOX_WRITE_REORDER);
5772 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005773 }
5774
Michael Chan944d9802005-05-29 14:57:48 -07005775 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005776 err = tg3_init_hw(tp, 1);
5777 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005778 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779
5780 tg3_netif_start(tp);
5781
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 if (restart_timer)
5783 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005784
Michael Chanb9ec6c12006-07-25 16:37:27 -07005785out:
Michael Chan7faa0062006-02-02 17:29:28 -08005786 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005787
5788 if (!err)
5789 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790}
5791
5792static void tg3_tx_timeout(struct net_device *dev)
5793{
5794 struct tg3 *tp = netdev_priv(dev);
5795
Michael Chanb0408752007-02-13 12:18:30 -08005796 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005797 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005798 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
5801 schedule_work(&tp->reset_task);
5802}
5803
Michael Chanc58ec932005-09-17 00:46:27 -07005804/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5805static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5806{
5807 u32 base = (u32) mapping & 0xffffffff;
5808
Eric Dumazet807540b2010-09-23 05:40:09 +00005809 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005810}
5811
Michael Chan72f2afb2006-03-06 19:28:35 -08005812/* Test for DMA addresses > 40-bit */
5813static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5814 int len)
5815{
5816#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00005817 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00005818 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005819 return 0;
5820#else
5821 return 0;
5822#endif
5823}
5824
Matt Carlson2ffcc982011-05-19 12:12:44 +00005825static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
5826 dma_addr_t mapping, int len, u32 flags,
5827 u32 mss_and_is_end)
5828{
5829 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
5830 int is_end = (mss_and_is_end & 0x1);
5831 u32 mss = (mss_and_is_end >> 1);
5832 u32 vlan_tag = 0;
5833
5834 if (is_end)
5835 flags |= TXD_FLAG_END;
5836 if (flags & TXD_FLAG_VLAN) {
5837 vlan_tag = flags >> 16;
5838 flags &= 0xffff;
5839 }
5840 vlan_tag |= (mss << TXD_MSS_SHIFT);
5841
5842 txd->addr_hi = ((u64) mapping >> 32);
5843 txd->addr_lo = ((u64) mapping & 0xffffffff);
5844 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5845 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5846}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847
Matt Carlson432aa7e2011-05-19 12:12:45 +00005848static void tg3_skb_error_unmap(struct tg3_napi *tnapi,
5849 struct sk_buff *skb, int last)
5850{
5851 int i;
5852 u32 entry = tnapi->tx_prod;
5853 struct ring_info *txb = &tnapi->tx_buffers[entry];
5854
5855 pci_unmap_single(tnapi->tp->pdev,
5856 dma_unmap_addr(txb, mapping),
5857 skb_headlen(skb),
5858 PCI_DMA_TODEVICE);
Matt Carlson9a2e0fb2011-06-02 13:01:39 +00005859 for (i = 0; i < last; i++) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005860 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5861
5862 entry = NEXT_TX(entry);
5863 txb = &tnapi->tx_buffers[entry];
5864
5865 pci_unmap_page(tnapi->tp->pdev,
5866 dma_unmap_addr(txb, mapping),
5867 frag->size, PCI_DMA_TODEVICE);
5868 }
5869}
5870
Michael Chan72f2afb2006-03-06 19:28:35 -08005871/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005872static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
Matt Carlson432aa7e2011-05-19 12:12:45 +00005873 struct sk_buff *skb,
5874 u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005876 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005877 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005878 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005879 u32 entry = tnapi->tx_prod;
5880 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881
Matt Carlson41588ba2008-04-19 18:12:33 -07005882 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5883 new_skb = skb_copy(skb, GFP_ATOMIC);
5884 else {
5885 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5886
5887 new_skb = skb_copy_expand(skb,
5888 skb_headroom(skb) + more_headroom,
5889 skb_tailroom(skb), GFP_ATOMIC);
5890 }
5891
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005893 ret = -1;
5894 } else {
5895 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005896 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5897 PCI_DMA_TODEVICE);
5898 /* Make sure the mapping succeeded */
5899 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5900 ret = -1;
5901 dev_kfree_skb(new_skb);
David S. Miller90079ce2008-09-11 04:52:51 -07005902
Michael Chanc58ec932005-09-17 00:46:27 -07005903 /* Make sure new skb does not cross any 4G boundaries.
5904 * Drop the packet if it does.
5905 */
Matt Carlsoneb69d562011-06-13 13:38:57 +00005906 } else if (tg3_4g_overflow_test(new_addr, new_skb->len)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005907 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5908 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005909 ret = -1;
5910 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07005911 } else {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005912 tnapi->tx_buffers[entry].skb = new_skb;
5913 dma_unmap_addr_set(&tnapi->tx_buffers[entry],
5914 mapping, new_addr);
5915
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005916 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005917 base_flags, 1 | (mss << 1));
Michael Chanc58ec932005-09-17 00:46:27 -07005918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 }
5920
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 dev_kfree_skb(skb);
5922
Michael Chanc58ec932005-09-17 00:46:27 -07005923 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924}
5925
Matt Carlson2ffcc982011-05-19 12:12:44 +00005926static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005927
5928/* Use GSO to workaround a rare TSO bug that may be triggered when the
5929 * TSO header is greater than 80 bytes.
5930 */
5931static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5932{
5933 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005934 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005935
5936 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005937 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005938 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005939
5940 /* netif_tx_stop_queue() must be done before checking
5941 * checking tx index in tg3_tx_avail() below, because in
5942 * tg3_tx(), we update tx index before checking for
5943 * netif_tx_queue_stopped().
5944 */
5945 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005946 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005947 return NETDEV_TX_BUSY;
5948
5949 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005950 }
5951
5952 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005953 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005954 goto tg3_tso_bug_end;
5955
5956 do {
5957 nskb = segs;
5958 segs = segs->next;
5959 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00005960 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005961 } while (segs);
5962
5963tg3_tso_bug_end:
5964 dev_kfree_skb(skb);
5965
5966 return NETDEV_TX_OK;
5967}
Michael Chan52c0fd82006-06-29 20:15:54 -07005968
Michael Chan5a6f3072006-03-20 22:28:05 -08005969/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00005970 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005971 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00005972static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005973{
5974 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005975 u32 len, entry, base_flags, mss;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005976 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005977 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005978 struct tg3_napi *tnapi;
5979 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005980 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005981
Matt Carlson24f4efd2009-11-13 13:03:35 +00005982 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5983 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00005984 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005985 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986
Michael Chan00b70502006-06-17 21:58:45 -07005987 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005988 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005989 * interrupt. Furthermore, IRQ processing runs lockless so we have
5990 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005992 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005993 if (!netif_tx_queue_stopped(txq)) {
5994 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005995
5996 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005997 netdev_err(dev,
5998 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 return NETDEV_TX_BUSY;
6001 }
6002
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006003 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006005 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006007
Matt Carlsonbe98da62010-07-11 09:31:46 +00006008 mss = skb_shinfo(skb)->gso_size;
6009 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006010 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006011 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
6013 if (skb_header_cloned(skb) &&
6014 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
6015 dev_kfree_skb(skb);
6016 goto out_unlock;
6017 }
6018
Matt Carlson34195c32010-07-11 09:31:42 +00006019 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006020 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021
Matt Carlson02e96082010-09-15 08:59:59 +00006022 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006023 hdr_len = skb_headlen(skb) - ETH_HLEN;
6024 } else {
6025 u32 ip_tcp_len;
6026
6027 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6028 hdr_len = ip_tcp_len + tcp_opt_len;
6029
6030 iph->check = 0;
6031 iph->tot_len = htons(mss + hdr_len);
6032 }
6033
Michael Chan52c0fd82006-06-29 20:15:54 -07006034 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006035 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006036 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006037
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6039 TXD_FLAG_CPU_POST_DMA);
6040
Joe Perches63c3a662011-04-26 08:12:10 +00006041 if (tg3_flag(tp, HW_TSO_1) ||
6042 tg3_flag(tp, HW_TSO_2) ||
6043 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006044 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006046 } else
6047 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6048 iph->daddr, 0,
6049 IPPROTO_TCP,
6050 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
Joe Perches63c3a662011-04-26 08:12:10 +00006052 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006053 mss |= (hdr_len & 0xc) << 12;
6054 if (hdr_len & 0x10)
6055 base_flags |= 0x00000010;
6056 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006057 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006058 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006059 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006060 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006061 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 int tsflags;
6063
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006064 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 mss |= (tsflags << 11);
6066 }
6067 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006068 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 int tsflags;
6070
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006071 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 base_flags |= tsflags << 12;
6073 }
6074 }
6075 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006076
Jesse Grosseab6d182010-10-20 13:56:03 +00006077 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 base_flags |= (TXD_FLAG_VLAN |
6079 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
Joe Perches63c3a662011-04-26 08:12:10 +00006081 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006082 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006083 base_flags |= TXD_FLAG_JMB_PKT;
6084
Alexander Duyckf4188d82009-12-02 16:48:38 +00006085 len = skb_headlen(skb);
6086
6087 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6088 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006089 dev_kfree_skb(skb);
6090 goto out_unlock;
6091 }
6092
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006093 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006094 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095
6096 would_hit_hwbug = 0;
6097
Joe Perches63c3a662011-04-26 08:12:10 +00006098 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006099 would_hit_hwbug = 1;
6100
Matt Carlsoneb69d562011-06-13 13:38:57 +00006101 if (tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006102 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006103
Matt Carlsondaf9a552011-06-13 13:38:56 +00006104 if (tg3_40bit_overflow_test(tp, mapping, len))
Matt Carlson0e1406d2009-11-02 12:33:33 +00006105 would_hit_hwbug = 1;
6106
Joe Perches63c3a662011-04-26 08:12:10 +00006107 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006108 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006110 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6112
6113 entry = NEXT_TX(entry);
6114
6115 /* Now loop through additional data fragments, and queue them. */
6116 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 last = skb_shinfo(skb)->nr_frags - 1;
6118 for (i = 0; i <= last; i++) {
6119 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6120
6121 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006122 mapping = pci_map_page(tp->pdev,
6123 frag->page,
6124 frag->page_offset,
6125 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006127 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006128 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006129 mapping);
6130 if (pci_dma_mapping_error(tp->pdev, mapping))
6131 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132
Joe Perches63c3a662011-04-26 08:12:10 +00006133 if (tg3_flag(tp, SHORT_DMA_BUG) &&
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006134 len <= 8)
6135 would_hit_hwbug = 1;
6136
Matt Carlsoneb69d562011-06-13 13:38:57 +00006137 if (tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006138 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
Matt Carlsondaf9a552011-06-13 13:38:56 +00006140 if (tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006141 would_hit_hwbug = 1;
6142
Joe Perches63c3a662011-04-26 08:12:10 +00006143 if (tg3_flag(tp, HW_TSO_1) ||
6144 tg3_flag(tp, HW_TSO_2) ||
6145 tg3_flag(tp, HW_TSO_3))
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006146 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 base_flags, (i == last)|(mss << 1));
6148 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006149 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 base_flags, (i == last));
6151
6152 entry = NEXT_TX(entry);
6153 }
6154 }
6155
6156 if (would_hit_hwbug) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00006157 tg3_skb_error_unmap(tnapi, skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158
6159 /* If the workaround fails due to memory/mapping
6160 * failure, silently drop this packet.
6161 */
Matt Carlson432aa7e2011-05-19 12:12:45 +00006162 if (tigon3_dma_hwbug_workaround(tnapi, skb, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 goto out_unlock;
6164
Matt Carlson432aa7e2011-05-19 12:12:45 +00006165 entry = NEXT_TX(tnapi->tx_prod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 }
6167
Richard Cochrand515b452011-06-19 03:31:41 +00006168 skb_tx_timestamp(skb);
6169
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006171 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006173 tnapi->tx_prod = entry;
6174 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006175 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006176
6177 /* netif_tx_stop_queue() must be done before checking
6178 * checking tx index in tg3_tx_avail() below, because in
6179 * tg3_tx(), we update tx index before checking for
6180 * netif_tx_queue_stopped().
6181 */
6182 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006183 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006184 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186
6187out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006188 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189
6190 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006191
6192dma_error:
Matt Carlson432aa7e2011-05-19 12:12:45 +00006193 tg3_skb_error_unmap(tnapi, skb, i);
Alexander Duyckf4188d82009-12-02 16:48:38 +00006194 dev_kfree_skb(skb);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006195 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006196 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197}
6198
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006199static void tg3_set_loopback(struct net_device *dev, u32 features)
6200{
6201 struct tg3 *tp = netdev_priv(dev);
6202
6203 if (features & NETIF_F_LOOPBACK) {
6204 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6205 return;
6206
6207 /*
6208 * Clear MAC_MODE_HALF_DUPLEX or you won't get packets back in
6209 * loopback mode if Half-Duplex mode was negotiated earlier.
6210 */
6211 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
6212
6213 /* Enable internal MAC loopback mode */
6214 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6215 spin_lock_bh(&tp->lock);
6216 tw32(MAC_MODE, tp->mac_mode);
6217 netif_carrier_on(tp->dev);
6218 spin_unlock_bh(&tp->lock);
6219 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6220 } else {
6221 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6222 return;
6223
6224 /* Disable internal MAC loopback mode */
6225 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6226 spin_lock_bh(&tp->lock);
6227 tw32(MAC_MODE, tp->mac_mode);
6228 /* Force link status check */
6229 tg3_setup_phy(tp, 1);
6230 spin_unlock_bh(&tp->lock);
6231 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6232 }
6233}
6234
Michał Mirosławdc668912011-04-07 03:35:07 +00006235static u32 tg3_fix_features(struct net_device *dev, u32 features)
6236{
6237 struct tg3 *tp = netdev_priv(dev);
6238
Joe Perches63c3a662011-04-26 08:12:10 +00006239 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006240 features &= ~NETIF_F_ALL_TSO;
6241
6242 return features;
6243}
6244
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006245static int tg3_set_features(struct net_device *dev, u32 features)
6246{
6247 u32 changed = dev->features ^ features;
6248
6249 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6250 tg3_set_loopback(dev, features);
6251
6252 return 0;
6253}
6254
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6256 int new_mtu)
6257{
6258 dev->mtu = new_mtu;
6259
Michael Chanef7f5ec2005-07-25 12:32:25 -07006260 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006261 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006262 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006263 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006264 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006265 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006266 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006267 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006268 if (tg3_flag(tp, 5780_CLASS)) {
6269 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00006270 netdev_update_features(dev);
6271 }
Joe Perches63c3a662011-04-26 08:12:10 +00006272 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274}
6275
6276static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6277{
6278 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006279 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280
6281 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6282 return -EINVAL;
6283
6284 if (!netif_running(dev)) {
6285 /* We'll just catch it later when the
6286 * device is up'd.
6287 */
6288 tg3_set_mtu(dev, tp, new_mtu);
6289 return 0;
6290 }
6291
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006292 tg3_phy_stop(tp);
6293
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006295
6296 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
Michael Chan944d9802005-05-29 14:57:48 -07006298 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299
6300 tg3_set_mtu(dev, tp, new_mtu);
6301
Michael Chanb9ec6c12006-07-25 16:37:27 -07006302 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303
Michael Chanb9ec6c12006-07-25 16:37:27 -07006304 if (!err)
6305 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306
David S. Millerf47c11e2005-06-24 20:18:35 -07006307 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006309 if (!err)
6310 tg3_phy_start(tp);
6311
Michael Chanb9ec6c12006-07-25 16:37:27 -07006312 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313}
6314
Matt Carlson21f581a2009-08-28 14:00:25 +00006315static void tg3_rx_prodring_free(struct tg3 *tp,
6316 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 int i;
6319
Matt Carlson8fea32b2010-09-15 08:59:58 +00006320 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006321 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006322 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006323 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6324 tp->rx_pkt_map_sz);
6325
Joe Perches63c3a662011-04-26 08:12:10 +00006326 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006327 for (i = tpr->rx_jmb_cons_idx;
6328 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006329 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006330 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6331 TG3_RX_JMB_MAP_SZ);
6332 }
6333 }
6334
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006335 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337
Matt Carlson2c49a442010-09-30 10:34:35 +00006338 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006339 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6340 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341
Joe Perches63c3a662011-04-26 08:12:10 +00006342 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006343 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006344 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6345 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 }
6347}
6348
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006349/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 *
6351 * The chip has been shut down and the driver detached from
6352 * the networking, so no interrupts or new tx packets will
6353 * end up in the driver. tp->{tx,}lock are held and thus
6354 * we may not sleep.
6355 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006356static int tg3_rx_prodring_alloc(struct tg3 *tp,
6357 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358{
Matt Carlson287be122009-08-28 13:58:46 +00006359 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006361 tpr->rx_std_cons_idx = 0;
6362 tpr->rx_std_prod_idx = 0;
6363 tpr->rx_jmb_cons_idx = 0;
6364 tpr->rx_jmb_prod_idx = 0;
6365
Matt Carlson8fea32b2010-09-15 08:59:58 +00006366 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006367 memset(&tpr->rx_std_buffers[0], 0,
6368 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006369 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006370 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006371 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006372 goto done;
6373 }
6374
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006376 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
Matt Carlson287be122009-08-28 13:58:46 +00006378 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00006379 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006380 tp->dev->mtu > ETH_DATA_LEN)
6381 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6382 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006383
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 /* Initialize invariants of the rings, we only set this
6385 * stuff once. This works because the card does not
6386 * write into the rx buffer posting rings.
6387 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006388 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 struct tg3_rx_buffer_desc *rxd;
6390
Matt Carlson21f581a2009-08-28 14:00:25 +00006391 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006392 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6394 rxd->opaque = (RXD_OPAQUE_RING_STD |
6395 (i << RXD_OPAQUE_INDEX_SHIFT));
6396 }
6397
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006398 /* Now allocate fresh SKBs for each rx ring. */
6399 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006400 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006401 netdev_warn(tp->dev,
6402 "Using a smaller RX standard ring. Only "
6403 "%d out of %d buffers were allocated "
6404 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006405 if (i == 0)
6406 goto initfail;
6407 tp->rx_pending = i;
6408 break;
6409 }
6410 }
6411
Joe Perches63c3a662011-04-26 08:12:10 +00006412 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006413 goto done;
6414
Matt Carlson2c49a442010-09-30 10:34:35 +00006415 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006416
Joe Perches63c3a662011-04-26 08:12:10 +00006417 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00006418 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419
Matt Carlson2c49a442010-09-30 10:34:35 +00006420 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006421 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422
Matt Carlson0d86df82010-02-17 15:17:00 +00006423 rxd = &tpr->rx_jmb[i].std;
6424 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6425 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6426 RXD_FLAG_JUMBO;
6427 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6428 (i << RXD_OPAQUE_INDEX_SHIFT));
6429 }
6430
6431 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6432 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006433 netdev_warn(tp->dev,
6434 "Using a smaller RX jumbo ring. Only %d "
6435 "out of %d buffers were allocated "
6436 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006437 if (i == 0)
6438 goto initfail;
6439 tp->rx_jumbo_pending = i;
6440 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 }
6442 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006443
6444done:
Michael Chan32d8c572006-07-25 16:38:29 -07006445 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006446
6447initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006448 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006449 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450}
6451
Matt Carlson21f581a2009-08-28 14:00:25 +00006452static void tg3_rx_prodring_fini(struct tg3 *tp,
6453 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454{
Matt Carlson21f581a2009-08-28 14:00:25 +00006455 kfree(tpr->rx_std_buffers);
6456 tpr->rx_std_buffers = NULL;
6457 kfree(tpr->rx_jmb_buffers);
6458 tpr->rx_jmb_buffers = NULL;
6459 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006460 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6461 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006462 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006464 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006465 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6466 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006467 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006469}
6470
Matt Carlson21f581a2009-08-28 14:00:25 +00006471static int tg3_rx_prodring_init(struct tg3 *tp,
6472 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006473{
Matt Carlson2c49a442010-09-30 10:34:35 +00006474 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6475 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006476 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006477 return -ENOMEM;
6478
Matt Carlson4bae65c2010-11-24 08:31:52 +00006479 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6480 TG3_RX_STD_RING_BYTES(tp),
6481 &tpr->rx_std_mapping,
6482 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006483 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006484 goto err_out;
6485
Joe Perches63c3a662011-04-26 08:12:10 +00006486 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006487 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006488 GFP_KERNEL);
6489 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006490 goto err_out;
6491
Matt Carlson4bae65c2010-11-24 08:31:52 +00006492 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6493 TG3_RX_JMB_RING_BYTES(tp),
6494 &tpr->rx_jmb_mapping,
6495 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006496 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006497 goto err_out;
6498 }
6499
6500 return 0;
6501
6502err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006503 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006504 return -ENOMEM;
6505}
6506
6507/* Free up pending packets in all rx/tx rings.
6508 *
6509 * The chip has been shut down and the driver detached from
6510 * the networking, so no interrupts or new tx packets will
6511 * end up in the driver. tp->{tx,}lock is not held and we are not
6512 * in an interrupt context and thus may sleep.
6513 */
6514static void tg3_free_rings(struct tg3 *tp)
6515{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006516 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006517
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006518 for (j = 0; j < tp->irq_cnt; j++) {
6519 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006520
Matt Carlson8fea32b2010-09-15 08:59:58 +00006521 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006522
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006523 if (!tnapi->tx_buffers)
6524 continue;
6525
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006526 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006527 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006528 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006529 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006530
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006531 txp = &tnapi->tx_buffers[i];
6532 skb = txp->skb;
6533
6534 if (skb == NULL) {
6535 i++;
6536 continue;
6537 }
6538
Alexander Duyckf4188d82009-12-02 16:48:38 +00006539 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006540 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006541 skb_headlen(skb),
6542 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006543 txp->skb = NULL;
6544
Alexander Duyckf4188d82009-12-02 16:48:38 +00006545 i++;
6546
6547 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6548 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6549 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006550 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006551 skb_shinfo(skb)->frags[k].size,
6552 PCI_DMA_TODEVICE);
6553 i++;
6554 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006555
6556 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006557 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006558 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006559}
6560
6561/* Initialize tx/rx rings for packet processing.
6562 *
6563 * The chip has been shut down and the driver detached from
6564 * the networking, so no interrupts or new tx packets will
6565 * end up in the driver. tp->{tx,}lock are held and thus
6566 * we may not sleep.
6567 */
6568static int tg3_init_rings(struct tg3 *tp)
6569{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006570 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006571
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006572 /* Free up all the SKBs. */
6573 tg3_free_rings(tp);
6574
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006575 for (i = 0; i < tp->irq_cnt; i++) {
6576 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006577
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006578 tnapi->last_tag = 0;
6579 tnapi->last_irq_tag = 0;
6580 tnapi->hw_status->status = 0;
6581 tnapi->hw_status->status_tag = 0;
6582 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6583
6584 tnapi->tx_prod = 0;
6585 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006586 if (tnapi->tx_ring)
6587 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006588
6589 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006590 if (tnapi->rx_rcb)
6591 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006592
Matt Carlson8fea32b2010-09-15 08:59:58 +00006593 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006594 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006595 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006596 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006597 }
Matt Carlson72334482009-08-28 14:03:01 +00006598
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006599 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006600}
6601
6602/*
6603 * Must not be invoked with interrupt sources disabled and
6604 * the hardware shutdown down.
6605 */
6606static void tg3_free_consistent(struct tg3 *tp)
6607{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006608 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006609
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006610 for (i = 0; i < tp->irq_cnt; i++) {
6611 struct tg3_napi *tnapi = &tp->napi[i];
6612
6613 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006614 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006615 tnapi->tx_ring, tnapi->tx_desc_mapping);
6616 tnapi->tx_ring = NULL;
6617 }
6618
6619 kfree(tnapi->tx_buffers);
6620 tnapi->tx_buffers = NULL;
6621
6622 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006623 dma_free_coherent(&tp->pdev->dev,
6624 TG3_RX_RCB_RING_BYTES(tp),
6625 tnapi->rx_rcb,
6626 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006627 tnapi->rx_rcb = NULL;
6628 }
6629
Matt Carlson8fea32b2010-09-15 08:59:58 +00006630 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6631
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006632 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006633 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6634 tnapi->hw_status,
6635 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006636 tnapi->hw_status = NULL;
6637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006639
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006641 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6642 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 tp->hw_stats = NULL;
6644 }
6645}
6646
6647/*
6648 * Must not be invoked with interrupt sources disabled and
6649 * the hardware shutdown down. Can sleep.
6650 */
6651static int tg3_alloc_consistent(struct tg3 *tp)
6652{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006653 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006654
Matt Carlson4bae65c2010-11-24 08:31:52 +00006655 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6656 sizeof(struct tg3_hw_stats),
6657 &tp->stats_mapping,
6658 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 if (!tp->hw_stats)
6660 goto err_out;
6661
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6663
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006664 for (i = 0; i < tp->irq_cnt; i++) {
6665 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006666 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006667
Matt Carlson4bae65c2010-11-24 08:31:52 +00006668 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6669 TG3_HW_STATUS_SIZE,
6670 &tnapi->status_mapping,
6671 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006672 if (!tnapi->hw_status)
6673 goto err_out;
6674
6675 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006676 sblk = tnapi->hw_status;
6677
Matt Carlson8fea32b2010-09-15 08:59:58 +00006678 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6679 goto err_out;
6680
Matt Carlson19cfaec2009-12-03 08:36:20 +00006681 /* If multivector TSS is enabled, vector 0 does not handle
6682 * tx interrupts. Don't allocate any resources for it.
6683 */
Joe Perches63c3a662011-04-26 08:12:10 +00006684 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
6685 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00006686 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6687 TG3_TX_RING_SIZE,
6688 GFP_KERNEL);
6689 if (!tnapi->tx_buffers)
6690 goto err_out;
6691
Matt Carlson4bae65c2010-11-24 08:31:52 +00006692 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6693 TG3_TX_RING_BYTES,
6694 &tnapi->tx_desc_mapping,
6695 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006696 if (!tnapi->tx_ring)
6697 goto err_out;
6698 }
6699
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006700 /*
6701 * When RSS is enabled, the status block format changes
6702 * slightly. The "rx_jumbo_consumer", "reserved",
6703 * and "rx_mini_consumer" members get mapped to the
6704 * other three rx return ring producer indexes.
6705 */
6706 switch (i) {
6707 default:
6708 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6709 break;
6710 case 2:
6711 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6712 break;
6713 case 3:
6714 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6715 break;
6716 case 4:
6717 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6718 break;
6719 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006720
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006721 /*
6722 * If multivector RSS is enabled, vector 0 does not handle
6723 * rx or tx interrupts. Don't allocate any resources for it.
6724 */
Joe Perches63c3a662011-04-26 08:12:10 +00006725 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006726 continue;
6727
Matt Carlson4bae65c2010-11-24 08:31:52 +00006728 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6729 TG3_RX_RCB_RING_BYTES(tp),
6730 &tnapi->rx_rcb_mapping,
6731 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006732 if (!tnapi->rx_rcb)
6733 goto err_out;
6734
6735 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006736 }
6737
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738 return 0;
6739
6740err_out:
6741 tg3_free_consistent(tp);
6742 return -ENOMEM;
6743}
6744
6745#define MAX_WAIT_CNT 1000
6746
6747/* To stop a block, clear the enable bit and poll till it
6748 * clears. tp->lock is held.
6749 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006750static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751{
6752 unsigned int i;
6753 u32 val;
6754
Joe Perches63c3a662011-04-26 08:12:10 +00006755 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 switch (ofs) {
6757 case RCVLSC_MODE:
6758 case DMAC_MODE:
6759 case MBFREE_MODE:
6760 case BUFMGR_MODE:
6761 case MEMARB_MODE:
6762 /* We can't enable/disable these bits of the
6763 * 5705/5750, just say success.
6764 */
6765 return 0;
6766
6767 default:
6768 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 }
6771
6772 val = tr32(ofs);
6773 val &= ~enable_bit;
6774 tw32_f(ofs, val);
6775
6776 for (i = 0; i < MAX_WAIT_CNT; i++) {
6777 udelay(100);
6778 val = tr32(ofs);
6779 if ((val & enable_bit) == 0)
6780 break;
6781 }
6782
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006783 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006784 dev_err(&tp->pdev->dev,
6785 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6786 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 return -ENODEV;
6788 }
6789
6790 return 0;
6791}
6792
6793/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006794static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795{
6796 int i, err;
6797
6798 tg3_disable_ints(tp);
6799
6800 tp->rx_mode &= ~RX_MODE_ENABLE;
6801 tw32_f(MAC_RX_MODE, tp->rx_mode);
6802 udelay(10);
6803
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006804 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6805 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6806 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6807 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6808 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6809 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006811 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6812 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6813 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6814 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6815 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6816 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6817 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818
6819 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6820 tw32_f(MAC_MODE, tp->mac_mode);
6821 udelay(40);
6822
6823 tp->tx_mode &= ~TX_MODE_ENABLE;
6824 tw32_f(MAC_TX_MODE, tp->tx_mode);
6825
6826 for (i = 0; i < MAX_WAIT_CNT; i++) {
6827 udelay(100);
6828 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6829 break;
6830 }
6831 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006832 dev_err(&tp->pdev->dev,
6833 "%s timed out, TX_MODE_ENABLE will not clear "
6834 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006835 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 }
6837
Michael Chane6de8ad2005-05-05 14:42:41 -07006838 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006839 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6840 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841
6842 tw32(FTQ_RESET, 0xffffffff);
6843 tw32(FTQ_RESET, 0x00000000);
6844
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006845 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6846 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006848 for (i = 0; i < tp->irq_cnt; i++) {
6849 struct tg3_napi *tnapi = &tp->napi[i];
6850 if (tnapi->hw_status)
6851 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 if (tp->hw_stats)
6854 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6855
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856 return err;
6857}
6858
Matt Carlson0d3031d2007-10-10 18:02:43 -07006859static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6860{
6861 int i;
6862 u32 apedata;
6863
Matt Carlsondc6d0742010-09-15 08:59:55 +00006864 /* NCSI does not support APE events */
Joe Perches63c3a662011-04-26 08:12:10 +00006865 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsondc6d0742010-09-15 08:59:55 +00006866 return;
6867
Matt Carlson0d3031d2007-10-10 18:02:43 -07006868 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6869 if (apedata != APE_SEG_SIG_MAGIC)
6870 return;
6871
6872 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006873 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006874 return;
6875
6876 /* Wait for up to 1 millisecond for APE to service previous event. */
6877 for (i = 0; i < 10; i++) {
6878 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6879 return;
6880
6881 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6882
6883 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6884 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6885 event | APE_EVENT_STATUS_EVENT_PENDING);
6886
6887 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6888
6889 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6890 break;
6891
6892 udelay(100);
6893 }
6894
6895 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6896 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6897}
6898
6899static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6900{
6901 u32 event;
6902 u32 apedata;
6903
Joe Perches63c3a662011-04-26 08:12:10 +00006904 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006905 return;
6906
6907 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006908 case RESET_KIND_INIT:
6909 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6910 APE_HOST_SEG_SIG_MAGIC);
6911 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6912 APE_HOST_SEG_LEN_MAGIC);
6913 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6914 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6915 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006916 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006917 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6918 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006919 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6920 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006921
Matt Carlson33f401a2010-04-05 10:19:27 +00006922 event = APE_EVENT_STATUS_STATE_START;
6923 break;
6924 case RESET_KIND_SHUTDOWN:
6925 /* With the interface we are currently using,
6926 * APE does not track driver state. Wiping
6927 * out the HOST SEGMENT SIGNATURE forces
6928 * the APE to assume OS absent status.
6929 */
6930 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006931
Matt Carlsondc6d0742010-09-15 08:59:55 +00006932 if (device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006933 tg3_flag(tp, WOL_ENABLE)) {
Matt Carlsondc6d0742010-09-15 08:59:55 +00006934 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6935 TG3_APE_HOST_WOL_SPEED_AUTO);
6936 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6937 } else
6938 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6939
6940 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6941
Matt Carlson33f401a2010-04-05 10:19:27 +00006942 event = APE_EVENT_STATUS_STATE_UNLOAD;
6943 break;
6944 case RESET_KIND_SUSPEND:
6945 event = APE_EVENT_STATUS_STATE_SUSPEND;
6946 break;
6947 default:
6948 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006949 }
6950
6951 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6952
6953 tg3_ape_send_event(tp, event);
6954}
6955
Michael Chane6af3012005-04-21 17:12:05 -07006956/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6958{
David S. Millerf49639e2006-06-09 11:58:36 -07006959 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6960 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961
Joe Perches63c3a662011-04-26 08:12:10 +00006962 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 switch (kind) {
6964 case RESET_KIND_INIT:
6965 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6966 DRV_STATE_START);
6967 break;
6968
6969 case RESET_KIND_SHUTDOWN:
6970 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6971 DRV_STATE_UNLOAD);
6972 break;
6973
6974 case RESET_KIND_SUSPEND:
6975 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6976 DRV_STATE_SUSPEND);
6977 break;
6978
6979 default:
6980 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006983
6984 if (kind == RESET_KIND_INIT ||
6985 kind == RESET_KIND_SUSPEND)
6986 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987}
6988
6989/* tp->lock is held. */
6990static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6991{
Joe Perches63c3a662011-04-26 08:12:10 +00006992 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 switch (kind) {
6994 case RESET_KIND_INIT:
6995 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6996 DRV_STATE_START_DONE);
6997 break;
6998
6999 case RESET_KIND_SHUTDOWN:
7000 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7001 DRV_STATE_UNLOAD_DONE);
7002 break;
7003
7004 default:
7005 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007008
7009 if (kind == RESET_KIND_SHUTDOWN)
7010 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011}
7012
7013/* tp->lock is held. */
7014static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
7015{
Joe Perches63c3a662011-04-26 08:12:10 +00007016 if (tg3_flag(tp, ENABLE_ASF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 switch (kind) {
7018 case RESET_KIND_INIT:
7019 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7020 DRV_STATE_START);
7021 break;
7022
7023 case RESET_KIND_SHUTDOWN:
7024 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7025 DRV_STATE_UNLOAD);
7026 break;
7027
7028 case RESET_KIND_SUSPEND:
7029 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7030 DRV_STATE_SUSPEND);
7031 break;
7032
7033 default:
7034 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 }
7037}
7038
Michael Chan7a6f4362006-09-27 16:03:31 -07007039static int tg3_poll_fw(struct tg3 *tp)
7040{
7041 int i;
7042 u32 val;
7043
Michael Chanb5d37722006-09-27 16:06:21 -07007044 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08007045 /* Wait up to 20ms for init done. */
7046 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07007047 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
7048 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08007049 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07007050 }
7051 return -ENODEV;
7052 }
7053
Michael Chan7a6f4362006-09-27 16:03:31 -07007054 /* Wait for firmware initialization to complete. */
7055 for (i = 0; i < 100000; i++) {
7056 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
7057 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
7058 break;
7059 udelay(10);
7060 }
7061
7062 /* Chip might not be fitted with firmware. Some Sun onboard
7063 * parts are configured like that. So don't signal the timeout
7064 * of the above loop as an error, but do report the lack of
7065 * running firmware once.
7066 */
Joe Perches63c3a662011-04-26 08:12:10 +00007067 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
7068 tg3_flag_set(tp, NO_FWARE_REPORTED);
Michael Chan7a6f4362006-09-27 16:03:31 -07007069
Joe Perches05dbe002010-02-17 19:44:19 +00007070 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07007071 }
7072
Matt Carlson6b10c162010-02-12 14:47:08 +00007073 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7074 /* The 57765 A0 needs a little more
7075 * time to do some important work.
7076 */
7077 mdelay(10);
7078 }
7079
Michael Chan7a6f4362006-09-27 16:03:31 -07007080 return 0;
7081}
7082
Michael Chanee6a99b2007-07-18 21:49:10 -07007083/* Save PCI command register before chip reset */
7084static void tg3_save_pci_state(struct tg3 *tp)
7085{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007086 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007087}
7088
7089/* Restore PCI state after chip reset */
7090static void tg3_restore_pci_state(struct tg3 *tp)
7091{
7092 u32 val;
7093
7094 /* Re-enable indirect register accesses. */
7095 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7096 tp->misc_host_ctrl);
7097
7098 /* Set MAX PCI retry to zero. */
7099 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7100 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007101 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007102 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007103 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007104 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007105 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00007106 PCISTATE_ALLOW_APE_SHMEM_WR |
7107 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007108 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7109
Matt Carlson8a6eac92007-10-21 16:17:55 -07007110 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007111
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007112 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007113 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007114 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007115 else {
7116 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7117 tp->pci_cacheline_sz);
7118 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7119 tp->pci_lat_timer);
7120 }
Michael Chan114342f2007-10-15 02:12:26 -07007121 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007122
Michael Chanee6a99b2007-07-18 21:49:10 -07007123 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007124 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007125 u16 pcix_cmd;
7126
7127 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7128 &pcix_cmd);
7129 pcix_cmd &= ~PCI_X_CMD_ERO;
7130 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7131 pcix_cmd);
7132 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007133
Joe Perches63c3a662011-04-26 08:12:10 +00007134 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007135
7136 /* Chip reset on 5780 will reset MSI enable bit,
7137 * so need to restore it.
7138 */
Joe Perches63c3a662011-04-26 08:12:10 +00007139 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007140 u16 ctrl;
7141
7142 pci_read_config_word(tp->pdev,
7143 tp->msi_cap + PCI_MSI_FLAGS,
7144 &ctrl);
7145 pci_write_config_word(tp->pdev,
7146 tp->msi_cap + PCI_MSI_FLAGS,
7147 ctrl | PCI_MSI_FLAGS_ENABLE);
7148 val = tr32(MSGINT_MODE);
7149 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7150 }
7151 }
7152}
7153
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154static void tg3_stop_fw(struct tg3 *);
7155
7156/* tp->lock is held. */
7157static int tg3_chip_reset(struct tg3 *tp)
7158{
7159 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007160 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007161 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162
David S. Millerf49639e2006-06-09 11:58:36 -07007163 tg3_nvram_lock(tp);
7164
Matt Carlson77b483f2008-08-15 14:07:24 -07007165 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7166
David S. Millerf49639e2006-06-09 11:58:36 -07007167 /* No matching tg3_nvram_unlock() after this because
7168 * chip reset below will undo the nvram lock.
7169 */
7170 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171
Michael Chanee6a99b2007-07-18 21:49:10 -07007172 /* GRC_MISC_CFG core clock reset will clear the memory
7173 * enable bit in PCI register 4 and the MSI enable bit
7174 * on some chips, so we save relevant registers here.
7175 */
7176 tg3_save_pci_state(tp);
7177
Michael Chand9ab5ad2006-03-20 22:27:35 -08007178 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007179 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007180 tw32(GRC_FASTBOOT_PC, 0);
7181
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182 /*
7183 * We must avoid the readl() that normally takes place.
7184 * It locks machines, causes machine checks, and other
7185 * fun things. So, temporarily disable the 5701
7186 * hardware workaround, while we do the reset.
7187 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007188 write_op = tp->write32;
7189 if (write_op == tg3_write_flush_reg32)
7190 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191
Michael Chand18edcb2007-03-24 20:57:11 -07007192 /* Prevent the irq handler from reading or writing PCI registers
7193 * during chip reset when the memory enable bit in the PCI command
7194 * register may be cleared. The chip does not generate interrupt
7195 * at this time, but the irq handler may still be called due to irq
7196 * sharing or irqpoll.
7197 */
Joe Perches63c3a662011-04-26 08:12:10 +00007198 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007199 for (i = 0; i < tp->irq_cnt; i++) {
7200 struct tg3_napi *tnapi = &tp->napi[i];
7201 if (tnapi->hw_status) {
7202 tnapi->hw_status->status = 0;
7203 tnapi->hw_status->status_tag = 0;
7204 }
7205 tnapi->last_tag = 0;
7206 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007207 }
Michael Chand18edcb2007-03-24 20:57:11 -07007208 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007209
7210 for (i = 0; i < tp->irq_cnt; i++)
7211 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007212
Matt Carlson255ca312009-08-25 10:07:27 +00007213 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7214 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7215 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7216 }
7217
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 /* do the reset */
7219 val = GRC_MISC_CFG_CORECLK_RESET;
7220
Joe Perches63c3a662011-04-26 08:12:10 +00007221 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007222 /* Force PCIe 1.0a mode */
7223 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007224 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007225 tr32(TG3_PCIE_PHY_TSTCTL) ==
7226 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7227 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7228
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7230 tw32(GRC_MISC_CFG, (1 << 29));
7231 val |= (1 << 29);
7232 }
7233 }
7234
Michael Chanb5d37722006-09-27 16:06:21 -07007235 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7236 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7237 tw32(GRC_VCPU_EXT_CTRL,
7238 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7239 }
7240
Matt Carlsonf37500d2010-08-02 11:25:59 +00007241 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007242 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007244
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245 tw32(GRC_MISC_CFG, val);
7246
Michael Chan1ee582d2005-08-09 20:16:46 -07007247 /* restore 5701 hardware bug workaround write method */
7248 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249
7250 /* Unfortunately, we have to delay before the PCI read back.
7251 * Some 575X chips even will not respond to a PCI cfg access
7252 * when the reset command is given to the chip.
7253 *
7254 * How do these hardware designers expect things to work
7255 * properly if the PCI write is posted for a long period
7256 * of time? It is always necessary to have some method by
7257 * which a register read back can occur to push the write
7258 * out which does the reset.
7259 *
7260 * For most tg3 variants the trick below was working.
7261 * Ho hum...
7262 */
7263 udelay(120);
7264
7265 /* Flush PCI posted writes. The normal MMIO registers
7266 * are inaccessible at this time so this is the only
7267 * way to make this reliably (actually, this is no longer
7268 * the case, see above). I tried to use indirect
7269 * register read/write but this upset some 5701 variants.
7270 */
7271 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7272
7273 udelay(120);
7274
Jon Mason708ebb32011-06-27 12:56:50 +00007275 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007276 u16 val16;
7277
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7279 int i;
7280 u32 cfg_val;
7281
7282 /* Wait for link training to complete. */
7283 for (i = 0; i < 5000; i++)
7284 udelay(100);
7285
7286 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7287 pci_write_config_dword(tp->pdev, 0xc4,
7288 cfg_val | (1 << 15));
7289 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007290
Matt Carlsone7126992009-08-25 10:08:16 +00007291 /* Clear the "no snoop" and "relaxed ordering" bits. */
7292 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007293 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007294 &val16);
7295 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7296 PCI_EXP_DEVCTL_NOSNOOP_EN);
7297 /*
7298 * Older PCIe devices only support the 128 byte
7299 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007300 */
Joe Perches63c3a662011-04-26 08:12:10 +00007301 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007302 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007303 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007304 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007305 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007306
Matt Carlsoncf790032010-11-24 08:31:48 +00007307 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007308
7309 /* Clear error status */
7310 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007311 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007312 PCI_EXP_DEVSTA_CED |
7313 PCI_EXP_DEVSTA_NFED |
7314 PCI_EXP_DEVSTA_FED |
7315 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316 }
7317
Michael Chanee6a99b2007-07-18 21:49:10 -07007318 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319
Joe Perches63c3a662011-04-26 08:12:10 +00007320 tg3_flag_clear(tp, CHIP_RESETTING);
7321 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007322
Michael Chanee6a99b2007-07-18 21:49:10 -07007323 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007324 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007325 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007326 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327
7328 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7329 tg3_stop_fw(tp);
7330 tw32(0x5000, 0x400);
7331 }
7332
7333 tw32(GRC_MODE, tp->grc_mode);
7334
7335 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007336 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337
7338 tw32(0xc4, val | (1 << 15));
7339 }
7340
7341 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7342 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7343 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7344 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7345 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7346 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7347 }
7348
Joe Perches63c3a662011-04-26 08:12:10 +00007349 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007350 tp->mac_mode = MAC_MODE_APE_TX_EN |
7351 MAC_MODE_APE_RX_EN |
7352 MAC_MODE_TDE_ENABLE;
7353
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007354 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007355 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7356 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007357 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007358 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7359 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007361 val = 0;
7362
7363 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364 udelay(40);
7365
Matt Carlson77b483f2008-08-15 14:07:24 -07007366 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7367
Michael Chan7a6f4362006-09-27 16:03:31 -07007368 err = tg3_poll_fw(tp);
7369 if (err)
7370 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371
Matt Carlson0a9140c2009-08-28 12:27:50 +00007372 tg3_mdio_start(tp);
7373
Joe Perches63c3a662011-04-26 08:12:10 +00007374 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007375 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7376 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007377 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007378 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379
7380 tw32(0x7c00, val | (1 << 25));
7381 }
7382
Matt Carlsond78b59f2011-04-05 14:22:46 +00007383 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7384 val = tr32(TG3_CPMU_CLCK_ORIDE);
7385 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7386 }
7387
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007389 tg3_flag_clear(tp, ENABLE_ASF);
7390 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7392 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7393 u32 nic_cfg;
7394
7395 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7396 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007397 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007398 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007399 if (tg3_flag(tp, 5750_PLUS))
7400 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401 }
7402 }
7403
7404 return 0;
7405}
7406
7407/* tp->lock is held. */
7408static void tg3_stop_fw(struct tg3 *tp)
7409{
Joe Perches63c3a662011-04-26 08:12:10 +00007410 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007411 /* Wait for RX cpu to ACK the previous event. */
7412 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413
7414 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007415
7416 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417
Matt Carlson7c5026a2008-05-02 16:49:29 -07007418 /* Wait for RX cpu to ACK this event. */
7419 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420 }
7421}
7422
7423/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007424static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425{
7426 int err;
7427
7428 tg3_stop_fw(tp);
7429
Michael Chan944d9802005-05-29 14:57:48 -07007430 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007432 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 err = tg3_chip_reset(tp);
7434
Matt Carlsondaba2a62009-04-20 06:58:52 +00007435 __tg3_set_mac_addr(tp, 0);
7436
Michael Chan944d9802005-05-29 14:57:48 -07007437 tg3_write_sig_legacy(tp, kind);
7438 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439
7440 if (err)
7441 return err;
7442
7443 return 0;
7444}
7445
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446#define RX_CPU_SCRATCH_BASE 0x30000
7447#define RX_CPU_SCRATCH_SIZE 0x04000
7448#define TX_CPU_SCRATCH_BASE 0x34000
7449#define TX_CPU_SCRATCH_SIZE 0x04000
7450
7451/* tp->lock is held. */
7452static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7453{
7454 int i;
7455
Joe Perches63c3a662011-04-26 08:12:10 +00007456 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457
Michael Chanb5d37722006-09-27 16:06:21 -07007458 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7459 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7460
7461 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7462 return 0;
7463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 if (offset == RX_CPU_BASE) {
7465 for (i = 0; i < 10000; i++) {
7466 tw32(offset + CPU_STATE, 0xffffffff);
7467 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7468 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7469 break;
7470 }
7471
7472 tw32(offset + CPU_STATE, 0xffffffff);
7473 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7474 udelay(10);
7475 } else {
7476 for (i = 0; i < 10000; i++) {
7477 tw32(offset + CPU_STATE, 0xffffffff);
7478 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7479 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7480 break;
7481 }
7482 }
7483
7484 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007485 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7486 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487 return -ENODEV;
7488 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007489
7490 /* Clear firmware's nvram arbitration. */
Joe Perches63c3a662011-04-26 08:12:10 +00007491 if (tg3_flag(tp, NVRAM))
Michael Chanec41c7d2006-01-17 02:40:55 -08007492 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 return 0;
7494}
7495
7496struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007497 unsigned int fw_base;
7498 unsigned int fw_len;
7499 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007500};
7501
7502/* tp->lock is held. */
7503static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7504 int cpu_scratch_size, struct fw_info *info)
7505{
Michael Chanec41c7d2006-01-17 02:40:55 -08007506 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507 void (*write_op)(struct tg3 *, u32, u32);
7508
Joe Perches63c3a662011-04-26 08:12:10 +00007509 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007510 netdev_err(tp->dev,
7511 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007512 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 return -EINVAL;
7514 }
7515
Joe Perches63c3a662011-04-26 08:12:10 +00007516 if (tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517 write_op = tg3_write_mem;
7518 else
7519 write_op = tg3_write_indirect_reg32;
7520
Michael Chan1b628152005-05-29 14:59:49 -07007521 /* It is possible that bootcode is still loading at this point.
7522 * Get the nvram lock first before halting the cpu.
7523 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007524 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007526 if (!lock_err)
7527 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 if (err)
7529 goto out;
7530
7531 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7532 write_op(tp, cpu_scratch_base + i, 0);
7533 tw32(cpu_base + CPU_STATE, 0xffffffff);
7534 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007535 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007537 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007539 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007540
7541 err = 0;
7542
7543out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 return err;
7545}
7546
7547/* tp->lock is held. */
7548static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7549{
7550 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007551 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552 int err, i;
7553
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007554 fw_data = (void *)tp->fw->data;
7555
7556 /* Firmware blob starts with version numbers, followed by
7557 start address and length. We are setting complete length.
7558 length = end_address_of_bss - start_address_of_text.
7559 Remainder is the blob to be loaded contiguously
7560 from start address. */
7561
7562 info.fw_base = be32_to_cpu(fw_data[1]);
7563 info.fw_len = tp->fw->size - 12;
7564 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565
7566 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7567 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7568 &info);
7569 if (err)
7570 return err;
7571
7572 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7573 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7574 &info);
7575 if (err)
7576 return err;
7577
7578 /* Now startup only the RX cpu. */
7579 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007580 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581
7582 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007583 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584 break;
7585 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7586 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007587 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588 udelay(1000);
7589 }
7590 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007591 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7592 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007593 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 return -ENODEV;
7595 }
7596 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7597 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7598
7599 return 0;
7600}
7601
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602/* tp->lock is held. */
7603static int tg3_load_tso_firmware(struct tg3 *tp)
7604{
7605 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007606 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7608 int err, i;
7609
Joe Perches63c3a662011-04-26 08:12:10 +00007610 if (tg3_flag(tp, HW_TSO_1) ||
7611 tg3_flag(tp, HW_TSO_2) ||
7612 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613 return 0;
7614
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007615 fw_data = (void *)tp->fw->data;
7616
7617 /* Firmware blob starts with version numbers, followed by
7618 start address and length. We are setting complete length.
7619 length = end_address_of_bss - start_address_of_text.
7620 Remainder is the blob to be loaded contiguously
7621 from start address. */
7622
7623 info.fw_base = be32_to_cpu(fw_data[1]);
7624 cpu_scratch_size = tp->fw_len;
7625 info.fw_len = tp->fw->size - 12;
7626 info.fw_data = &fw_data[3];
7627
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629 cpu_base = RX_CPU_BASE;
7630 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632 cpu_base = TX_CPU_BASE;
7633 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7634 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7635 }
7636
7637 err = tg3_load_firmware_cpu(tp, cpu_base,
7638 cpu_scratch_base, cpu_scratch_size,
7639 &info);
7640 if (err)
7641 return err;
7642
7643 /* Now startup the cpu. */
7644 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007645 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646
7647 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007648 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007649 break;
7650 tw32(cpu_base + CPU_STATE, 0xffffffff);
7651 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007652 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653 udelay(1000);
7654 }
7655 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007656 netdev_err(tp->dev,
7657 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007658 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007659 return -ENODEV;
7660 }
7661 tw32(cpu_base + CPU_STATE, 0xffffffff);
7662 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7663 return 0;
7664}
7665
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666
Linus Torvalds1da177e2005-04-16 15:20:36 -07007667static int tg3_set_mac_addr(struct net_device *dev, void *p)
7668{
7669 struct tg3 *tp = netdev_priv(dev);
7670 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007671 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672
Michael Chanf9804dd2005-09-27 12:13:10 -07007673 if (!is_valid_ether_addr(addr->sa_data))
7674 return -EINVAL;
7675
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7677
Michael Chane75f7c92006-03-20 21:33:26 -08007678 if (!netif_running(dev))
7679 return 0;
7680
Joe Perches63c3a662011-04-26 08:12:10 +00007681 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007682 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007683
Michael Chan986e0ae2007-05-05 12:10:20 -07007684 addr0_high = tr32(MAC_ADDR_0_HIGH);
7685 addr0_low = tr32(MAC_ADDR_0_LOW);
7686 addr1_high = tr32(MAC_ADDR_1_HIGH);
7687 addr1_low = tr32(MAC_ADDR_1_LOW);
7688
7689 /* Skip MAC addr 1 if ASF is using it. */
7690 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7691 !(addr1_high == 0 && addr1_low == 0))
7692 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007693 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007694 spin_lock_bh(&tp->lock);
7695 __tg3_set_mac_addr(tp, skip_mac_1);
7696 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697
Michael Chanb9ec6c12006-07-25 16:37:27 -07007698 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699}
7700
7701/* tp->lock is held. */
7702static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7703 dma_addr_t mapping, u32 maxlen_flags,
7704 u32 nic_addr)
7705{
7706 tg3_write_mem(tp,
7707 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7708 ((u64) mapping >> 32));
7709 tg3_write_mem(tp,
7710 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7711 ((u64) mapping & 0xffffffff));
7712 tg3_write_mem(tp,
7713 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7714 maxlen_flags);
7715
Joe Perches63c3a662011-04-26 08:12:10 +00007716 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 tg3_write_mem(tp,
7718 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7719 nic_addr);
7720}
7721
7722static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007723static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007724{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007725 int i;
7726
Joe Perches63c3a662011-04-26 08:12:10 +00007727 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007728 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7729 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7730 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007731 } else {
7732 tw32(HOSTCC_TXCOL_TICKS, 0);
7733 tw32(HOSTCC_TXMAX_FRAMES, 0);
7734 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007735 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007736
Joe Perches63c3a662011-04-26 08:12:10 +00007737 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007738 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7739 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7740 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7741 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007742 tw32(HOSTCC_RXCOL_TICKS, 0);
7743 tw32(HOSTCC_RXMAX_FRAMES, 0);
7744 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007745 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007746
Joe Perches63c3a662011-04-26 08:12:10 +00007747 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07007748 u32 val = ec->stats_block_coalesce_usecs;
7749
Matt Carlsonb6080e12009-09-01 13:12:00 +00007750 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7751 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7752
David S. Miller15f98502005-05-18 22:49:26 -07007753 if (!netif_carrier_ok(tp->dev))
7754 val = 0;
7755
7756 tw32(HOSTCC_STAT_COAL_TICKS, val);
7757 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007758
7759 for (i = 0; i < tp->irq_cnt - 1; i++) {
7760 u32 reg;
7761
7762 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7763 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007764 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7765 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007766 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7767 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007768
Joe Perches63c3a662011-04-26 08:12:10 +00007769 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007770 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7771 tw32(reg, ec->tx_coalesce_usecs);
7772 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7773 tw32(reg, ec->tx_max_coalesced_frames);
7774 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7775 tw32(reg, ec->tx_max_coalesced_frames_irq);
7776 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007777 }
7778
7779 for (; i < tp->irq_max - 1; i++) {
7780 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007781 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007782 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007783
Joe Perches63c3a662011-04-26 08:12:10 +00007784 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007785 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7786 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7787 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7788 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007789 }
David S. Miller15f98502005-05-18 22:49:26 -07007790}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791
7792/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007793static void tg3_rings_reset(struct tg3 *tp)
7794{
7795 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007796 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007797 struct tg3_napi *tnapi = &tp->napi[0];
7798
7799 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007800 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007801 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00007802 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00007803 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007804 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7805 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007806 else
7807 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7808
7809 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7810 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7811 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7812 BDINFO_FLAGS_DISABLED);
7813
7814
7815 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007816 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007817 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00007818 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007819 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007820 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7821 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007822 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7823 else
7824 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7825
7826 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7827 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7828 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7829 BDINFO_FLAGS_DISABLED);
7830
7831 /* Disable interrupts */
7832 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00007833 tp->napi[0].chk_msi_cnt = 0;
7834 tp->napi[0].last_rx_cons = 0;
7835 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007836
7837 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00007838 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007839 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007840 tp->napi[i].tx_prod = 0;
7841 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007842 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007843 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007844 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7845 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00007846 tp->napi[0].chk_msi_cnt = 0;
7847 tp->napi[i].last_rx_cons = 0;
7848 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007849 }
Joe Perches63c3a662011-04-26 08:12:10 +00007850 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007851 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007852 } else {
7853 tp->napi[0].tx_prod = 0;
7854 tp->napi[0].tx_cons = 0;
7855 tw32_mailbox(tp->napi[0].prodmbox, 0);
7856 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7857 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007858
7859 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00007860 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00007861 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7862 for (i = 0; i < 16; i++)
7863 tw32_tx_mbox(mbox + i * 8, 0);
7864 }
7865
7866 txrcb = NIC_SRAM_SEND_RCB;
7867 rxrcb = NIC_SRAM_RCV_RET_RCB;
7868
7869 /* Clear status block in ram. */
7870 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7871
7872 /* Set status block DMA address */
7873 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7874 ((u64) tnapi->status_mapping >> 32));
7875 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7876 ((u64) tnapi->status_mapping & 0xffffffff));
7877
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007878 if (tnapi->tx_ring) {
7879 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7880 (TG3_TX_RING_SIZE <<
7881 BDINFO_FLAGS_MAXLEN_SHIFT),
7882 NIC_SRAM_TX_BUFFER_DESC);
7883 txrcb += TG3_BDINFO_SIZE;
7884 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007885
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007886 if (tnapi->rx_rcb) {
7887 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007888 (tp->rx_ret_ring_mask + 1) <<
7889 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007890 rxrcb += TG3_BDINFO_SIZE;
7891 }
7892
7893 stblk = HOSTCC_STATBLCK_RING1;
7894
7895 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7896 u64 mapping = (u64)tnapi->status_mapping;
7897 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7898 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7899
7900 /* Clear status block in ram. */
7901 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7902
Matt Carlson19cfaec2009-12-03 08:36:20 +00007903 if (tnapi->tx_ring) {
7904 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7905 (TG3_TX_RING_SIZE <<
7906 BDINFO_FLAGS_MAXLEN_SHIFT),
7907 NIC_SRAM_TX_BUFFER_DESC);
7908 txrcb += TG3_BDINFO_SIZE;
7909 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007910
7911 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007912 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007913 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7914
7915 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007916 rxrcb += TG3_BDINFO_SIZE;
7917 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007918}
7919
Matt Carlsoneb07a942011-04-20 07:57:36 +00007920static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
7921{
7922 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
7923
Joe Perches63c3a662011-04-26 08:12:10 +00007924 if (!tg3_flag(tp, 5750_PLUS) ||
7925 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00007926 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
7927 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7928 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
7929 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7930 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
7931 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
7932 else
7933 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
7934
7935 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
7936 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
7937
7938 val = min(nic_rep_thresh, host_rep_thresh);
7939 tw32(RCVBDI_STD_THRESH, val);
7940
Joe Perches63c3a662011-04-26 08:12:10 +00007941 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007942 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
7943
Joe Perches63c3a662011-04-26 08:12:10 +00007944 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007945 return;
7946
Joe Perches63c3a662011-04-26 08:12:10 +00007947 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007948 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
7949 else
7950 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
7951
7952 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
7953
7954 val = min(bdcache_maxcnt / 2, host_rep_thresh);
7955 tw32(RCVBDI_JUMBO_THRESH, val);
7956
Joe Perches63c3a662011-04-26 08:12:10 +00007957 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007958 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
7959}
7960
Matt Carlson2d31eca2009-09-01 12:53:31 +00007961/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007962static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963{
7964 u32 val, rdmac_mode;
7965 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007966 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007967
7968 tg3_disable_ints(tp);
7969
7970 tg3_stop_fw(tp);
7971
7972 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7973
Joe Perches63c3a662011-04-26 08:12:10 +00007974 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07007975 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007976
Matt Carlson699c0192010-12-06 08:28:51 +00007977 /* Enable MAC control of LPI */
7978 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7979 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7980 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7981 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7982
7983 tw32_f(TG3_CPMU_EEE_CTRL,
7984 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7985
Matt Carlsona386b902010-12-06 08:28:53 +00007986 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7987 TG3_CPMU_EEEMD_LPI_IN_TX |
7988 TG3_CPMU_EEEMD_LPI_IN_RX |
7989 TG3_CPMU_EEEMD_EEE_ENABLE;
7990
7991 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
7992 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
7993
Joe Perches63c3a662011-04-26 08:12:10 +00007994 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00007995 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
7996
7997 tw32_f(TG3_CPMU_EEE_MODE, val);
7998
7999 tw32_f(TG3_CPMU_EEE_DBTMR1,
8000 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8001 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8002
8003 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008004 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008005 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008006 }
8007
Matt Carlson603f1172010-02-12 14:47:10 +00008008 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008009 tg3_phy_reset(tp);
8010
Linus Torvalds1da177e2005-04-16 15:20:36 -07008011 err = tg3_chip_reset(tp);
8012 if (err)
8013 return err;
8014
8015 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8016
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008017 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008018 val = tr32(TG3_CPMU_CTRL);
8019 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8020 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008021
8022 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8023 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8024 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8025 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8026
8027 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8028 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8029 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8030 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8031
8032 val = tr32(TG3_CPMU_HST_ACC);
8033 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8034 val |= CPMU_HST_ACC_MACCLK_6_25;
8035 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008036 }
8037
Matt Carlson33466d92009-04-20 06:57:41 +00008038 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8039 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8040 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8041 PCIE_PWR_MGMT_L1_THRESH_4MS;
8042 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008043
8044 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8045 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8046
8047 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008048
Matt Carlsonf40386c2009-11-02 14:24:02 +00008049 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8050 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008051 }
8052
Joe Perches63c3a662011-04-26 08:12:10 +00008053 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b05902010-01-20 16:58:02 +00008054 u32 grc_mode = tr32(GRC_MODE);
8055
8056 /* Access the lower 1K of PL PCIE block registers. */
8057 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8058 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8059
8060 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8061 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8062 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8063
8064 tw32(GRC_MODE, grc_mode);
8065 }
8066
Matt Carlson5093eed2010-11-24 08:31:45 +00008067 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8068 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8069 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008070
Matt Carlson5093eed2010-11-24 08:31:45 +00008071 /* Access the lower 1K of PL PCIE block registers. */
8072 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8073 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008074
Matt Carlson5093eed2010-11-24 08:31:45 +00008075 val = tr32(TG3_PCIE_TLDLPL_PORT +
8076 TG3_PCIE_PL_LO_PHYCTL5);
8077 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8078 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008079
Matt Carlson5093eed2010-11-24 08:31:45 +00008080 tw32(GRC_MODE, grc_mode);
8081 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008082
Matt Carlson1ff30a52011-05-19 12:12:46 +00008083 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8084 u32 grc_mode = tr32(GRC_MODE);
8085
8086 /* Access the lower 1K of DL PCIE block registers. */
8087 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8088 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8089
8090 val = tr32(TG3_PCIE_TLDLPL_PORT +
8091 TG3_PCIE_DL_LO_FTSMAX);
8092 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8093 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8094 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8095
8096 tw32(GRC_MODE, grc_mode);
8097 }
8098
Matt Carlsona977dbe2010-04-12 06:58:26 +00008099 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8100 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8101 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8102 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008103 }
8104
Linus Torvalds1da177e2005-04-16 15:20:36 -07008105 /* This works around an issue with Athlon chipsets on
8106 * B3 tigon3 silicon. This bit has no effect on any
8107 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008108 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008109 */
Joe Perches63c3a662011-04-26 08:12:10 +00008110 if (!tg3_flag(tp, CPMU_PRESENT)) {
8111 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008112 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8113 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008115
8116 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008117 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118 val = tr32(TG3PCI_PCISTATE);
8119 val |= PCISTATE_RETRY_SAME_DMA;
8120 tw32(TG3PCI_PCISTATE, val);
8121 }
8122
Joe Perches63c3a662011-04-26 08:12:10 +00008123 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008124 /* Allow reads and writes to the
8125 * APE register and memory space.
8126 */
8127 val = tr32(TG3PCI_PCISTATE);
8128 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00008129 PCISTATE_ALLOW_APE_SHMEM_WR |
8130 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008131 tw32(TG3PCI_PCISTATE, val);
8132 }
8133
Linus Torvalds1da177e2005-04-16 15:20:36 -07008134 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8135 /* Enable some hw fixes. */
8136 val = tr32(TG3PCI_MSI_DATA);
8137 val |= (1 << 26) | (1 << 28) | (1 << 29);
8138 tw32(TG3PCI_MSI_DATA, val);
8139 }
8140
8141 /* Descriptor ring init may make accesses to the
8142 * NIC SRAM area to setup the TX descriptors, so we
8143 * can only do this after the hardware has been
8144 * successfully reset.
8145 */
Michael Chan32d8c572006-07-25 16:38:29 -07008146 err = tg3_init_rings(tp);
8147 if (err)
8148 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008149
Joe Perches63c3a662011-04-26 08:12:10 +00008150 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008151 val = tr32(TG3PCI_DMA_RW_CTRL) &
8152 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008153 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8154 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008155 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8156 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8157 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008158 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8159 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8160 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008161 /* This value is determined during the probe time DMA
8162 * engine test, tg3_test_dma.
8163 */
8164 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166
8167 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8168 GRC_MODE_4X_NIC_SEND_RINGS |
8169 GRC_MODE_NO_TX_PHDR_CSUM |
8170 GRC_MODE_NO_RX_PHDR_CSUM);
8171 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008172
8173 /* Pseudo-header checksum is done by hardware logic and not
8174 * the offload processers, so make the chip do the pseudo-
8175 * header checksums on receive. For transmit it is more
8176 * convenient to do the pseudo-header checksum in software
8177 * as Linux does that on transmit for us in all cases.
8178 */
8179 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008180
8181 tw32(GRC_MODE,
8182 tp->grc_mode |
8183 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8184
8185 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8186 val = tr32(GRC_MISC_CFG);
8187 val &= ~0xff;
8188 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8189 tw32(GRC_MISC_CFG, val);
8190
8191 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008192 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193 /* Do nothing. */
8194 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8195 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8196 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8197 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8198 else
8199 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8200 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8201 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008202 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008203 int fw_len;
8204
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008205 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008206 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8207 tw32(BUFMGR_MB_POOL_ADDR,
8208 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8209 tw32(BUFMGR_MB_POOL_SIZE,
8210 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008212
Michael Chan0f893dc2005-07-25 12:30:38 -07008213 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8215 tp->bufmgr_config.mbuf_read_dma_low_water);
8216 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8217 tp->bufmgr_config.mbuf_mac_rx_low_water);
8218 tw32(BUFMGR_MB_HIGH_WATER,
8219 tp->bufmgr_config.mbuf_high_water);
8220 } else {
8221 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8222 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8223 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8224 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8225 tw32(BUFMGR_MB_HIGH_WATER,
8226 tp->bufmgr_config.mbuf_high_water_jumbo);
8227 }
8228 tw32(BUFMGR_DMA_LOW_WATER,
8229 tp->bufmgr_config.dma_low_water);
8230 tw32(BUFMGR_DMA_HIGH_WATER,
8231 tp->bufmgr_config.dma_high_water);
8232
Matt Carlsond309a462010-09-30 10:34:31 +00008233 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8234 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8235 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008236 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8237 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8238 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8239 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008240 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008241 for (i = 0; i < 2000; i++) {
8242 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8243 break;
8244 udelay(10);
8245 }
8246 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008247 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248 return -ENODEV;
8249 }
8250
Matt Carlsoneb07a942011-04-20 07:57:36 +00008251 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8252 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008253
Matt Carlsoneb07a942011-04-20 07:57:36 +00008254 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008255
8256 /* Initialize TG3_BDINFO's at:
8257 * RCVDBDI_STD_BD: standard eth size rx ring
8258 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8259 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8260 *
8261 * like so:
8262 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8263 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8264 * ring attribute flags
8265 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8266 *
8267 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8268 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8269 *
8270 * The size of each ring is fixed in the firmware, but the location is
8271 * configurable.
8272 */
8273 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008274 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008275 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008276 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008277 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008278 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8279 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008280
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008281 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008282 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008283 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8284 BDINFO_FLAGS_DISABLED);
8285
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008286 /* Program the jumbo buffer descriptor ring control
8287 * blocks on those devices that have them.
8288 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008289 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008290 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008291
Joe Perches63c3a662011-04-26 08:12:10 +00008292 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008294 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008295 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008296 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008297 val = TG3_RX_JMB_RING_SIZE(tp) <<
8298 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008299 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008300 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008301 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008302 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008303 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8304 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008305 } else {
8306 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8307 BDINFO_FLAGS_DISABLED);
8308 }
8309
Joe Perches63c3a662011-04-26 08:12:10 +00008310 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008311 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008312 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008313 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008314 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008315 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8316 val |= (TG3_RX_STD_DMA_SZ << 2);
8317 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008318 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008319 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008320 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008321
8322 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008323
Matt Carlson411da642009-11-13 13:03:46 +00008324 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008325 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008326
Joe Perches63c3a662011-04-26 08:12:10 +00008327 tpr->rx_jmb_prod_idx =
8328 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008329 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008330
Matt Carlson2d31eca2009-09-01 12:53:31 +00008331 tg3_rings_reset(tp);
8332
Linus Torvalds1da177e2005-04-16 15:20:36 -07008333 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008334 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008335
8336 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008337 tw32(MAC_RX_MTU_SIZE,
8338 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008339
8340 /* The slot time is changed by tg3_setup_phy if we
8341 * run at gigabit with half duplex.
8342 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008343 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8344 (6 << TX_LENGTHS_IPG_SHIFT) |
8345 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8346
8347 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8348 val |= tr32(MAC_TX_LENGTHS) &
8349 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8350 TX_LENGTHS_CNT_DWN_VAL_MSK);
8351
8352 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008353
8354 /* Receive rules. */
8355 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8356 tw32(RCVLPC_CONFIG, 0x0181);
8357
8358 /* Calculate RDMAC_MODE setting early, we need it to determine
8359 * the RCVLPC_STATE_ENABLE mask.
8360 */
8361 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8362 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8363 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8364 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8365 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008366
Matt Carlsondeabaac2010-11-24 08:31:50 +00008367 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008368 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8369
Matt Carlson57e69832008-05-25 23:48:31 -07008370 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008371 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8372 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008373 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8374 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8375 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8376
Matt Carlsonc5908932011-03-09 16:58:25 +00008377 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8378 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008379 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008380 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8382 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008383 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8385 }
8386 }
8387
Joe Perches63c3a662011-04-26 08:12:10 +00008388 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008389 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8390
Joe Perches63c3a662011-04-26 08:12:10 +00008391 if (tg3_flag(tp, HW_TSO_1) ||
8392 tg3_flag(tp, HW_TSO_2) ||
8393 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008394 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8395
Matt Carlson108a6c12011-05-19 12:12:47 +00008396 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008397 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008398 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8399 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400
Matt Carlsonf2096f92011-04-05 14:22:48 +00008401 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8402 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8403
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008404 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8405 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8406 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8407 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008408 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008409 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008410 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8411 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008412 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8413 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8414 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8415 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8416 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8417 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008418 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008419 tw32(TG3_RDMA_RSRVCTRL_REG,
8420 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8421 }
8422
Matt Carlsond78b59f2011-04-05 14:22:46 +00008423 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8424 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008425 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8426 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8427 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8428 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8429 }
8430
Linus Torvalds1da177e2005-04-16 15:20:36 -07008431 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008432 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008433 val = tr32(RCVLPC_STATS_ENABLE);
8434 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8435 tw32(RCVLPC_STATS_ENABLE, val);
8436 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008437 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008438 val = tr32(RCVLPC_STATS_ENABLE);
8439 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8440 tw32(RCVLPC_STATS_ENABLE, val);
8441 } else {
8442 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8443 }
8444 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8445 tw32(SNDDATAI_STATSENAB, 0xffffff);
8446 tw32(SNDDATAI_STATSCTRL,
8447 (SNDDATAI_SCTRL_ENABLE |
8448 SNDDATAI_SCTRL_FASTUPD));
8449
8450 /* Setup host coalescing engine. */
8451 tw32(HOSTCC_MODE, 0);
8452 for (i = 0; i < 2000; i++) {
8453 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8454 break;
8455 udelay(10);
8456 }
8457
Michael Chand244c892005-07-05 14:42:33 -07008458 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459
Joe Perches63c3a662011-04-26 08:12:10 +00008460 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008461 /* Status/statistics block address. See tg3_timer,
8462 * the tg3_periodic_fetch_stats call there, and
8463 * tg3_get_stats to see how this works for 5705/5750 chips.
8464 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008465 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8466 ((u64) tp->stats_mapping >> 32));
8467 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8468 ((u64) tp->stats_mapping & 0xffffffff));
8469 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008470
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008472
8473 /* Clear statistics and status block memory areas */
8474 for (i = NIC_SRAM_STATS_BLK;
8475 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8476 i += sizeof(u32)) {
8477 tg3_write_mem(tp, i, 0);
8478 udelay(40);
8479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008480 }
8481
8482 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8483
8484 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8485 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008486 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008487 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8488
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008489 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8490 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008491 /* reset to prevent losing 1st rx packet intermittently */
8492 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8493 udelay(10);
8494 }
8495
Joe Perches63c3a662011-04-26 08:12:10 +00008496 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008497 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008498 else
8499 tp->mac_mode = 0;
8500 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008502 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008503 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008504 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8505 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008506 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8507 udelay(40);
8508
Michael Chan314fba32005-04-21 17:07:04 -07008509 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008510 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008511 * register to preserve the GPIO settings for LOMs. The GPIOs,
8512 * whether used as inputs or outputs, are set by boot code after
8513 * reset.
8514 */
Joe Perches63c3a662011-04-26 08:12:10 +00008515 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008516 u32 gpio_mask;
8517
Michael Chan9d26e212006-12-07 00:21:14 -08008518 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8519 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8520 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008521
8522 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8523 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8524 GRC_LCLCTRL_GPIO_OUTPUT3;
8525
Michael Chanaf36e6b2006-03-23 01:28:06 -08008526 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8527 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8528
Gary Zambranoaaf84462007-05-05 11:51:45 -07008529 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008530 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8531
8532 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008533 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008534 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8535 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008537 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8538 udelay(100);
8539
Joe Perches63c3a662011-04-26 08:12:10 +00008540 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008541 val = tr32(MSGINT_MODE);
8542 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8543 tw32(MSGINT_MODE, val);
8544 }
8545
Joe Perches63c3a662011-04-26 08:12:10 +00008546 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008547 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8548 udelay(40);
8549 }
8550
8551 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8552 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8553 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8554 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8555 WDMAC_MODE_LNGREAD_ENAB);
8556
Matt Carlsonc5908932011-03-09 16:58:25 +00008557 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8558 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008559 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008560 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8561 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8562 /* nothing */
8563 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008564 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565 val |= WDMAC_MODE_RX_ACCEL;
8566 }
8567 }
8568
Michael Chand9ab5ad2006-03-20 22:27:35 -08008569 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008570 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008571 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008572
Matt Carlson788a0352009-11-02 14:26:03 +00008573 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8574 val |= WDMAC_MODE_BURST_ALL_DATA;
8575
Linus Torvalds1da177e2005-04-16 15:20:36 -07008576 tw32_f(WDMAC_MODE, val);
8577 udelay(40);
8578
Joe Perches63c3a662011-04-26 08:12:10 +00008579 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008580 u16 pcix_cmd;
8581
8582 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8583 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008584 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008585 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8586 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008587 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008588 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8589 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590 }
Matt Carlson9974a352007-10-07 23:27:28 -07008591 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8592 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008593 }
8594
8595 tw32_f(RDMAC_MODE, rdmac_mode);
8596 udelay(40);
8597
8598 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008599 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008600 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008601
8602 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8603 tw32(SNDDATAC_MODE,
8604 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8605 else
8606 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8607
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8609 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008610 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008611 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008612 val |= RCVDBDI_MODE_LRG_RING_SZ;
8613 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008614 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008615 if (tg3_flag(tp, HW_TSO_1) ||
8616 tg3_flag(tp, HW_TSO_2) ||
8617 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008618 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008619 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008620 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008621 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8622 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008623 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8624
8625 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8626 err = tg3_load_5701_a0_firmware_fix(tp);
8627 if (err)
8628 return err;
8629 }
8630
Joe Perches63c3a662011-04-26 08:12:10 +00008631 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008632 err = tg3_load_tso_firmware(tp);
8633 if (err)
8634 return err;
8635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008636
8637 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008638
Joe Perches63c3a662011-04-26 08:12:10 +00008639 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008640 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8641 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008642
8643 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8644 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8645 tp->tx_mode &= ~val;
8646 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8647 }
8648
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649 tw32_f(MAC_TX_MODE, tp->tx_mode);
8650 udelay(100);
8651
Joe Perches63c3a662011-04-26 08:12:10 +00008652 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008653 u32 reg = MAC_RSS_INDIR_TBL_0;
8654 u8 *ent = (u8 *)&val;
8655
8656 /* Setup the indirection table */
8657 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8658 int idx = i % sizeof(val);
8659
Matt Carlson5efeeea2010-07-11 09:31:40 +00008660 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008661 if (idx == sizeof(val) - 1) {
8662 tw32(reg, val);
8663 reg += 4;
8664 }
8665 }
8666
8667 /* Setup the "secret" hash key. */
8668 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8669 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8670 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8671 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8672 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8673 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8674 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8675 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8676 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8677 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8678 }
8679
Linus Torvalds1da177e2005-04-16 15:20:36 -07008680 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008681 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008682 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8683
Joe Perches63c3a662011-04-26 08:12:10 +00008684 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008685 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8686 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8687 RX_MODE_RSS_IPV6_HASH_EN |
8688 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8689 RX_MODE_RSS_IPV4_HASH_EN |
8690 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8691
Linus Torvalds1da177e2005-04-16 15:20:36 -07008692 tw32_f(MAC_RX_MODE, tp->rx_mode);
8693 udelay(10);
8694
Linus Torvalds1da177e2005-04-16 15:20:36 -07008695 tw32(MAC_LED_CTRL, tp->led_ctrl);
8696
8697 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008698 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008699 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8700 udelay(10);
8701 }
8702 tw32_f(MAC_RX_MODE, tp->rx_mode);
8703 udelay(10);
8704
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008705 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008706 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008707 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008708 /* Set drive transmission level to 1.2V */
8709 /* only if the signal pre-emphasis bit is not set */
8710 val = tr32(MAC_SERDES_CFG);
8711 val &= 0xfffff000;
8712 val |= 0x880;
8713 tw32(MAC_SERDES_CFG, val);
8714 }
8715 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8716 tw32(MAC_SERDES_CFG, 0x616000);
8717 }
8718
8719 /* Prevent chip from dropping frames when flow control
8720 * is enabled.
8721 */
Matt Carlson666bc832010-01-20 16:58:03 +00008722 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8723 val = 1;
8724 else
8725 val = 2;
8726 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727
8728 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008729 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008730 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00008731 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008732 }
8733
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008734 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00008735 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08008736 u32 tmp;
8737
8738 tmp = tr32(SERDES_RX_CTRL);
8739 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8740 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8741 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8742 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8743 }
8744
Joe Perches63c3a662011-04-26 08:12:10 +00008745 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008746 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8747 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008748 tp->link_config.speed = tp->link_config.orig_speed;
8749 tp->link_config.duplex = tp->link_config.orig_duplex;
8750 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008752
Matt Carlsondd477002008-05-25 23:45:58 -07008753 err = tg3_setup_phy(tp, 0);
8754 if (err)
8755 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008756
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008757 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8758 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008759 u32 tmp;
8760
8761 /* Clear CRC stats. */
8762 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8763 tg3_writephy(tp, MII_TG3_TEST1,
8764 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008765 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008767 }
8768 }
8769
8770 __tg3_set_rx_mode(tp->dev);
8771
8772 /* Initialize receive rules. */
8773 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8774 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8775 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8776 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8777
Joe Perches63c3a662011-04-26 08:12:10 +00008778 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008779 limit = 8;
8780 else
8781 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008782 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008783 limit -= 4;
8784 switch (limit) {
8785 case 16:
8786 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8787 case 15:
8788 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8789 case 14:
8790 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8791 case 13:
8792 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8793 case 12:
8794 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8795 case 11:
8796 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8797 case 10:
8798 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8799 case 9:
8800 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8801 case 8:
8802 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8803 case 7:
8804 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8805 case 6:
8806 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8807 case 5:
8808 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8809 case 4:
8810 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8811 case 3:
8812 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8813 case 2:
8814 case 1:
8815
8816 default:
8817 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008819
Joe Perches63c3a662011-04-26 08:12:10 +00008820 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07008821 /* Write our heartbeat update interval to APE. */
8822 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8823 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008824
Linus Torvalds1da177e2005-04-16 15:20:36 -07008825 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8826
Linus Torvalds1da177e2005-04-16 15:20:36 -07008827 return 0;
8828}
8829
8830/* Called at device open time to get the chip ready for
8831 * packet processing. Invoked with tp->lock held.
8832 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008833static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008834{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008835 tg3_switch_clocks(tp);
8836
8837 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8838
Matt Carlson2f751b62008-08-04 23:17:34 -07008839 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008840}
8841
8842#define TG3_STAT_ADD32(PSTAT, REG) \
8843do { u32 __val = tr32(REG); \
8844 (PSTAT)->low += __val; \
8845 if ((PSTAT)->low < __val) \
8846 (PSTAT)->high += 1; \
8847} while (0)
8848
8849static void tg3_periodic_fetch_stats(struct tg3 *tp)
8850{
8851 struct tg3_hw_stats *sp = tp->hw_stats;
8852
8853 if (!netif_carrier_ok(tp->dev))
8854 return;
8855
8856 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8857 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8858 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8859 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8860 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8861 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8862 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8863 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8864 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8865 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8866 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8867 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8868 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8869
8870 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8871 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8872 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8873 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8874 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8875 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8876 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8877 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8878 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8879 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8880 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8881 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8882 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8883 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008884
8885 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00008886 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8887 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
8888 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00008889 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8890 } else {
8891 u32 val = tr32(HOSTCC_FLOW_ATTN);
8892 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
8893 if (val) {
8894 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
8895 sp->rx_discards.low += val;
8896 if (sp->rx_discards.low < val)
8897 sp->rx_discards.high += 1;
8898 }
8899 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
8900 }
Michael Chan463d3052006-05-22 16:36:27 -07008901 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008902}
8903
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008904static void tg3_chk_missed_msi(struct tg3 *tp)
8905{
8906 u32 i;
8907
8908 for (i = 0; i < tp->irq_cnt; i++) {
8909 struct tg3_napi *tnapi = &tp->napi[i];
8910
8911 if (tg3_has_work(tnapi)) {
8912 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
8913 tnapi->last_tx_cons == tnapi->tx_cons) {
8914 if (tnapi->chk_msi_cnt < 1) {
8915 tnapi->chk_msi_cnt++;
8916 return;
8917 }
8918 tw32_mailbox(tnapi->int_mbox,
8919 tnapi->last_tag << 24);
8920 }
8921 }
8922 tnapi->chk_msi_cnt = 0;
8923 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
8924 tnapi->last_tx_cons = tnapi->tx_cons;
8925 }
8926}
8927
Linus Torvalds1da177e2005-04-16 15:20:36 -07008928static void tg3_timer(unsigned long __opaque)
8929{
8930 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931
Michael Chanf475f162006-03-27 23:20:14 -08008932 if (tp->irq_sync)
8933 goto restart_timer;
8934
David S. Millerf47c11e2005-06-24 20:18:35 -07008935 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008936
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008937 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8938 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8939 tg3_chk_missed_msi(tp);
8940
Joe Perches63c3a662011-04-26 08:12:10 +00008941 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07008942 /* All of this garbage is because when using non-tagged
8943 * IRQ status the mailbox/status_block protocol the chip
8944 * uses with the cpu is race prone.
8945 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008946 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008947 tw32(GRC_LOCAL_CTRL,
8948 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8949 } else {
8950 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008951 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008953
David S. Millerfac9b832005-05-18 22:46:34 -07008954 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +00008955 tg3_flag_set(tp, RESTART_TIMER);
David S. Millerf47c11e2005-06-24 20:18:35 -07008956 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008957 schedule_work(&tp->reset_task);
8958 return;
8959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008960 }
8961
Linus Torvalds1da177e2005-04-16 15:20:36 -07008962 /* This part only runs once per second. */
8963 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008964 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07008965 tg3_periodic_fetch_stats(tp);
8966
Matt Carlsonb0c59432011-05-19 12:12:48 +00008967 if (tp->setlpicnt && !--tp->setlpicnt)
8968 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00008969
Joe Perches63c3a662011-04-26 08:12:10 +00008970 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971 u32 mac_stat;
8972 int phy_event;
8973
8974 mac_stat = tr32(MAC_STATUS);
8975
8976 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008977 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008978 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8979 phy_event = 1;
8980 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8981 phy_event = 1;
8982
8983 if (phy_event)
8984 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00008985 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008986 u32 mac_stat = tr32(MAC_STATUS);
8987 int need_setup = 0;
8988
8989 if (netif_carrier_ok(tp->dev) &&
8990 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8991 need_setup = 1;
8992 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008993 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008994 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8995 MAC_STATUS_SIGNAL_DET))) {
8996 need_setup = 1;
8997 }
8998 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008999 if (!tp->serdes_counter) {
9000 tw32_f(MAC_MODE,
9001 (tp->mac_mode &
9002 ~MAC_MODE_PORT_MODE_MASK));
9003 udelay(40);
9004 tw32_f(MAC_MODE, tp->mac_mode);
9005 udelay(40);
9006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009007 tg3_setup_phy(tp, 0);
9008 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009009 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009010 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009011 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009013
9014 tp->timer_counter = tp->timer_multiplier;
9015 }
9016
Michael Chan130b8e42006-09-27 16:00:40 -07009017 /* Heartbeat is only sent once every 2 seconds.
9018 *
9019 * The heartbeat is to tell the ASF firmware that the host
9020 * driver is still alive. In the event that the OS crashes,
9021 * ASF needs to reset the hardware to free up the FIFO space
9022 * that may be filled with rx packets destined for the host.
9023 * If the FIFO is full, ASF will no longer function properly.
9024 *
9025 * Unintended resets have been reported on real time kernels
9026 * where the timer doesn't run on time. Netpoll will also have
9027 * same problem.
9028 *
9029 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9030 * to check the ring condition when the heartbeat is expiring
9031 * before doing the reset. This will prevent most unintended
9032 * resets.
9033 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009034 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009035 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009036 tg3_wait_for_event_ack(tp);
9037
Michael Chanbbadf502006-04-06 21:46:34 -07009038 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009039 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009040 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009041 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9042 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009043
9044 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009045 }
9046 tp->asf_counter = tp->asf_multiplier;
9047 }
9048
David S. Millerf47c11e2005-06-24 20:18:35 -07009049 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009050
Michael Chanf475f162006-03-27 23:20:14 -08009051restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009052 tp->timer.expires = jiffies + tp->timer_offset;
9053 add_timer(&tp->timer);
9054}
9055
Matt Carlson4f125f42009-09-01 12:55:02 +00009056static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009057{
David Howells7d12e782006-10-05 14:55:46 +01009058 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009059 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009060 char *name;
9061 struct tg3_napi *tnapi = &tp->napi[irq_num];
9062
9063 if (tp->irq_cnt == 1)
9064 name = tp->dev->name;
9065 else {
9066 name = &tnapi->irq_lbl[0];
9067 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9068 name[IFNAMSIZ-1] = 0;
9069 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009070
Joe Perches63c3a662011-04-26 08:12:10 +00009071 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009072 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009073 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009074 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009075 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009076 } else {
9077 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009078 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009079 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009080 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009081 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009082
9083 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009084}
9085
Michael Chan79381092005-04-21 17:13:59 -07009086static int tg3_test_interrupt(struct tg3 *tp)
9087{
Matt Carlson09943a12009-08-28 14:01:57 +00009088 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009089 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009090 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009091 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009092
Michael Chand4bc3922005-05-29 14:59:20 -07009093 if (!netif_running(dev))
9094 return -ENODEV;
9095
Michael Chan79381092005-04-21 17:13:59 -07009096 tg3_disable_ints(tp);
9097
Matt Carlson4f125f42009-09-01 12:55:02 +00009098 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009099
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009100 /*
9101 * Turn off MSI one shot mode. Otherwise this test has no
9102 * observable way to know whether the interrupt was delivered.
9103 */
Joe Perches63c3a662011-04-26 08:12:10 +00009104 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009105 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9106 tw32(MSGINT_MODE, val);
9107 }
9108
Matt Carlson4f125f42009-09-01 12:55:02 +00009109 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009110 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009111 if (err)
9112 return err;
9113
Matt Carlson898a56f2009-08-28 14:02:40 +00009114 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009115 tg3_enable_ints(tp);
9116
9117 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009118 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009119
9120 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009121 u32 int_mbox, misc_host_ctrl;
9122
Matt Carlson898a56f2009-08-28 14:02:40 +00009123 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009124 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9125
9126 if ((int_mbox != 0) ||
9127 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9128 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009129 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009130 }
9131
Michael Chan79381092005-04-21 17:13:59 -07009132 msleep(10);
9133 }
9134
9135 tg3_disable_ints(tp);
9136
Matt Carlson4f125f42009-09-01 12:55:02 +00009137 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009138
Matt Carlson4f125f42009-09-01 12:55:02 +00009139 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009140
9141 if (err)
9142 return err;
9143
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009144 if (intr_ok) {
9145 /* Reenable MSI one shot mode. */
Joe Perches63c3a662011-04-26 08:12:10 +00009146 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009147 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9148 tw32(MSGINT_MODE, val);
9149 }
Michael Chan79381092005-04-21 17:13:59 -07009150 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009151 }
Michael Chan79381092005-04-21 17:13:59 -07009152
9153 return -EIO;
9154}
9155
9156/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9157 * successfully restored
9158 */
9159static int tg3_test_msi(struct tg3 *tp)
9160{
Michael Chan79381092005-04-21 17:13:59 -07009161 int err;
9162 u16 pci_cmd;
9163
Joe Perches63c3a662011-04-26 08:12:10 +00009164 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009165 return 0;
9166
9167 /* Turn off SERR reporting in case MSI terminates with Master
9168 * Abort.
9169 */
9170 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9171 pci_write_config_word(tp->pdev, PCI_COMMAND,
9172 pci_cmd & ~PCI_COMMAND_SERR);
9173
9174 err = tg3_test_interrupt(tp);
9175
9176 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9177
9178 if (!err)
9179 return 0;
9180
9181 /* other failures */
9182 if (err != -EIO)
9183 return err;
9184
9185 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009186 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9187 "to INTx mode. Please report this failure to the PCI "
9188 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009189
Matt Carlson4f125f42009-09-01 12:55:02 +00009190 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009191
Michael Chan79381092005-04-21 17:13:59 -07009192 pci_disable_msi(tp->pdev);
9193
Joe Perches63c3a662011-04-26 08:12:10 +00009194 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009195 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009196
Matt Carlson4f125f42009-09-01 12:55:02 +00009197 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009198 if (err)
9199 return err;
9200
9201 /* Need to reset the chip because the MSI cycle may have terminated
9202 * with Master Abort.
9203 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009204 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009205
Michael Chan944d9802005-05-29 14:57:48 -07009206 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009207 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009208
David S. Millerf47c11e2005-06-24 20:18:35 -07009209 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009210
9211 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009212 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009213
9214 return err;
9215}
9216
Matt Carlson9e9fd122009-01-19 16:57:45 -08009217static int tg3_request_firmware(struct tg3 *tp)
9218{
9219 const __be32 *fw_data;
9220
9221 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009222 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9223 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009224 return -ENOENT;
9225 }
9226
9227 fw_data = (void *)tp->fw->data;
9228
9229 /* Firmware blob starts with version numbers, followed by
9230 * start address and _full_ length including BSS sections
9231 * (which must be longer than the actual data, of course
9232 */
9233
9234 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9235 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009236 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9237 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009238 release_firmware(tp->fw);
9239 tp->fw = NULL;
9240 return -EINVAL;
9241 }
9242
9243 /* We no longer need firmware; we have it. */
9244 tp->fw_needed = NULL;
9245 return 0;
9246}
9247
Matt Carlson679563f2009-09-01 12:55:46 +00009248static bool tg3_enable_msix(struct tg3 *tp)
9249{
9250 int i, rc, cpus = num_online_cpus();
9251 struct msix_entry msix_ent[tp->irq_max];
9252
9253 if (cpus == 1)
9254 /* Just fallback to the simpler MSI mode. */
9255 return false;
9256
9257 /*
9258 * We want as many rx rings enabled as there are cpus.
9259 * The first MSIX vector only deals with link interrupts, etc,
9260 * so we add one to the number of vectors we are requesting.
9261 */
9262 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9263
9264 for (i = 0; i < tp->irq_max; i++) {
9265 msix_ent[i].entry = i;
9266 msix_ent[i].vector = 0;
9267 }
9268
9269 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009270 if (rc < 0) {
9271 return false;
9272 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009273 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9274 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009275 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9276 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009277 tp->irq_cnt = rc;
9278 }
9279
9280 for (i = 0; i < tp->irq_max; i++)
9281 tp->napi[i].irq_vec = msix_ent[i].vector;
9282
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009283 netif_set_real_num_tx_queues(tp->dev, 1);
9284 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9285 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9286 pci_disable_msix(tp->pdev);
9287 return false;
9288 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009289
9290 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009291 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009292
9293 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9294 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009295 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009296 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9297 }
9298 }
Matt Carlson2430b032010-06-05 17:24:34 +00009299
Matt Carlson679563f2009-09-01 12:55:46 +00009300 return true;
9301}
9302
Matt Carlson07b01732009-08-28 14:01:15 +00009303static void tg3_ints_init(struct tg3 *tp)
9304{
Joe Perches63c3a662011-04-26 08:12:10 +00009305 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9306 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009307 /* All MSI supporting chips should support tagged
9308 * status. Assert that this is the case.
9309 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009310 netdev_warn(tp->dev,
9311 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009312 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009313 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009314
Joe Perches63c3a662011-04-26 08:12:10 +00009315 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9316 tg3_flag_set(tp, USING_MSIX);
9317 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9318 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009319
Joe Perches63c3a662011-04-26 08:12:10 +00009320 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009321 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009322 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009323 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009324 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9325 }
9326defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009327 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009328 tp->irq_cnt = 1;
9329 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009330 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009331 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009332 }
Matt Carlson07b01732009-08-28 14:01:15 +00009333}
9334
9335static void tg3_ints_fini(struct tg3 *tp)
9336{
Joe Perches63c3a662011-04-26 08:12:10 +00009337 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009338 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009339 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009340 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009341 tg3_flag_clear(tp, USING_MSI);
9342 tg3_flag_clear(tp, USING_MSIX);
9343 tg3_flag_clear(tp, ENABLE_RSS);
9344 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009345}
9346
Linus Torvalds1da177e2005-04-16 15:20:36 -07009347static int tg3_open(struct net_device *dev)
9348{
9349 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009350 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009351
Matt Carlson9e9fd122009-01-19 16:57:45 -08009352 if (tp->fw_needed) {
9353 err = tg3_request_firmware(tp);
9354 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9355 if (err)
9356 return err;
9357 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009358 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009359 tg3_flag_clear(tp, TSO_CAPABLE);
9360 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009361 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009362 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009363 }
9364 }
9365
Michael Chanc49a1562006-12-17 17:07:29 -08009366 netif_carrier_off(tp->dev);
9367
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009368 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009369 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009370 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009371
9372 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009373
Linus Torvalds1da177e2005-04-16 15:20:36 -07009374 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009375 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009376
David S. Millerf47c11e2005-06-24 20:18:35 -07009377 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009378
Matt Carlson679563f2009-09-01 12:55:46 +00009379 /*
9380 * Setup interrupts first so we know how
9381 * many NAPI resources to allocate
9382 */
9383 tg3_ints_init(tp);
9384
Linus Torvalds1da177e2005-04-16 15:20:36 -07009385 /* The placement of this call is tied
9386 * to the setup and use of Host TX descriptors.
9387 */
9388 err = tg3_alloc_consistent(tp);
9389 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009390 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009391
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009392 tg3_napi_init(tp);
9393
Matt Carlsonfed97812009-09-01 13:10:19 +00009394 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009395
Matt Carlson4f125f42009-09-01 12:55:02 +00009396 for (i = 0; i < tp->irq_cnt; i++) {
9397 struct tg3_napi *tnapi = &tp->napi[i];
9398 err = tg3_request_irq(tp, i);
9399 if (err) {
9400 for (i--; i >= 0; i--)
9401 free_irq(tnapi->irq_vec, tnapi);
9402 break;
9403 }
9404 }
Matt Carlson07b01732009-08-28 14:01:15 +00009405
9406 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009407 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009408
David S. Millerf47c11e2005-06-24 20:18:35 -07009409 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009410
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009411 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009412 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009413 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009414 tg3_free_rings(tp);
9415 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009416 if (tg3_flag(tp, TAGGED_STATUS) &&
9417 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9418 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765)
David S. Millerfac9b832005-05-18 22:46:34 -07009419 tp->timer_offset = HZ;
9420 else
9421 tp->timer_offset = HZ / 10;
9422
9423 BUG_ON(tp->timer_offset > HZ);
9424 tp->timer_counter = tp->timer_multiplier =
9425 (HZ / tp->timer_offset);
9426 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009427 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428
9429 init_timer(&tp->timer);
9430 tp->timer.expires = jiffies + tp->timer_offset;
9431 tp->timer.data = (unsigned long) tp;
9432 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433 }
9434
David S. Millerf47c11e2005-06-24 20:18:35 -07009435 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009436
Matt Carlson07b01732009-08-28 14:01:15 +00009437 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009438 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439
Joe Perches63c3a662011-04-26 08:12:10 +00009440 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009441 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009442
Michael Chan79381092005-04-21 17:13:59 -07009443 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009444 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009445 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009446 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009447 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009448
Matt Carlson679563f2009-09-01 12:55:46 +00009449 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009450 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009451
Joe Perches63c3a662011-04-26 08:12:10 +00009452 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009453 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009454
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009455 tw32(PCIE_TRANSACTION_CFG,
9456 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009457 }
Michael Chan79381092005-04-21 17:13:59 -07009458 }
9459
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009460 tg3_phy_start(tp);
9461
David S. Millerf47c11e2005-06-24 20:18:35 -07009462 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463
Michael Chan79381092005-04-21 17:13:59 -07009464 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009465 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009466 tg3_enable_ints(tp);
9467
David S. Millerf47c11e2005-06-24 20:18:35 -07009468 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009469
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009470 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009471
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009472 /*
9473 * Reset loopback feature if it was turned on while the device was down
9474 * make sure that it's installed properly now.
9475 */
9476 if (dev->features & NETIF_F_LOOPBACK)
9477 tg3_set_loopback(dev, dev->features);
9478
Linus Torvalds1da177e2005-04-16 15:20:36 -07009479 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009480
Matt Carlson679563f2009-09-01 12:55:46 +00009481err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009482 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9483 struct tg3_napi *tnapi = &tp->napi[i];
9484 free_irq(tnapi->irq_vec, tnapi);
9485 }
Matt Carlson07b01732009-08-28 14:01:15 +00009486
Matt Carlson679563f2009-09-01 12:55:46 +00009487err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009488 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009489 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009490 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009491
9492err_out1:
9493 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009494 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009495}
9496
Eric Dumazet511d2222010-07-07 20:44:24 +00009497static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9498 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009499static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9500
9501static int tg3_close(struct net_device *dev)
9502{
Matt Carlson4f125f42009-09-01 12:55:02 +00009503 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009504 struct tg3 *tp = netdev_priv(dev);
9505
Matt Carlsonfed97812009-09-01 13:10:19 +00009506 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009507 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009508
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009509 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009510
9511 del_timer_sync(&tp->timer);
9512
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009513 tg3_phy_stop(tp);
9514
David S. Millerf47c11e2005-06-24 20:18:35 -07009515 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009516
9517 tg3_disable_ints(tp);
9518
Michael Chan944d9802005-05-29 14:57:48 -07009519 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009520 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009521 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009522
David S. Millerf47c11e2005-06-24 20:18:35 -07009523 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009524
Matt Carlson4f125f42009-09-01 12:55:02 +00009525 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9526 struct tg3_napi *tnapi = &tp->napi[i];
9527 free_irq(tnapi->irq_vec, tnapi);
9528 }
Matt Carlson07b01732009-08-28 14:01:15 +00009529
9530 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009531
Eric Dumazet511d2222010-07-07 20:44:24 +00009532 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9533
Linus Torvalds1da177e2005-04-16 15:20:36 -07009534 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9535 sizeof(tp->estats_prev));
9536
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009537 tg3_napi_fini(tp);
9538
Linus Torvalds1da177e2005-04-16 15:20:36 -07009539 tg3_free_consistent(tp);
9540
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009541 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009542
9543 netif_carrier_off(tp->dev);
9544
Linus Torvalds1da177e2005-04-16 15:20:36 -07009545 return 0;
9546}
9547
Eric Dumazet511d2222010-07-07 20:44:24 +00009548static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009549{
9550 return ((u64)val->high << 32) | ((u64)val->low);
9551}
9552
Eric Dumazet511d2222010-07-07 20:44:24 +00009553static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009554{
9555 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9556
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009557 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009558 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9559 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009560 u32 val;
9561
David S. Millerf47c11e2005-06-24 20:18:35 -07009562 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009563 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9564 tg3_writephy(tp, MII_TG3_TEST1,
9565 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009566 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009567 } else
9568 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009569 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009570
9571 tp->phy_crc_errors += val;
9572
9573 return tp->phy_crc_errors;
9574 }
9575
9576 return get_stat64(&hw_stats->rx_fcs_errors);
9577}
9578
9579#define ESTAT_ADD(member) \
9580 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009581 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009582
9583static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9584{
9585 struct tg3_ethtool_stats *estats = &tp->estats;
9586 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9587 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9588
9589 if (!hw_stats)
9590 return old_estats;
9591
9592 ESTAT_ADD(rx_octets);
9593 ESTAT_ADD(rx_fragments);
9594 ESTAT_ADD(rx_ucast_packets);
9595 ESTAT_ADD(rx_mcast_packets);
9596 ESTAT_ADD(rx_bcast_packets);
9597 ESTAT_ADD(rx_fcs_errors);
9598 ESTAT_ADD(rx_align_errors);
9599 ESTAT_ADD(rx_xon_pause_rcvd);
9600 ESTAT_ADD(rx_xoff_pause_rcvd);
9601 ESTAT_ADD(rx_mac_ctrl_rcvd);
9602 ESTAT_ADD(rx_xoff_entered);
9603 ESTAT_ADD(rx_frame_too_long_errors);
9604 ESTAT_ADD(rx_jabbers);
9605 ESTAT_ADD(rx_undersize_packets);
9606 ESTAT_ADD(rx_in_length_errors);
9607 ESTAT_ADD(rx_out_length_errors);
9608 ESTAT_ADD(rx_64_or_less_octet_packets);
9609 ESTAT_ADD(rx_65_to_127_octet_packets);
9610 ESTAT_ADD(rx_128_to_255_octet_packets);
9611 ESTAT_ADD(rx_256_to_511_octet_packets);
9612 ESTAT_ADD(rx_512_to_1023_octet_packets);
9613 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9614 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9615 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9616 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9617 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9618
9619 ESTAT_ADD(tx_octets);
9620 ESTAT_ADD(tx_collisions);
9621 ESTAT_ADD(tx_xon_sent);
9622 ESTAT_ADD(tx_xoff_sent);
9623 ESTAT_ADD(tx_flow_control);
9624 ESTAT_ADD(tx_mac_errors);
9625 ESTAT_ADD(tx_single_collisions);
9626 ESTAT_ADD(tx_mult_collisions);
9627 ESTAT_ADD(tx_deferred);
9628 ESTAT_ADD(tx_excessive_collisions);
9629 ESTAT_ADD(tx_late_collisions);
9630 ESTAT_ADD(tx_collide_2times);
9631 ESTAT_ADD(tx_collide_3times);
9632 ESTAT_ADD(tx_collide_4times);
9633 ESTAT_ADD(tx_collide_5times);
9634 ESTAT_ADD(tx_collide_6times);
9635 ESTAT_ADD(tx_collide_7times);
9636 ESTAT_ADD(tx_collide_8times);
9637 ESTAT_ADD(tx_collide_9times);
9638 ESTAT_ADD(tx_collide_10times);
9639 ESTAT_ADD(tx_collide_11times);
9640 ESTAT_ADD(tx_collide_12times);
9641 ESTAT_ADD(tx_collide_13times);
9642 ESTAT_ADD(tx_collide_14times);
9643 ESTAT_ADD(tx_collide_15times);
9644 ESTAT_ADD(tx_ucast_packets);
9645 ESTAT_ADD(tx_mcast_packets);
9646 ESTAT_ADD(tx_bcast_packets);
9647 ESTAT_ADD(tx_carrier_sense_errors);
9648 ESTAT_ADD(tx_discards);
9649 ESTAT_ADD(tx_errors);
9650
9651 ESTAT_ADD(dma_writeq_full);
9652 ESTAT_ADD(dma_write_prioq_full);
9653 ESTAT_ADD(rxbds_empty);
9654 ESTAT_ADD(rx_discards);
9655 ESTAT_ADD(rx_errors);
9656 ESTAT_ADD(rx_threshold_hit);
9657
9658 ESTAT_ADD(dma_readq_full);
9659 ESTAT_ADD(dma_read_prioq_full);
9660 ESTAT_ADD(tx_comp_queue_full);
9661
9662 ESTAT_ADD(ring_set_send_prod_index);
9663 ESTAT_ADD(ring_status_update);
9664 ESTAT_ADD(nic_irqs);
9665 ESTAT_ADD(nic_avoided_irqs);
9666 ESTAT_ADD(nic_tx_threshold_hit);
9667
Matt Carlson4452d092011-05-19 12:12:51 +00009668 ESTAT_ADD(mbuf_lwm_thresh_hit);
9669
Linus Torvalds1da177e2005-04-16 15:20:36 -07009670 return estats;
9671}
9672
Eric Dumazet511d2222010-07-07 20:44:24 +00009673static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9674 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009675{
9676 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009677 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009678 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9679
9680 if (!hw_stats)
9681 return old_stats;
9682
9683 stats->rx_packets = old_stats->rx_packets +
9684 get_stat64(&hw_stats->rx_ucast_packets) +
9685 get_stat64(&hw_stats->rx_mcast_packets) +
9686 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009687
Linus Torvalds1da177e2005-04-16 15:20:36 -07009688 stats->tx_packets = old_stats->tx_packets +
9689 get_stat64(&hw_stats->tx_ucast_packets) +
9690 get_stat64(&hw_stats->tx_mcast_packets) +
9691 get_stat64(&hw_stats->tx_bcast_packets);
9692
9693 stats->rx_bytes = old_stats->rx_bytes +
9694 get_stat64(&hw_stats->rx_octets);
9695 stats->tx_bytes = old_stats->tx_bytes +
9696 get_stat64(&hw_stats->tx_octets);
9697
9698 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009699 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009700 stats->tx_errors = old_stats->tx_errors +
9701 get_stat64(&hw_stats->tx_errors) +
9702 get_stat64(&hw_stats->tx_mac_errors) +
9703 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9704 get_stat64(&hw_stats->tx_discards);
9705
9706 stats->multicast = old_stats->multicast +
9707 get_stat64(&hw_stats->rx_mcast_packets);
9708 stats->collisions = old_stats->collisions +
9709 get_stat64(&hw_stats->tx_collisions);
9710
9711 stats->rx_length_errors = old_stats->rx_length_errors +
9712 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9713 get_stat64(&hw_stats->rx_undersize_packets);
9714
9715 stats->rx_over_errors = old_stats->rx_over_errors +
9716 get_stat64(&hw_stats->rxbds_empty);
9717 stats->rx_frame_errors = old_stats->rx_frame_errors +
9718 get_stat64(&hw_stats->rx_align_errors);
9719 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9720 get_stat64(&hw_stats->tx_discards);
9721 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9722 get_stat64(&hw_stats->tx_carrier_sense_errors);
9723
9724 stats->rx_crc_errors = old_stats->rx_crc_errors +
9725 calc_crc_errors(tp);
9726
John W. Linville4f63b872005-09-12 14:43:18 -07009727 stats->rx_missed_errors = old_stats->rx_missed_errors +
9728 get_stat64(&hw_stats->rx_discards);
9729
Eric Dumazetb0057c52010-10-10 19:55:52 +00009730 stats->rx_dropped = tp->rx_dropped;
9731
Linus Torvalds1da177e2005-04-16 15:20:36 -07009732 return stats;
9733}
9734
9735static inline u32 calc_crc(unsigned char *buf, int len)
9736{
9737 u32 reg;
9738 u32 tmp;
9739 int j, k;
9740
9741 reg = 0xffffffff;
9742
9743 for (j = 0; j < len; j++) {
9744 reg ^= buf[j];
9745
9746 for (k = 0; k < 8; k++) {
9747 tmp = reg & 0x01;
9748
9749 reg >>= 1;
9750
Matt Carlson859a5882010-04-05 10:19:28 +00009751 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009752 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009753 }
9754 }
9755
9756 return ~reg;
9757}
9758
9759static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9760{
9761 /* accept or reject all multicast frames */
9762 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9763 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9764 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9765 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9766}
9767
9768static void __tg3_set_rx_mode(struct net_device *dev)
9769{
9770 struct tg3 *tp = netdev_priv(dev);
9771 u32 rx_mode;
9772
9773 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9774 RX_MODE_KEEP_VLAN_TAG);
9775
Matt Carlsonbf933c82011-01-25 15:58:49 +00009776#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009777 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9778 * flag clear.
9779 */
Joe Perches63c3a662011-04-26 08:12:10 +00009780 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009781 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9782#endif
9783
9784 if (dev->flags & IFF_PROMISC) {
9785 /* Promiscuous mode. */
9786 rx_mode |= RX_MODE_PROMISC;
9787 } else if (dev->flags & IFF_ALLMULTI) {
9788 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009789 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009790 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009791 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009792 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009793 } else {
9794 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +00009795 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796 u32 mc_filter[4] = { 0, };
9797 u32 regidx;
9798 u32 bit;
9799 u32 crc;
9800
Jiri Pirko22bedad2010-04-01 21:22:57 +00009801 netdev_for_each_mc_addr(ha, dev) {
9802 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803 bit = ~crc & 0x7f;
9804 regidx = (bit & 0x60) >> 5;
9805 bit &= 0x1f;
9806 mc_filter[regidx] |= (1 << bit);
9807 }
9808
9809 tw32(MAC_HASH_REG_0, mc_filter[0]);
9810 tw32(MAC_HASH_REG_1, mc_filter[1]);
9811 tw32(MAC_HASH_REG_2, mc_filter[2]);
9812 tw32(MAC_HASH_REG_3, mc_filter[3]);
9813 }
9814
9815 if (rx_mode != tp->rx_mode) {
9816 tp->rx_mode = rx_mode;
9817 tw32_f(MAC_RX_MODE, rx_mode);
9818 udelay(10);
9819 }
9820}
9821
9822static void tg3_set_rx_mode(struct net_device *dev)
9823{
9824 struct tg3 *tp = netdev_priv(dev);
9825
Michael Chane75f7c92006-03-20 21:33:26 -08009826 if (!netif_running(dev))
9827 return;
9828
David S. Millerf47c11e2005-06-24 20:18:35 -07009829 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009830 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009831 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009832}
9833
Linus Torvalds1da177e2005-04-16 15:20:36 -07009834static int tg3_get_regs_len(struct net_device *dev)
9835{
Matt Carlson97bd8e42011-04-13 11:05:04 +00009836 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837}
9838
9839static void tg3_get_regs(struct net_device *dev,
9840 struct ethtool_regs *regs, void *_p)
9841{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009842 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843
9844 regs->version = 0;
9845
Matt Carlson97bd8e42011-04-13 11:05:04 +00009846 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009847
Matt Carlson80096062010-08-02 11:26:06 +00009848 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009849 return;
9850
David S. Millerf47c11e2005-06-24 20:18:35 -07009851 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009852
Matt Carlson97bd8e42011-04-13 11:05:04 +00009853 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009854
David S. Millerf47c11e2005-06-24 20:18:35 -07009855 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009856}
9857
9858static int tg3_get_eeprom_len(struct net_device *dev)
9859{
9860 struct tg3 *tp = netdev_priv(dev);
9861
9862 return tp->nvram_size;
9863}
9864
Linus Torvalds1da177e2005-04-16 15:20:36 -07009865static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9866{
9867 struct tg3 *tp = netdev_priv(dev);
9868 int ret;
9869 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009870 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009871 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009872
Joe Perches63c3a662011-04-26 08:12:10 +00009873 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +00009874 return -EINVAL;
9875
Matt Carlson80096062010-08-02 11:26:06 +00009876 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009877 return -EAGAIN;
9878
Linus Torvalds1da177e2005-04-16 15:20:36 -07009879 offset = eeprom->offset;
9880 len = eeprom->len;
9881 eeprom->len = 0;
9882
9883 eeprom->magic = TG3_EEPROM_MAGIC;
9884
9885 if (offset & 3) {
9886 /* adjustments to start on required 4 byte boundary */
9887 b_offset = offset & 3;
9888 b_count = 4 - b_offset;
9889 if (b_count > len) {
9890 /* i.e. offset=1 len=2 */
9891 b_count = len;
9892 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009893 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009894 if (ret)
9895 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009896 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009897 len -= b_count;
9898 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009899 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009900 }
9901
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009902 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009903 pd = &data[eeprom->len];
9904 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009905 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009906 if (ret) {
9907 eeprom->len += i;
9908 return ret;
9909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009910 memcpy(pd + i, &val, 4);
9911 }
9912 eeprom->len += i;
9913
9914 if (len & 3) {
9915 /* read last bytes not ending on 4 byte boundary */
9916 pd = &data[eeprom->len];
9917 b_count = len & 3;
9918 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009919 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009920 if (ret)
9921 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009922 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009923 eeprom->len += b_count;
9924 }
9925 return 0;
9926}
9927
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009928static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009929
9930static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9931{
9932 struct tg3 *tp = netdev_priv(dev);
9933 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009934 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009935 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009936 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009937
Matt Carlson80096062010-08-02 11:26:06 +00009938 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009939 return -EAGAIN;
9940
Joe Perches63c3a662011-04-26 08:12:10 +00009941 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +00009942 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009943 return -EINVAL;
9944
9945 offset = eeprom->offset;
9946 len = eeprom->len;
9947
9948 if ((b_offset = (offset & 3))) {
9949 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009950 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009951 if (ret)
9952 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009953 len += b_offset;
9954 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009955 if (len < 4)
9956 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009957 }
9958
9959 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009960 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009961 /* adjustments to end on required 4 byte boundary */
9962 odd_len = 1;
9963 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009964 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009965 if (ret)
9966 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009967 }
9968
9969 buf = data;
9970 if (b_offset || odd_len) {
9971 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009972 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009973 return -ENOMEM;
9974 if (b_offset)
9975 memcpy(buf, &start, 4);
9976 if (odd_len)
9977 memcpy(buf+len-4, &end, 4);
9978 memcpy(buf + b_offset, data, eeprom->len);
9979 }
9980
9981 ret = tg3_nvram_write_block(tp, offset, len, buf);
9982
9983 if (buf != data)
9984 kfree(buf);
9985
9986 return ret;
9987}
9988
9989static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9990{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009991 struct tg3 *tp = netdev_priv(dev);
9992
Joe Perches63c3a662011-04-26 08:12:10 +00009993 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009994 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009995 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009996 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009997 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9998 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009999 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010000
Linus Torvalds1da177e2005-04-16 15:20:36 -070010001 cmd->supported = (SUPPORTED_Autoneg);
10002
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010003 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010004 cmd->supported |= (SUPPORTED_1000baseT_Half |
10005 SUPPORTED_1000baseT_Full);
10006
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010007 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010008 cmd->supported |= (SUPPORTED_100baseT_Half |
10009 SUPPORTED_100baseT_Full |
10010 SUPPORTED_10baseT_Half |
10011 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010012 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010013 cmd->port = PORT_TP;
10014 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010016 cmd->port = PORT_FIBRE;
10017 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010018
Linus Torvalds1da177e2005-04-16 15:20:36 -070010019 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010020 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10021 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10022 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10023 cmd->advertising |= ADVERTISED_Pause;
10024 } else {
10025 cmd->advertising |= ADVERTISED_Pause |
10026 ADVERTISED_Asym_Pause;
10027 }
10028 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10029 cmd->advertising |= ADVERTISED_Asym_Pause;
10030 }
10031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010032 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010033 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010034 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +000010035 } else {
David Decotigny70739492011-04-27 18:32:40 +000010036 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010037 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010038 }
Matt Carlson882e9792009-09-01 13:21:36 +000010039 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010040 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010041 cmd->autoneg = tp->link_config.autoneg;
10042 cmd->maxtxpkt = 0;
10043 cmd->maxrxpkt = 0;
10044 return 0;
10045}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010046
Linus Torvalds1da177e2005-04-16 15:20:36 -070010047static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10048{
10049 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010050 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010051
Joe Perches63c3a662011-04-26 08:12:10 +000010052 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010053 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010054 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010055 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010056 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10057 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010058 }
10059
Matt Carlson7e5856b2009-02-25 14:23:01 +000010060 if (cmd->autoneg != AUTONEG_ENABLE &&
10061 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010062 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010063
10064 if (cmd->autoneg == AUTONEG_DISABLE &&
10065 cmd->duplex != DUPLEX_FULL &&
10066 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010067 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010068
Matt Carlson7e5856b2009-02-25 14:23:01 +000010069 if (cmd->autoneg == AUTONEG_ENABLE) {
10070 u32 mask = ADVERTISED_Autoneg |
10071 ADVERTISED_Pause |
10072 ADVERTISED_Asym_Pause;
10073
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010074 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010075 mask |= ADVERTISED_1000baseT_Half |
10076 ADVERTISED_1000baseT_Full;
10077
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010078 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010079 mask |= ADVERTISED_100baseT_Half |
10080 ADVERTISED_100baseT_Full |
10081 ADVERTISED_10baseT_Half |
10082 ADVERTISED_10baseT_Full |
10083 ADVERTISED_TP;
10084 else
10085 mask |= ADVERTISED_FIBRE;
10086
10087 if (cmd->advertising & ~mask)
10088 return -EINVAL;
10089
10090 mask &= (ADVERTISED_1000baseT_Half |
10091 ADVERTISED_1000baseT_Full |
10092 ADVERTISED_100baseT_Half |
10093 ADVERTISED_100baseT_Full |
10094 ADVERTISED_10baseT_Half |
10095 ADVERTISED_10baseT_Full);
10096
10097 cmd->advertising &= mask;
10098 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010099 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010100 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010101 return -EINVAL;
10102
10103 if (cmd->duplex != DUPLEX_FULL)
10104 return -EINVAL;
10105 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010106 if (speed != SPEED_100 &&
10107 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010108 return -EINVAL;
10109 }
10110 }
10111
David S. Millerf47c11e2005-06-24 20:18:35 -070010112 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113
10114 tp->link_config.autoneg = cmd->autoneg;
10115 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010116 tp->link_config.advertising = (cmd->advertising |
10117 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010118 tp->link_config.speed = SPEED_INVALID;
10119 tp->link_config.duplex = DUPLEX_INVALID;
10120 } else {
10121 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010122 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010123 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010124 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010125
Michael Chan24fcad62006-12-17 17:06:46 -080010126 tp->link_config.orig_speed = tp->link_config.speed;
10127 tp->link_config.orig_duplex = tp->link_config.duplex;
10128 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10129
Linus Torvalds1da177e2005-04-16 15:20:36 -070010130 if (netif_running(dev))
10131 tg3_setup_phy(tp, 1);
10132
David S. Millerf47c11e2005-06-24 20:18:35 -070010133 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010134
Linus Torvalds1da177e2005-04-16 15:20:36 -070010135 return 0;
10136}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010137
Linus Torvalds1da177e2005-04-16 15:20:36 -070010138static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10139{
10140 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010141
Linus Torvalds1da177e2005-04-16 15:20:36 -070010142 strcpy(info->driver, DRV_MODULE_NAME);
10143 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010144 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145 strcpy(info->bus_info, pci_name(tp->pdev));
10146}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010147
Linus Torvalds1da177e2005-04-16 15:20:36 -070010148static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10149{
10150 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010151
Joe Perches63c3a662011-04-26 08:12:10 +000010152 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010153 wol->supported = WAKE_MAGIC;
10154 else
10155 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010156 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010157 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010158 wol->wolopts = WAKE_MAGIC;
10159 memset(&wol->sopass, 0, sizeof(wol->sopass));
10160}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010161
Linus Torvalds1da177e2005-04-16 15:20:36 -070010162static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10163{
10164 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010165 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010166
Linus Torvalds1da177e2005-04-16 15:20:36 -070010167 if (wol->wolopts & ~WAKE_MAGIC)
10168 return -EINVAL;
10169 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010170 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010171 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010172
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010173 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10174
David S. Millerf47c11e2005-06-24 20:18:35 -070010175 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010176 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010177 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010178 else
Joe Perches63c3a662011-04-26 08:12:10 +000010179 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010180 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010181
Linus Torvalds1da177e2005-04-16 15:20:36 -070010182 return 0;
10183}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010184
Linus Torvalds1da177e2005-04-16 15:20:36 -070010185static u32 tg3_get_msglevel(struct net_device *dev)
10186{
10187 struct tg3 *tp = netdev_priv(dev);
10188 return tp->msg_enable;
10189}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010190
Linus Torvalds1da177e2005-04-16 15:20:36 -070010191static void tg3_set_msglevel(struct net_device *dev, u32 value)
10192{
10193 struct tg3 *tp = netdev_priv(dev);
10194 tp->msg_enable = value;
10195}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010196
Linus Torvalds1da177e2005-04-16 15:20:36 -070010197static int tg3_nway_reset(struct net_device *dev)
10198{
10199 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010200 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010201
Linus Torvalds1da177e2005-04-16 15:20:36 -070010202 if (!netif_running(dev))
10203 return -EAGAIN;
10204
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010205 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010206 return -EINVAL;
10207
Joe Perches63c3a662011-04-26 08:12:10 +000010208 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010209 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010210 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010211 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010212 } else {
10213 u32 bmcr;
10214
10215 spin_lock_bh(&tp->lock);
10216 r = -EINVAL;
10217 tg3_readphy(tp, MII_BMCR, &bmcr);
10218 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10219 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010220 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010221 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10222 BMCR_ANENABLE);
10223 r = 0;
10224 }
10225 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010226 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010227
Linus Torvalds1da177e2005-04-16 15:20:36 -070010228 return r;
10229}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010230
Linus Torvalds1da177e2005-04-16 15:20:36 -070010231static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10232{
10233 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010234
Matt Carlson2c49a442010-09-30 10:34:35 +000010235 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010236 ering->rx_mini_max_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010237 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010238 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010239 else
10240 ering->rx_jumbo_max_pending = 0;
10241
10242 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010243
10244 ering->rx_pending = tp->rx_pending;
10245 ering->rx_mini_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010246 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010247 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10248 else
10249 ering->rx_jumbo_pending = 0;
10250
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010251 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010252}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010253
Linus Torvalds1da177e2005-04-16 15:20:36 -070010254static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10255{
10256 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010257 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010258
Matt Carlson2c49a442010-09-30 10:34:35 +000010259 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10260 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010261 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10262 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010263 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010264 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010265 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010266
Michael Chanbbe832c2005-06-24 20:20:04 -070010267 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010268 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010269 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010270 irq_sync = 1;
10271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010272
Michael Chanbbe832c2005-06-24 20:20:04 -070010273 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010274
Linus Torvalds1da177e2005-04-16 15:20:36 -070010275 tp->rx_pending = ering->rx_pending;
10276
Joe Perches63c3a662011-04-26 08:12:10 +000010277 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010278 tp->rx_pending > 63)
10279 tp->rx_pending = 63;
10280 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010281
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010282 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010283 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010284
10285 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010286 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010287 err = tg3_restart_hw(tp, 1);
10288 if (!err)
10289 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010290 }
10291
David S. Millerf47c11e2005-06-24 20:18:35 -070010292 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010293
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010294 if (irq_sync && !err)
10295 tg3_phy_start(tp);
10296
Michael Chanb9ec6c12006-07-25 16:37:27 -070010297 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010298}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010299
Linus Torvalds1da177e2005-04-16 15:20:36 -070010300static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10301{
10302 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010303
Joe Perches63c3a662011-04-26 08:12:10 +000010304 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010305
Steve Glendinninge18ce342008-12-16 02:00:00 -080010306 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010307 epause->rx_pause = 1;
10308 else
10309 epause->rx_pause = 0;
10310
Steve Glendinninge18ce342008-12-16 02:00:00 -080010311 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010312 epause->tx_pause = 1;
10313 else
10314 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010315}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010316
Linus Torvalds1da177e2005-04-16 15:20:36 -070010317static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10318{
10319 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010320 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010321
Joe Perches63c3a662011-04-26 08:12:10 +000010322 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010323 u32 newadv;
10324 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010325
Matt Carlson27121682010-02-17 15:16:57 +000010326 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010327
Matt Carlson27121682010-02-17 15:16:57 +000010328 if (!(phydev->supported & SUPPORTED_Pause) ||
10329 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010330 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010331 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010332
Matt Carlson27121682010-02-17 15:16:57 +000010333 tp->link_config.flowctrl = 0;
10334 if (epause->rx_pause) {
10335 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010336
Matt Carlson27121682010-02-17 15:16:57 +000010337 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010338 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010339 newadv = ADVERTISED_Pause;
10340 } else
10341 newadv = ADVERTISED_Pause |
10342 ADVERTISED_Asym_Pause;
10343 } else if (epause->tx_pause) {
10344 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10345 newadv = ADVERTISED_Asym_Pause;
10346 } else
10347 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010348
Matt Carlson27121682010-02-17 15:16:57 +000010349 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010350 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010351 else
Joe Perches63c3a662011-04-26 08:12:10 +000010352 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010353
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010354 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010355 u32 oldadv = phydev->advertising &
10356 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10357 if (oldadv != newadv) {
10358 phydev->advertising &=
10359 ~(ADVERTISED_Pause |
10360 ADVERTISED_Asym_Pause);
10361 phydev->advertising |= newadv;
10362 if (phydev->autoneg) {
10363 /*
10364 * Always renegotiate the link to
10365 * inform our link partner of our
10366 * flow control settings, even if the
10367 * flow control is forced. Let
10368 * tg3_adjust_link() do the final
10369 * flow control setup.
10370 */
10371 return phy_start_aneg(phydev);
10372 }
10373 }
10374
10375 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010376 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010377 } else {
10378 tp->link_config.orig_advertising &=
10379 ~(ADVERTISED_Pause |
10380 ADVERTISED_Asym_Pause);
10381 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010382 }
10383 } else {
10384 int irq_sync = 0;
10385
10386 if (netif_running(dev)) {
10387 tg3_netif_stop(tp);
10388 irq_sync = 1;
10389 }
10390
10391 tg3_full_lock(tp, irq_sync);
10392
10393 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010394 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010395 else
Joe Perches63c3a662011-04-26 08:12:10 +000010396 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010397 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010398 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010399 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010400 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010401 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010402 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010403 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010404 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010405
10406 if (netif_running(dev)) {
10407 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10408 err = tg3_restart_hw(tp, 1);
10409 if (!err)
10410 tg3_netif_start(tp);
10411 }
10412
10413 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010415
Michael Chanb9ec6c12006-07-25 16:37:27 -070010416 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010417}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010418
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010419static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010420{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010421 switch (sset) {
10422 case ETH_SS_TEST:
10423 return TG3_NUM_TEST;
10424 case ETH_SS_STATS:
10425 return TG3_NUM_STATS;
10426 default:
10427 return -EOPNOTSUPP;
10428 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010429}
10430
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010431static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010432{
10433 switch (stringset) {
10434 case ETH_SS_STATS:
10435 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10436 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010437 case ETH_SS_TEST:
10438 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10439 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010440 default:
10441 WARN_ON(1); /* we need a WARN() */
10442 break;
10443 }
10444}
10445
stephen hemminger81b87092011-04-04 08:43:50 +000010446static int tg3_set_phys_id(struct net_device *dev,
10447 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010448{
10449 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010450
10451 if (!netif_running(tp->dev))
10452 return -EAGAIN;
10453
stephen hemminger81b87092011-04-04 08:43:50 +000010454 switch (state) {
10455 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010456 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010457
stephen hemminger81b87092011-04-04 08:43:50 +000010458 case ETHTOOL_ID_ON:
10459 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10460 LED_CTRL_1000MBPS_ON |
10461 LED_CTRL_100MBPS_ON |
10462 LED_CTRL_10MBPS_ON |
10463 LED_CTRL_TRAFFIC_OVERRIDE |
10464 LED_CTRL_TRAFFIC_BLINK |
10465 LED_CTRL_TRAFFIC_LED);
10466 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010467
stephen hemminger81b87092011-04-04 08:43:50 +000010468 case ETHTOOL_ID_OFF:
10469 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10470 LED_CTRL_TRAFFIC_OVERRIDE);
10471 break;
Michael Chan4009a932005-09-05 17:52:54 -070010472
stephen hemminger81b87092011-04-04 08:43:50 +000010473 case ETHTOOL_ID_INACTIVE:
10474 tw32(MAC_LED_CTRL, tp->led_ctrl);
10475 break;
Michael Chan4009a932005-09-05 17:52:54 -070010476 }
stephen hemminger81b87092011-04-04 08:43:50 +000010477
Michael Chan4009a932005-09-05 17:52:54 -070010478 return 0;
10479}
10480
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010481static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010482 struct ethtool_stats *estats, u64 *tmp_stats)
10483{
10484 struct tg3 *tp = netdev_priv(dev);
10485 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10486}
10487
Matt Carlsonc3e94502011-04-13 11:05:08 +000010488static __be32 * tg3_vpd_readblock(struct tg3 *tp)
10489{
10490 int i;
10491 __be32 *buf;
10492 u32 offset = 0, len = 0;
10493 u32 magic, val;
10494
Joe Perches63c3a662011-04-26 08:12:10 +000010495 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010496 return NULL;
10497
10498 if (magic == TG3_EEPROM_MAGIC) {
10499 for (offset = TG3_NVM_DIR_START;
10500 offset < TG3_NVM_DIR_END;
10501 offset += TG3_NVM_DIRENT_SIZE) {
10502 if (tg3_nvram_read(tp, offset, &val))
10503 return NULL;
10504
10505 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10506 TG3_NVM_DIRTYPE_EXTVPD)
10507 break;
10508 }
10509
10510 if (offset != TG3_NVM_DIR_END) {
10511 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10512 if (tg3_nvram_read(tp, offset + 4, &offset))
10513 return NULL;
10514
10515 offset = tg3_nvram_logical_addr(tp, offset);
10516 }
10517 }
10518
10519 if (!offset || !len) {
10520 offset = TG3_NVM_VPD_OFF;
10521 len = TG3_NVM_VPD_LEN;
10522 }
10523
10524 buf = kmalloc(len, GFP_KERNEL);
10525 if (buf == NULL)
10526 return NULL;
10527
10528 if (magic == TG3_EEPROM_MAGIC) {
10529 for (i = 0; i < len; i += 4) {
10530 /* The data is in little-endian format in NVRAM.
10531 * Use the big-endian read routines to preserve
10532 * the byte order as it exists in NVRAM.
10533 */
10534 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10535 goto error;
10536 }
10537 } else {
10538 u8 *ptr;
10539 ssize_t cnt;
10540 unsigned int pos = 0;
10541
10542 ptr = (u8 *)&buf[0];
10543 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10544 cnt = pci_read_vpd(tp->pdev, pos,
10545 len - pos, ptr);
10546 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10547 cnt = 0;
10548 else if (cnt < 0)
10549 goto error;
10550 }
10551 if (pos != len)
10552 goto error;
10553 }
10554
10555 return buf;
10556
10557error:
10558 kfree(buf);
10559 return NULL;
10560}
10561
Michael Chan566f86a2005-05-29 14:56:58 -070010562#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010563#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10564#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10565#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010566#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10567#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
10568#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x4c
Michael Chanb16250e2006-09-27 16:10:14 -070010569#define NVRAM_SELFBOOT_HW_SIZE 0x20
10570#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010571
10572static int tg3_test_nvram(struct tg3 *tp)
10573{
Al Virob9fc7dc2007-12-17 22:59:57 -080010574 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010575 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010576 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010577
Joe Perches63c3a662011-04-26 08:12:10 +000010578 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010579 return 0;
10580
Matt Carlsone4f34112009-02-25 14:25:00 +000010581 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010582 return -EIO;
10583
Michael Chan1b277772006-03-20 22:27:48 -080010584 if (magic == TG3_EEPROM_MAGIC)
10585 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010586 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010587 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10588 TG3_EEPROM_SB_FORMAT_1) {
10589 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10590 case TG3_EEPROM_SB_REVISION_0:
10591 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10592 break;
10593 case TG3_EEPROM_SB_REVISION_2:
10594 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10595 break;
10596 case TG3_EEPROM_SB_REVISION_3:
10597 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10598 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010599 case TG3_EEPROM_SB_REVISION_4:
10600 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10601 break;
10602 case TG3_EEPROM_SB_REVISION_5:
10603 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10604 break;
10605 case TG3_EEPROM_SB_REVISION_6:
10606 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10607 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010608 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010609 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010610 }
10611 } else
Michael Chan1b277772006-03-20 22:27:48 -080010612 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010613 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10614 size = NVRAM_SELFBOOT_HW_SIZE;
10615 else
Michael Chan1b277772006-03-20 22:27:48 -080010616 return -EIO;
10617
10618 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010619 if (buf == NULL)
10620 return -ENOMEM;
10621
Michael Chan1b277772006-03-20 22:27:48 -080010622 err = -EIO;
10623 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010624 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10625 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010626 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010627 }
Michael Chan1b277772006-03-20 22:27:48 -080010628 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010629 goto out;
10630
Michael Chan1b277772006-03-20 22:27:48 -080010631 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010632 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010633 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010634 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010635 u8 *buf8 = (u8 *) buf, csum8 = 0;
10636
Al Virob9fc7dc2007-12-17 22:59:57 -080010637 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010638 TG3_EEPROM_SB_REVISION_2) {
10639 /* For rev 2, the csum doesn't include the MBA. */
10640 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10641 csum8 += buf8[i];
10642 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10643 csum8 += buf8[i];
10644 } else {
10645 for (i = 0; i < size; i++)
10646 csum8 += buf8[i];
10647 }
Michael Chan1b277772006-03-20 22:27:48 -080010648
Adrian Bunkad96b482006-04-05 22:21:04 -070010649 if (csum8 == 0) {
10650 err = 0;
10651 goto out;
10652 }
10653
10654 err = -EIO;
10655 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010656 }
Michael Chan566f86a2005-05-29 14:56:58 -070010657
Al Virob9fc7dc2007-12-17 22:59:57 -080010658 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010659 TG3_EEPROM_MAGIC_HW) {
10660 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010661 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010662 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010663
10664 /* Separate the parity bits and the data bytes. */
10665 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10666 if ((i == 0) || (i == 8)) {
10667 int l;
10668 u8 msk;
10669
10670 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10671 parity[k++] = buf8[i] & msk;
10672 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010673 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010674 int l;
10675 u8 msk;
10676
10677 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10678 parity[k++] = buf8[i] & msk;
10679 i++;
10680
10681 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10682 parity[k++] = buf8[i] & msk;
10683 i++;
10684 }
10685 data[j++] = buf8[i];
10686 }
10687
10688 err = -EIO;
10689 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10690 u8 hw8 = hweight8(data[i]);
10691
10692 if ((hw8 & 0x1) && parity[i])
10693 goto out;
10694 else if (!(hw8 & 0x1) && !parity[i])
10695 goto out;
10696 }
10697 err = 0;
10698 goto out;
10699 }
10700
Matt Carlson01c3a392011-03-09 16:58:20 +000010701 err = -EIO;
10702
Michael Chan566f86a2005-05-29 14:56:58 -070010703 /* Bootstrap checksum at offset 0x10 */
10704 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010705 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010706 goto out;
10707
10708 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10709 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010710 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010711 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010712
Matt Carlsonc3e94502011-04-13 11:05:08 +000010713 kfree(buf);
10714
10715 buf = tg3_vpd_readblock(tp);
10716 if (!buf)
10717 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010718
10719 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10720 PCI_VPD_LRDT_RO_DATA);
10721 if (i > 0) {
10722 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10723 if (j < 0)
10724 goto out;
10725
10726 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10727 goto out;
10728
10729 i += PCI_VPD_LRDT_TAG_SIZE;
10730 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10731 PCI_VPD_RO_KEYWORD_CHKSUM);
10732 if (j > 0) {
10733 u8 csum8 = 0;
10734
10735 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10736
10737 for (i = 0; i <= j; i++)
10738 csum8 += ((u8 *)buf)[i];
10739
10740 if (csum8)
10741 goto out;
10742 }
10743 }
10744
Michael Chan566f86a2005-05-29 14:56:58 -070010745 err = 0;
10746
10747out:
10748 kfree(buf);
10749 return err;
10750}
10751
Michael Chanca430072005-05-29 14:57:23 -070010752#define TG3_SERDES_TIMEOUT_SEC 2
10753#define TG3_COPPER_TIMEOUT_SEC 6
10754
10755static int tg3_test_link(struct tg3 *tp)
10756{
10757 int i, max;
10758
10759 if (!netif_running(tp->dev))
10760 return -ENODEV;
10761
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010762 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010763 max = TG3_SERDES_TIMEOUT_SEC;
10764 else
10765 max = TG3_COPPER_TIMEOUT_SEC;
10766
10767 for (i = 0; i < max; i++) {
10768 if (netif_carrier_ok(tp->dev))
10769 return 0;
10770
10771 if (msleep_interruptible(1000))
10772 break;
10773 }
10774
10775 return -EIO;
10776}
10777
Michael Chana71116d2005-05-29 14:58:11 -070010778/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010779static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010780{
Michael Chanb16250e2006-09-27 16:10:14 -070010781 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010782 u32 offset, read_mask, write_mask, val, save_val, read_val;
10783 static struct {
10784 u16 offset;
10785 u16 flags;
10786#define TG3_FL_5705 0x1
10787#define TG3_FL_NOT_5705 0x2
10788#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010789#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010790 u32 read_mask;
10791 u32 write_mask;
10792 } reg_tbl[] = {
10793 /* MAC Control Registers */
10794 { MAC_MODE, TG3_FL_NOT_5705,
10795 0x00000000, 0x00ef6f8c },
10796 { MAC_MODE, TG3_FL_5705,
10797 0x00000000, 0x01ef6b8c },
10798 { MAC_STATUS, TG3_FL_NOT_5705,
10799 0x03800107, 0x00000000 },
10800 { MAC_STATUS, TG3_FL_5705,
10801 0x03800100, 0x00000000 },
10802 { MAC_ADDR_0_HIGH, 0x0000,
10803 0x00000000, 0x0000ffff },
10804 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010805 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010806 { MAC_RX_MTU_SIZE, 0x0000,
10807 0x00000000, 0x0000ffff },
10808 { MAC_TX_MODE, 0x0000,
10809 0x00000000, 0x00000070 },
10810 { MAC_TX_LENGTHS, 0x0000,
10811 0x00000000, 0x00003fff },
10812 { MAC_RX_MODE, TG3_FL_NOT_5705,
10813 0x00000000, 0x000007fc },
10814 { MAC_RX_MODE, TG3_FL_5705,
10815 0x00000000, 0x000007dc },
10816 { MAC_HASH_REG_0, 0x0000,
10817 0x00000000, 0xffffffff },
10818 { MAC_HASH_REG_1, 0x0000,
10819 0x00000000, 0xffffffff },
10820 { MAC_HASH_REG_2, 0x0000,
10821 0x00000000, 0xffffffff },
10822 { MAC_HASH_REG_3, 0x0000,
10823 0x00000000, 0xffffffff },
10824
10825 /* Receive Data and Receive BD Initiator Control Registers. */
10826 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10827 0x00000000, 0xffffffff },
10828 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10829 0x00000000, 0xffffffff },
10830 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10831 0x00000000, 0x00000003 },
10832 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10833 0x00000000, 0xffffffff },
10834 { RCVDBDI_STD_BD+0, 0x0000,
10835 0x00000000, 0xffffffff },
10836 { RCVDBDI_STD_BD+4, 0x0000,
10837 0x00000000, 0xffffffff },
10838 { RCVDBDI_STD_BD+8, 0x0000,
10839 0x00000000, 0xffff0002 },
10840 { RCVDBDI_STD_BD+0xc, 0x0000,
10841 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010842
Michael Chana71116d2005-05-29 14:58:11 -070010843 /* Receive BD Initiator Control Registers. */
10844 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10845 0x00000000, 0xffffffff },
10846 { RCVBDI_STD_THRESH, TG3_FL_5705,
10847 0x00000000, 0x000003ff },
10848 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10849 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010850
Michael Chana71116d2005-05-29 14:58:11 -070010851 /* Host Coalescing Control Registers. */
10852 { HOSTCC_MODE, TG3_FL_NOT_5705,
10853 0x00000000, 0x00000004 },
10854 { HOSTCC_MODE, TG3_FL_5705,
10855 0x00000000, 0x000000f6 },
10856 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10857 0x00000000, 0xffffffff },
10858 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10859 0x00000000, 0x000003ff },
10860 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10861 0x00000000, 0xffffffff },
10862 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10863 0x00000000, 0x000003ff },
10864 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10865 0x00000000, 0xffffffff },
10866 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10867 0x00000000, 0x000000ff },
10868 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10869 0x00000000, 0xffffffff },
10870 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10871 0x00000000, 0x000000ff },
10872 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10873 0x00000000, 0xffffffff },
10874 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10875 0x00000000, 0xffffffff },
10876 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10877 0x00000000, 0xffffffff },
10878 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10879 0x00000000, 0x000000ff },
10880 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10881 0x00000000, 0xffffffff },
10882 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10883 0x00000000, 0x000000ff },
10884 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10885 0x00000000, 0xffffffff },
10886 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10887 0x00000000, 0xffffffff },
10888 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10889 0x00000000, 0xffffffff },
10890 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10891 0x00000000, 0xffffffff },
10892 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10893 0x00000000, 0xffffffff },
10894 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10895 0xffffffff, 0x00000000 },
10896 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10897 0xffffffff, 0x00000000 },
10898
10899 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010900 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010901 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010902 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010903 0x00000000, 0x007fffff },
10904 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10905 0x00000000, 0x0000003f },
10906 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10907 0x00000000, 0x000001ff },
10908 { BUFMGR_MB_HIGH_WATER, 0x0000,
10909 0x00000000, 0x000001ff },
10910 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10911 0xffffffff, 0x00000000 },
10912 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10913 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010914
Michael Chana71116d2005-05-29 14:58:11 -070010915 /* Mailbox Registers */
10916 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10917 0x00000000, 0x000001ff },
10918 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10919 0x00000000, 0x000001ff },
10920 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10921 0x00000000, 0x000007ff },
10922 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10923 0x00000000, 0x000001ff },
10924
10925 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10926 };
10927
Michael Chanb16250e2006-09-27 16:10:14 -070010928 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010929 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070010930 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000010931 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070010932 is_5750 = 1;
10933 }
Michael Chana71116d2005-05-29 14:58:11 -070010934
10935 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10936 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10937 continue;
10938
10939 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10940 continue;
10941
Joe Perches63c3a662011-04-26 08:12:10 +000010942 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070010943 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10944 continue;
10945
Michael Chanb16250e2006-09-27 16:10:14 -070010946 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10947 continue;
10948
Michael Chana71116d2005-05-29 14:58:11 -070010949 offset = (u32) reg_tbl[i].offset;
10950 read_mask = reg_tbl[i].read_mask;
10951 write_mask = reg_tbl[i].write_mask;
10952
10953 /* Save the original register content */
10954 save_val = tr32(offset);
10955
10956 /* Determine the read-only value. */
10957 read_val = save_val & read_mask;
10958
10959 /* Write zero to the register, then make sure the read-only bits
10960 * are not changed and the read/write bits are all zeros.
10961 */
10962 tw32(offset, 0);
10963
10964 val = tr32(offset);
10965
10966 /* Test the read-only and read/write bits. */
10967 if (((val & read_mask) != read_val) || (val & write_mask))
10968 goto out;
10969
10970 /* Write ones to all the bits defined by RdMask and WrMask, then
10971 * make sure the read-only bits are not changed and the
10972 * read/write bits are all ones.
10973 */
10974 tw32(offset, read_mask | write_mask);
10975
10976 val = tr32(offset);
10977
10978 /* Test the read-only bits. */
10979 if ((val & read_mask) != read_val)
10980 goto out;
10981
10982 /* Test the read/write bits. */
10983 if ((val & write_mask) != write_mask)
10984 goto out;
10985
10986 tw32(offset, save_val);
10987 }
10988
10989 return 0;
10990
10991out:
Michael Chan9f88f292006-12-07 00:22:54 -080010992 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010993 netdev_err(tp->dev,
10994 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010995 tw32(offset, save_val);
10996 return -EIO;
10997}
10998
Michael Chan7942e1d2005-05-29 14:58:36 -070010999static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11000{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011001 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011002 int i;
11003 u32 j;
11004
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011005 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011006 for (j = 0; j < len; j += 4) {
11007 u32 val;
11008
11009 tg3_write_mem(tp, offset + j, test_pattern[i]);
11010 tg3_read_mem(tp, offset + j, &val);
11011 if (val != test_pattern[i])
11012 return -EIO;
11013 }
11014 }
11015 return 0;
11016}
11017
11018static int tg3_test_memory(struct tg3 *tp)
11019{
11020 static struct mem_entry {
11021 u32 offset;
11022 u32 len;
11023 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011024 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011025 { 0x00002000, 0x1c000},
11026 { 0xffffffff, 0x00000}
11027 }, mem_tbl_5705[] = {
11028 { 0x00000100, 0x0000c},
11029 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011030 { 0x00004000, 0x00800},
11031 { 0x00006000, 0x01000},
11032 { 0x00008000, 0x02000},
11033 { 0x00010000, 0x0e000},
11034 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011035 }, mem_tbl_5755[] = {
11036 { 0x00000200, 0x00008},
11037 { 0x00004000, 0x00800},
11038 { 0x00006000, 0x00800},
11039 { 0x00008000, 0x02000},
11040 { 0x00010000, 0x0c000},
11041 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011042 }, mem_tbl_5906[] = {
11043 { 0x00000200, 0x00008},
11044 { 0x00004000, 0x00400},
11045 { 0x00006000, 0x00400},
11046 { 0x00008000, 0x01000},
11047 { 0x00010000, 0x01000},
11048 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011049 }, mem_tbl_5717[] = {
11050 { 0x00000200, 0x00008},
11051 { 0x00010000, 0x0a000},
11052 { 0x00020000, 0x13c00},
11053 { 0xffffffff, 0x00000}
11054 }, mem_tbl_57765[] = {
11055 { 0x00000200, 0x00008},
11056 { 0x00004000, 0x00800},
11057 { 0x00006000, 0x09800},
11058 { 0x00010000, 0x0a000},
11059 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011060 };
11061 struct mem_entry *mem_tbl;
11062 int err = 0;
11063 int i;
11064
Joe Perches63c3a662011-04-26 08:12:10 +000011065 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011066 mem_tbl = mem_tbl_5717;
11067 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11068 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011069 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011070 mem_tbl = mem_tbl_5755;
11071 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11072 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011073 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011074 mem_tbl = mem_tbl_5705;
11075 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011076 mem_tbl = mem_tbl_570x;
11077
11078 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011079 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11080 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011081 break;
11082 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011083
Michael Chan7942e1d2005-05-29 14:58:36 -070011084 return err;
11085}
11086
Michael Chan9f40dea2005-09-05 17:53:06 -070011087#define TG3_MAC_LOOPBACK 0
11088#define TG3_PHY_LOOPBACK 1
Matt Carlsonbb158d62011-04-25 12:42:47 +000011089#define TG3_TSO_LOOPBACK 2
11090
11091#define TG3_TSO_MSS 500
11092
11093#define TG3_TSO_IP_HDR_LEN 20
11094#define TG3_TSO_TCP_HDR_LEN 20
11095#define TG3_TSO_TCP_OPT_LEN 12
11096
11097static const u8 tg3_tso_header[] = {
110980x08, 0x00,
110990x45, 0x00, 0x00, 0x00,
111000x00, 0x00, 0x40, 0x00,
111010x40, 0x06, 0x00, 0x00,
111020x0a, 0x00, 0x00, 0x01,
111030x0a, 0x00, 0x00, 0x02,
111040x0d, 0x00, 0xe0, 0x00,
111050x00, 0x00, 0x01, 0x00,
111060x00, 0x00, 0x02, 0x00,
111070x80, 0x10, 0x10, 0x00,
111080x14, 0x09, 0x00, 0x00,
111090x01, 0x01, 0x08, 0x0a,
111100x11, 0x11, 0x11, 0x11,
111110x11, 0x11, 0x11, 0x11,
11112};
Michael Chan9f40dea2005-09-05 17:53:06 -070011113
Matt Carlson4852a862011-04-13 11:05:07 +000011114static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070011115{
Michael Chan9f40dea2005-09-05 17:53:06 -070011116 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011117 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Michael Chanc76949a2005-05-29 14:58:59 -070011118 struct sk_buff *skb, *rx_skb;
11119 u8 *tx_data;
11120 dma_addr_t map;
11121 int num_pkts, tx_len, rx_len, i, err;
11122 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011123 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011124 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011125
Matt Carlsonc8873402010-02-12 14:47:11 +000011126 tnapi = &tp->napi[0];
11127 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011128 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011129 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011130 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011131 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011132 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011133 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011134 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011135
Michael Chan9f40dea2005-09-05 17:53:06 -070011136 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070011137 /* HW errata - mac loopback fails in some cases on 5780.
11138 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000011139 * errata. Also, the MAC loopback test is deprecated for
11140 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070011141 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000011142 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011143 tg3_flag(tp, CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070011144 return 0;
11145
Matt Carlson49692ca2011-01-25 15:58:52 +000011146 mac_mode = tp->mac_mode &
11147 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
11148 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Joe Perches63c3a662011-04-26 08:12:10 +000011149 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011150 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011151 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070011152 mac_mode |= MAC_MODE_PORT_MODE_MII;
11153 else
11154 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070011155 tw32(MAC_MODE, mac_mode);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011156 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011157 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000011158 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080011159 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
11160 } else
11161 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070011162
Matt Carlson9ef8ca92007-07-11 19:48:29 -070011163 tg3_phy_toggle_automdix(tp, 0);
11164
Michael Chan3f7045c2006-09-27 16:02:29 -070011165 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070011166 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080011167
Matt Carlson49692ca2011-01-25 15:58:52 +000011168 mac_mode = tp->mac_mode &
11169 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011170 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000011171 tg3_writephy(tp, MII_TG3_FET_PTEST,
11172 MII_TG3_FET_PTEST_FRC_TX_LINK |
11173 MII_TG3_FET_PTEST_FRC_TX_LOCK);
11174 /* The write needs to be flushed for the AC131 */
11175 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11176 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080011177 mac_mode |= MAC_MODE_PORT_MODE_MII;
11178 } else
11179 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070011180
Michael Chanc94e3942005-09-27 12:12:42 -070011181 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011182 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070011183 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
11184 udelay(10);
11185 tw32_f(MAC_RX_MODE, tp->rx_mode);
11186 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011187 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000011188 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
11189 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011190 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000011191 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011192 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080011193 tg3_writephy(tp, MII_TG3_EXT_CTRL,
11194 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
11195 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011196 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000011197
11198 /* Wait for link */
11199 for (i = 0; i < 100; i++) {
11200 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11201 break;
11202 mdelay(1);
11203 }
Matt Carlson859a5882010-04-05 10:19:28 +000011204 }
Michael Chanc76949a2005-05-29 14:58:59 -070011205
11206 err = -EIO;
11207
Matt Carlson4852a862011-04-13 11:05:07 +000011208 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011209 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011210 if (!skb)
11211 return -ENOMEM;
11212
Michael Chanc76949a2005-05-29 14:58:59 -070011213 tx_data = skb_put(skb, tx_len);
11214 memcpy(tx_data, tp->dev->dev_addr, 6);
11215 memset(tx_data + 6, 0x0, 8);
11216
Matt Carlson4852a862011-04-13 11:05:07 +000011217 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011218
Matt Carlsonbb158d62011-04-25 12:42:47 +000011219 if (loopback_mode == TG3_TSO_LOOPBACK) {
11220 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11221
11222 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11223 TG3_TSO_TCP_OPT_LEN;
11224
11225 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11226 sizeof(tg3_tso_header));
11227 mss = TG3_TSO_MSS;
11228
11229 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11230 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11231
11232 /* Set the total length field in the IP header */
11233 iph->tot_len = htons((u16)(mss + hdr_len));
11234
11235 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11236 TXD_FLAG_CPU_POST_DMA);
11237
Joe Perches63c3a662011-04-26 08:12:10 +000011238 if (tg3_flag(tp, HW_TSO_1) ||
11239 tg3_flag(tp, HW_TSO_2) ||
11240 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011241 struct tcphdr *th;
11242 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11243 th = (struct tcphdr *)&tx_data[val];
11244 th->check = 0;
11245 } else
11246 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11247
Joe Perches63c3a662011-04-26 08:12:10 +000011248 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011249 mss |= (hdr_len & 0xc) << 12;
11250 if (hdr_len & 0x10)
11251 base_flags |= 0x00000010;
11252 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011253 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011254 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011255 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011256 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11257 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11258 } else {
11259 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11260 }
11261
11262 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11263 } else {
11264 num_pkts = 1;
11265 data_off = ETH_HLEN;
11266 }
11267
11268 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011269 tx_data[i] = (u8) (i & 0xff);
11270
Alexander Duyckf4188d82009-12-02 16:48:38 +000011271 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11272 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011273 dev_kfree_skb(skb);
11274 return -EIO;
11275 }
Michael Chanc76949a2005-05-29 14:58:59 -070011276
11277 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011278 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011279
11280 udelay(10);
11281
Matt Carlson898a56f2009-08-28 14:02:40 +000011282 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011283
Matt Carlsonbb158d62011-04-25 12:42:47 +000011284 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len,
11285 base_flags, (mss << 1) | 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011286
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011287 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011288
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011289 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11290 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011291
11292 udelay(10);
11293
Matt Carlson303fc922009-11-02 14:27:34 +000011294 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11295 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011296 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011297 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011298
11299 udelay(10);
11300
Matt Carlson898a56f2009-08-28 14:02:40 +000011301 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11302 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011303 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011304 (rx_idx == (rx_start_idx + num_pkts)))
11305 break;
11306 }
11307
Alexander Duyckf4188d82009-12-02 16:48:38 +000011308 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011309 dev_kfree_skb(skb);
11310
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011311 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011312 goto out;
11313
11314 if (rx_idx != rx_start_idx + num_pkts)
11315 goto out;
11316
Matt Carlsonbb158d62011-04-25 12:42:47 +000011317 val = data_off;
11318 while (rx_idx != rx_start_idx) {
11319 desc = &rnapi->rx_rcb[rx_start_idx++];
11320 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11321 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011322
Matt Carlsonbb158d62011-04-25 12:42:47 +000011323 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11324 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011325 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011326
Matt Carlsonbb158d62011-04-25 12:42:47 +000011327 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11328 - ETH_FCS_LEN;
11329
11330 if (loopback_mode != TG3_TSO_LOOPBACK) {
11331 if (rx_len != tx_len)
11332 goto out;
11333
11334 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11335 if (opaque_key != RXD_OPAQUE_RING_STD)
11336 goto out;
11337 } else {
11338 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11339 goto out;
11340 }
11341 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11342 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011343 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011344 goto out;
11345 }
11346
11347 if (opaque_key == RXD_OPAQUE_RING_STD) {
11348 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11349 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11350 mapping);
11351 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
11352 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11353 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11354 mapping);
11355 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011356 goto out;
11357
Matt Carlsonbb158d62011-04-25 12:42:47 +000011358 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11359 PCI_DMA_FROMDEVICE);
11360
11361 for (i = data_off; i < rx_len; i++, val++) {
11362 if (*(rx_skb->data + i) != (u8) (val & 0xff))
11363 goto out;
11364 }
Matt Carlson4852a862011-04-13 11:05:07 +000011365 }
11366
Michael Chanc76949a2005-05-29 14:58:59 -070011367 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011368
Michael Chanc76949a2005-05-29 14:58:59 -070011369 /* tg3_free_rings will unmap and free the rx_skb */
11370out:
11371 return err;
11372}
11373
Matt Carlson00c266b2011-04-25 12:42:46 +000011374#define TG3_STD_LOOPBACK_FAILED 1
11375#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011376#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson00c266b2011-04-25 12:42:46 +000011377
11378#define TG3_MAC_LOOPBACK_SHIFT 0
11379#define TG3_PHY_LOOPBACK_SHIFT 4
Matt Carlsonbb158d62011-04-25 12:42:47 +000011380#define TG3_LOOPBACK_FAILED 0x00000077
Michael Chan9f40dea2005-09-05 17:53:06 -070011381
11382static int tg3_test_loopback(struct tg3 *tp)
11383{
11384 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011385 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011386
11387 if (!netif_running(tp->dev))
11388 return TG3_LOOPBACK_FAILED;
11389
Matt Carlsonab789042011-01-25 15:58:54 +000011390 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11391 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11392
Michael Chanb9ec6c12006-07-25 16:37:27 -070011393 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011394 if (err) {
11395 err = TG3_LOOPBACK_FAILED;
11396 goto done;
11397 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011398
Joe Perches63c3a662011-04-26 08:12:10 +000011399 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011400 int i;
11401
11402 /* Reroute all rx packets to the 1st queue */
11403 for (i = MAC_RSS_INDIR_TBL_0;
11404 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11405 tw32(i, 0x0);
11406 }
11407
Matt Carlson6833c042008-11-21 17:18:59 -080011408 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011409 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011410 tg3_phy_toggle_apd(tp, false);
11411
Joe Perches63c3a662011-04-26 08:12:10 +000011412 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011413 int i;
11414 u32 status;
11415
11416 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11417
11418 /* Wait for up to 40 microseconds to acquire lock. */
11419 for (i = 0; i < 4; i++) {
11420 status = tr32(TG3_CPMU_MUTEX_GNT);
11421 if (status == CPMU_MUTEX_GNT_DRIVER)
11422 break;
11423 udelay(10);
11424 }
11425
Matt Carlsonab789042011-01-25 15:58:54 +000011426 if (status != CPMU_MUTEX_GNT_DRIVER) {
11427 err = TG3_LOOPBACK_FAILED;
11428 goto done;
11429 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011430
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011431 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011432 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011433 tw32(TG3_CPMU_CTRL,
11434 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11435 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011436 }
11437
Matt Carlson4852a862011-04-13 11:05:07 +000011438 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011439 err |= TG3_STD_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011440
Joe Perches63c3a662011-04-26 08:12:10 +000011441 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011442 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011443 err |= TG3_JMB_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson4852a862011-04-13 11:05:07 +000011444
Joe Perches63c3a662011-04-26 08:12:10 +000011445 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011446 tw32(TG3_CPMU_CTRL, cpmuctrl);
11447
11448 /* Release the mutex */
11449 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11450 }
11451
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011452 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011453 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson4852a862011-04-13 11:05:07 +000011454 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011455 err |= TG3_STD_LOOPBACK_FAILED <<
11456 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011457 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonbb158d62011-04-25 12:42:47 +000011458 tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_TSO_LOOPBACK))
11459 err |= TG3_TSO_LOOPBACK_FAILED <<
11460 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011461 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011462 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011463 err |= TG3_JMB_LOOPBACK_FAILED <<
11464 TG3_PHY_LOOPBACK_SHIFT;
Michael Chan9f40dea2005-09-05 17:53:06 -070011465 }
11466
Matt Carlson6833c042008-11-21 17:18:59 -080011467 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011468 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011469 tg3_phy_toggle_apd(tp, true);
11470
Matt Carlsonab789042011-01-25 15:58:54 +000011471done:
11472 tp->phy_flags |= eee_cap;
11473
Michael Chan9f40dea2005-09-05 17:53:06 -070011474 return err;
11475}
11476
Michael Chan4cafd3f2005-05-29 14:56:34 -070011477static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11478 u64 *data)
11479{
Michael Chan566f86a2005-05-29 14:56:58 -070011480 struct tg3 *tp = netdev_priv(dev);
11481
Matt Carlsonbed98292011-07-13 09:27:29 +000011482 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11483 tg3_power_up(tp)) {
11484 etest->flags |= ETH_TEST_FL_FAILED;
11485 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11486 return;
11487 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011488
Michael Chan566f86a2005-05-29 14:56:58 -070011489 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11490
11491 if (tg3_test_nvram(tp) != 0) {
11492 etest->flags |= ETH_TEST_FL_FAILED;
11493 data[0] = 1;
11494 }
Michael Chanca430072005-05-29 14:57:23 -070011495 if (tg3_test_link(tp) != 0) {
11496 etest->flags |= ETH_TEST_FL_FAILED;
11497 data[1] = 1;
11498 }
Michael Chana71116d2005-05-29 14:58:11 -070011499 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011500 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011501
Michael Chanbbe832c2005-06-24 20:20:04 -070011502 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011503 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011504 tg3_netif_stop(tp);
11505 irq_sync = 1;
11506 }
11507
11508 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011509
11510 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011511 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011512 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011513 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011514 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011515 if (!err)
11516 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011517
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011518 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011519 tg3_phy_reset(tp);
11520
Michael Chana71116d2005-05-29 14:58:11 -070011521 if (tg3_test_registers(tp) != 0) {
11522 etest->flags |= ETH_TEST_FL_FAILED;
11523 data[2] = 1;
11524 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011525 if (tg3_test_memory(tp) != 0) {
11526 etest->flags |= ETH_TEST_FL_FAILED;
11527 data[3] = 1;
11528 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011529 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011530 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011531
David S. Millerf47c11e2005-06-24 20:18:35 -070011532 tg3_full_unlock(tp);
11533
Michael Chand4bc3922005-05-29 14:59:20 -070011534 if (tg3_test_interrupt(tp) != 0) {
11535 etest->flags |= ETH_TEST_FL_FAILED;
11536 data[5] = 1;
11537 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011538
11539 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011540
Michael Chana71116d2005-05-29 14:58:11 -070011541 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11542 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011543 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011544 err2 = tg3_restart_hw(tp, 1);
11545 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011546 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011547 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011548
11549 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011550
11551 if (irq_sync && !err2)
11552 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011553 }
Matt Carlson80096062010-08-02 11:26:06 +000011554 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011555 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011556
Michael Chan4cafd3f2005-05-29 14:56:34 -070011557}
11558
Linus Torvalds1da177e2005-04-16 15:20:36 -070011559static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11560{
11561 struct mii_ioctl_data *data = if_mii(ifr);
11562 struct tg3 *tp = netdev_priv(dev);
11563 int err;
11564
Joe Perches63c3a662011-04-26 08:12:10 +000011565 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011566 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011567 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011568 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011569 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011570 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011571 }
11572
Matt Carlson33f401a2010-04-05 10:19:27 +000011573 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011574 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011575 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011576
11577 /* fallthru */
11578 case SIOCGMIIREG: {
11579 u32 mii_regval;
11580
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011581 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011582 break; /* We have no PHY */
11583
Matt Carlson34eea5a2011-04-20 07:57:38 +000011584 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011585 return -EAGAIN;
11586
David S. Millerf47c11e2005-06-24 20:18:35 -070011587 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011588 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011589 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011590
11591 data->val_out = mii_regval;
11592
11593 return err;
11594 }
11595
11596 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011597 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011598 break; /* We have no PHY */
11599
Matt Carlson34eea5a2011-04-20 07:57:38 +000011600 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011601 return -EAGAIN;
11602
David S. Millerf47c11e2005-06-24 20:18:35 -070011603 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011604 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011605 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011606
11607 return err;
11608
11609 default:
11610 /* do nothing */
11611 break;
11612 }
11613 return -EOPNOTSUPP;
11614}
11615
David S. Miller15f98502005-05-18 22:49:26 -070011616static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11617{
11618 struct tg3 *tp = netdev_priv(dev);
11619
11620 memcpy(ec, &tp->coal, sizeof(*ec));
11621 return 0;
11622}
11623
Michael Chand244c892005-07-05 14:42:33 -070011624static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11625{
11626 struct tg3 *tp = netdev_priv(dev);
11627 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11628 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11629
Joe Perches63c3a662011-04-26 08:12:10 +000011630 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011631 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11632 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11633 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11634 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11635 }
11636
11637 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11638 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11639 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11640 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11641 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11642 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11643 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11644 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11645 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11646 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11647 return -EINVAL;
11648
11649 /* No rx interrupts will be generated if both are zero */
11650 if ((ec->rx_coalesce_usecs == 0) &&
11651 (ec->rx_max_coalesced_frames == 0))
11652 return -EINVAL;
11653
11654 /* No tx interrupts will be generated if both are zero */
11655 if ((ec->tx_coalesce_usecs == 0) &&
11656 (ec->tx_max_coalesced_frames == 0))
11657 return -EINVAL;
11658
11659 /* Only copy relevant parameters, ignore all others. */
11660 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11661 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11662 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11663 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11664 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11665 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11666 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11667 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11668 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11669
11670 if (netif_running(dev)) {
11671 tg3_full_lock(tp, 0);
11672 __tg3_set_coalesce(tp, &tp->coal);
11673 tg3_full_unlock(tp);
11674 }
11675 return 0;
11676}
11677
Jeff Garzik7282d492006-09-13 14:30:00 -040011678static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011679 .get_settings = tg3_get_settings,
11680 .set_settings = tg3_set_settings,
11681 .get_drvinfo = tg3_get_drvinfo,
11682 .get_regs_len = tg3_get_regs_len,
11683 .get_regs = tg3_get_regs,
11684 .get_wol = tg3_get_wol,
11685 .set_wol = tg3_set_wol,
11686 .get_msglevel = tg3_get_msglevel,
11687 .set_msglevel = tg3_set_msglevel,
11688 .nway_reset = tg3_nway_reset,
11689 .get_link = ethtool_op_get_link,
11690 .get_eeprom_len = tg3_get_eeprom_len,
11691 .get_eeprom = tg3_get_eeprom,
11692 .set_eeprom = tg3_set_eeprom,
11693 .get_ringparam = tg3_get_ringparam,
11694 .set_ringparam = tg3_set_ringparam,
11695 .get_pauseparam = tg3_get_pauseparam,
11696 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011697 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011698 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011699 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011700 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011701 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011702 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011703 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011704};
11705
11706static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11707{
Michael Chan1b277772006-03-20 22:27:48 -080011708 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011709
11710 tp->nvram_size = EEPROM_CHIP_SIZE;
11711
Matt Carlsone4f34112009-02-25 14:25:00 +000011712 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011713 return;
11714
Michael Chanb16250e2006-09-27 16:10:14 -070011715 if ((magic != TG3_EEPROM_MAGIC) &&
11716 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11717 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011718 return;
11719
11720 /*
11721 * Size the chip by reading offsets at increasing powers of two.
11722 * When we encounter our validation signature, we know the addressing
11723 * has wrapped around, and thus have our chip size.
11724 */
Michael Chan1b277772006-03-20 22:27:48 -080011725 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011726
11727 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011728 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011729 return;
11730
Michael Chan18201802006-03-20 22:29:15 -080011731 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011732 break;
11733
11734 cursize <<= 1;
11735 }
11736
11737 tp->nvram_size = cursize;
11738}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011739
Linus Torvalds1da177e2005-04-16 15:20:36 -070011740static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11741{
11742 u32 val;
11743
Joe Perches63c3a662011-04-26 08:12:10 +000011744 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011745 return;
11746
11747 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011748 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011749 tg3_get_eeprom_size(tp);
11750 return;
11751 }
11752
Matt Carlson6d348f22009-02-25 14:25:52 +000011753 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011754 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011755 /* This is confusing. We want to operate on the
11756 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11757 * call will read from NVRAM and byteswap the data
11758 * according to the byteswapping settings for all
11759 * other register accesses. This ensures the data we
11760 * want will always reside in the lower 16-bits.
11761 * However, the data in NVRAM is in LE format, which
11762 * means the data from the NVRAM read will always be
11763 * opposite the endianness of the CPU. The 16-bit
11764 * byteswap then brings the data to CPU endianness.
11765 */
11766 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011767 return;
11768 }
11769 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011770 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011771}
11772
11773static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11774{
11775 u32 nvcfg1;
11776
11777 nvcfg1 = tr32(NVRAM_CFG1);
11778 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000011779 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011780 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011781 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11782 tw32(NVRAM_CFG1, nvcfg1);
11783 }
11784
Matt Carlson6ff6f812011-05-19 12:12:54 +000011785 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011786 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011787 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011788 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11789 tp->nvram_jedecnum = JEDEC_ATMEL;
11790 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011791 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011792 break;
11793 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11794 tp->nvram_jedecnum = JEDEC_ATMEL;
11795 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11796 break;
11797 case FLASH_VENDOR_ATMEL_EEPROM:
11798 tp->nvram_jedecnum = JEDEC_ATMEL;
11799 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011800 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011801 break;
11802 case FLASH_VENDOR_ST:
11803 tp->nvram_jedecnum = JEDEC_ST;
11804 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011805 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011806 break;
11807 case FLASH_VENDOR_SAIFUN:
11808 tp->nvram_jedecnum = JEDEC_SAIFUN;
11809 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11810 break;
11811 case FLASH_VENDOR_SST_SMALL:
11812 case FLASH_VENDOR_SST_LARGE:
11813 tp->nvram_jedecnum = JEDEC_SST;
11814 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11815 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011816 }
Matt Carlson8590a602009-08-28 12:29:16 +000011817 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011818 tp->nvram_jedecnum = JEDEC_ATMEL;
11819 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011820 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011821 }
11822}
11823
Matt Carlsona1b950d2009-09-01 13:20:17 +000011824static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11825{
11826 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11827 case FLASH_5752PAGE_SIZE_256:
11828 tp->nvram_pagesize = 256;
11829 break;
11830 case FLASH_5752PAGE_SIZE_512:
11831 tp->nvram_pagesize = 512;
11832 break;
11833 case FLASH_5752PAGE_SIZE_1K:
11834 tp->nvram_pagesize = 1024;
11835 break;
11836 case FLASH_5752PAGE_SIZE_2K:
11837 tp->nvram_pagesize = 2048;
11838 break;
11839 case FLASH_5752PAGE_SIZE_4K:
11840 tp->nvram_pagesize = 4096;
11841 break;
11842 case FLASH_5752PAGE_SIZE_264:
11843 tp->nvram_pagesize = 264;
11844 break;
11845 case FLASH_5752PAGE_SIZE_528:
11846 tp->nvram_pagesize = 528;
11847 break;
11848 }
11849}
11850
Michael Chan361b4ac2005-04-21 17:11:21 -070011851static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11852{
11853 u32 nvcfg1;
11854
11855 nvcfg1 = tr32(NVRAM_CFG1);
11856
Michael Chane6af3012005-04-21 17:12:05 -070011857 /* NVRAM protection for TPM */
11858 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000011859 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070011860
Michael Chan361b4ac2005-04-21 17:11:21 -070011861 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011862 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11863 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11864 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011865 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011866 break;
11867 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11868 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011869 tg3_flag_set(tp, NVRAM_BUFFERED);
11870 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011871 break;
11872 case FLASH_5752VENDOR_ST_M45PE10:
11873 case FLASH_5752VENDOR_ST_M45PE20:
11874 case FLASH_5752VENDOR_ST_M45PE40:
11875 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011876 tg3_flag_set(tp, NVRAM_BUFFERED);
11877 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011878 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011879 }
11880
Joe Perches63c3a662011-04-26 08:12:10 +000011881 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011882 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011883 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011884 /* For eeprom, set pagesize to maximum eeprom size */
11885 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11886
11887 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11888 tw32(NVRAM_CFG1, nvcfg1);
11889 }
11890}
11891
Michael Chand3c7b882006-03-23 01:28:25 -080011892static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11893{
Matt Carlson989a9d22007-05-05 11:51:05 -070011894 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011895
11896 nvcfg1 = tr32(NVRAM_CFG1);
11897
11898 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011899 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011900 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070011901 protect = 1;
11902 }
Michael Chand3c7b882006-03-23 01:28:25 -080011903
Matt Carlson989a9d22007-05-05 11:51:05 -070011904 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11905 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011906 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11907 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11908 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11909 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11910 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011911 tg3_flag_set(tp, NVRAM_BUFFERED);
11912 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011913 tp->nvram_pagesize = 264;
11914 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11915 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11916 tp->nvram_size = (protect ? 0x3e200 :
11917 TG3_NVRAM_SIZE_512KB);
11918 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11919 tp->nvram_size = (protect ? 0x1f200 :
11920 TG3_NVRAM_SIZE_256KB);
11921 else
11922 tp->nvram_size = (protect ? 0x1f200 :
11923 TG3_NVRAM_SIZE_128KB);
11924 break;
11925 case FLASH_5752VENDOR_ST_M45PE10:
11926 case FLASH_5752VENDOR_ST_M45PE20:
11927 case FLASH_5752VENDOR_ST_M45PE40:
11928 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011929 tg3_flag_set(tp, NVRAM_BUFFERED);
11930 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011931 tp->nvram_pagesize = 256;
11932 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11933 tp->nvram_size = (protect ?
11934 TG3_NVRAM_SIZE_64KB :
11935 TG3_NVRAM_SIZE_128KB);
11936 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11937 tp->nvram_size = (protect ?
11938 TG3_NVRAM_SIZE_64KB :
11939 TG3_NVRAM_SIZE_256KB);
11940 else
11941 tp->nvram_size = (protect ?
11942 TG3_NVRAM_SIZE_128KB :
11943 TG3_NVRAM_SIZE_512KB);
11944 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011945 }
11946}
11947
Michael Chan1b277772006-03-20 22:27:48 -080011948static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11949{
11950 u32 nvcfg1;
11951
11952 nvcfg1 = tr32(NVRAM_CFG1);
11953
11954 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011955 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11956 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11957 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11958 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11959 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011960 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011961 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011962
Matt Carlson8590a602009-08-28 12:29:16 +000011963 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11964 tw32(NVRAM_CFG1, nvcfg1);
11965 break;
11966 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11967 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11968 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11969 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11970 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011971 tg3_flag_set(tp, NVRAM_BUFFERED);
11972 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011973 tp->nvram_pagesize = 264;
11974 break;
11975 case FLASH_5752VENDOR_ST_M45PE10:
11976 case FLASH_5752VENDOR_ST_M45PE20:
11977 case FLASH_5752VENDOR_ST_M45PE40:
11978 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011979 tg3_flag_set(tp, NVRAM_BUFFERED);
11980 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011981 tp->nvram_pagesize = 256;
11982 break;
Michael Chan1b277772006-03-20 22:27:48 -080011983 }
11984}
11985
Matt Carlson6b91fa02007-10-10 18:01:09 -070011986static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11987{
11988 u32 nvcfg1, protect = 0;
11989
11990 nvcfg1 = tr32(NVRAM_CFG1);
11991
11992 /* NVRAM protection for TPM */
11993 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011994 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011995 protect = 1;
11996 }
11997
11998 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11999 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012000 case FLASH_5761VENDOR_ATMEL_ADB021D:
12001 case FLASH_5761VENDOR_ATMEL_ADB041D:
12002 case FLASH_5761VENDOR_ATMEL_ADB081D:
12003 case FLASH_5761VENDOR_ATMEL_ADB161D:
12004 case FLASH_5761VENDOR_ATMEL_MDB021D:
12005 case FLASH_5761VENDOR_ATMEL_MDB041D:
12006 case FLASH_5761VENDOR_ATMEL_MDB081D:
12007 case FLASH_5761VENDOR_ATMEL_MDB161D:
12008 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012009 tg3_flag_set(tp, NVRAM_BUFFERED);
12010 tg3_flag_set(tp, FLASH);
12011 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012012 tp->nvram_pagesize = 256;
12013 break;
12014 case FLASH_5761VENDOR_ST_A_M45PE20:
12015 case FLASH_5761VENDOR_ST_A_M45PE40:
12016 case FLASH_5761VENDOR_ST_A_M45PE80:
12017 case FLASH_5761VENDOR_ST_A_M45PE16:
12018 case FLASH_5761VENDOR_ST_M_M45PE20:
12019 case FLASH_5761VENDOR_ST_M_M45PE40:
12020 case FLASH_5761VENDOR_ST_M_M45PE80:
12021 case FLASH_5761VENDOR_ST_M_M45PE16:
12022 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012023 tg3_flag_set(tp, NVRAM_BUFFERED);
12024 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012025 tp->nvram_pagesize = 256;
12026 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012027 }
12028
12029 if (protect) {
12030 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12031 } else {
12032 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012033 case FLASH_5761VENDOR_ATMEL_ADB161D:
12034 case FLASH_5761VENDOR_ATMEL_MDB161D:
12035 case FLASH_5761VENDOR_ST_A_M45PE16:
12036 case FLASH_5761VENDOR_ST_M_M45PE16:
12037 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12038 break;
12039 case FLASH_5761VENDOR_ATMEL_ADB081D:
12040 case FLASH_5761VENDOR_ATMEL_MDB081D:
12041 case FLASH_5761VENDOR_ST_A_M45PE80:
12042 case FLASH_5761VENDOR_ST_M_M45PE80:
12043 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12044 break;
12045 case FLASH_5761VENDOR_ATMEL_ADB041D:
12046 case FLASH_5761VENDOR_ATMEL_MDB041D:
12047 case FLASH_5761VENDOR_ST_A_M45PE40:
12048 case FLASH_5761VENDOR_ST_M_M45PE40:
12049 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12050 break;
12051 case FLASH_5761VENDOR_ATMEL_ADB021D:
12052 case FLASH_5761VENDOR_ATMEL_MDB021D:
12053 case FLASH_5761VENDOR_ST_A_M45PE20:
12054 case FLASH_5761VENDOR_ST_M_M45PE20:
12055 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12056 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012057 }
12058 }
12059}
12060
Michael Chanb5d37722006-09-27 16:06:21 -070012061static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12062{
12063 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012064 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012065 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12066}
12067
Matt Carlson321d32a2008-11-21 17:22:19 -080012068static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12069{
12070 u32 nvcfg1;
12071
12072 nvcfg1 = tr32(NVRAM_CFG1);
12073
12074 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12075 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12076 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12077 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012078 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012079 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12080
12081 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12082 tw32(NVRAM_CFG1, nvcfg1);
12083 return;
12084 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12085 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12086 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12087 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12088 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12089 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12090 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12091 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012092 tg3_flag_set(tp, NVRAM_BUFFERED);
12093 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012094
12095 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12096 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12097 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12098 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12099 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12100 break;
12101 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12102 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12103 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12104 break;
12105 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12106 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12107 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12108 break;
12109 }
12110 break;
12111 case FLASH_5752VENDOR_ST_M45PE10:
12112 case FLASH_5752VENDOR_ST_M45PE20:
12113 case FLASH_5752VENDOR_ST_M45PE40:
12114 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012115 tg3_flag_set(tp, NVRAM_BUFFERED);
12116 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012117
12118 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12119 case FLASH_5752VENDOR_ST_M45PE10:
12120 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12121 break;
12122 case FLASH_5752VENDOR_ST_M45PE20:
12123 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12124 break;
12125 case FLASH_5752VENDOR_ST_M45PE40:
12126 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12127 break;
12128 }
12129 break;
12130 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012131 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012132 return;
12133 }
12134
Matt Carlsona1b950d2009-09-01 13:20:17 +000012135 tg3_nvram_get_pagesize(tp, nvcfg1);
12136 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012137 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012138}
12139
12140
12141static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12142{
12143 u32 nvcfg1;
12144
12145 nvcfg1 = tr32(NVRAM_CFG1);
12146
12147 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12148 case FLASH_5717VENDOR_ATMEL_EEPROM:
12149 case FLASH_5717VENDOR_MICRO_EEPROM:
12150 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012151 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012152 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12153
12154 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12155 tw32(NVRAM_CFG1, nvcfg1);
12156 return;
12157 case FLASH_5717VENDOR_ATMEL_MDB011D:
12158 case FLASH_5717VENDOR_ATMEL_ADB011B:
12159 case FLASH_5717VENDOR_ATMEL_ADB011D:
12160 case FLASH_5717VENDOR_ATMEL_MDB021D:
12161 case FLASH_5717VENDOR_ATMEL_ADB021B:
12162 case FLASH_5717VENDOR_ATMEL_ADB021D:
12163 case FLASH_5717VENDOR_ATMEL_45USPT:
12164 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012165 tg3_flag_set(tp, NVRAM_BUFFERED);
12166 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012167
12168 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12169 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012170 /* Detect size with tg3_nvram_get_size() */
12171 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012172 case FLASH_5717VENDOR_ATMEL_ADB021B:
12173 case FLASH_5717VENDOR_ATMEL_ADB021D:
12174 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12175 break;
12176 default:
12177 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12178 break;
12179 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012180 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012181 case FLASH_5717VENDOR_ST_M_M25PE10:
12182 case FLASH_5717VENDOR_ST_A_M25PE10:
12183 case FLASH_5717VENDOR_ST_M_M45PE10:
12184 case FLASH_5717VENDOR_ST_A_M45PE10:
12185 case FLASH_5717VENDOR_ST_M_M25PE20:
12186 case FLASH_5717VENDOR_ST_A_M25PE20:
12187 case FLASH_5717VENDOR_ST_M_M45PE20:
12188 case FLASH_5717VENDOR_ST_A_M45PE20:
12189 case FLASH_5717VENDOR_ST_25USPT:
12190 case FLASH_5717VENDOR_ST_45USPT:
12191 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012192 tg3_flag_set(tp, NVRAM_BUFFERED);
12193 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012194
12195 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12196 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012197 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012198 /* Detect size with tg3_nvram_get_size() */
12199 break;
12200 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012201 case FLASH_5717VENDOR_ST_A_M45PE20:
12202 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12203 break;
12204 default:
12205 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12206 break;
12207 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012208 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012209 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012210 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012211 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012212 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012213
12214 tg3_nvram_get_pagesize(tp, nvcfg1);
12215 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012216 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012217}
12218
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012219static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12220{
12221 u32 nvcfg1, nvmpinstrp;
12222
12223 nvcfg1 = tr32(NVRAM_CFG1);
12224 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12225
12226 switch (nvmpinstrp) {
12227 case FLASH_5720_EEPROM_HD:
12228 case FLASH_5720_EEPROM_LD:
12229 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012230 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012231
12232 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12233 tw32(NVRAM_CFG1, nvcfg1);
12234 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12235 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12236 else
12237 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12238 return;
12239 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12240 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12241 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12242 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12243 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12244 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12245 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12246 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12247 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12248 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12249 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12250 case FLASH_5720VENDOR_ATMEL_45USPT:
12251 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012252 tg3_flag_set(tp, NVRAM_BUFFERED);
12253 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012254
12255 switch (nvmpinstrp) {
12256 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12257 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12258 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12259 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12260 break;
12261 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12262 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12263 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12264 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12265 break;
12266 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12267 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12268 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12269 break;
12270 default:
12271 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12272 break;
12273 }
12274 break;
12275 case FLASH_5720VENDOR_M_ST_M25PE10:
12276 case FLASH_5720VENDOR_M_ST_M45PE10:
12277 case FLASH_5720VENDOR_A_ST_M25PE10:
12278 case FLASH_5720VENDOR_A_ST_M45PE10:
12279 case FLASH_5720VENDOR_M_ST_M25PE20:
12280 case FLASH_5720VENDOR_M_ST_M45PE20:
12281 case FLASH_5720VENDOR_A_ST_M25PE20:
12282 case FLASH_5720VENDOR_A_ST_M45PE20:
12283 case FLASH_5720VENDOR_M_ST_M25PE40:
12284 case FLASH_5720VENDOR_M_ST_M45PE40:
12285 case FLASH_5720VENDOR_A_ST_M25PE40:
12286 case FLASH_5720VENDOR_A_ST_M45PE40:
12287 case FLASH_5720VENDOR_M_ST_M25PE80:
12288 case FLASH_5720VENDOR_M_ST_M45PE80:
12289 case FLASH_5720VENDOR_A_ST_M25PE80:
12290 case FLASH_5720VENDOR_A_ST_M45PE80:
12291 case FLASH_5720VENDOR_ST_25USPT:
12292 case FLASH_5720VENDOR_ST_45USPT:
12293 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012294 tg3_flag_set(tp, NVRAM_BUFFERED);
12295 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012296
12297 switch (nvmpinstrp) {
12298 case FLASH_5720VENDOR_M_ST_M25PE20:
12299 case FLASH_5720VENDOR_M_ST_M45PE20:
12300 case FLASH_5720VENDOR_A_ST_M25PE20:
12301 case FLASH_5720VENDOR_A_ST_M45PE20:
12302 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12303 break;
12304 case FLASH_5720VENDOR_M_ST_M25PE40:
12305 case FLASH_5720VENDOR_M_ST_M45PE40:
12306 case FLASH_5720VENDOR_A_ST_M25PE40:
12307 case FLASH_5720VENDOR_A_ST_M45PE40:
12308 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12309 break;
12310 case FLASH_5720VENDOR_M_ST_M25PE80:
12311 case FLASH_5720VENDOR_M_ST_M45PE80:
12312 case FLASH_5720VENDOR_A_ST_M25PE80:
12313 case FLASH_5720VENDOR_A_ST_M45PE80:
12314 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12315 break;
12316 default:
12317 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12318 break;
12319 }
12320 break;
12321 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012322 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012323 return;
12324 }
12325
12326 tg3_nvram_get_pagesize(tp, nvcfg1);
12327 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012328 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012329}
12330
Linus Torvalds1da177e2005-04-16 15:20:36 -070012331/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12332static void __devinit tg3_nvram_init(struct tg3 *tp)
12333{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012334 tw32_f(GRC_EEPROM_ADDR,
12335 (EEPROM_ADDR_FSM_RESET |
12336 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12337 EEPROM_ADDR_CLKPERD_SHIFT)));
12338
Michael Chan9d57f012006-12-07 00:23:25 -080012339 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012340
12341 /* Enable seeprom accesses. */
12342 tw32_f(GRC_LOCAL_CTRL,
12343 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12344 udelay(100);
12345
12346 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12347 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012348 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012349
Michael Chanec41c7d2006-01-17 02:40:55 -080012350 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012351 netdev_warn(tp->dev,
12352 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012353 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012354 return;
12355 }
Michael Chane6af3012005-04-21 17:12:05 -070012356 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012357
Matt Carlson989a9d22007-05-05 11:51:05 -070012358 tp->nvram_size = 0;
12359
Michael Chan361b4ac2005-04-21 17:11:21 -070012360 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12361 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012362 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12363 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012364 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012365 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12366 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012367 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012368 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12369 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012370 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12371 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012372 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12373 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012374 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012375 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12376 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012377 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012378 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12379 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012380 else
12381 tg3_get_nvram_info(tp);
12382
Matt Carlson989a9d22007-05-05 11:51:05 -070012383 if (tp->nvram_size == 0)
12384 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012385
Michael Chane6af3012005-04-21 17:12:05 -070012386 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012387 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012388
12389 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012390 tg3_flag_clear(tp, NVRAM);
12391 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012392
12393 tg3_get_eeprom_size(tp);
12394 }
12395}
12396
Linus Torvalds1da177e2005-04-16 15:20:36 -070012397static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12398 u32 offset, u32 len, u8 *buf)
12399{
12400 int i, j, rc = 0;
12401 u32 val;
12402
12403 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012404 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012405 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012406
12407 addr = offset + i;
12408
12409 memcpy(&data, buf + i, 4);
12410
Matt Carlson62cedd12009-04-20 14:52:29 -070012411 /*
12412 * The SEEPROM interface expects the data to always be opposite
12413 * the native endian format. We accomplish this by reversing
12414 * all the operations that would have been performed on the
12415 * data from a call to tg3_nvram_read_be32().
12416 */
12417 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012418
12419 val = tr32(GRC_EEPROM_ADDR);
12420 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12421
12422 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12423 EEPROM_ADDR_READ);
12424 tw32(GRC_EEPROM_ADDR, val |
12425 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12426 (addr & EEPROM_ADDR_ADDR_MASK) |
12427 EEPROM_ADDR_START |
12428 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012429
Michael Chan9d57f012006-12-07 00:23:25 -080012430 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012431 val = tr32(GRC_EEPROM_ADDR);
12432
12433 if (val & EEPROM_ADDR_COMPLETE)
12434 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012435 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012436 }
12437 if (!(val & EEPROM_ADDR_COMPLETE)) {
12438 rc = -EBUSY;
12439 break;
12440 }
12441 }
12442
12443 return rc;
12444}
12445
12446/* offset and length are dword aligned */
12447static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12448 u8 *buf)
12449{
12450 int ret = 0;
12451 u32 pagesize = tp->nvram_pagesize;
12452 u32 pagemask = pagesize - 1;
12453 u32 nvram_cmd;
12454 u8 *tmp;
12455
12456 tmp = kmalloc(pagesize, GFP_KERNEL);
12457 if (tmp == NULL)
12458 return -ENOMEM;
12459
12460 while (len) {
12461 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012462 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012463
12464 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012465
Linus Torvalds1da177e2005-04-16 15:20:36 -070012466 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012467 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12468 (__be32 *) (tmp + j));
12469 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012470 break;
12471 }
12472 if (ret)
12473 break;
12474
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012475 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012476 size = pagesize;
12477 if (len < size)
12478 size = len;
12479
12480 len -= size;
12481
12482 memcpy(tmp + page_off, buf, size);
12483
12484 offset = offset + (pagesize - page_off);
12485
Michael Chane6af3012005-04-21 17:12:05 -070012486 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012487
12488 /*
12489 * Before we can erase the flash page, we need
12490 * to issue a special "write enable" command.
12491 */
12492 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12493
12494 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12495 break;
12496
12497 /* Erase the target page */
12498 tw32(NVRAM_ADDR, phy_addr);
12499
12500 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12501 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12502
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012503 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012504 break;
12505
12506 /* Issue another write enable to start the write. */
12507 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12508
12509 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12510 break;
12511
12512 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012513 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012514
Al Virob9fc7dc2007-12-17 22:59:57 -080012515 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012516
Al Virob9fc7dc2007-12-17 22:59:57 -080012517 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012518
12519 tw32(NVRAM_ADDR, phy_addr + j);
12520
12521 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12522 NVRAM_CMD_WR;
12523
12524 if (j == 0)
12525 nvram_cmd |= NVRAM_CMD_FIRST;
12526 else if (j == (pagesize - 4))
12527 nvram_cmd |= NVRAM_CMD_LAST;
12528
12529 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12530 break;
12531 }
12532 if (ret)
12533 break;
12534 }
12535
12536 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12537 tg3_nvram_exec_cmd(tp, nvram_cmd);
12538
12539 kfree(tmp);
12540
12541 return ret;
12542}
12543
12544/* offset and length are dword aligned */
12545static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12546 u8 *buf)
12547{
12548 int i, ret = 0;
12549
12550 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012551 u32 page_off, phy_addr, nvram_cmd;
12552 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012553
12554 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012555 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012556
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012557 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012558
Michael Chan18201802006-03-20 22:29:15 -080012559 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012560
12561 tw32(NVRAM_ADDR, phy_addr);
12562
12563 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12564
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012565 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012566 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012567 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012568 nvram_cmd |= NVRAM_CMD_LAST;
12569
12570 if (i == (len - 4))
12571 nvram_cmd |= NVRAM_CMD_LAST;
12572
Matt Carlson321d32a2008-11-21 17:22:19 -080012573 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012574 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012575 (tp->nvram_jedecnum == JEDEC_ST) &&
12576 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012577
12578 if ((ret = tg3_nvram_exec_cmd(tp,
12579 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12580 NVRAM_CMD_DONE)))
12581
12582 break;
12583 }
Joe Perches63c3a662011-04-26 08:12:10 +000012584 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012585 /* We always do complete word writes to eeprom. */
12586 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12587 }
12588
12589 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12590 break;
12591 }
12592 return ret;
12593}
12594
12595/* offset and length are dword aligned */
12596static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12597{
12598 int ret;
12599
Joe Perches63c3a662011-04-26 08:12:10 +000012600 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012601 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12602 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012603 udelay(40);
12604 }
12605
Joe Perches63c3a662011-04-26 08:12:10 +000012606 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012607 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012608 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012609 u32 grc_mode;
12610
Michael Chanec41c7d2006-01-17 02:40:55 -080012611 ret = tg3_nvram_lock(tp);
12612 if (ret)
12613 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012614
Michael Chane6af3012005-04-21 17:12:05 -070012615 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012616 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012617 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012618
12619 grc_mode = tr32(GRC_MODE);
12620 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12621
Joe Perches63c3a662011-04-26 08:12:10 +000012622 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012623 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12624 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012625 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012626 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12627 buf);
12628 }
12629
12630 grc_mode = tr32(GRC_MODE);
12631 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12632
Michael Chane6af3012005-04-21 17:12:05 -070012633 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012634 tg3_nvram_unlock(tp);
12635 }
12636
Joe Perches63c3a662011-04-26 08:12:10 +000012637 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012638 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012639 udelay(40);
12640 }
12641
12642 return ret;
12643}
12644
12645struct subsys_tbl_ent {
12646 u16 subsys_vendor, subsys_devid;
12647 u32 phy_id;
12648};
12649
Matt Carlson24daf2b2010-02-17 15:17:02 +000012650static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012651 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012652 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012653 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012654 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012655 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012656 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012657 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012658 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12659 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12660 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012661 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012662 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012663 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012664 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12665 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12666 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012667 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012668 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012669 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012670 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012671 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012672 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012673 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012674
12675 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012676 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012677 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012678 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012679 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012680 { TG3PCI_SUBVENDOR_ID_3COM,
12681 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12682 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012683 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012684 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012685 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012686
12687 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012688 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012689 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012690 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012691 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012692 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012693 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012694 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012695 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012696
12697 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012698 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012699 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012700 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012701 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012702 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12703 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12704 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012705 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012706 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012707 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012708
12709 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012710 { TG3PCI_SUBVENDOR_ID_IBM,
12711 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012712};
12713
Matt Carlson24daf2b2010-02-17 15:17:02 +000012714static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012715{
12716 int i;
12717
12718 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12719 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12720 tp->pdev->subsystem_vendor) &&
12721 (subsys_id_to_phy_id[i].subsys_devid ==
12722 tp->pdev->subsystem_device))
12723 return &subsys_id_to_phy_id[i];
12724 }
12725 return NULL;
12726}
12727
Michael Chan7d0c41e2005-04-21 17:06:20 -070012728static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012729{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012730 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070012731
Matt Carlson79eb6902010-02-17 15:17:03 +000012732 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012733 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12734
Gary Zambranoa85feb82007-05-05 11:52:19 -070012735 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012736 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12737 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012738
Michael Chanb5d37722006-09-27 16:06:21 -070012739 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012740 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012741 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12742 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012743 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012744 val = tr32(VCPU_CFGSHDW);
12745 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012746 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012747 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012748 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012749 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012750 device_set_wakeup_enable(&tp->pdev->dev, true);
12751 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012752 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012753 }
12754
Linus Torvalds1da177e2005-04-16 15:20:36 -070012755 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12756 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12757 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012758 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012759 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012760
12761 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12762 tp->nic_sram_data_cfg = nic_cfg;
12763
12764 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12765 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000012766 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12767 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
12768 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012769 (ver > 0) && (ver < 0x100))
12770 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12771
Matt Carlsona9daf362008-05-25 23:49:44 -070012772 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12773 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12774
Linus Torvalds1da177e2005-04-16 15:20:36 -070012775 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12776 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12777 eeprom_phy_serdes = 1;
12778
12779 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12780 if (nic_phy_id != 0) {
12781 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12782 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12783
12784 eeprom_phy_id = (id1 >> 16) << 10;
12785 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12786 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12787 } else
12788 eeprom_phy_id = 0;
12789
Michael Chan7d0c41e2005-04-21 17:06:20 -070012790 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012791 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000012792 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012793 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012794 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012795 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012796 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012797
Joe Perches63c3a662011-04-26 08:12:10 +000012798 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012799 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12800 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012801 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012802 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12803
12804 switch (led_cfg) {
12805 default:
12806 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12807 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12808 break;
12809
12810 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12811 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12812 break;
12813
12814 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12815 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012816
12817 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12818 * read on some older 5700/5701 bootcode.
12819 */
12820 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12821 ASIC_REV_5700 ||
12822 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12823 ASIC_REV_5701)
12824 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12825
Linus Torvalds1da177e2005-04-16 15:20:36 -070012826 break;
12827
12828 case SHASTA_EXT_LED_SHARED:
12829 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12830 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12831 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12832 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12833 LED_CTRL_MODE_PHY_2);
12834 break;
12835
12836 case SHASTA_EXT_LED_MAC:
12837 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12838 break;
12839
12840 case SHASTA_EXT_LED_COMBO:
12841 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12842 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12843 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12844 LED_CTRL_MODE_PHY_2);
12845 break;
12846
Stephen Hemminger855e1112008-04-16 16:37:28 -070012847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012848
12849 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12850 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12851 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12852 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12853
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012854 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12855 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012856
Michael Chan9d26e212006-12-07 00:21:14 -080012857 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000012858 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012859 if ((tp->pdev->subsystem_vendor ==
12860 PCI_VENDOR_ID_ARIMA) &&
12861 (tp->pdev->subsystem_device == 0x205a ||
12862 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000012863 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012864 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012865 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12866 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012868
12869 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000012870 tg3_flag_set(tp, ENABLE_ASF);
12871 if (tg3_flag(tp, 5750_PLUS))
12872 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012873 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012874
12875 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012876 tg3_flag(tp, 5750_PLUS))
12877 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012878
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012879 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012880 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000012881 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012882
Joe Perches63c3a662011-04-26 08:12:10 +000012883 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012884 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012885 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012886 device_set_wakeup_enable(&tp->pdev->dev, true);
12887 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012888
Linus Torvalds1da177e2005-04-16 15:20:36 -070012889 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012890 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012891
12892 /* serdes signal pre-emphasis in register 0x590 set by */
12893 /* bootcode if bit 18 is set */
12894 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012895 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012896
Joe Perches63c3a662011-04-26 08:12:10 +000012897 if ((tg3_flag(tp, 57765_PLUS) ||
12898 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12899 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012900 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012901 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012902
Joe Perches63c3a662011-04-26 08:12:10 +000012903 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012904 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012905 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012906 u32 cfg3;
12907
12908 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12909 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000012910 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070012911 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012912
Matt Carlson14417062010-02-17 15:16:59 +000012913 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000012914 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070012915 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012916 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070012917 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012918 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012919 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012920done:
Joe Perches63c3a662011-04-26 08:12:10 +000012921 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012922 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000012923 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012924 else
12925 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012926}
12927
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012928static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12929{
12930 int i;
12931 u32 val;
12932
12933 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12934 tw32(OTP_CTRL, cmd);
12935
12936 /* Wait for up to 1 ms for command to execute. */
12937 for (i = 0; i < 100; i++) {
12938 val = tr32(OTP_STATUS);
12939 if (val & OTP_STATUS_CMD_DONE)
12940 break;
12941 udelay(10);
12942 }
12943
12944 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12945}
12946
12947/* Read the gphy configuration from the OTP region of the chip. The gphy
12948 * configuration is a 32-bit value that straddles the alignment boundary.
12949 * We do two 32-bit reads and then shift and merge the results.
12950 */
12951static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12952{
12953 u32 bhalf_otp, thalf_otp;
12954
12955 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12956
12957 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12958 return 0;
12959
12960 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12961
12962 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12963 return 0;
12964
12965 thalf_otp = tr32(OTP_READ_DATA);
12966
12967 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12968
12969 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12970 return 0;
12971
12972 bhalf_otp = tr32(OTP_READ_DATA);
12973
12974 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12975}
12976
Matt Carlsone256f8a2011-03-09 16:58:24 +000012977static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12978{
12979 u32 adv = ADVERTISED_Autoneg |
12980 ADVERTISED_Pause;
12981
12982 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12983 adv |= ADVERTISED_1000baseT_Half |
12984 ADVERTISED_1000baseT_Full;
12985
12986 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12987 adv |= ADVERTISED_100baseT_Half |
12988 ADVERTISED_100baseT_Full |
12989 ADVERTISED_10baseT_Half |
12990 ADVERTISED_10baseT_Full |
12991 ADVERTISED_TP;
12992 else
12993 adv |= ADVERTISED_FIBRE;
12994
12995 tp->link_config.advertising = adv;
12996 tp->link_config.speed = SPEED_INVALID;
12997 tp->link_config.duplex = DUPLEX_INVALID;
12998 tp->link_config.autoneg = AUTONEG_ENABLE;
12999 tp->link_config.active_speed = SPEED_INVALID;
13000 tp->link_config.active_duplex = DUPLEX_INVALID;
13001 tp->link_config.orig_speed = SPEED_INVALID;
13002 tp->link_config.orig_duplex = DUPLEX_INVALID;
13003 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13004}
13005
Michael Chan7d0c41e2005-04-21 17:06:20 -070013006static int __devinit tg3_phy_probe(struct tg3 *tp)
13007{
13008 u32 hw_phy_id_1, hw_phy_id_2;
13009 u32 hw_phy_id, hw_phy_id_masked;
13010 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013011
Matt Carlsone256f8a2011-03-09 16:58:24 +000013012 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013013 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013014 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13015
Joe Perches63c3a662011-04-26 08:12:10 +000013016 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013017 return tg3_phy_init(tp);
13018
Linus Torvalds1da177e2005-04-16 15:20:36 -070013019 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013020 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013021 */
13022 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013023 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013024 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013025 } else {
13026 /* Now read the physical PHY_ID from the chip and verify
13027 * that it is sane. If it doesn't look good, we fall back
13028 * to either the hard-coded table based PHY_ID and failing
13029 * that the value found in the eeprom area.
13030 */
13031 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13032 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13033
13034 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13035 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13036 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13037
Matt Carlson79eb6902010-02-17 15:17:03 +000013038 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013039 }
13040
Matt Carlson79eb6902010-02-17 15:17:03 +000013041 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013042 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013043 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013044 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013045 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013046 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013047 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013048 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013049 /* Do nothing, phy ID already set up in
13050 * tg3_get_eeprom_hw_cfg().
13051 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013052 } else {
13053 struct subsys_tbl_ent *p;
13054
13055 /* No eeprom signature? Try the hardcoded
13056 * subsys device table.
13057 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013058 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013059 if (!p)
13060 return -ENODEV;
13061
13062 tp->phy_id = p->phy_id;
13063 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013064 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013065 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013066 }
13067 }
13068
Matt Carlsona6b68da2010-12-06 08:28:52 +000013069 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
13070 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
13071 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13072 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13073 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013074 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13075
Matt Carlsone256f8a2011-03-09 16:58:24 +000013076 tg3_phy_init_link_config(tp);
13077
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013078 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013079 !tg3_flag(tp, ENABLE_APE) &&
13080 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013081 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013082
13083 tg3_readphy(tp, MII_BMSR, &bmsr);
13084 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13085 (bmsr & BMSR_LSTATUS))
13086 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013087
Linus Torvalds1da177e2005-04-16 15:20:36 -070013088 err = tg3_phy_reset(tp);
13089 if (err)
13090 return err;
13091
Matt Carlson42b64a42011-05-19 12:12:49 +000013092 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013093
Michael Chan3600d912006-12-07 00:21:48 -080013094 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13095 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13096 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13097 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013098 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13099 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013100
13101 tg3_writephy(tp, MII_BMCR,
13102 BMCR_ANENABLE | BMCR_ANRESTART);
13103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013104 }
13105
13106skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013107 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013108 err = tg3_init_5401phy_dsp(tp);
13109 if (err)
13110 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013111
Linus Torvalds1da177e2005-04-16 15:20:36 -070013112 err = tg3_init_5401phy_dsp(tp);
13113 }
13114
Linus Torvalds1da177e2005-04-16 15:20:36 -070013115 return err;
13116}
13117
Matt Carlson184b8902010-04-05 10:19:25 +000013118static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013119{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013120 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013121 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000013122 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013123
Matt Carlsonc3e94502011-04-13 11:05:08 +000013124 vpd_data = (u8 *)tg3_vpd_readblock(tp);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013125 if (!vpd_data)
13126 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013127
Matt Carlson4181b2c2010-02-26 14:04:45 +000013128 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
13129 PCI_VPD_LRDT_RO_DATA);
13130 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013131 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013132
13133 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13134 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13135 i += PCI_VPD_LRDT_TAG_SIZE;
13136
13137 if (block_end > TG3_NVM_VPD_LEN)
13138 goto out_not_found;
13139
Matt Carlson184b8902010-04-05 10:19:25 +000013140 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13141 PCI_VPD_RO_KEYWORD_MFR_ID);
13142 if (j > 0) {
13143 len = pci_vpd_info_field_size(&vpd_data[j]);
13144
13145 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13146 if (j + len > block_end || len != 4 ||
13147 memcmp(&vpd_data[j], "1028", 4))
13148 goto partno;
13149
13150 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13151 PCI_VPD_RO_KEYWORD_VENDOR0);
13152 if (j < 0)
13153 goto partno;
13154
13155 len = pci_vpd_info_field_size(&vpd_data[j]);
13156
13157 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13158 if (j + len > block_end)
13159 goto partno;
13160
13161 memcpy(tp->fw_ver, &vpd_data[j], len);
13162 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
13163 }
13164
13165partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013166 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13167 PCI_VPD_RO_KEYWORD_PARTNO);
13168 if (i < 0)
13169 goto out_not_found;
13170
13171 len = pci_vpd_info_field_size(&vpd_data[i]);
13172
13173 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13174 if (len > TG3_BPN_SIZE ||
13175 (len + i) > TG3_NVM_VPD_LEN)
13176 goto out_not_found;
13177
13178 memcpy(tp->board_part_number, &vpd_data[i], len);
13179
Linus Torvalds1da177e2005-04-16 15:20:36 -070013180out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013181 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013182 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013183 return;
13184
13185out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013186 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13187 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13188 strcpy(tp->board_part_number, "BCM5717");
13189 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13190 strcpy(tp->board_part_number, "BCM5718");
13191 else
13192 goto nomatch;
13193 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13194 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13195 strcpy(tp->board_part_number, "BCM57780");
13196 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13197 strcpy(tp->board_part_number, "BCM57760");
13198 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13199 strcpy(tp->board_part_number, "BCM57790");
13200 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13201 strcpy(tp->board_part_number, "BCM57788");
13202 else
13203 goto nomatch;
13204 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13205 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13206 strcpy(tp->board_part_number, "BCM57761");
13207 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13208 strcpy(tp->board_part_number, "BCM57765");
13209 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13210 strcpy(tp->board_part_number, "BCM57781");
13211 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13212 strcpy(tp->board_part_number, "BCM57785");
13213 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13214 strcpy(tp->board_part_number, "BCM57791");
13215 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13216 strcpy(tp->board_part_number, "BCM57795");
13217 else
13218 goto nomatch;
13219 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013220 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013221 } else {
13222nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013223 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013225}
13226
Matt Carlson9c8a6202007-10-21 16:16:08 -070013227static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13228{
13229 u32 val;
13230
Matt Carlsone4f34112009-02-25 14:25:00 +000013231 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013232 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013233 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013234 val != 0)
13235 return 0;
13236
13237 return 1;
13238}
13239
Matt Carlsonacd9c112009-02-25 14:26:33 +000013240static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13241{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013242 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013243 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013244 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013245
13246 if (tg3_nvram_read(tp, 0xc, &offset) ||
13247 tg3_nvram_read(tp, 0x4, &start))
13248 return;
13249
13250 offset = tg3_nvram_logical_addr(tp, offset);
13251
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013252 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013253 return;
13254
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013255 if ((val & 0xfc000000) == 0x0c000000) {
13256 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013257 return;
13258
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013259 if (val == 0)
13260 newver = true;
13261 }
13262
Matt Carlson75f99362010-04-05 10:19:24 +000013263 dst_off = strlen(tp->fw_ver);
13264
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013265 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013266 if (TG3_VER_SIZE - dst_off < 16 ||
13267 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013268 return;
13269
13270 offset = offset + ver_offset - start;
13271 for (i = 0; i < 16; i += 4) {
13272 __be32 v;
13273 if (tg3_nvram_read_be32(tp, offset + i, &v))
13274 return;
13275
Matt Carlson75f99362010-04-05 10:19:24 +000013276 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013277 }
13278 } else {
13279 u32 major, minor;
13280
13281 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13282 return;
13283
13284 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13285 TG3_NVM_BCVER_MAJSFT;
13286 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013287 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13288 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013289 }
13290}
13291
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013292static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13293{
13294 u32 val, major, minor;
13295
13296 /* Use native endian representation */
13297 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13298 return;
13299
13300 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13301 TG3_NVM_HWSB_CFG1_MAJSFT;
13302 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13303 TG3_NVM_HWSB_CFG1_MINSFT;
13304
13305 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13306}
13307
Matt Carlsondfe00d72008-11-21 17:19:41 -080013308static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13309{
13310 u32 offset, major, minor, build;
13311
Matt Carlson75f99362010-04-05 10:19:24 +000013312 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013313
13314 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13315 return;
13316
13317 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13318 case TG3_EEPROM_SB_REVISION_0:
13319 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13320 break;
13321 case TG3_EEPROM_SB_REVISION_2:
13322 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13323 break;
13324 case TG3_EEPROM_SB_REVISION_3:
13325 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13326 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013327 case TG3_EEPROM_SB_REVISION_4:
13328 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13329 break;
13330 case TG3_EEPROM_SB_REVISION_5:
13331 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13332 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013333 case TG3_EEPROM_SB_REVISION_6:
13334 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13335 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013336 default:
13337 return;
13338 }
13339
Matt Carlsone4f34112009-02-25 14:25:00 +000013340 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013341 return;
13342
13343 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13344 TG3_EEPROM_SB_EDH_BLD_SHFT;
13345 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13346 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13347 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13348
13349 if (minor > 99 || build > 26)
13350 return;
13351
Matt Carlson75f99362010-04-05 10:19:24 +000013352 offset = strlen(tp->fw_ver);
13353 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13354 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013355
13356 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013357 offset = strlen(tp->fw_ver);
13358 if (offset < TG3_VER_SIZE - 1)
13359 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013360 }
13361}
13362
Matt Carlsonacd9c112009-02-25 14:26:33 +000013363static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013364{
13365 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013366 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013367
13368 for (offset = TG3_NVM_DIR_START;
13369 offset < TG3_NVM_DIR_END;
13370 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013371 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013372 return;
13373
13374 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13375 break;
13376 }
13377
13378 if (offset == TG3_NVM_DIR_END)
13379 return;
13380
Joe Perches63c3a662011-04-26 08:12:10 +000013381 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013382 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013383 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013384 return;
13385
Matt Carlsone4f34112009-02-25 14:25:00 +000013386 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013387 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013388 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013389 return;
13390
13391 offset += val - start;
13392
Matt Carlsonacd9c112009-02-25 14:26:33 +000013393 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013394
Matt Carlsonacd9c112009-02-25 14:26:33 +000013395 tp->fw_ver[vlen++] = ',';
13396 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013397
13398 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013399 __be32 v;
13400 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013401 return;
13402
Al Virob9fc7dc2007-12-17 22:59:57 -080013403 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013404
Matt Carlsonacd9c112009-02-25 14:26:33 +000013405 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13406 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013407 break;
13408 }
13409
Matt Carlsonacd9c112009-02-25 14:26:33 +000013410 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13411 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013412 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013413}
13414
Matt Carlson7fd76442009-02-25 14:27:20 +000013415static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13416{
13417 int vlen;
13418 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013419 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013420
Joe Perches63c3a662011-04-26 08:12:10 +000013421 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013422 return;
13423
13424 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13425 if (apedata != APE_SEG_SIG_MAGIC)
13426 return;
13427
13428 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13429 if (!(apedata & APE_FW_STATUS_READY))
13430 return;
13431
13432 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13433
Matt Carlsondc6d0742010-09-15 08:59:55 +000013434 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013435 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013436 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013437 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013438 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013439 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013440
Matt Carlson7fd76442009-02-25 14:27:20 +000013441 vlen = strlen(tp->fw_ver);
13442
Matt Carlsonecc79642010-08-02 11:26:01 +000013443 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13444 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013445 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13446 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13447 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13448 (apedata & APE_FW_VERSION_BLDMSK));
13449}
13450
Matt Carlsonacd9c112009-02-25 14:26:33 +000013451static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13452{
13453 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013454 bool vpd_vers = false;
13455
13456 if (tp->fw_ver[0] != 0)
13457 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013458
Joe Perches63c3a662011-04-26 08:12:10 +000013459 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013460 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013461 return;
13462 }
13463
Matt Carlsonacd9c112009-02-25 14:26:33 +000013464 if (tg3_nvram_read(tp, 0, &val))
13465 return;
13466
13467 if (val == TG3_EEPROM_MAGIC)
13468 tg3_read_bc_ver(tp);
13469 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13470 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013471 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13472 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013473 else
13474 return;
13475
Matt Carlsonc9cab242011-07-13 09:27:27 +000013476 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013477 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013478
Matt Carlsonc9cab242011-07-13 09:27:27 +000013479 if (tg3_flag(tp, ENABLE_APE)) {
13480 if (tg3_flag(tp, ENABLE_ASF))
13481 tg3_read_dash_ver(tp);
13482 } else if (tg3_flag(tp, ENABLE_ASF)) {
13483 tg3_read_mgmtfw_ver(tp);
13484 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013485
Matt Carlson75f99362010-04-05 10:19:24 +000013486done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013487 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013488}
13489
Michael Chan7544b092007-05-05 13:08:32 -070013490static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13491
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013492static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13493{
Joe Perches63c3a662011-04-26 08:12:10 +000013494 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013495 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013496 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013497 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013498 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013499 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013500}
13501
Matt Carlson41434702011-03-09 16:58:22 +000013502static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013503 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13504 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13505 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13506 { },
13507};
13508
Linus Torvalds1da177e2005-04-16 15:20:36 -070013509static int __devinit tg3_get_invariants(struct tg3 *tp)
13510{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013511 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013512 u32 pci_state_reg, grc_misc_cfg;
13513 u32 val;
13514 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013515 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013516
Linus Torvalds1da177e2005-04-16 15:20:36 -070013517 /* Force memory write invalidate off. If we leave it on,
13518 * then on 5700_BX chips we have to enable a workaround.
13519 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13520 * to match the cacheline size. The Broadcom driver have this
13521 * workaround but turns MWI off all the times so never uses
13522 * it. This seems to suggest that the workaround is insufficient.
13523 */
13524 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13525 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13526 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13527
Matt Carlson16821282011-07-13 09:27:28 +000013528 /* Important! -- Make sure register accesses are byteswapped
13529 * correctly. Also, for those chips that require it, make
13530 * sure that indirect register accesses are enabled before
13531 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013532 */
13533 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13534 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013535 tp->misc_host_ctrl |= (misc_ctrl_reg &
13536 MISC_HOST_CTRL_CHIPREV);
13537 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13538 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013539
13540 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13541 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013542 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13543 u32 prod_id_asic_rev;
13544
Matt Carlson5001e2f2009-11-13 13:03:51 +000013545 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13546 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013547 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13548 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013549 pci_read_config_dword(tp->pdev,
13550 TG3PCI_GEN2_PRODID_ASICREV,
13551 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013552 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13553 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13554 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13555 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13556 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13557 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13558 pci_read_config_dword(tp->pdev,
13559 TG3PCI_GEN15_PRODID_ASICREV,
13560 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013561 else
13562 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13563 &prod_id_asic_rev);
13564
Matt Carlson321d32a2008-11-21 17:22:19 -080013565 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013567
Michael Chanff645be2005-04-21 17:09:53 -070013568 /* Wrong chip ID in 5752 A0. This code can be removed later
13569 * as A0 is not in production.
13570 */
13571 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13572 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13573
Michael Chan68929142005-08-09 20:17:14 -070013574 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13575 * we need to disable memory and use config. cycles
13576 * only to access all registers. The 5702/03 chips
13577 * can mistakenly decode the special cycles from the
13578 * ICH chipsets as memory write cycles, causing corruption
13579 * of register and memory space. Only certain ICH bridges
13580 * will drive special cycles with non-zero data during the
13581 * address phase which can fall within the 5703's address
13582 * range. This is not an ICH bug as the PCI spec allows
13583 * non-zero address during special cycles. However, only
13584 * these ICH bridges are known to drive non-zero addresses
13585 * during special cycles.
13586 *
13587 * Since special cycles do not cross PCI bridges, we only
13588 * enable this workaround if the 5703 is on the secondary
13589 * bus of these ICH bridges.
13590 */
13591 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13592 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13593 static struct tg3_dev_id {
13594 u32 vendor;
13595 u32 device;
13596 u32 rev;
13597 } ich_chipsets[] = {
13598 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13599 PCI_ANY_ID },
13600 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13601 PCI_ANY_ID },
13602 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13603 0xa },
13604 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13605 PCI_ANY_ID },
13606 { },
13607 };
13608 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13609 struct pci_dev *bridge = NULL;
13610
13611 while (pci_id->vendor != 0) {
13612 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13613 bridge);
13614 if (!bridge) {
13615 pci_id++;
13616 continue;
13617 }
13618 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013619 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013620 continue;
13621 }
13622 if (bridge->subordinate &&
13623 (bridge->subordinate->number ==
13624 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013625 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013626 pci_dev_put(bridge);
13627 break;
13628 }
13629 }
13630 }
13631
Matt Carlson6ff6f812011-05-19 12:12:54 +000013632 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013633 static struct tg3_dev_id {
13634 u32 vendor;
13635 u32 device;
13636 } bridge_chipsets[] = {
13637 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13638 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13639 { },
13640 };
13641 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13642 struct pci_dev *bridge = NULL;
13643
13644 while (pci_id->vendor != 0) {
13645 bridge = pci_get_device(pci_id->vendor,
13646 pci_id->device,
13647 bridge);
13648 if (!bridge) {
13649 pci_id++;
13650 continue;
13651 }
13652 if (bridge->subordinate &&
13653 (bridge->subordinate->number <=
13654 tp->pdev->bus->number) &&
13655 (bridge->subordinate->subordinate >=
13656 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013657 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013658 pci_dev_put(bridge);
13659 break;
13660 }
13661 }
13662 }
13663
Michael Chan4a29cc22006-03-19 13:21:12 -080013664 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13665 * DMA addresses > 40-bit. This bridge may have other additional
13666 * 57xx devices behind it in some 4-port NIC designs for example.
13667 * Any tg3 device found behind the bridge will also need the 40-bit
13668 * DMA workaround.
13669 */
Michael Chana4e2b342005-10-26 15:46:52 -070013670 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13671 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013672 tg3_flag_set(tp, 5780_CLASS);
13673 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013674 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013675 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013676 struct pci_dev *bridge = NULL;
13677
13678 do {
13679 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13680 PCI_DEVICE_ID_SERVERWORKS_EPB,
13681 bridge);
13682 if (bridge && bridge->subordinate &&
13683 (bridge->subordinate->number <=
13684 tp->pdev->bus->number) &&
13685 (bridge->subordinate->subordinate >=
13686 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013687 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013688 pci_dev_put(bridge);
13689 break;
13690 }
13691 } while (bridge);
13692 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013693
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013694 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13695 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013696 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13697 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013698 tp->pdev_peer = tg3_find_peer(tp);
13699
Matt Carlsonc885e822010-08-02 11:25:57 +000013700 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013701 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13702 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013703 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013704
13705 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013706 tg3_flag(tp, 5717_PLUS))
13707 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013708
Matt Carlson321d32a2008-11-21 17:22:19 -080013709 /* Intentionally exclude ASIC_REV_5906 */
13710 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013711 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013712 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013713 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013714 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013715 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013716 tg3_flag(tp, 57765_PLUS))
13717 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013718
13719 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13720 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013721 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013722 tg3_flag(tp, 5755_PLUS) ||
13723 tg3_flag(tp, 5780_CLASS))
13724 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013725
Matt Carlson6ff6f812011-05-19 12:12:54 +000013726 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013727 tg3_flag(tp, 5750_PLUS))
13728 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013729
Matt Carlson507399f2009-11-13 13:03:37 +000013730 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013731 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013732 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013733 else if (tg3_flag(tp, 57765_PLUS))
13734 tg3_flag_set(tp, HW_TSO_3);
13735 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013736 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013737 tg3_flag_set(tp, HW_TSO_2);
13738 else if (tg3_flag(tp, 5750_PLUS)) {
13739 tg3_flag_set(tp, HW_TSO_1);
13740 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013741 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13742 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013743 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013744 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13745 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13746 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013747 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013748 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13749 tp->fw_needed = FIRMWARE_TG3TSO5;
13750 else
13751 tp->fw_needed = FIRMWARE_TG3TSO;
13752 }
13753
Matt Carlsondabc5c62011-05-19 12:12:52 +000013754 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013755 if (tg3_flag(tp, HW_TSO_1) ||
13756 tg3_flag(tp, HW_TSO_2) ||
13757 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsondabc5c62011-05-19 12:12:52 +000013758 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13759 tg3_flag_set(tp, TSO_CAPABLE);
13760 else {
13761 tg3_flag_clear(tp, TSO_CAPABLE);
13762 tg3_flag_clear(tp, TSO_BUG);
13763 tp->fw_needed = NULL;
13764 }
13765
13766 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
13767 tp->fw_needed = FIRMWARE_TG3;
13768
Matt Carlson507399f2009-11-13 13:03:37 +000013769 tp->irq_max = 1;
13770
Joe Perches63c3a662011-04-26 08:12:10 +000013771 if (tg3_flag(tp, 5750_PLUS)) {
13772 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013773 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13774 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13775 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13776 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13777 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000013778 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013779
Joe Perches63c3a662011-04-26 08:12:10 +000013780 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013781 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013782 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070013783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013784
Joe Perches63c3a662011-04-26 08:12:10 +000013785 if (tg3_flag(tp, 57765_PLUS)) {
13786 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000013787 tp->irq_max = TG3_IRQ_MAX_VECS;
13788 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013789 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013790
Matt Carlson2ffcc982011-05-19 12:12:44 +000013791 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000013792 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013793
Joe Perches63c3a662011-04-26 08:12:10 +000013794 if (tg3_flag(tp, 5717_PLUS))
13795 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000013796
Joe Perches63c3a662011-04-26 08:12:10 +000013797 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013798 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Joe Perches63c3a662011-04-26 08:12:10 +000013799 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000013800
Joe Perches63c3a662011-04-26 08:12:10 +000013801 if (!tg3_flag(tp, 5705_PLUS) ||
13802 tg3_flag(tp, 5780_CLASS) ||
13803 tg3_flag(tp, USE_JUMBO_BDFLAG))
13804 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070013805
Matt Carlson52f44902008-11-21 17:17:04 -080013806 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13807 &pci_state_reg);
13808
Jon Mason708ebb32011-06-27 12:56:50 +000013809 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013810 u16 lnkctl;
13811
Joe Perches63c3a662011-04-26 08:12:10 +000013812 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013813
Matt Carlsoncf790032010-11-24 08:31:48 +000013814 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013815 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13816 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013817 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013818
13819 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013820
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013821 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +000013822 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013823 &lnkctl);
13824 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000013825 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13826 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013827 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000013828 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000013829 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013830 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013831 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013832 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13833 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000013834 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b05902010-01-20 16:58:02 +000013835 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013836 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080013837 }
Matt Carlson52f44902008-11-21 17:17:04 -080013838 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000013839 /* BCM5785 devices are effectively PCIe devices, and should
13840 * follow PCIe codepaths, but do not have a PCIe capabilities
13841 * section.
13842 */
Joe Perches63c3a662011-04-26 08:12:10 +000013843 tg3_flag_set(tp, PCI_EXPRESS);
13844 } else if (!tg3_flag(tp, 5705_PLUS) ||
13845 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080013846 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13847 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013848 dev_err(&tp->pdev->dev,
13849 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013850 return -EIO;
13851 }
13852
13853 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000013854 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080013855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013856
Michael Chan399de502005-10-03 14:02:39 -070013857 /* If we have an AMD 762 or VIA K8T800 chipset, write
13858 * reordering to the mailbox registers done by the host
13859 * controller can cause major troubles. We read back from
13860 * every mailbox register write to force the writes to be
13861 * posted to the chip in order.
13862 */
Matt Carlson41434702011-03-09 16:58:22 +000013863 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013864 !tg3_flag(tp, PCI_EXPRESS))
13865 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070013866
Matt Carlson69fc4052008-12-21 20:19:57 -080013867 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13868 &tp->pci_cacheline_sz);
13869 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13870 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013871 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13872 tp->pci_lat_timer < 64) {
13873 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013874 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13875 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013876 }
13877
Matt Carlson16821282011-07-13 09:27:28 +000013878 /* Important! -- It is critical that the PCI-X hw workaround
13879 * situation is decided before the first MMIO register access.
13880 */
Matt Carlson52f44902008-11-21 17:17:04 -080013881 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13882 /* 5700 BX chips need to have their TX producer index
13883 * mailboxes written twice to workaround a bug.
13884 */
Joe Perches63c3a662011-04-26 08:12:10 +000013885 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070013886
Matt Carlson52f44902008-11-21 17:17:04 -080013887 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013888 *
13889 * The workaround is to use indirect register accesses
13890 * for all chip writes not to mailbox registers.
13891 */
Joe Perches63c3a662011-04-26 08:12:10 +000013892 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013893 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013894
Joe Perches63c3a662011-04-26 08:12:10 +000013895 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013896
13897 /* The chip can have it's power management PCI config
13898 * space registers clobbered due to this bug.
13899 * So explicitly force the chip into D0 here.
13900 */
Matt Carlson9974a352007-10-07 23:27:28 -070013901 pci_read_config_dword(tp->pdev,
13902 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013903 &pm_reg);
13904 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13905 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013906 pci_write_config_dword(tp->pdev,
13907 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013908 pm_reg);
13909
13910 /* Also, force SERR#/PERR# in PCI command. */
13911 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13912 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13913 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13914 }
13915 }
13916
Linus Torvalds1da177e2005-04-16 15:20:36 -070013917 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013918 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013919 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013920 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013921
13922 /* Chip-specific fixup from Broadcom driver */
13923 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13924 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13925 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13926 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13927 }
13928
Michael Chan1ee582d2005-08-09 20:16:46 -070013929 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013930 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013931 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013932 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013933 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013934 tp->write32_tx_mbox = tg3_write32;
13935 tp->write32_rx_mbox = tg3_write32;
13936
13937 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000013938 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070013939 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013940 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013941 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070013942 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13943 /*
13944 * Back to back register writes can cause problems on these
13945 * chips, the workaround is to read back all reg writes
13946 * except those to mailbox regs.
13947 *
13948 * See tg3_write_indirect_reg32().
13949 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013950 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013951 }
13952
Joe Perches63c3a662011-04-26 08:12:10 +000013953 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070013954 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000013955 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070013956 tp->write32_rx_mbox = tg3_write_flush_reg32;
13957 }
Michael Chan20094932005-08-09 20:16:32 -070013958
Joe Perches63c3a662011-04-26 08:12:10 +000013959 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070013960 tp->read32 = tg3_read_indirect_reg32;
13961 tp->write32 = tg3_write_indirect_reg32;
13962 tp->read32_mbox = tg3_read_indirect_mbox;
13963 tp->write32_mbox = tg3_write_indirect_mbox;
13964 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13965 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13966
13967 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013968 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013969
13970 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13971 pci_cmd &= ~PCI_COMMAND_MEMORY;
13972 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13973 }
Michael Chanb5d37722006-09-27 16:06:21 -070013974 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13975 tp->read32_mbox = tg3_read32_mbox_5906;
13976 tp->write32_mbox = tg3_write32_mbox_5906;
13977 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13978 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13979 }
Michael Chan68929142005-08-09 20:17:14 -070013980
Michael Chanbbadf502006-04-06 21:46:34 -070013981 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013982 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070013983 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013984 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000013985 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070013986
Matt Carlson16821282011-07-13 09:27:28 +000013987 /* The memory arbiter has to be enabled in order for SRAM accesses
13988 * to succeed. Normally on powerup the tg3 chip firmware will make
13989 * sure it is enabled, but other entities such as system netboot
13990 * code might disable it.
13991 */
13992 val = tr32(MEMARB_MODE);
13993 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
13994
Matt Carlson69f11c92011-07-13 09:27:30 +000013995 if (tg3_flag(tp, PCIX_MODE)) {
13996 pci_read_config_dword(tp->pdev,
13997 tp->pcix_cap + PCI_X_STATUS, &val);
13998 tp->pci_fn = val & 0x7;
13999 } else {
14000 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14001 }
14002
Michael Chan7d0c41e2005-04-21 17:06:20 -070014003 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014004 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014005 * determined before calling tg3_set_power_state() so that
14006 * we know whether or not to switch out of Vaux power.
14007 * When the flag is set, it means that GPIO1 is used for eeprom
14008 * write protect and also implies that it is a LOM where GPIOs
14009 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014010 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014011 tg3_get_eeprom_hw_cfg(tp);
14012
Joe Perches63c3a662011-04-26 08:12:10 +000014013 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014014 /* Allow reads and writes to the
14015 * APE register and memory space.
14016 */
14017 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +000014018 PCISTATE_ALLOW_APE_SHMEM_WR |
14019 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014020 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14021 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014022
14023 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014024 }
14025
Matt Carlson9936bcf2007-10-10 18:03:07 -070014026 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014027 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014028 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014029 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014030 tg3_flag(tp, 57765_PLUS))
14031 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014032
Matt Carlson16821282011-07-13 09:27:28 +000014033 /* Set up tp->grc_local_ctrl before calling
14034 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14035 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014036 * It is also used as eeprom write protect on LOMs.
14037 */
14038 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014039 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014040 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014041 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14042 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014043 /* Unused GPIO3 must be driven as output on 5752 because there
14044 * are no pull-up resistors on unused GPIO pins.
14045 */
14046 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14047 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014048
Matt Carlson321d32a2008-11-21 17:22:19 -080014049 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014050 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14051 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080014052 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14053
Matt Carlson8d519ab2009-04-20 06:58:01 +000014054 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14055 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014056 /* Turn off the debug UART. */
14057 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014058 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014059 /* Keep VMain power. */
14060 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14061 GRC_LCLCTRL_GPIO_OUTPUT0;
14062 }
14063
Matt Carlson16821282011-07-13 09:27:28 +000014064 /* Switch out of Vaux if it is a NIC */
14065 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014066
Linus Torvalds1da177e2005-04-16 15:20:36 -070014067 /* Derive initial jumbo mode from MTU assigned in
14068 * ether_setup() via the alloc_etherdev() call
14069 */
Joe Perches63c3a662011-04-26 08:12:10 +000014070 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14071 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014072
14073 /* Determine WakeOnLan speed to use. */
14074 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14075 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14076 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14077 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014078 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014079 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014080 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014081 }
14082
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014083 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014084 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014085
Linus Torvalds1da177e2005-04-16 15:20:36 -070014086 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014087 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14088 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014089 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014090 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014091 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14092 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14093 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014094
14095 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14096 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014097 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014098 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014099 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014100
Joe Perches63c3a662011-04-26 08:12:10 +000014101 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014102 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014103 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014104 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014105 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014106 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014107 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014108 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14109 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014110 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14111 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014112 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014113 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014114 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014115 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014116 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014118
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014119 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14120 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14121 tp->phy_otp = tg3_read_otp_phycfg(tp);
14122 if (tp->phy_otp == 0)
14123 tp->phy_otp = TG3_OTP_DEFAULT;
14124 }
14125
Joe Perches63c3a662011-04-26 08:12:10 +000014126 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014127 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14128 else
14129 tp->mi_mode = MAC_MI_MODE_BASE;
14130
Linus Torvalds1da177e2005-04-16 15:20:36 -070014131 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014132 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14133 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14134 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14135
Matt Carlson4d958472011-04-20 07:57:35 +000014136 /* Set these bits to enable statistics workaround. */
14137 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14138 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14139 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14140 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14141 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14142 }
14143
Matt Carlson321d32a2008-11-21 17:22:19 -080014144 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14145 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014146 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014147
Matt Carlson158d7ab2008-05-29 01:37:54 -070014148 err = tg3_mdio_init(tp);
14149 if (err)
14150 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014151
14152 /* Initialize data/descriptor byte/word swapping. */
14153 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014154 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14155 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14156 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14157 GRC_MODE_B2HRX_ENABLE |
14158 GRC_MODE_HTX2B_ENABLE |
14159 GRC_MODE_HOST_STACKUP);
14160 else
14161 val &= GRC_MODE_HOST_STACKUP;
14162
Linus Torvalds1da177e2005-04-16 15:20:36 -070014163 tw32(GRC_MODE, val | tp->grc_mode);
14164
14165 tg3_switch_clocks(tp);
14166
14167 /* Clear this out for sanity. */
14168 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14169
14170 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14171 &pci_state_reg);
14172 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014173 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014174 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14175
14176 if (chiprevid == CHIPREV_ID_5701_A0 ||
14177 chiprevid == CHIPREV_ID_5701_B0 ||
14178 chiprevid == CHIPREV_ID_5701_B2 ||
14179 chiprevid == CHIPREV_ID_5701_B5) {
14180 void __iomem *sram_base;
14181
14182 /* Write some dummy words into the SRAM status block
14183 * area, see if it reads back correctly. If the return
14184 * value is bad, force enable the PCIX workaround.
14185 */
14186 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14187
14188 writel(0x00000000, sram_base);
14189 writel(0x00000000, sram_base + 4);
14190 writel(0xffffffff, sram_base + 4);
14191 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014192 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014193 }
14194 }
14195
14196 udelay(50);
14197 tg3_nvram_init(tp);
14198
14199 grc_misc_cfg = tr32(GRC_MISC_CFG);
14200 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14201
Linus Torvalds1da177e2005-04-16 15:20:36 -070014202 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14203 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14204 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014205 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014206
Joe Perches63c3a662011-04-26 08:12:10 +000014207 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014208 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014209 tg3_flag_set(tp, TAGGED_STATUS);
14210 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014211 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14212 HOSTCC_MODE_CLRTICK_TXBD);
14213
14214 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14215 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14216 tp->misc_host_ctrl);
14217 }
14218
Matt Carlson3bda1252008-08-15 14:08:22 -070014219 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014220 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014221 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014222 else
14223 tp->mac_mode = TG3_DEF_MAC_MODE;
14224
Linus Torvalds1da177e2005-04-16 15:20:36 -070014225 /* these are limited to 10/100 only */
14226 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14227 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14228 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14229 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14230 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14231 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14232 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14233 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14234 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014235 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14236 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014237 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014238 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14239 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014240 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14241 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014242
14243 err = tg3_phy_probe(tp);
14244 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014245 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014246 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014247 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014248 }
14249
Matt Carlson184b8902010-04-05 10:19:25 +000014250 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014251 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014252
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014253 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14254 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014255 } else {
14256 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014257 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014258 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014259 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014260 }
14261
14262 /* 5700 {AX,BX} chips have a broken status block link
14263 * change bit implementation, so we must use the
14264 * status register in those cases.
14265 */
14266 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014267 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014268 else
Joe Perches63c3a662011-04-26 08:12:10 +000014269 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014270
14271 /* The led_ctrl is set during tg3_phy_probe, here we might
14272 * have to force the link status polling mechanism based
14273 * upon subsystem IDs.
14274 */
14275 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014276 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014277 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14278 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014279 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014280 }
14281
14282 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014283 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014284 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014285 else
Joe Perches63c3a662011-04-26 08:12:10 +000014286 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014287
Matt Carlsonbf933c82011-01-25 15:58:49 +000014288 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014289 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014290 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014291 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014292 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014293#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014294 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014295#endif
14296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014297
Matt Carlson2c49a442010-09-30 10:34:35 +000014298 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14299 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014300 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14301
Matt Carlson2c49a442010-09-30 10:34:35 +000014302 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014303
14304 /* Increment the rx prod index on the rx std ring by at most
14305 * 8 for these chips to workaround hw errata.
14306 */
14307 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14308 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14309 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14310 tp->rx_std_max_post = 8;
14311
Joe Perches63c3a662011-04-26 08:12:10 +000014312 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014313 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14314 PCIE_PWR_MGMT_L1_THRESH_MSK;
14315
Linus Torvalds1da177e2005-04-16 15:20:36 -070014316 return err;
14317}
14318
David S. Miller49b6e95f2007-03-29 01:38:42 -070014319#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014320static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14321{
14322 struct net_device *dev = tp->dev;
14323 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014324 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014325 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014326 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014327
David S. Miller49b6e95f2007-03-29 01:38:42 -070014328 addr = of_get_property(dp, "local-mac-address", &len);
14329 if (addr && len == 6) {
14330 memcpy(dev->dev_addr, addr, 6);
14331 memcpy(dev->perm_addr, dev->dev_addr, 6);
14332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014333 }
14334 return -ENODEV;
14335}
14336
14337static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14338{
14339 struct net_device *dev = tp->dev;
14340
14341 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014342 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014343 return 0;
14344}
14345#endif
14346
14347static int __devinit tg3_get_device_address(struct tg3 *tp)
14348{
14349 struct net_device *dev = tp->dev;
14350 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014351 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014352
David S. Miller49b6e95f2007-03-29 01:38:42 -070014353#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014354 if (!tg3_get_macaddr_sparc(tp))
14355 return 0;
14356#endif
14357
14358 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014359 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014360 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014361 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14362 mac_offset = 0xcc;
14363 if (tg3_nvram_lock(tp))
14364 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14365 else
14366 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014367 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014368 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014369 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014370 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014371 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014372 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014373 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014374
14375 /* First try to get it from MAC address mailbox. */
14376 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14377 if ((hi >> 16) == 0x484b) {
14378 dev->dev_addr[0] = (hi >> 8) & 0xff;
14379 dev->dev_addr[1] = (hi >> 0) & 0xff;
14380
14381 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14382 dev->dev_addr[2] = (lo >> 24) & 0xff;
14383 dev->dev_addr[3] = (lo >> 16) & 0xff;
14384 dev->dev_addr[4] = (lo >> 8) & 0xff;
14385 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014386
Michael Chan008652b2006-03-27 23:14:53 -080014387 /* Some old bootcode may report a 0 MAC address in SRAM */
14388 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14389 }
14390 if (!addr_ok) {
14391 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014392 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014393 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014394 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014395 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14396 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014397 }
14398 /* Finally just fetch it out of the MAC control regs. */
14399 else {
14400 hi = tr32(MAC_ADDR_0_HIGH);
14401 lo = tr32(MAC_ADDR_0_LOW);
14402
14403 dev->dev_addr[5] = lo & 0xff;
14404 dev->dev_addr[4] = (lo >> 8) & 0xff;
14405 dev->dev_addr[3] = (lo >> 16) & 0xff;
14406 dev->dev_addr[2] = (lo >> 24) & 0xff;
14407 dev->dev_addr[1] = hi & 0xff;
14408 dev->dev_addr[0] = (hi >> 8) & 0xff;
14409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014410 }
14411
14412 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014413#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014414 if (!tg3_get_default_macaddr_sparc(tp))
14415 return 0;
14416#endif
14417 return -EINVAL;
14418 }
John W. Linville2ff43692005-09-12 14:44:20 -070014419 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014420 return 0;
14421}
14422
David S. Miller59e6b432005-05-18 22:50:10 -070014423#define BOUNDARY_SINGLE_CACHELINE 1
14424#define BOUNDARY_MULTI_CACHELINE 2
14425
14426static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14427{
14428 int cacheline_size;
14429 u8 byte;
14430 int goal;
14431
14432 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14433 if (byte == 0)
14434 cacheline_size = 1024;
14435 else
14436 cacheline_size = (int) byte * 4;
14437
14438 /* On 5703 and later chips, the boundary bits have no
14439 * effect.
14440 */
14441 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14442 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014443 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014444 goto out;
14445
14446#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14447 goal = BOUNDARY_MULTI_CACHELINE;
14448#else
14449#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14450 goal = BOUNDARY_SINGLE_CACHELINE;
14451#else
14452 goal = 0;
14453#endif
14454#endif
14455
Joe Perches63c3a662011-04-26 08:12:10 +000014456 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014457 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14458 goto out;
14459 }
14460
David S. Miller59e6b432005-05-18 22:50:10 -070014461 if (!goal)
14462 goto out;
14463
14464 /* PCI controllers on most RISC systems tend to disconnect
14465 * when a device tries to burst across a cache-line boundary.
14466 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14467 *
14468 * Unfortunately, for PCI-E there are only limited
14469 * write-side controls for this, and thus for reads
14470 * we will still get the disconnects. We'll also waste
14471 * these PCI cycles for both read and write for chips
14472 * other than 5700 and 5701 which do not implement the
14473 * boundary bits.
14474 */
Joe Perches63c3a662011-04-26 08:12:10 +000014475 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014476 switch (cacheline_size) {
14477 case 16:
14478 case 32:
14479 case 64:
14480 case 128:
14481 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14482 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14483 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14484 } else {
14485 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14486 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14487 }
14488 break;
14489
14490 case 256:
14491 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14492 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14493 break;
14494
14495 default:
14496 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14497 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14498 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014499 }
Joe Perches63c3a662011-04-26 08:12:10 +000014500 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014501 switch (cacheline_size) {
14502 case 16:
14503 case 32:
14504 case 64:
14505 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14506 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14507 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14508 break;
14509 }
14510 /* fallthrough */
14511 case 128:
14512 default:
14513 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14514 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14515 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014516 }
David S. Miller59e6b432005-05-18 22:50:10 -070014517 } else {
14518 switch (cacheline_size) {
14519 case 16:
14520 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14521 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14522 DMA_RWCTRL_WRITE_BNDRY_16);
14523 break;
14524 }
14525 /* fallthrough */
14526 case 32:
14527 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14528 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14529 DMA_RWCTRL_WRITE_BNDRY_32);
14530 break;
14531 }
14532 /* fallthrough */
14533 case 64:
14534 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14535 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14536 DMA_RWCTRL_WRITE_BNDRY_64);
14537 break;
14538 }
14539 /* fallthrough */
14540 case 128:
14541 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14542 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14543 DMA_RWCTRL_WRITE_BNDRY_128);
14544 break;
14545 }
14546 /* fallthrough */
14547 case 256:
14548 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14549 DMA_RWCTRL_WRITE_BNDRY_256);
14550 break;
14551 case 512:
14552 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14553 DMA_RWCTRL_WRITE_BNDRY_512);
14554 break;
14555 case 1024:
14556 default:
14557 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14558 DMA_RWCTRL_WRITE_BNDRY_1024);
14559 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014560 }
David S. Miller59e6b432005-05-18 22:50:10 -070014561 }
14562
14563out:
14564 return val;
14565}
14566
Linus Torvalds1da177e2005-04-16 15:20:36 -070014567static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14568{
14569 struct tg3_internal_buffer_desc test_desc;
14570 u32 sram_dma_descs;
14571 int i, ret;
14572
14573 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14574
14575 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14576 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14577 tw32(RDMAC_STATUS, 0);
14578 tw32(WDMAC_STATUS, 0);
14579
14580 tw32(BUFMGR_MODE, 0);
14581 tw32(FTQ_RESET, 0);
14582
14583 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14584 test_desc.addr_lo = buf_dma & 0xffffffff;
14585 test_desc.nic_mbuf = 0x00002100;
14586 test_desc.len = size;
14587
14588 /*
14589 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14590 * the *second* time the tg3 driver was getting loaded after an
14591 * initial scan.
14592 *
14593 * Broadcom tells me:
14594 * ...the DMA engine is connected to the GRC block and a DMA
14595 * reset may affect the GRC block in some unpredictable way...
14596 * The behavior of resets to individual blocks has not been tested.
14597 *
14598 * Broadcom noted the GRC reset will also reset all sub-components.
14599 */
14600 if (to_device) {
14601 test_desc.cqid_sqid = (13 << 8) | 2;
14602
14603 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14604 udelay(40);
14605 } else {
14606 test_desc.cqid_sqid = (16 << 8) | 7;
14607
14608 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14609 udelay(40);
14610 }
14611 test_desc.flags = 0x00000005;
14612
14613 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14614 u32 val;
14615
14616 val = *(((u32 *)&test_desc) + i);
14617 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14618 sram_dma_descs + (i * sizeof(u32)));
14619 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14620 }
14621 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14622
Matt Carlson859a5882010-04-05 10:19:28 +000014623 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014624 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014625 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014626 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014627
14628 ret = -ENODEV;
14629 for (i = 0; i < 40; i++) {
14630 u32 val;
14631
14632 if (to_device)
14633 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14634 else
14635 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14636 if ((val & 0xffff) == sram_dma_descs) {
14637 ret = 0;
14638 break;
14639 }
14640
14641 udelay(100);
14642 }
14643
14644 return ret;
14645}
14646
David S. Millerded73402005-05-23 13:59:47 -070014647#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014648
Matt Carlson41434702011-03-09 16:58:22 +000014649static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014650 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14651 { },
14652};
14653
Linus Torvalds1da177e2005-04-16 15:20:36 -070014654static int __devinit tg3_test_dma(struct tg3 *tp)
14655{
14656 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014657 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014658 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014659
Matt Carlson4bae65c2010-11-24 08:31:52 +000014660 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14661 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014662 if (!buf) {
14663 ret = -ENOMEM;
14664 goto out_nofree;
14665 }
14666
14667 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14668 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14669
David S. Miller59e6b432005-05-18 22:50:10 -070014670 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014671
Joe Perches63c3a662011-04-26 08:12:10 +000014672 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014673 goto out;
14674
Joe Perches63c3a662011-04-26 08:12:10 +000014675 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014676 /* DMA read watermark not used on PCIE */
14677 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014678 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014679 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14680 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014681 tp->dma_rwctrl |= 0x003f0000;
14682 else
14683 tp->dma_rwctrl |= 0x003f000f;
14684 } else {
14685 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14686 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14687 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014688 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014689
Michael Chan4a29cc22006-03-19 13:21:12 -080014690 /* If the 5704 is behind the EPB bridge, we can
14691 * do the less restrictive ONE_DMA workaround for
14692 * better performance.
14693 */
Joe Perches63c3a662011-04-26 08:12:10 +000014694 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014695 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14696 tp->dma_rwctrl |= 0x8000;
14697 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014698 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14699
Michael Chan49afdeb2007-02-13 12:17:03 -080014700 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14701 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014702 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014703 tp->dma_rwctrl |=
14704 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14705 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14706 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014707 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14708 /* 5780 always in PCIX mode */
14709 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014710 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14711 /* 5714 always in PCIX mode */
14712 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014713 } else {
14714 tp->dma_rwctrl |= 0x001b000f;
14715 }
14716 }
14717
14718 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14719 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14720 tp->dma_rwctrl &= 0xfffffff0;
14721
14722 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14723 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14724 /* Remove this if it causes problems for some boards. */
14725 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14726
14727 /* On 5700/5701 chips, we need to set this bit.
14728 * Otherwise the chip will issue cacheline transactions
14729 * to streamable DMA memory with not all the byte
14730 * enables turned on. This is an error on several
14731 * RISC PCI controllers, in particular sparc64.
14732 *
14733 * On 5703/5704 chips, this bit has been reassigned
14734 * a different meaning. In particular, it is used
14735 * on those chips to enable a PCI-X workaround.
14736 */
14737 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14738 }
14739
14740 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14741
14742#if 0
14743 /* Unneeded, already done by tg3_get_invariants. */
14744 tg3_switch_clocks(tp);
14745#endif
14746
Linus Torvalds1da177e2005-04-16 15:20:36 -070014747 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14748 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14749 goto out;
14750
David S. Miller59e6b432005-05-18 22:50:10 -070014751 /* It is best to perform DMA test with maximum write burst size
14752 * to expose the 5700/5701 write DMA bug.
14753 */
14754 saved_dma_rwctrl = tp->dma_rwctrl;
14755 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14756 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14757
Linus Torvalds1da177e2005-04-16 15:20:36 -070014758 while (1) {
14759 u32 *p = buf, i;
14760
14761 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14762 p[i] = i;
14763
14764 /* Send the buffer to the chip. */
14765 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14766 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014767 dev_err(&tp->pdev->dev,
14768 "%s: Buffer write failed. err = %d\n",
14769 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014770 break;
14771 }
14772
14773#if 0
14774 /* validate data reached card RAM correctly. */
14775 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14776 u32 val;
14777 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14778 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014779 dev_err(&tp->pdev->dev,
14780 "%s: Buffer corrupted on device! "
14781 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014782 /* ret = -ENODEV here? */
14783 }
14784 p[i] = 0;
14785 }
14786#endif
14787 /* Now read it back. */
14788 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14789 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014790 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14791 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014792 break;
14793 }
14794
14795 /* Verify it. */
14796 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14797 if (p[i] == i)
14798 continue;
14799
David S. Miller59e6b432005-05-18 22:50:10 -070014800 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14801 DMA_RWCTRL_WRITE_BNDRY_16) {
14802 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014803 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14804 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14805 break;
14806 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014807 dev_err(&tp->pdev->dev,
14808 "%s: Buffer corrupted on read back! "
14809 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014810 ret = -ENODEV;
14811 goto out;
14812 }
14813 }
14814
14815 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14816 /* Success. */
14817 ret = 0;
14818 break;
14819 }
14820 }
David S. Miller59e6b432005-05-18 22:50:10 -070014821 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14822 DMA_RWCTRL_WRITE_BNDRY_16) {
14823 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014824 * now look for chipsets that are known to expose the
14825 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014826 */
Matt Carlson41434702011-03-09 16:58:22 +000014827 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014828 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14829 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000014830 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014831 /* Safe to use the calculated DMA boundary. */
14832 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000014833 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014834
David S. Miller59e6b432005-05-18 22:50:10 -070014835 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014837
14838out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014839 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014840out_nofree:
14841 return ret;
14842}
14843
Linus Torvalds1da177e2005-04-16 15:20:36 -070014844static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14845{
Joe Perches63c3a662011-04-26 08:12:10 +000014846 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000014847 tp->bufmgr_config.mbuf_read_dma_low_water =
14848 DEFAULT_MB_RDMA_LOW_WATER_5705;
14849 tp->bufmgr_config.mbuf_mac_rx_low_water =
14850 DEFAULT_MB_MACRX_LOW_WATER_57765;
14851 tp->bufmgr_config.mbuf_high_water =
14852 DEFAULT_MB_HIGH_WATER_57765;
14853
14854 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14855 DEFAULT_MB_RDMA_LOW_WATER_5705;
14856 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14857 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14858 tp->bufmgr_config.mbuf_high_water_jumbo =
14859 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000014860 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec172005-07-25 12:31:48 -070014861 tp->bufmgr_config.mbuf_read_dma_low_water =
14862 DEFAULT_MB_RDMA_LOW_WATER_5705;
14863 tp->bufmgr_config.mbuf_mac_rx_low_water =
14864 DEFAULT_MB_MACRX_LOW_WATER_5705;
14865 tp->bufmgr_config.mbuf_high_water =
14866 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014867 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14868 tp->bufmgr_config.mbuf_mac_rx_low_water =
14869 DEFAULT_MB_MACRX_LOW_WATER_5906;
14870 tp->bufmgr_config.mbuf_high_water =
14871 DEFAULT_MB_HIGH_WATER_5906;
14872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014873
Michael Chanfdfec172005-07-25 12:31:48 -070014874 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14875 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14876 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14877 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14878 tp->bufmgr_config.mbuf_high_water_jumbo =
14879 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14880 } else {
14881 tp->bufmgr_config.mbuf_read_dma_low_water =
14882 DEFAULT_MB_RDMA_LOW_WATER;
14883 tp->bufmgr_config.mbuf_mac_rx_low_water =
14884 DEFAULT_MB_MACRX_LOW_WATER;
14885 tp->bufmgr_config.mbuf_high_water =
14886 DEFAULT_MB_HIGH_WATER;
14887
14888 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14889 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14890 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14891 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14892 tp->bufmgr_config.mbuf_high_water_jumbo =
14893 DEFAULT_MB_HIGH_WATER_JUMBO;
14894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014895
14896 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14897 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14898}
14899
14900static char * __devinit tg3_phy_string(struct tg3 *tp)
14901{
Matt Carlson79eb6902010-02-17 15:17:03 +000014902 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14903 case TG3_PHY_ID_BCM5400: return "5400";
14904 case TG3_PHY_ID_BCM5401: return "5401";
14905 case TG3_PHY_ID_BCM5411: return "5411";
14906 case TG3_PHY_ID_BCM5701: return "5701";
14907 case TG3_PHY_ID_BCM5703: return "5703";
14908 case TG3_PHY_ID_BCM5704: return "5704";
14909 case TG3_PHY_ID_BCM5705: return "5705";
14910 case TG3_PHY_ID_BCM5750: return "5750";
14911 case TG3_PHY_ID_BCM5752: return "5752";
14912 case TG3_PHY_ID_BCM5714: return "5714";
14913 case TG3_PHY_ID_BCM5780: return "5780";
14914 case TG3_PHY_ID_BCM5755: return "5755";
14915 case TG3_PHY_ID_BCM5787: return "5787";
14916 case TG3_PHY_ID_BCM5784: return "5784";
14917 case TG3_PHY_ID_BCM5756: return "5722/5756";
14918 case TG3_PHY_ID_BCM5906: return "5906";
14919 case TG3_PHY_ID_BCM5761: return "5761";
14920 case TG3_PHY_ID_BCM5718C: return "5718C";
14921 case TG3_PHY_ID_BCM5718S: return "5718S";
14922 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014923 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000014924 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014925 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014926 case 0: return "serdes";
14927 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014929}
14930
Michael Chanf9804dd2005-09-27 12:13:10 -070014931static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14932{
Joe Perches63c3a662011-04-26 08:12:10 +000014933 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014934 strcpy(str, "PCI Express");
14935 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000014936 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014937 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14938
14939 strcpy(str, "PCIX:");
14940
14941 if ((clock_ctrl == 7) ||
14942 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14943 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14944 strcat(str, "133MHz");
14945 else if (clock_ctrl == 0)
14946 strcat(str, "33MHz");
14947 else if (clock_ctrl == 2)
14948 strcat(str, "50MHz");
14949 else if (clock_ctrl == 4)
14950 strcat(str, "66MHz");
14951 else if (clock_ctrl == 6)
14952 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014953 } else {
14954 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000014955 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070014956 strcat(str, "66MHz");
14957 else
14958 strcat(str, "33MHz");
14959 }
Joe Perches63c3a662011-04-26 08:12:10 +000014960 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070014961 strcat(str, ":32-bit");
14962 else
14963 strcat(str, ":64-bit");
14964 return str;
14965}
14966
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014967static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014968{
14969 struct pci_dev *peer;
14970 unsigned int func, devnr = tp->pdev->devfn & ~7;
14971
14972 for (func = 0; func < 8; func++) {
14973 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14974 if (peer && peer != tp->pdev)
14975 break;
14976 pci_dev_put(peer);
14977 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014978 /* 5704 can be configured in single-port mode, set peer to
14979 * tp->pdev in that case.
14980 */
14981 if (!peer) {
14982 peer = tp->pdev;
14983 return peer;
14984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014985
14986 /*
14987 * We don't need to keep the refcount elevated; there's no way
14988 * to remove one half of this device without removing the other
14989 */
14990 pci_dev_put(peer);
14991
14992 return peer;
14993}
14994
David S. Miller15f98502005-05-18 22:49:26 -070014995static void __devinit tg3_init_coal(struct tg3 *tp)
14996{
14997 struct ethtool_coalesce *ec = &tp->coal;
14998
14999 memset(ec, 0, sizeof(*ec));
15000 ec->cmd = ETHTOOL_GCOALESCE;
15001 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15002 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15003 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15004 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15005 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15006 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15007 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15008 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15009 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15010
15011 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15012 HOSTCC_MODE_CLRTICK_TXBD)) {
15013 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15014 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15015 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15016 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15017 }
Michael Chand244c892005-07-05 14:42:33 -070015018
Joe Perches63c3a662011-04-26 08:12:10 +000015019 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015020 ec->rx_coalesce_usecs_irq = 0;
15021 ec->tx_coalesce_usecs_irq = 0;
15022 ec->stats_block_coalesce_usecs = 0;
15023 }
David S. Miller15f98502005-05-18 22:49:26 -070015024}
15025
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015026static const struct net_device_ops tg3_netdev_ops = {
15027 .ndo_open = tg3_open,
15028 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080015029 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000015030 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080015031 .ndo_validate_addr = eth_validate_addr,
15032 .ndo_set_multicast_list = tg3_set_rx_mode,
15033 .ndo_set_mac_address = tg3_set_mac_addr,
15034 .ndo_do_ioctl = tg3_ioctl,
15035 .ndo_tx_timeout = tg3_tx_timeout,
15036 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000015037 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015038 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080015039#ifdef CONFIG_NET_POLL_CONTROLLER
15040 .ndo_poll_controller = tg3_poll_controller,
15041#endif
15042};
15043
Linus Torvalds1da177e2005-04-16 15:20:36 -070015044static int __devinit tg3_init_one(struct pci_dev *pdev,
15045 const struct pci_device_id *ent)
15046{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015047 struct net_device *dev;
15048 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015049 int i, err, pm_cap;
15050 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015051 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015052 u64 dma_mask, persist_dma_mask;
Matt Carlson0da06062011-05-19 12:12:53 +000015053 u32 features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015054
Joe Perches05dbe002010-02-17 19:44:19 +000015055 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015056
15057 err = pci_enable_device(pdev);
15058 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015059 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015060 return err;
15061 }
15062
Linus Torvalds1da177e2005-04-16 15:20:36 -070015063 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15064 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015065 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015066 goto err_out_disable_pdev;
15067 }
15068
15069 pci_set_master(pdev);
15070
15071 /* Find power-management capability. */
15072 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15073 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015074 dev_err(&pdev->dev,
15075 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015076 err = -EIO;
15077 goto err_out_free_res;
15078 }
15079
Matt Carlson16821282011-07-13 09:27:28 +000015080 err = pci_set_power_state(pdev, PCI_D0);
15081 if (err) {
15082 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15083 goto err_out_free_res;
15084 }
15085
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015086 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015087 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015088 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015089 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015090 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015091 }
15092
Linus Torvalds1da177e2005-04-16 15:20:36 -070015093 SET_NETDEV_DEV(dev, &pdev->dev);
15094
Linus Torvalds1da177e2005-04-16 15:20:36 -070015095 tp = netdev_priv(dev);
15096 tp->pdev = pdev;
15097 tp->dev = dev;
15098 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015099 tp->rx_mode = TG3_DEF_RX_MODE;
15100 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015101
Linus Torvalds1da177e2005-04-16 15:20:36 -070015102 if (tg3_debug > 0)
15103 tp->msg_enable = tg3_debug;
15104 else
15105 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15106
15107 /* The word/byte swap controls here control register access byte
15108 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15109 * setting below.
15110 */
15111 tp->misc_host_ctrl =
15112 MISC_HOST_CTRL_MASK_PCI_INT |
15113 MISC_HOST_CTRL_WORD_SWAP |
15114 MISC_HOST_CTRL_INDIR_ACCESS |
15115 MISC_HOST_CTRL_PCISTATE_RW;
15116
15117 /* The NONFRM (non-frame) byte/word swap controls take effect
15118 * on descriptor entries, anything which isn't packet data.
15119 *
15120 * The StrongARM chips on the board (one for tx, one for rx)
15121 * are running in big-endian mode.
15122 */
15123 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15124 GRC_MODE_WSWAP_NONFRM_DATA);
15125#ifdef __BIG_ENDIAN
15126 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15127#endif
15128 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015129 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015130 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015131
Matt Carlsond5fe4882008-11-21 17:20:32 -080015132 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015133 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015134 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015135 err = -ENOMEM;
15136 goto err_out_free_dev;
15137 }
15138
Matt Carlsonc9cab242011-07-13 09:27:27 +000015139 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15140 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15141 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15142 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15143 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15144 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15145 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15146 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15147 tg3_flag_set(tp, ENABLE_APE);
15148 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15149 if (!tp->aperegs) {
15150 dev_err(&pdev->dev,
15151 "Cannot map APE registers, aborting\n");
15152 err = -ENOMEM;
15153 goto err_out_iounmap;
15154 }
15155 }
15156
Linus Torvalds1da177e2005-04-16 15:20:36 -070015157 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15158 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015159
Linus Torvalds1da177e2005-04-16 15:20:36 -070015160 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015161 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015162 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015163 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015164
15165 err = tg3_get_invariants(tp);
15166 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015167 dev_err(&pdev->dev,
15168 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015169 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015170 }
15171
Michael Chan4a29cc22006-03-19 13:21:12 -080015172 /* The EPB bridge inside 5714, 5715, and 5780 and any
15173 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015174 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15175 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15176 * do DMA address check in tg3_start_xmit().
15177 */
Joe Perches63c3a662011-04-26 08:12:10 +000015178 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015179 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015180 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015181 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015182#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015183 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015184#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015185 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015186 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015187
15188 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015189 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015190 err = pci_set_dma_mask(pdev, dma_mask);
15191 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015192 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015193 err = pci_set_consistent_dma_mask(pdev,
15194 persist_dma_mask);
15195 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015196 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15197 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015198 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015199 }
15200 }
15201 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015202 if (err || dma_mask == DMA_BIT_MASK(32)) {
15203 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015204 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015205 dev_err(&pdev->dev,
15206 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015207 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015208 }
15209 }
15210
Michael Chanfdfec172005-07-25 12:31:48 -070015211 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015212
Matt Carlson0da06062011-05-19 12:12:53 +000015213 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15214
15215 /* 5700 B0 chips do not support checksumming correctly due
15216 * to hardware bugs.
15217 */
15218 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15219 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15220
15221 if (tg3_flag(tp, 5755_PLUS))
15222 features |= NETIF_F_IPV6_CSUM;
15223 }
15224
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015225 /* TSO is on by default on chips that support hardware TSO.
15226 * Firmware TSO on older chips gives lower performance, so it
15227 * is off by default, but can be enabled using ethtool.
15228 */
Joe Perches63c3a662011-04-26 08:12:10 +000015229 if ((tg3_flag(tp, HW_TSO_1) ||
15230 tg3_flag(tp, HW_TSO_2) ||
15231 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015232 (features & NETIF_F_IP_CSUM))
15233 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015234 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015235 if (features & NETIF_F_IPV6_CSUM)
15236 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015237 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015238 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015239 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15240 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015241 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015242 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015243 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015245
Matt Carlsond542fe22011-05-19 16:02:43 +000015246 dev->features |= features;
15247 dev->vlan_features |= features;
15248
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015249 /*
15250 * Add loopback capability only for a subset of devices that support
15251 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15252 * loopback for the remaining devices.
15253 */
15254 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15255 !tg3_flag(tp, CPMU_PRESENT))
15256 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015257 features |= NETIF_F_LOOPBACK;
15258
Matt Carlson0da06062011-05-19 12:12:53 +000015259 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015260
Linus Torvalds1da177e2005-04-16 15:20:36 -070015261 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015262 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015263 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015264 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015265 tp->rx_pending = 63;
15266 }
15267
Linus Torvalds1da177e2005-04-16 15:20:36 -070015268 err = tg3_get_device_address(tp);
15269 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015270 dev_err(&pdev->dev,
15271 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015272 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015273 }
15274
Matt Carlsonc88864d2007-11-12 21:07:01 -080015275 /*
15276 * Reset chip in case UNDI or EFI driver did not shutdown
15277 * DMA self test will enable WDMAC and we'll see (spurious)
15278 * pending DMA on the PCI bus at that point.
15279 */
15280 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15281 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15282 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15283 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15284 }
15285
15286 err = tg3_test_dma(tp);
15287 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015288 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015289 goto err_out_apeunmap;
15290 }
15291
Matt Carlson78f90dc2009-11-13 13:03:42 +000015292 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15293 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15294 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015295 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015296 struct tg3_napi *tnapi = &tp->napi[i];
15297
15298 tnapi->tp = tp;
15299 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15300
15301 tnapi->int_mbox = intmbx;
15302 if (i < 4)
15303 intmbx += 0x8;
15304 else
15305 intmbx += 0x4;
15306
15307 tnapi->consmbox = rcvmbx;
15308 tnapi->prodmbox = sndmbx;
15309
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015310 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015311 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015312 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015313 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015314
Joe Perches63c3a662011-04-26 08:12:10 +000015315 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015316 break;
15317
15318 /*
15319 * If we support MSIX, we'll be using RSS. If we're using
15320 * RSS, the first vector only handles link interrupts and the
15321 * remaining vectors handle rx and tx interrupts. Reuse the
15322 * mailbox values for the next iteration. The values we setup
15323 * above are still useful for the single vectored mode.
15324 */
15325 if (!i)
15326 continue;
15327
15328 rcvmbx += 0x8;
15329
15330 if (sndmbx & 0x4)
15331 sndmbx -= 0x4;
15332 else
15333 sndmbx += 0xc;
15334 }
15335
Matt Carlsonc88864d2007-11-12 21:07:01 -080015336 tg3_init_coal(tp);
15337
Michael Chanc49a1562006-12-17 17:07:29 -080015338 pci_set_drvdata(pdev, dev);
15339
Linus Torvalds1da177e2005-04-16 15:20:36 -070015340 err = register_netdev(dev);
15341 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015342 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015343 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015344 }
15345
Joe Perches05dbe002010-02-17 19:44:19 +000015346 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15347 tp->board_part_number,
15348 tp->pci_chip_rev_id,
15349 tg3_bus_string(tp, str),
15350 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015351
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015352 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015353 struct phy_device *phydev;
15354 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015355 netdev_info(dev,
15356 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015357 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015358 } else {
15359 char *ethtype;
15360
15361 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15362 ethtype = "10/100Base-TX";
15363 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15364 ethtype = "1000Base-SX";
15365 else
15366 ethtype = "10/100/1000Base-T";
15367
Matt Carlson5129c3a2010-04-05 10:19:23 +000015368 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015369 "(WireSpeed[%d], EEE[%d])\n",
15370 tg3_phy_string(tp), ethtype,
15371 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15372 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015373 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015374
Joe Perches05dbe002010-02-17 19:44:19 +000015375 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015376 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015377 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015378 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015379 tg3_flag(tp, ENABLE_ASF) != 0,
15380 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015381 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15382 tp->dma_rwctrl,
15383 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15384 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015385
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015386 pci_save_state(pdev);
15387
Linus Torvalds1da177e2005-04-16 15:20:36 -070015388 return 0;
15389
Matt Carlson0d3031d2007-10-10 18:02:43 -070015390err_out_apeunmap:
15391 if (tp->aperegs) {
15392 iounmap(tp->aperegs);
15393 tp->aperegs = NULL;
15394 }
15395
Linus Torvalds1da177e2005-04-16 15:20:36 -070015396err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015397 if (tp->regs) {
15398 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015399 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015401
15402err_out_free_dev:
15403 free_netdev(dev);
15404
Matt Carlson16821282011-07-13 09:27:28 +000015405err_out_power_down:
15406 pci_set_power_state(pdev, PCI_D3hot);
15407
Linus Torvalds1da177e2005-04-16 15:20:36 -070015408err_out_free_res:
15409 pci_release_regions(pdev);
15410
15411err_out_disable_pdev:
15412 pci_disable_device(pdev);
15413 pci_set_drvdata(pdev, NULL);
15414 return err;
15415}
15416
15417static void __devexit tg3_remove_one(struct pci_dev *pdev)
15418{
15419 struct net_device *dev = pci_get_drvdata(pdev);
15420
15421 if (dev) {
15422 struct tg3 *tp = netdev_priv(dev);
15423
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015424 if (tp->fw)
15425 release_firmware(tp->fw);
15426
Tejun Heo23f333a2010-12-12 16:45:14 +010015427 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015428
Joe Perches63c3a662011-04-26 08:12:10 +000015429 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015430 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015431 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015432 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015433
Linus Torvalds1da177e2005-04-16 15:20:36 -070015434 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015435 if (tp->aperegs) {
15436 iounmap(tp->aperegs);
15437 tp->aperegs = NULL;
15438 }
Michael Chan68929142005-08-09 20:17:14 -070015439 if (tp->regs) {
15440 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015441 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015443 free_netdev(dev);
15444 pci_release_regions(pdev);
15445 pci_disable_device(pdev);
15446 pci_set_drvdata(pdev, NULL);
15447 }
15448}
15449
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015450#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015451static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015452{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015453 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015454 struct net_device *dev = pci_get_drvdata(pdev);
15455 struct tg3 *tp = netdev_priv(dev);
15456 int err;
15457
15458 if (!netif_running(dev))
15459 return 0;
15460
Tejun Heo23f333a2010-12-12 16:45:14 +010015461 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015462 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015463 tg3_netif_stop(tp);
15464
15465 del_timer_sync(&tp->timer);
15466
David S. Millerf47c11e2005-06-24 20:18:35 -070015467 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015468 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015469 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015470
15471 netif_device_detach(dev);
15472
David S. Millerf47c11e2005-06-24 20:18:35 -070015473 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015474 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015475 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015476 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015477
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015478 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015479 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015480 int err2;
15481
David S. Millerf47c11e2005-06-24 20:18:35 -070015482 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015483
Joe Perches63c3a662011-04-26 08:12:10 +000015484 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015485 err2 = tg3_restart_hw(tp, 1);
15486 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015487 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015488
15489 tp->timer.expires = jiffies + tp->timer_offset;
15490 add_timer(&tp->timer);
15491
15492 netif_device_attach(dev);
15493 tg3_netif_start(tp);
15494
Michael Chanb9ec6c12006-07-25 16:37:27 -070015495out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015496 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015497
15498 if (!err2)
15499 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015500 }
15501
15502 return err;
15503}
15504
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015505static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015506{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015507 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015508 struct net_device *dev = pci_get_drvdata(pdev);
15509 struct tg3 *tp = netdev_priv(dev);
15510 int err;
15511
15512 if (!netif_running(dev))
15513 return 0;
15514
Linus Torvalds1da177e2005-04-16 15:20:36 -070015515 netif_device_attach(dev);
15516
David S. Millerf47c11e2005-06-24 20:18:35 -070015517 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015518
Joe Perches63c3a662011-04-26 08:12:10 +000015519 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015520 err = tg3_restart_hw(tp, 1);
15521 if (err)
15522 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015523
15524 tp->timer.expires = jiffies + tp->timer_offset;
15525 add_timer(&tp->timer);
15526
Linus Torvalds1da177e2005-04-16 15:20:36 -070015527 tg3_netif_start(tp);
15528
Michael Chanb9ec6c12006-07-25 16:37:27 -070015529out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015530 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015531
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015532 if (!err)
15533 tg3_phy_start(tp);
15534
Michael Chanb9ec6c12006-07-25 16:37:27 -070015535 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015536}
15537
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015538static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015539#define TG3_PM_OPS (&tg3_pm_ops)
15540
15541#else
15542
15543#define TG3_PM_OPS NULL
15544
15545#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015546
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015547/**
15548 * tg3_io_error_detected - called when PCI error is detected
15549 * @pdev: Pointer to PCI device
15550 * @state: The current pci connection state
15551 *
15552 * This function is called after a PCI bus error affecting
15553 * this device has been detected.
15554 */
15555static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15556 pci_channel_state_t state)
15557{
15558 struct net_device *netdev = pci_get_drvdata(pdev);
15559 struct tg3 *tp = netdev_priv(netdev);
15560 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15561
15562 netdev_info(netdev, "PCI I/O error detected\n");
15563
15564 rtnl_lock();
15565
15566 if (!netif_running(netdev))
15567 goto done;
15568
15569 tg3_phy_stop(tp);
15570
15571 tg3_netif_stop(tp);
15572
15573 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015574 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015575
15576 /* Want to make sure that the reset task doesn't run */
15577 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015578 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15579 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015580
15581 netif_device_detach(netdev);
15582
15583 /* Clean up software state, even if MMIO is blocked */
15584 tg3_full_lock(tp, 0);
15585 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15586 tg3_full_unlock(tp);
15587
15588done:
15589 if (state == pci_channel_io_perm_failure)
15590 err = PCI_ERS_RESULT_DISCONNECT;
15591 else
15592 pci_disable_device(pdev);
15593
15594 rtnl_unlock();
15595
15596 return err;
15597}
15598
15599/**
15600 * tg3_io_slot_reset - called after the pci bus has been reset.
15601 * @pdev: Pointer to PCI device
15602 *
15603 * Restart the card from scratch, as if from a cold-boot.
15604 * At this point, the card has exprienced a hard reset,
15605 * followed by fixups by BIOS, and has its config space
15606 * set up identically to what it was at cold boot.
15607 */
15608static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15609{
15610 struct net_device *netdev = pci_get_drvdata(pdev);
15611 struct tg3 *tp = netdev_priv(netdev);
15612 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15613 int err;
15614
15615 rtnl_lock();
15616
15617 if (pci_enable_device(pdev)) {
15618 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15619 goto done;
15620 }
15621
15622 pci_set_master(pdev);
15623 pci_restore_state(pdev);
15624 pci_save_state(pdev);
15625
15626 if (!netif_running(netdev)) {
15627 rc = PCI_ERS_RESULT_RECOVERED;
15628 goto done;
15629 }
15630
15631 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000015632 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015633 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015634
15635 rc = PCI_ERS_RESULT_RECOVERED;
15636
15637done:
15638 rtnl_unlock();
15639
15640 return rc;
15641}
15642
15643/**
15644 * tg3_io_resume - called when traffic can start flowing again.
15645 * @pdev: Pointer to PCI device
15646 *
15647 * This callback is called when the error recovery driver tells
15648 * us that its OK to resume normal operation.
15649 */
15650static void tg3_io_resume(struct pci_dev *pdev)
15651{
15652 struct net_device *netdev = pci_get_drvdata(pdev);
15653 struct tg3 *tp = netdev_priv(netdev);
15654 int err;
15655
15656 rtnl_lock();
15657
15658 if (!netif_running(netdev))
15659 goto done;
15660
15661 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015662 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015663 err = tg3_restart_hw(tp, 1);
15664 tg3_full_unlock(tp);
15665 if (err) {
15666 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15667 goto done;
15668 }
15669
15670 netif_device_attach(netdev);
15671
15672 tp->timer.expires = jiffies + tp->timer_offset;
15673 add_timer(&tp->timer);
15674
15675 tg3_netif_start(tp);
15676
15677 tg3_phy_start(tp);
15678
15679done:
15680 rtnl_unlock();
15681}
15682
15683static struct pci_error_handlers tg3_err_handler = {
15684 .error_detected = tg3_io_error_detected,
15685 .slot_reset = tg3_io_slot_reset,
15686 .resume = tg3_io_resume
15687};
15688
Linus Torvalds1da177e2005-04-16 15:20:36 -070015689static struct pci_driver tg3_driver = {
15690 .name = DRV_MODULE_NAME,
15691 .id_table = tg3_pci_tbl,
15692 .probe = tg3_init_one,
15693 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015694 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015695 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015696};
15697
15698static int __init tg3_init(void)
15699{
Jeff Garzik29917622006-08-19 17:48:59 -040015700 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015701}
15702
15703static void __exit tg3_cleanup(void)
15704{
15705 pci_unregister_driver(&tg3_driver);
15706}
15707
15708module_init(tg3_init);
15709module_exit(tg3_cleanup);