blob: 6f1f36c89055f4eb26b9aa4664163da956465a19 [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 Carlson66711e662009-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 Carlsonf92d9dc12010-06-05 17:24:30 +0000611 u32 regbase;
612
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. */
619 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000620 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700621}
622
623static int tg3_ape_lock(struct tg3 *tp, int locknum)
624{
625 int i, off;
626 int ret = 0;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000627 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700628
Joe Perches63c3a662011-04-26 08:12:10 +0000629 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700630 return 0;
631
632 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000633 case TG3_APE_LOCK_GRC:
634 case TG3_APE_LOCK_MEM:
635 break;
636 default:
637 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700638 }
639
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000640 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
641 req = TG3_APE_LOCK_REQ;
642 gnt = TG3_APE_LOCK_GRANT;
643 } else {
644 req = TG3_APE_PER_LOCK_REQ;
645 gnt = TG3_APE_PER_LOCK_GRANT;
646 }
647
Matt Carlson0d3031d2007-10-10 18:02:43 -0700648 off = 4 * locknum;
649
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000650 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700651
652 /* Wait for up to 1 millisecond to acquire lock. */
653 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000654 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700655 if (status == APE_LOCK_GRANT_DRIVER)
656 break;
657 udelay(10);
658 }
659
660 if (status != APE_LOCK_GRANT_DRIVER) {
661 /* Revoke the lock request. */
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000662 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700663 APE_LOCK_GRANT_DRIVER);
664
665 ret = -EBUSY;
666 }
667
668 return ret;
669}
670
671static void tg3_ape_unlock(struct tg3 *tp, int locknum)
672{
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000673 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700674
Joe Perches63c3a662011-04-26 08:12:10 +0000675 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700676 return;
677
678 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000679 case TG3_APE_LOCK_GRC:
680 case TG3_APE_LOCK_MEM:
681 break;
682 default:
683 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700684 }
685
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000686 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
687 gnt = TG3_APE_LOCK_GRANT;
688 else
689 gnt = TG3_APE_PER_LOCK_GRANT;
690
691 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700692}
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static void tg3_disable_ints(struct tg3 *tp)
695{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000696 int i;
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 tw32(TG3PCI_MISC_HOST_CTRL,
699 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000700 for (i = 0; i < tp->irq_max; i++)
701 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704static void tg3_enable_ints(struct tg3 *tp)
705{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000706 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000707
Michael Chanbbe832c2005-06-24 20:20:04 -0700708 tp->irq_sync = 0;
709 wmb();
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 tw32(TG3PCI_MISC_HOST_CTRL,
712 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000713
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000714 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000715 for (i = 0; i < tp->irq_cnt; i++) {
716 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000717
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000718 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000719 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000720 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
721
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000722 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000723 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000724
725 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000726 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000727 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
728 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
729 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000730 tw32(HOSTCC_MODE, tp->coal_now);
731
732 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Matt Carlson17375d22009-08-28 14:02:18 +0000735static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700736{
Matt Carlson17375d22009-08-28 14:02:18 +0000737 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000738 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700739 unsigned int work_exists = 0;
740
741 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000742 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700743 if (sblk->status & SD_STATUS_LINK_CHG)
744 work_exists = 1;
745 }
746 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000747 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000748 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700749 work_exists = 1;
750
751 return work_exists;
752}
753
Matt Carlson17375d22009-08-28 14:02:18 +0000754/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700755 * similar to tg3_enable_ints, but it accurately determines whether there
756 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400757 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 */
Matt Carlson17375d22009-08-28 14:02:18 +0000759static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Matt Carlson17375d22009-08-28 14:02:18 +0000761 struct tg3 *tp = tnapi->tp;
762
Matt Carlson898a56f2009-08-28 14:02:40 +0000763 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 mmiowb();
765
David S. Millerfac9b832005-05-18 22:46:34 -0700766 /* When doing tagged status, this work check is unnecessary.
767 * The last_tag we write above tells the chip which piece of
768 * work we've completed.
769 */
Joe Perches63c3a662011-04-26 08:12:10 +0000770 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700771 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000772 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775static void tg3_switch_clocks(struct tg3 *tp)
776{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000777 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 u32 orig_clock_ctrl;
779
Joe Perches63c3a662011-04-26 08:12:10 +0000780 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700781 return;
782
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000783 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 orig_clock_ctrl = clock_ctrl;
786 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
787 CLOCK_CTRL_CLKRUN_OENABLE |
788 0x1f);
789 tp->pci_clock_ctrl = clock_ctrl;
790
Joe Perches63c3a662011-04-26 08:12:10 +0000791 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800793 tw32_wait_f(TG3PCI_CLOCK_CTRL,
794 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800797 tw32_wait_f(TG3PCI_CLOCK_CTRL,
798 clock_ctrl |
799 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
800 40);
801 tw32_wait_f(TG3PCI_CLOCK_CTRL,
802 clock_ctrl | (CLOCK_CTRL_ALTCLK),
803 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800805 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
808#define PHY_BUSY_LOOPS 5000
809
810static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
811{
812 u32 frame_val;
813 unsigned int loops;
814 int ret;
815
816 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
817 tw32_f(MAC_MI_MODE,
818 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
819 udelay(80);
820 }
821
822 *val = 0x0;
823
Matt Carlson882e9792009-09-01 13:21:36 +0000824 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 MI_COM_PHY_ADDR_MASK);
826 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
827 MI_COM_REG_ADDR_MASK);
828 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 tw32_f(MAC_MI_COM, frame_val);
831
832 loops = PHY_BUSY_LOOPS;
833 while (loops != 0) {
834 udelay(10);
835 frame_val = tr32(MAC_MI_COM);
836
837 if ((frame_val & MI_COM_BUSY) == 0) {
838 udelay(5);
839 frame_val = tr32(MAC_MI_COM);
840 break;
841 }
842 loops -= 1;
843 }
844
845 ret = -EBUSY;
846 if (loops != 0) {
847 *val = frame_val & MI_COM_DATA_MASK;
848 ret = 0;
849 }
850
851 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
852 tw32_f(MAC_MI_MODE, tp->mi_mode);
853 udelay(80);
854 }
855
856 return ret;
857}
858
859static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
860{
861 u32 frame_val;
862 unsigned int loops;
863 int ret;
864
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000865 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +0000866 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -0700867 return 0;
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
870 tw32_f(MAC_MI_MODE,
871 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
872 udelay(80);
873 }
874
Matt Carlson882e9792009-09-01 13:21:36 +0000875 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 MI_COM_PHY_ADDR_MASK);
877 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
878 MI_COM_REG_ADDR_MASK);
879 frame_val |= (val & MI_COM_DATA_MASK);
880 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 tw32_f(MAC_MI_COM, frame_val);
883
884 loops = PHY_BUSY_LOOPS;
885 while (loops != 0) {
886 udelay(10);
887 frame_val = tr32(MAC_MI_COM);
888 if ((frame_val & MI_COM_BUSY) == 0) {
889 udelay(5);
890 frame_val = tr32(MAC_MI_COM);
891 break;
892 }
893 loops -= 1;
894 }
895
896 ret = -EBUSY;
897 if (loops != 0)
898 ret = 0;
899
900 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
901 tw32_f(MAC_MI_MODE, tp->mi_mode);
902 udelay(80);
903 }
904
905 return ret;
906}
907
Matt Carlsonb0988c12011-04-20 07:57:39 +0000908static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
909{
910 int err;
911
912 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
913 if (err)
914 goto done;
915
916 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
917 if (err)
918 goto done;
919
920 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
921 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
922 if (err)
923 goto done;
924
925 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
926
927done:
928 return err;
929}
930
931static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
932{
933 int err;
934
935 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
936 if (err)
937 goto done;
938
939 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
940 if (err)
941 goto done;
942
943 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
944 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
945 if (err)
946 goto done;
947
948 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
949
950done:
951 return err;
952}
953
954static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
955{
956 int err;
957
958 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
959 if (!err)
960 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
961
962 return err;
963}
964
965static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
966{
967 int err;
968
969 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
970 if (!err)
971 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
972
973 return err;
974}
975
Matt Carlson15ee95c2011-04-20 07:57:40 +0000976static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
977{
978 int err;
979
980 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
981 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
982 MII_TG3_AUXCTL_SHDWSEL_MISC);
983 if (!err)
984 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
985
986 return err;
987}
988
Matt Carlsonb4bd2922011-04-20 07:57:41 +0000989static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
990{
991 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
992 set |= MII_TG3_AUXCTL_MISC_WREN;
993
994 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
995}
996
Matt Carlson1d36ba42011-04-20 07:57:42 +0000997#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
998 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
999 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1000 MII_TG3_AUXCTL_ACTL_TX_6DB)
1001
1002#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1003 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1004 MII_TG3_AUXCTL_ACTL_TX_6DB);
1005
Matt Carlson95e28692008-05-25 23:44:14 -07001006static int tg3_bmcr_reset(struct tg3 *tp)
1007{
1008 u32 phy_control;
1009 int limit, err;
1010
1011 /* OK, reset it, and poll the BMCR_RESET bit until it
1012 * clears or we time out.
1013 */
1014 phy_control = BMCR_RESET;
1015 err = tg3_writephy(tp, MII_BMCR, phy_control);
1016 if (err != 0)
1017 return -EBUSY;
1018
1019 limit = 5000;
1020 while (limit--) {
1021 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1022 if (err != 0)
1023 return -EBUSY;
1024
1025 if ((phy_control & BMCR_RESET) == 0) {
1026 udelay(40);
1027 break;
1028 }
1029 udelay(10);
1030 }
Roel Kluind4675b52009-02-12 16:33:27 -08001031 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001032 return -EBUSY;
1033
1034 return 0;
1035}
1036
Matt Carlson158d7ab2008-05-29 01:37:54 -07001037static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1038{
Francois Romieu3d165432009-01-19 16:56:50 -08001039 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001040 u32 val;
1041
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001042 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001043
1044 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001045 val = -EIO;
1046
1047 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001048
1049 return val;
1050}
1051
1052static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1053{
Francois Romieu3d165432009-01-19 16:56:50 -08001054 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001055 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001056
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001057 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001058
1059 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001060 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001061
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001062 spin_unlock_bh(&tp->lock);
1063
1064 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001065}
1066
1067static int tg3_mdio_reset(struct mii_bus *bp)
1068{
1069 return 0;
1070}
1071
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001072static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001073{
1074 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001075 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001076
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001077 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001078 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001079 case PHY_ID_BCM50610:
1080 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001081 val = MAC_PHYCFG2_50610_LED_MODES;
1082 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001083 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001084 val = MAC_PHYCFG2_AC131_LED_MODES;
1085 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001086 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001087 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1088 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001089 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001090 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1091 break;
1092 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001093 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001094 }
1095
1096 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1097 tw32(MAC_PHYCFG2, val);
1098
1099 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001100 val &= ~(MAC_PHYCFG1_RGMII_INT |
1101 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1102 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001103 tw32(MAC_PHYCFG1, val);
1104
1105 return;
1106 }
1107
Joe Perches63c3a662011-04-26 08:12:10 +00001108 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001109 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1110 MAC_PHYCFG2_FMODE_MASK_MASK |
1111 MAC_PHYCFG2_GMODE_MASK_MASK |
1112 MAC_PHYCFG2_ACT_MASK_MASK |
1113 MAC_PHYCFG2_QUAL_MASK_MASK |
1114 MAC_PHYCFG2_INBAND_ENABLE;
1115
1116 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001117
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001118 val = tr32(MAC_PHYCFG1);
1119 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1120 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001121 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1122 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001123 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001124 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001125 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1126 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001127 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1128 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1129 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001130
Matt Carlsona9daf362008-05-25 23:49:44 -07001131 val = tr32(MAC_EXT_RGMII_MODE);
1132 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1133 MAC_RGMII_MODE_RX_QUALITY |
1134 MAC_RGMII_MODE_RX_ACTIVITY |
1135 MAC_RGMII_MODE_RX_ENG_DET |
1136 MAC_RGMII_MODE_TX_ENABLE |
1137 MAC_RGMII_MODE_TX_LOWPWR |
1138 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001139 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1140 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001141 val |= MAC_RGMII_MODE_RX_INT_B |
1142 MAC_RGMII_MODE_RX_QUALITY |
1143 MAC_RGMII_MODE_RX_ACTIVITY |
1144 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001145 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001146 val |= MAC_RGMII_MODE_TX_ENABLE |
1147 MAC_RGMII_MODE_TX_LOWPWR |
1148 MAC_RGMII_MODE_TX_RESET;
1149 }
1150 tw32(MAC_EXT_RGMII_MODE, val);
1151}
1152
Matt Carlson158d7ab2008-05-29 01:37:54 -07001153static void tg3_mdio_start(struct tg3 *tp)
1154{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001155 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1156 tw32_f(MAC_MI_MODE, tp->mi_mode);
1157 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001158
Joe Perches63c3a662011-04-26 08:12:10 +00001159 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001160 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1161 tg3_mdio_config_5785(tp);
1162}
1163
1164static int tg3_mdio_init(struct tg3 *tp)
1165{
1166 int i;
1167 u32 reg;
1168 struct phy_device *phydev;
1169
Joe Perches63c3a662011-04-26 08:12:10 +00001170 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001171 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001172
Matt Carlson69f11c92011-07-13 09:27:30 +00001173 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001174
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001175 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1176 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1177 else
1178 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1179 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001180 if (is_serdes)
1181 tp->phy_addr += 7;
1182 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001183 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001184
Matt Carlson158d7ab2008-05-29 01:37:54 -07001185 tg3_mdio_start(tp);
1186
Joe Perches63c3a662011-04-26 08:12:10 +00001187 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001188 return 0;
1189
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001190 tp->mdio_bus = mdiobus_alloc();
1191 if (tp->mdio_bus == NULL)
1192 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001193
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001194 tp->mdio_bus->name = "tg3 mdio bus";
1195 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001196 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001197 tp->mdio_bus->priv = tp;
1198 tp->mdio_bus->parent = &tp->pdev->dev;
1199 tp->mdio_bus->read = &tg3_mdio_read;
1200 tp->mdio_bus->write = &tg3_mdio_write;
1201 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001202 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001203 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001204
1205 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001206 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001207
1208 /* The bus registration will look for all the PHYs on the mdio bus.
1209 * Unfortunately, it does not ensure the PHY is powered up before
1210 * accessing the PHY ID registers. A chip reset is the
1211 * quickest way to bring the device back to an operational state..
1212 */
1213 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1214 tg3_bmcr_reset(tp);
1215
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001216 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001217 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001218 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001219 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001220 return i;
1221 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001222
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001223 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001224
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001225 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001226 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001227 mdiobus_unregister(tp->mdio_bus);
1228 mdiobus_free(tp->mdio_bus);
1229 return -ENODEV;
1230 }
1231
1232 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001233 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001234 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001235 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001236 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001237 case PHY_ID_BCM50610:
1238 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001239 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001240 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001241 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001242 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001243 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001244 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001245 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001246 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001247 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001248 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001249 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001250 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001251 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001252 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001253 case PHY_ID_RTL8201E:
1254 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001255 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001256 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001257 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001258 break;
1259 }
1260
Joe Perches63c3a662011-04-26 08:12:10 +00001261 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001262
1263 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1264 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001265
1266 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001267}
1268
1269static void tg3_mdio_fini(struct tg3 *tp)
1270{
Joe Perches63c3a662011-04-26 08:12:10 +00001271 if (tg3_flag(tp, MDIOBUS_INITED)) {
1272 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001273 mdiobus_unregister(tp->mdio_bus);
1274 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001275 }
1276}
1277
Matt Carlson95e28692008-05-25 23:44:14 -07001278/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001279static inline void tg3_generate_fw_event(struct tg3 *tp)
1280{
1281 u32 val;
1282
1283 val = tr32(GRC_RX_CPU_EVENT);
1284 val |= GRC_RX_CPU_DRIVER_EVENT;
1285 tw32_f(GRC_RX_CPU_EVENT, val);
1286
1287 tp->last_event_jiffies = jiffies;
1288}
1289
1290#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1291
1292/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001293static void tg3_wait_for_event_ack(struct tg3 *tp)
1294{
1295 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001296 unsigned int delay_cnt;
1297 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001298
Matt Carlson4ba526c2008-08-15 14:10:04 -07001299 /* If enough time has passed, no wait is necessary. */
1300 time_remain = (long)(tp->last_event_jiffies + 1 +
1301 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1302 (long)jiffies;
1303 if (time_remain < 0)
1304 return;
1305
1306 /* Check if we can shorten the wait time. */
1307 delay_cnt = jiffies_to_usecs(time_remain);
1308 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1309 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1310 delay_cnt = (delay_cnt >> 3) + 1;
1311
1312 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001313 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1314 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001315 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001316 }
1317}
1318
1319/* tp->lock is held. */
1320static void tg3_ump_link_report(struct tg3 *tp)
1321{
1322 u32 reg;
1323 u32 val;
1324
Joe Perches63c3a662011-04-26 08:12:10 +00001325 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001326 return;
1327
1328 tg3_wait_for_event_ack(tp);
1329
1330 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1331
1332 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1333
1334 val = 0;
1335 if (!tg3_readphy(tp, MII_BMCR, &reg))
1336 val = reg << 16;
1337 if (!tg3_readphy(tp, MII_BMSR, &reg))
1338 val |= (reg & 0xffff);
1339 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1340
1341 val = 0;
1342 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1343 val = reg << 16;
1344 if (!tg3_readphy(tp, MII_LPA, &reg))
1345 val |= (reg & 0xffff);
1346 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1347
1348 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001349 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001350 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1351 val = reg << 16;
1352 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1353 val |= (reg & 0xffff);
1354 }
1355 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1356
1357 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1358 val = reg << 16;
1359 else
1360 val = 0;
1361 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1362
Matt Carlson4ba526c2008-08-15 14:10:04 -07001363 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001364}
1365
1366static void tg3_link_report(struct tg3 *tp)
1367{
1368 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001369 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001370 tg3_ump_link_report(tp);
1371 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001372 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1373 (tp->link_config.active_speed == SPEED_1000 ?
1374 1000 :
1375 (tp->link_config.active_speed == SPEED_100 ?
1376 100 : 10)),
1377 (tp->link_config.active_duplex == DUPLEX_FULL ?
1378 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001379
Joe Perches05dbe002010-02-17 19:44:19 +00001380 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1381 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1382 "on" : "off",
1383 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1384 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001385
1386 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1387 netdev_info(tp->dev, "EEE is %s\n",
1388 tp->setlpicnt ? "enabled" : "disabled");
1389
Matt Carlson95e28692008-05-25 23:44:14 -07001390 tg3_ump_link_report(tp);
1391 }
1392}
1393
1394static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1395{
1396 u16 miireg;
1397
Steve Glendinninge18ce342008-12-16 02:00:00 -08001398 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001399 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001400 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001401 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001402 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001403 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1404 else
1405 miireg = 0;
1406
1407 return miireg;
1408}
1409
1410static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1411{
1412 u16 miireg;
1413
Steve Glendinninge18ce342008-12-16 02:00:00 -08001414 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001415 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001416 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001417 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001418 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001419 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1420 else
1421 miireg = 0;
1422
1423 return miireg;
1424}
1425
Matt Carlson95e28692008-05-25 23:44:14 -07001426static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1427{
1428 u8 cap = 0;
1429
1430 if (lcladv & ADVERTISE_1000XPAUSE) {
1431 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1432 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001433 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001434 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001435 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001436 } else {
1437 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001438 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001439 }
1440 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1441 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001442 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001443 }
1444
1445 return cap;
1446}
1447
Matt Carlsonf51f3562008-05-25 23:45:08 -07001448static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001449{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001450 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001451 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001452 u32 old_rx_mode = tp->rx_mode;
1453 u32 old_tx_mode = tp->tx_mode;
1454
Joe Perches63c3a662011-04-26 08:12:10 +00001455 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001456 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001457 else
1458 autoneg = tp->link_config.autoneg;
1459
Joe Perches63c3a662011-04-26 08:12:10 +00001460 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001461 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001462 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001463 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001464 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001465 } else
1466 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001467
Matt Carlsonf51f3562008-05-25 23:45:08 -07001468 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001469
Steve Glendinninge18ce342008-12-16 02:00:00 -08001470 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001471 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1472 else
1473 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1474
Matt Carlsonf51f3562008-05-25 23:45:08 -07001475 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001476 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001477
Steve Glendinninge18ce342008-12-16 02:00:00 -08001478 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001479 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1480 else
1481 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1482
Matt Carlsonf51f3562008-05-25 23:45:08 -07001483 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001484 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001485}
1486
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001487static void tg3_adjust_link(struct net_device *dev)
1488{
1489 u8 oldflowctrl, linkmesg = 0;
1490 u32 mac_mode, lcl_adv, rmt_adv;
1491 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001492 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001493
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001494 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001495
1496 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1497 MAC_MODE_HALF_DUPLEX);
1498
1499 oldflowctrl = tp->link_config.active_flowctrl;
1500
1501 if (phydev->link) {
1502 lcl_adv = 0;
1503 rmt_adv = 0;
1504
1505 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1506 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001507 else if (phydev->speed == SPEED_1000 ||
1508 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001509 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001510 else
1511 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001512
1513 if (phydev->duplex == DUPLEX_HALF)
1514 mac_mode |= MAC_MODE_HALF_DUPLEX;
1515 else {
1516 lcl_adv = tg3_advert_flowctrl_1000T(
1517 tp->link_config.flowctrl);
1518
1519 if (phydev->pause)
1520 rmt_adv = LPA_PAUSE_CAP;
1521 if (phydev->asym_pause)
1522 rmt_adv |= LPA_PAUSE_ASYM;
1523 }
1524
1525 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1526 } else
1527 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1528
1529 if (mac_mode != tp->mac_mode) {
1530 tp->mac_mode = mac_mode;
1531 tw32_f(MAC_MODE, tp->mac_mode);
1532 udelay(40);
1533 }
1534
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001535 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1536 if (phydev->speed == SPEED_10)
1537 tw32(MAC_MI_STAT,
1538 MAC_MI_STAT_10MBPS_MODE |
1539 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1540 else
1541 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1542 }
1543
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001544 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1545 tw32(MAC_TX_LENGTHS,
1546 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1547 (6 << TX_LENGTHS_IPG_SHIFT) |
1548 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1549 else
1550 tw32(MAC_TX_LENGTHS,
1551 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1552 (6 << TX_LENGTHS_IPG_SHIFT) |
1553 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1554
1555 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1556 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1557 phydev->speed != tp->link_config.active_speed ||
1558 phydev->duplex != tp->link_config.active_duplex ||
1559 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001560 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001561
1562 tp->link_config.active_speed = phydev->speed;
1563 tp->link_config.active_duplex = phydev->duplex;
1564
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001565 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001566
1567 if (linkmesg)
1568 tg3_link_report(tp);
1569}
1570
1571static int tg3_phy_init(struct tg3 *tp)
1572{
1573 struct phy_device *phydev;
1574
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001575 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001576 return 0;
1577
1578 /* Bring the PHY back to a known state. */
1579 tg3_bmcr_reset(tp);
1580
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001581 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001582
1583 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001584 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001585 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001586 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001587 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001588 return PTR_ERR(phydev);
1589 }
1590
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001591 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001592 switch (phydev->interface) {
1593 case PHY_INTERFACE_MODE_GMII:
1594 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001595 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001596 phydev->supported &= (PHY_GBIT_FEATURES |
1597 SUPPORTED_Pause |
1598 SUPPORTED_Asym_Pause);
1599 break;
1600 }
1601 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001602 case PHY_INTERFACE_MODE_MII:
1603 phydev->supported &= (PHY_BASIC_FEATURES |
1604 SUPPORTED_Pause |
1605 SUPPORTED_Asym_Pause);
1606 break;
1607 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001608 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001609 return -EINVAL;
1610 }
1611
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001612 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001613
1614 phydev->advertising = phydev->supported;
1615
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001616 return 0;
1617}
1618
1619static void tg3_phy_start(struct tg3 *tp)
1620{
1621 struct phy_device *phydev;
1622
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001623 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001624 return;
1625
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001626 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001627
Matt Carlson80096062010-08-02 11:26:06 +00001628 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1629 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001630 phydev->speed = tp->link_config.orig_speed;
1631 phydev->duplex = tp->link_config.orig_duplex;
1632 phydev->autoneg = tp->link_config.orig_autoneg;
1633 phydev->advertising = tp->link_config.orig_advertising;
1634 }
1635
1636 phy_start(phydev);
1637
1638 phy_start_aneg(phydev);
1639}
1640
1641static void tg3_phy_stop(struct tg3 *tp)
1642{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001643 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001644 return;
1645
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001646 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001647}
1648
1649static void tg3_phy_fini(struct tg3 *tp)
1650{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001651 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001652 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001653 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001654 }
1655}
1656
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001657static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1658{
1659 u32 phytest;
1660
1661 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1662 u32 phy;
1663
1664 tg3_writephy(tp, MII_TG3_FET_TEST,
1665 phytest | MII_TG3_FET_SHADOW_EN);
1666 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1667 if (enable)
1668 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1669 else
1670 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1671 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1672 }
1673 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1674 }
1675}
1676
Matt Carlson6833c042008-11-21 17:18:59 -08001677static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1678{
1679 u32 reg;
1680
Joe Perches63c3a662011-04-26 08:12:10 +00001681 if (!tg3_flag(tp, 5705_PLUS) ||
1682 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001683 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001684 return;
1685
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001686 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001687 tg3_phy_fet_toggle_apd(tp, enable);
1688 return;
1689 }
1690
Matt Carlson6833c042008-11-21 17:18:59 -08001691 reg = MII_TG3_MISC_SHDW_WREN |
1692 MII_TG3_MISC_SHDW_SCR5_SEL |
1693 MII_TG3_MISC_SHDW_SCR5_LPED |
1694 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1695 MII_TG3_MISC_SHDW_SCR5_SDTL |
1696 MII_TG3_MISC_SHDW_SCR5_C125OE;
1697 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1698 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1699
1700 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1701
1702
1703 reg = MII_TG3_MISC_SHDW_WREN |
1704 MII_TG3_MISC_SHDW_APD_SEL |
1705 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1706 if (enable)
1707 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1708
1709 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1710}
1711
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001712static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1713{
1714 u32 phy;
1715
Joe Perches63c3a662011-04-26 08:12:10 +00001716 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001717 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001718 return;
1719
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001720 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001721 u32 ephy;
1722
Matt Carlson535ef6e2009-08-25 10:09:36 +00001723 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1724 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1725
1726 tg3_writephy(tp, MII_TG3_FET_TEST,
1727 ephy | MII_TG3_FET_SHADOW_EN);
1728 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001729 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001730 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001731 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001732 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1733 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001734 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001735 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001736 }
1737 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00001738 int ret;
1739
1740 ret = tg3_phy_auxctl_read(tp,
1741 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
1742 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001743 if (enable)
1744 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1745 else
1746 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001747 tg3_phy_auxctl_write(tp,
1748 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001749 }
1750 }
1751}
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753static void tg3_phy_set_wirespeed(struct tg3 *tp)
1754{
Matt Carlson15ee95c2011-04-20 07:57:40 +00001755 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 u32 val;
1757
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001758 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return;
1760
Matt Carlson15ee95c2011-04-20 07:57:40 +00001761 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
1762 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001763 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
1764 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001767static void tg3_phy_apply_otp(struct tg3 *tp)
1768{
1769 u32 otp, phy;
1770
1771 if (!tp->phy_otp)
1772 return;
1773
1774 otp = tp->phy_otp;
1775
Matt Carlson1d36ba42011-04-20 07:57:42 +00001776 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
1777 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001778
1779 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1780 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1781 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1782
1783 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1784 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1785 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1786
1787 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1788 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1789 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1790
1791 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1792 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1793
1794 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1795 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1796
1797 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1798 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1799 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1800
Matt Carlson1d36ba42011-04-20 07:57:42 +00001801 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001802}
1803
Matt Carlson52b02d02010-10-14 10:37:41 +00001804static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1805{
1806 u32 val;
1807
1808 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1809 return;
1810
1811 tp->setlpicnt = 0;
1812
1813 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1814 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001815 tp->link_config.active_duplex == DUPLEX_FULL &&
1816 (tp->link_config.active_speed == SPEED_100 ||
1817 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001818 u32 eeectl;
1819
1820 if (tp->link_config.active_speed == SPEED_1000)
1821 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1822 else
1823 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1824
1825 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1826
Matt Carlson3110f5f52010-12-06 08:28:50 +00001827 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1828 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001829
Matt Carlsonb0c59432011-05-19 12:12:48 +00001830 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
1831 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00001832 tp->setlpicnt = 2;
1833 }
1834
1835 if (!tp->setlpicnt) {
1836 val = tr32(TG3_CPMU_EEE_MODE);
1837 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1838 }
1839}
1840
Matt Carlsonb0c59432011-05-19 12:12:48 +00001841static void tg3_phy_eee_enable(struct tg3 *tp)
1842{
1843 u32 val;
1844
1845 if (tp->link_config.active_speed == SPEED_1000 &&
1846 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1847 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
1848 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
1849 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
1850 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0003);
1851 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1852 }
1853
1854 val = tr32(TG3_CPMU_EEE_MODE);
1855 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
1856}
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858static int tg3_wait_macro_done(struct tg3 *tp)
1859{
1860 int limit = 100;
1861
1862 while (limit--) {
1863 u32 tmp32;
1864
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001865 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if ((tmp32 & 0x1000) == 0)
1867 break;
1868 }
1869 }
Roel Kluind4675b52009-02-12 16:33:27 -08001870 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return -EBUSY;
1872
1873 return 0;
1874}
1875
1876static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1877{
1878 static const u32 test_pat[4][6] = {
1879 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1880 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1881 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1882 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1883 };
1884 int chan;
1885
1886 for (chan = 0; chan < 4; chan++) {
1887 int i;
1888
1889 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1890 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001891 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 for (i = 0; i < 6; i++)
1894 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1895 test_pat[chan][i]);
1896
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001897 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (tg3_wait_macro_done(tp)) {
1899 *resetp = 1;
1900 return -EBUSY;
1901 }
1902
1903 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1904 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001905 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (tg3_wait_macro_done(tp)) {
1907 *resetp = 1;
1908 return -EBUSY;
1909 }
1910
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001911 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (tg3_wait_macro_done(tp)) {
1913 *resetp = 1;
1914 return -EBUSY;
1915 }
1916
1917 for (i = 0; i < 6; i += 2) {
1918 u32 low, high;
1919
1920 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1921 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1922 tg3_wait_macro_done(tp)) {
1923 *resetp = 1;
1924 return -EBUSY;
1925 }
1926 low &= 0x7fff;
1927 high &= 0x000f;
1928 if (low != test_pat[chan][i] ||
1929 high != test_pat[chan][i+1]) {
1930 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1931 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1932 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1933
1934 return -EBUSY;
1935 }
1936 }
1937 }
1938
1939 return 0;
1940}
1941
1942static int tg3_phy_reset_chanpat(struct tg3 *tp)
1943{
1944 int chan;
1945
1946 for (chan = 0; chan < 4; chan++) {
1947 int i;
1948
1949 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1950 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001951 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 for (i = 0; i < 6; i++)
1953 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001954 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (tg3_wait_macro_done(tp))
1956 return -EBUSY;
1957 }
1958
1959 return 0;
1960}
1961
1962static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1963{
1964 u32 reg32, phy9_orig;
1965 int retries, do_phy_reset, err;
1966
1967 retries = 10;
1968 do_phy_reset = 1;
1969 do {
1970 if (do_phy_reset) {
1971 err = tg3_bmcr_reset(tp);
1972 if (err)
1973 return err;
1974 do_phy_reset = 0;
1975 }
1976
1977 /* Disable transmitter and interrupt. */
1978 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1979 continue;
1980
1981 reg32 |= 0x3000;
1982 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1983
1984 /* Set full-duplex, 1000 mbps. */
1985 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00001986 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00001989 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 continue;
1991
Matt Carlson221c5632011-06-13 13:39:01 +00001992 tg3_writephy(tp, MII_CTRL1000,
1993 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Matt Carlson1d36ba42011-04-20 07:57:42 +00001995 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
1996 if (err)
1997 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002000 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2003 if (!err)
2004 break;
2005 } while (--retries);
2006
2007 err = tg3_phy_reset_chanpat(tp);
2008 if (err)
2009 return err;
2010
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002011 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002014 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Matt Carlson1d36ba42011-04-20 07:57:42 +00002016 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Matt Carlson221c5632011-06-13 13:39:01 +00002018 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2021 reg32 &= ~0x3000;
2022 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2023 } else if (!err)
2024 err = -EBUSY;
2025
2026 return err;
2027}
2028
2029/* This will reset the tigon3 PHY if there is no valid
2030 * link unless the FORCE argument is non-zero.
2031 */
2032static int tg3_phy_reset(struct tg3 *tp)
2033{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002034 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 int err;
2036
Michael Chan60189dd2006-12-17 17:08:07 -08002037 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002038 val = tr32(GRC_MISC_CFG);
2039 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2040 udelay(40);
2041 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002042 err = tg3_readphy(tp, MII_BMSR, &val);
2043 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (err != 0)
2045 return -EBUSY;
2046
Michael Chanc8e1e822006-04-29 18:55:17 -07002047 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2048 netif_carrier_off(tp->dev);
2049 tg3_link_report(tp);
2050 }
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2053 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2054 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2055 err = tg3_phy_reset_5703_4_5(tp);
2056 if (err)
2057 return err;
2058 goto out;
2059 }
2060
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002061 cpmuctrl = 0;
2062 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2063 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2064 cpmuctrl = tr32(TG3_CPMU_CTRL);
2065 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2066 tw32(TG3_CPMU_CTRL,
2067 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2068 }
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 err = tg3_bmcr_reset(tp);
2071 if (err)
2072 return err;
2073
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002074 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002075 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2076 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002077
2078 tw32(TG3_CPMU_CTRL, cpmuctrl);
2079 }
2080
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002081 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2082 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002083 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2084 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2085 CPMU_LSPD_1000MB_MACCLK_12_5) {
2086 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2087 udelay(40);
2088 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2089 }
2090 }
2091
Joe Perches63c3a662011-04-26 08:12:10 +00002092 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002093 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002094 return 0;
2095
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002096 tg3_phy_apply_otp(tp);
2097
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002098 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002099 tg3_phy_toggle_apd(tp, true);
2100 else
2101 tg3_phy_toggle_apd(tp, false);
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002104 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2105 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002106 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2107 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002108 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002110
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002111 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002112 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2113 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002115
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002116 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002117 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2118 tg3_phydsp_write(tp, 0x000a, 0x310b);
2119 tg3_phydsp_write(tp, 0x201f, 0x9506);
2120 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2121 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2122 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002123 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002124 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2125 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2126 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2127 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2128 tg3_writephy(tp, MII_TG3_TEST1,
2129 MII_TG3_TEST1_TRIM_EN | 0x4);
2130 } else
2131 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2132
2133 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2134 }
Michael Chanc424cb22006-04-29 18:56:34 -07002135 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 /* Set Extended packet length bit (bit 14) on all chips that */
2138 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002139 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002141 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002142 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002144 err = tg3_phy_auxctl_read(tp,
2145 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2146 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002147 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2148 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
2150
2151 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2152 * jumbo frames transmission.
2153 */
Joe Perches63c3a662011-04-26 08:12:10 +00002154 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002155 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002156 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002157 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
2159
Michael Chan715116a2006-09-27 16:09:25 -07002160 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002161 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002162 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002163 }
2164
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002165 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 tg3_phy_set_wirespeed(tp);
2167 return 0;
2168}
2169
Matt Carlson520b2752011-06-13 13:39:02 +00002170static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2171{
2172 if (!tg3_flag(tp, IS_NIC))
2173 return 0;
2174
2175 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2176 TG3_GRC_LCLCTL_PWRSW_DELAY);
2177
2178 return 0;
2179}
2180
2181static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2182{
2183 u32 grc_local_ctrl;
2184
2185 if (!tg3_flag(tp, IS_NIC) ||
2186 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2187 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2188 return;
2189
2190 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2191
2192 tw32_wait_f(GRC_LOCAL_CTRL,
2193 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2194 TG3_GRC_LCLCTL_PWRSW_DELAY);
2195
2196 tw32_wait_f(GRC_LOCAL_CTRL,
2197 grc_local_ctrl,
2198 TG3_GRC_LCLCTL_PWRSW_DELAY);
2199
2200 tw32_wait_f(GRC_LOCAL_CTRL,
2201 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2202 TG3_GRC_LCLCTL_PWRSW_DELAY);
2203}
2204
2205static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2206{
2207 if (!tg3_flag(tp, IS_NIC))
2208 return;
2209
2210 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2211 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2212 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2213 (GRC_LCLCTRL_GPIO_OE0 |
2214 GRC_LCLCTRL_GPIO_OE1 |
2215 GRC_LCLCTRL_GPIO_OE2 |
2216 GRC_LCLCTRL_GPIO_OUTPUT0 |
2217 GRC_LCLCTRL_GPIO_OUTPUT1),
2218 TG3_GRC_LCLCTL_PWRSW_DELAY);
2219 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2220 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2221 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2222 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2223 GRC_LCLCTRL_GPIO_OE1 |
2224 GRC_LCLCTRL_GPIO_OE2 |
2225 GRC_LCLCTRL_GPIO_OUTPUT0 |
2226 GRC_LCLCTRL_GPIO_OUTPUT1 |
2227 tp->grc_local_ctrl;
2228 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2229 TG3_GRC_LCLCTL_PWRSW_DELAY);
2230
2231 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2232 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2233 TG3_GRC_LCLCTL_PWRSW_DELAY);
2234
2235 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2236 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2237 TG3_GRC_LCLCTL_PWRSW_DELAY);
2238 } else {
2239 u32 no_gpio2;
2240 u32 grc_local_ctrl = 0;
2241
2242 /* Workaround to prevent overdrawing Amps. */
2243 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2244 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2245 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2246 grc_local_ctrl,
2247 TG3_GRC_LCLCTL_PWRSW_DELAY);
2248 }
2249
2250 /* On 5753 and variants, GPIO2 cannot be used. */
2251 no_gpio2 = tp->nic_sram_data_cfg &
2252 NIC_SRAM_DATA_CFG_NO_GPIO2;
2253
2254 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2255 GRC_LCLCTRL_GPIO_OE1 |
2256 GRC_LCLCTRL_GPIO_OE2 |
2257 GRC_LCLCTRL_GPIO_OUTPUT1 |
2258 GRC_LCLCTRL_GPIO_OUTPUT2;
2259 if (no_gpio2) {
2260 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2261 GRC_LCLCTRL_GPIO_OUTPUT2);
2262 }
2263 tw32_wait_f(GRC_LOCAL_CTRL,
2264 tp->grc_local_ctrl | grc_local_ctrl,
2265 TG3_GRC_LCLCTL_PWRSW_DELAY);
2266
2267 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2268
2269 tw32_wait_f(GRC_LOCAL_CTRL,
2270 tp->grc_local_ctrl | grc_local_ctrl,
2271 TG3_GRC_LCLCTL_PWRSW_DELAY);
2272
2273 if (!no_gpio2) {
2274 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2275 tw32_wait_f(GRC_LOCAL_CTRL,
2276 tp->grc_local_ctrl | grc_local_ctrl,
2277 TG3_GRC_LCLCTL_PWRSW_DELAY);
2278 }
2279 }
2280}
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282static void tg3_frob_aux_power(struct tg3 *tp)
2283{
Matt Carlson683644b2011-03-09 16:58:23 +00002284 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Matt Carlson334355a2010-01-20 16:58:10 +00002286 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002287 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002288 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002289 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return;
2291
Matt Carlson683644b2011-03-09 16:58:23 +00002292 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2293 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +00002294 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2295 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
Matt Carlson683644b2011-03-09 16:58:23 +00002296 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002297 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002299 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002300
Michael Chanbc1c7562006-03-20 17:48:03 -08002301 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002302 if (dev_peer) {
2303 struct tg3 *tp_peer = netdev_priv(dev_peer);
2304
Joe Perches63c3a662011-04-26 08:12:10 +00002305 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002306 return;
2307
Joe Perches63c3a662011-04-26 08:12:10 +00002308 if (tg3_flag(tp_peer, WOL_ENABLE) ||
2309 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002310 need_vaux = true;
2311 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Joe Perches63c3a662011-04-26 08:12:10 +00002314 if (tg3_flag(tp, WOL_ENABLE) || tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002315 need_vaux = true;
2316
Matt Carlson520b2752011-06-13 13:39:02 +00002317 if (need_vaux)
2318 tg3_pwrsrc_switch_to_vaux(tp);
2319 else
2320 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002323static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2324{
2325 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2326 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002327 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002328 if (speed != SPEED_10)
2329 return 1;
2330 } else if (speed == SPEED_10)
2331 return 1;
2332
2333 return 0;
2334}
2335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336static int tg3_setup_phy(struct tg3 *, int);
2337
2338#define RESET_KIND_SHUTDOWN 0
2339#define RESET_KIND_INIT 1
2340#define RESET_KIND_SUSPEND 2
2341
2342static void tg3_write_sig_post_reset(struct tg3 *, int);
2343static int tg3_halt_cpu(struct tg3 *, u32);
2344
Matt Carlson0a459aa2008-11-03 16:54:15 -08002345static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002346{
Matt Carlsonce057f02007-11-12 21:08:03 -08002347 u32 val;
2348
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002349 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002350 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2351 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2352 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2353
2354 sg_dig_ctrl |=
2355 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2356 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2357 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2358 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002359 return;
Michael Chan51297242007-02-13 12:17:57 -08002360 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002361
Michael Chan60189dd2006-12-17 17:08:07 -08002362 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002363 tg3_bmcr_reset(tp);
2364 val = tr32(GRC_MISC_CFG);
2365 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2366 udelay(40);
2367 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002368 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002369 u32 phytest;
2370 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2371 u32 phy;
2372
2373 tg3_writephy(tp, MII_ADVERTISE, 0);
2374 tg3_writephy(tp, MII_BMCR,
2375 BMCR_ANENABLE | BMCR_ANRESTART);
2376
2377 tg3_writephy(tp, MII_TG3_FET_TEST,
2378 phytest | MII_TG3_FET_SHADOW_EN);
2379 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2380 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2381 tg3_writephy(tp,
2382 MII_TG3_FET_SHDW_AUXMODE4,
2383 phy);
2384 }
2385 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2386 }
2387 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002388 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002389 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2390 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002391
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002392 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2393 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2394 MII_TG3_AUXCTL_PCTL_VREG_11V;
2395 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002396 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002397
Michael Chan15c3b692006-03-22 01:06:52 -08002398 /* The PHY should not be powered down on some chips because
2399 * of bugs.
2400 */
2401 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2402 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2403 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002404 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002405 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002406
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002407 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2408 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002409 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2410 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2411 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2412 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2413 }
2414
Michael Chan15c3b692006-03-22 01:06:52 -08002415 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2416}
2417
Matt Carlson3f007892008-11-03 16:51:36 -08002418/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002419static int tg3_nvram_lock(struct tg3 *tp)
2420{
Joe Perches63c3a662011-04-26 08:12:10 +00002421 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002422 int i;
2423
2424 if (tp->nvram_lock_cnt == 0) {
2425 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2426 for (i = 0; i < 8000; i++) {
2427 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2428 break;
2429 udelay(20);
2430 }
2431 if (i == 8000) {
2432 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2433 return -ENODEV;
2434 }
2435 }
2436 tp->nvram_lock_cnt++;
2437 }
2438 return 0;
2439}
2440
2441/* tp->lock is held. */
2442static void tg3_nvram_unlock(struct tg3 *tp)
2443{
Joe Perches63c3a662011-04-26 08:12:10 +00002444 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002445 if (tp->nvram_lock_cnt > 0)
2446 tp->nvram_lock_cnt--;
2447 if (tp->nvram_lock_cnt == 0)
2448 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2449 }
2450}
2451
2452/* tp->lock is held. */
2453static void tg3_enable_nvram_access(struct tg3 *tp)
2454{
Joe Perches63c3a662011-04-26 08:12:10 +00002455 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002456 u32 nvaccess = tr32(NVRAM_ACCESS);
2457
2458 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2459 }
2460}
2461
2462/* tp->lock is held. */
2463static void tg3_disable_nvram_access(struct tg3 *tp)
2464{
Joe Perches63c3a662011-04-26 08:12:10 +00002465 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002466 u32 nvaccess = tr32(NVRAM_ACCESS);
2467
2468 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2469 }
2470}
2471
2472static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2473 u32 offset, u32 *val)
2474{
2475 u32 tmp;
2476 int i;
2477
2478 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2479 return -EINVAL;
2480
2481 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2482 EEPROM_ADDR_DEVID_MASK |
2483 EEPROM_ADDR_READ);
2484 tw32(GRC_EEPROM_ADDR,
2485 tmp |
2486 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2487 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2488 EEPROM_ADDR_ADDR_MASK) |
2489 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2490
2491 for (i = 0; i < 1000; i++) {
2492 tmp = tr32(GRC_EEPROM_ADDR);
2493
2494 if (tmp & EEPROM_ADDR_COMPLETE)
2495 break;
2496 msleep(1);
2497 }
2498 if (!(tmp & EEPROM_ADDR_COMPLETE))
2499 return -EBUSY;
2500
Matt Carlson62cedd12009-04-20 14:52:29 -07002501 tmp = tr32(GRC_EEPROM_DATA);
2502
2503 /*
2504 * The data will always be opposite the native endian
2505 * format. Perform a blind byteswap to compensate.
2506 */
2507 *val = swab32(tmp);
2508
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002509 return 0;
2510}
2511
2512#define NVRAM_CMD_TIMEOUT 10000
2513
2514static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2515{
2516 int i;
2517
2518 tw32(NVRAM_CMD, nvram_cmd);
2519 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2520 udelay(10);
2521 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2522 udelay(10);
2523 break;
2524 }
2525 }
2526
2527 if (i == NVRAM_CMD_TIMEOUT)
2528 return -EBUSY;
2529
2530 return 0;
2531}
2532
2533static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2534{
Joe Perches63c3a662011-04-26 08:12:10 +00002535 if (tg3_flag(tp, NVRAM) &&
2536 tg3_flag(tp, NVRAM_BUFFERED) &&
2537 tg3_flag(tp, FLASH) &&
2538 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002539 (tp->nvram_jedecnum == JEDEC_ATMEL))
2540
2541 addr = ((addr / tp->nvram_pagesize) <<
2542 ATMEL_AT45DB0X1B_PAGE_POS) +
2543 (addr % tp->nvram_pagesize);
2544
2545 return addr;
2546}
2547
2548static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2549{
Joe Perches63c3a662011-04-26 08:12:10 +00002550 if (tg3_flag(tp, NVRAM) &&
2551 tg3_flag(tp, NVRAM_BUFFERED) &&
2552 tg3_flag(tp, FLASH) &&
2553 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002554 (tp->nvram_jedecnum == JEDEC_ATMEL))
2555
2556 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2557 tp->nvram_pagesize) +
2558 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2559
2560 return addr;
2561}
2562
Matt Carlsone4f34112009-02-25 14:25:00 +00002563/* NOTE: Data read in from NVRAM is byteswapped according to
2564 * the byteswapping settings for all other register accesses.
2565 * tg3 devices are BE devices, so on a BE machine, the data
2566 * returned will be exactly as it is seen in NVRAM. On a LE
2567 * machine, the 32-bit value will be byteswapped.
2568 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002569static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2570{
2571 int ret;
2572
Joe Perches63c3a662011-04-26 08:12:10 +00002573 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002574 return tg3_nvram_read_using_eeprom(tp, offset, val);
2575
2576 offset = tg3_nvram_phys_addr(tp, offset);
2577
2578 if (offset > NVRAM_ADDR_MSK)
2579 return -EINVAL;
2580
2581 ret = tg3_nvram_lock(tp);
2582 if (ret)
2583 return ret;
2584
2585 tg3_enable_nvram_access(tp);
2586
2587 tw32(NVRAM_ADDR, offset);
2588 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2589 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2590
2591 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002592 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002593
2594 tg3_disable_nvram_access(tp);
2595
2596 tg3_nvram_unlock(tp);
2597
2598 return ret;
2599}
2600
Matt Carlsona9dc5292009-02-25 14:25:30 +00002601/* Ensures NVRAM data is in bytestream format. */
2602static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002603{
2604 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002605 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002606 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002607 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002608 return res;
2609}
2610
2611/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002612static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2613{
2614 u32 addr_high, addr_low;
2615 int i;
2616
2617 addr_high = ((tp->dev->dev_addr[0] << 8) |
2618 tp->dev->dev_addr[1]);
2619 addr_low = ((tp->dev->dev_addr[2] << 24) |
2620 (tp->dev->dev_addr[3] << 16) |
2621 (tp->dev->dev_addr[4] << 8) |
2622 (tp->dev->dev_addr[5] << 0));
2623 for (i = 0; i < 4; i++) {
2624 if (i == 1 && skip_mac_1)
2625 continue;
2626 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2627 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2628 }
2629
2630 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2631 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2632 for (i = 0; i < 12; i++) {
2633 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2634 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2635 }
2636 }
2637
2638 addr_high = (tp->dev->dev_addr[0] +
2639 tp->dev->dev_addr[1] +
2640 tp->dev->dev_addr[2] +
2641 tp->dev->dev_addr[3] +
2642 tp->dev->dev_addr[4] +
2643 tp->dev->dev_addr[5]) &
2644 TX_BACKOFF_SEED_MASK;
2645 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2646}
2647
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002648static void tg3_enable_register_access(struct tg3 *tp)
2649{
2650 /*
2651 * Make sure register accesses (indirect or otherwise) will function
2652 * correctly.
2653 */
2654 pci_write_config_dword(tp->pdev,
2655 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2656}
2657
2658static int tg3_power_up(struct tg3 *tp)
2659{
Matt Carlsonbed98292011-07-13 09:27:29 +00002660 int err;
2661
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002662 tg3_enable_register_access(tp);
2663
Matt Carlsonbed98292011-07-13 09:27:29 +00002664 err = pci_set_power_state(tp->pdev, PCI_D0);
2665 if (!err) {
2666 /* Switch out of Vaux if it is a NIC */
2667 tg3_pwrsrc_switch_to_vmain(tp);
2668 } else {
2669 netdev_err(tp->dev, "Transition to D0 failed\n");
2670 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002671
Matt Carlsonbed98292011-07-13 09:27:29 +00002672 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002673}
2674
2675static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
2677 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002678 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002680 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002681
2682 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00002683 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002684 u16 lnkctl;
2685
2686 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00002687 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002688 &lnkctl);
2689 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2690 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00002691 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002692 lnkctl);
2693 }
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2696 tw32(TG3PCI_MISC_HOST_CTRL,
2697 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2698
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002699 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00002700 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002701
Joe Perches63c3a662011-04-26 08:12:10 +00002702 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002703 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002704 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002705 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002706 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002707 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002708
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002709 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002710
Matt Carlson80096062010-08-02 11:26:06 +00002711 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002712
2713 tp->link_config.orig_speed = phydev->speed;
2714 tp->link_config.orig_duplex = phydev->duplex;
2715 tp->link_config.orig_autoneg = phydev->autoneg;
2716 tp->link_config.orig_advertising = phydev->advertising;
2717
2718 advertising = ADVERTISED_TP |
2719 ADVERTISED_Pause |
2720 ADVERTISED_Autoneg |
2721 ADVERTISED_10baseT_Half;
2722
Joe Perches63c3a662011-04-26 08:12:10 +00002723 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
2724 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002725 advertising |=
2726 ADVERTISED_100baseT_Half |
2727 ADVERTISED_100baseT_Full |
2728 ADVERTISED_10baseT_Full;
2729 else
2730 advertising |= ADVERTISED_10baseT_Full;
2731 }
2732
2733 phydev->advertising = advertising;
2734
2735 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002736
2737 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002738 if (phyid != PHY_ID_BCMAC131) {
2739 phyid &= PHY_BCM_OUI_MASK;
2740 if (phyid == PHY_BCM_OUI_1 ||
2741 phyid == PHY_BCM_OUI_2 ||
2742 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002743 do_low_power = true;
2744 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002745 }
Matt Carlsondd477002008-05-25 23:45:58 -07002746 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002747 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002748
Matt Carlson80096062010-08-02 11:26:06 +00002749 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2750 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002751 tp->link_config.orig_speed = tp->link_config.speed;
2752 tp->link_config.orig_duplex = tp->link_config.duplex;
2753 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002756 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002757 tp->link_config.speed = SPEED_10;
2758 tp->link_config.duplex = DUPLEX_HALF;
2759 tp->link_config.autoneg = AUTONEG_ENABLE;
2760 tg3_setup_phy(tp, 0);
2761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
2763
Michael Chanb5d37722006-09-27 16:06:21 -07002764 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2765 u32 val;
2766
2767 val = tr32(GRC_VCPU_EXT_CTRL);
2768 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00002769 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002770 int i;
2771 u32 val;
2772
2773 for (i = 0; i < 200; i++) {
2774 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2775 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2776 break;
2777 msleep(1);
2778 }
2779 }
Joe Perches63c3a662011-04-26 08:12:10 +00002780 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07002781 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2782 WOL_DRV_STATE_SHUTDOWN |
2783 WOL_DRV_WOL |
2784 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002785
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002786 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 u32 mac_mode;
2788
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002789 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002790 if (do_low_power &&
2791 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
2792 tg3_phy_auxctl_write(tp,
2793 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
2794 MII_TG3_AUXCTL_PCTL_WOL_EN |
2795 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2796 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07002797 udelay(40);
2798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002800 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002801 mac_mode = MAC_MODE_PORT_MODE_GMII;
2802 else
2803 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002805 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2806 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2807 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00002808 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002809 SPEED_100 : SPEED_10;
2810 if (tg3_5700_link_polarity(tp, speed))
2811 mac_mode |= MAC_MODE_LINK_POLARITY;
2812 else
2813 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 } else {
2816 mac_mode = MAC_MODE_PORT_MODE_TBI;
2817 }
2818
Joe Perches63c3a662011-04-26 08:12:10 +00002819 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 tw32(MAC_LED_CTRL, tp->led_ctrl);
2821
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002822 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00002823 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
2824 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002825 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Joe Perches63c3a662011-04-26 08:12:10 +00002827 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002828 mac_mode |= MAC_MODE_APE_TX_EN |
2829 MAC_MODE_APE_RX_EN |
2830 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 tw32_f(MAC_MODE, mac_mode);
2833 udelay(100);
2834
2835 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2836 udelay(10);
2837 }
2838
Joe Perches63c3a662011-04-26 08:12:10 +00002839 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2841 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2842 u32 base_val;
2843
2844 base_val = tp->pci_clock_ctrl;
2845 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2846 CLOCK_CTRL_TXCLK_DISABLE);
2847
Michael Chanb401e9e2005-12-19 16:27:04 -08002848 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2849 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00002850 } else if (tg3_flag(tp, 5780_CLASS) ||
2851 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00002852 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002853 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00002854 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 u32 newbits1, newbits2;
2856
2857 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2858 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2859 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2860 CLOCK_CTRL_TXCLK_DISABLE |
2861 CLOCK_CTRL_ALTCLK);
2862 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00002863 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 newbits1 = CLOCK_CTRL_625_CORE;
2865 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2866 } else {
2867 newbits1 = CLOCK_CTRL_ALTCLK;
2868 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2869 }
2870
Michael Chanb401e9e2005-12-19 16:27:04 -08002871 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2872 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Michael Chanb401e9e2005-12-19 16:27:04 -08002874 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2875 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Joe Perches63c3a662011-04-26 08:12:10 +00002877 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 u32 newbits3;
2879
2880 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2881 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2882 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2883 CLOCK_CTRL_TXCLK_DISABLE |
2884 CLOCK_CTRL_44MHZ_CORE);
2885 } else {
2886 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2887 }
2888
Michael Chanb401e9e2005-12-19 16:27:04 -08002889 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2890 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 }
2892 }
2893
Joe Perches63c3a662011-04-26 08:12:10 +00002894 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002895 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002896
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 tg3_frob_aux_power(tp);
2898
2899 /* Workaround for unstable PLL clock */
2900 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2901 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2902 u32 val = tr32(0x7d00);
2903
2904 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2905 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00002906 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002907 int err;
2908
2909 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002911 if (!err)
2912 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 }
2915
Michael Chanbbadf502006-04-06 21:46:34 -07002916 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return 0;
2919}
2920
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002921static void tg3_power_down(struct tg3 *tp)
2922{
2923 tg3_power_down_prepare(tp);
2924
Joe Perches63c3a662011-04-26 08:12:10 +00002925 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002926 pci_set_power_state(tp->pdev, PCI_D3hot);
2927}
2928
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2930{
2931 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2932 case MII_TG3_AUX_STAT_10HALF:
2933 *speed = SPEED_10;
2934 *duplex = DUPLEX_HALF;
2935 break;
2936
2937 case MII_TG3_AUX_STAT_10FULL:
2938 *speed = SPEED_10;
2939 *duplex = DUPLEX_FULL;
2940 break;
2941
2942 case MII_TG3_AUX_STAT_100HALF:
2943 *speed = SPEED_100;
2944 *duplex = DUPLEX_HALF;
2945 break;
2946
2947 case MII_TG3_AUX_STAT_100FULL:
2948 *speed = SPEED_100;
2949 *duplex = DUPLEX_FULL;
2950 break;
2951
2952 case MII_TG3_AUX_STAT_1000HALF:
2953 *speed = SPEED_1000;
2954 *duplex = DUPLEX_HALF;
2955 break;
2956
2957 case MII_TG3_AUX_STAT_1000FULL:
2958 *speed = SPEED_1000;
2959 *duplex = DUPLEX_FULL;
2960 break;
2961
2962 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002963 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002964 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2965 SPEED_10;
2966 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2967 DUPLEX_HALF;
2968 break;
2969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 *speed = SPEED_INVALID;
2971 *duplex = DUPLEX_INVALID;
2972 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974}
2975
Matt Carlson42b64a42011-05-19 12:12:49 +00002976static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
Matt Carlson42b64a42011-05-19 12:12:49 +00002978 int err = 0;
2979 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Matt Carlson42b64a42011-05-19 12:12:49 +00002981 new_adv = ADVERTISE_CSMA;
2982 if (advertise & ADVERTISED_10baseT_Half)
2983 new_adv |= ADVERTISE_10HALF;
2984 if (advertise & ADVERTISED_10baseT_Full)
2985 new_adv |= ADVERTISE_10FULL;
2986 if (advertise & ADVERTISED_100baseT_Half)
2987 new_adv |= ADVERTISE_100HALF;
2988 if (advertise & ADVERTISED_100baseT_Full)
2989 new_adv |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Matt Carlson42b64a42011-05-19 12:12:49 +00002991 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Matt Carlson42b64a42011-05-19 12:12:49 +00002993 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
2994 if (err)
2995 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Matt Carlson42b64a42011-05-19 12:12:49 +00002997 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
2998 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002999
Matt Carlson42b64a42011-05-19 12:12:49 +00003000 new_adv = 0;
3001 if (advertise & ADVERTISED_1000baseT_Half)
Matt Carlson221c5632011-06-13 13:39:01 +00003002 new_adv |= ADVERTISE_1000HALF;
Matt Carlson42b64a42011-05-19 12:12:49 +00003003 if (advertise & ADVERTISED_1000baseT_Full)
Matt Carlson221c5632011-06-13 13:39:01 +00003004 new_adv |= ADVERTISE_1000FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003005
Matt Carlson42b64a42011-05-19 12:12:49 +00003006 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3007 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
Matt Carlson221c5632011-06-13 13:39:01 +00003008 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Matt Carlson221c5632011-06-13 13:39:01 +00003010 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
Matt Carlson42b64a42011-05-19 12:12:49 +00003011 if (err)
3012 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003013
Matt Carlson42b64a42011-05-19 12:12:49 +00003014 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3015 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Matt Carlson42b64a42011-05-19 12:12:49 +00003017 tw32(TG3_CPMU_EEE_MODE,
3018 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003019
Matt Carlson42b64a42011-05-19 12:12:49 +00003020 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3021 if (!err) {
3022 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003023
Matt Carlson21a00ab2011-01-25 15:58:55 +00003024 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3025 case ASIC_REV_5717:
3026 case ASIC_REV_57765:
3027 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3028 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3029 MII_TG3_DSP_CH34TP2_HIBW01);
3030 /* Fall through */
3031 case ASIC_REV_5719:
3032 val = MII_TG3_DSP_TAP26_ALNOKO |
3033 MII_TG3_DSP_TAP26_RMRXSTO |
3034 MII_TG3_DSP_TAP26_OPCSINPT;
3035 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3036 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003037
Matt Carlsona6b68da2010-12-06 08:28:52 +00003038 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003039 /* Advertise 100-BaseTX EEE ability */
3040 if (advertise & ADVERTISED_100baseT_Full)
3041 val |= MDIO_AN_EEE_ADV_100TX;
3042 /* Advertise 1000-BaseT EEE ability */
3043 if (advertise & ADVERTISED_1000baseT_Full)
3044 val |= MDIO_AN_EEE_ADV_1000T;
3045 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003046
Matt Carlson42b64a42011-05-19 12:12:49 +00003047 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3048 if (!err)
3049 err = err2;
3050 }
3051
3052done:
3053 return err;
3054}
3055
3056static void tg3_phy_copper_begin(struct tg3 *tp)
3057{
3058 u32 new_adv;
3059 int i;
3060
3061 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3062 new_adv = ADVERTISED_10baseT_Half |
3063 ADVERTISED_10baseT_Full;
3064 if (tg3_flag(tp, WOL_SPEED_100MB))
3065 new_adv |= ADVERTISED_100baseT_Half |
3066 ADVERTISED_100baseT_Full;
3067
3068 tg3_phy_autoneg_cfg(tp, new_adv,
3069 FLOW_CTRL_TX | FLOW_CTRL_RX);
3070 } else if (tp->link_config.speed == SPEED_INVALID) {
3071 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3072 tp->link_config.advertising &=
3073 ~(ADVERTISED_1000baseT_Half |
3074 ADVERTISED_1000baseT_Full);
3075
3076 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3077 tp->link_config.flowctrl);
3078 } else {
3079 /* Asking for a specific link mode. */
3080 if (tp->link_config.speed == SPEED_1000) {
3081 if (tp->link_config.duplex == DUPLEX_FULL)
3082 new_adv = ADVERTISED_1000baseT_Full;
3083 else
3084 new_adv = ADVERTISED_1000baseT_Half;
3085 } else if (tp->link_config.speed == SPEED_100) {
3086 if (tp->link_config.duplex == DUPLEX_FULL)
3087 new_adv = ADVERTISED_100baseT_Full;
3088 else
3089 new_adv = ADVERTISED_100baseT_Half;
3090 } else {
3091 if (tp->link_config.duplex == DUPLEX_FULL)
3092 new_adv = ADVERTISED_10baseT_Full;
3093 else
3094 new_adv = ADVERTISED_10baseT_Half;
3095 }
3096
3097 tg3_phy_autoneg_cfg(tp, new_adv,
3098 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003099 }
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3102 tp->link_config.speed != SPEED_INVALID) {
3103 u32 bmcr, orig_bmcr;
3104
3105 tp->link_config.active_speed = tp->link_config.speed;
3106 tp->link_config.active_duplex = tp->link_config.duplex;
3107
3108 bmcr = 0;
3109 switch (tp->link_config.speed) {
3110 default:
3111 case SPEED_10:
3112 break;
3113
3114 case SPEED_100:
3115 bmcr |= BMCR_SPEED100;
3116 break;
3117
3118 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003119 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
3123 if (tp->link_config.duplex == DUPLEX_FULL)
3124 bmcr |= BMCR_FULLDPLX;
3125
3126 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3127 (bmcr != orig_bmcr)) {
3128 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3129 for (i = 0; i < 1500; i++) {
3130 u32 tmp;
3131
3132 udelay(10);
3133 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3134 tg3_readphy(tp, MII_BMSR, &tmp))
3135 continue;
3136 if (!(tmp & BMSR_LSTATUS)) {
3137 udelay(40);
3138 break;
3139 }
3140 }
3141 tg3_writephy(tp, MII_BMCR, bmcr);
3142 udelay(40);
3143 }
3144 } else {
3145 tg3_writephy(tp, MII_BMCR,
3146 BMCR_ANENABLE | BMCR_ANRESTART);
3147 }
3148}
3149
3150static int tg3_init_5401phy_dsp(struct tg3 *tp)
3151{
3152 int err;
3153
3154 /* Turn off tap power management. */
3155 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003156 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003158 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3159 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3160 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3161 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3162 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
3164 udelay(40);
3165
3166 return err;
3167}
3168
Michael Chan3600d912006-12-07 00:21:48 -08003169static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
Michael Chan3600d912006-12-07 00:21:48 -08003171 u32 adv_reg, all_mask = 0;
3172
3173 if (mask & ADVERTISED_10baseT_Half)
3174 all_mask |= ADVERTISE_10HALF;
3175 if (mask & ADVERTISED_10baseT_Full)
3176 all_mask |= ADVERTISE_10FULL;
3177 if (mask & ADVERTISED_100baseT_Half)
3178 all_mask |= ADVERTISE_100HALF;
3179 if (mask & ADVERTISED_100baseT_Full)
3180 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
3182 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3183 return 0;
3184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 if ((adv_reg & all_mask) != all_mask)
3186 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003187 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 u32 tg3_ctrl;
3189
Michael Chan3600d912006-12-07 00:21:48 -08003190 all_mask = 0;
3191 if (mask & ADVERTISED_1000baseT_Half)
3192 all_mask |= ADVERTISE_1000HALF;
3193 if (mask & ADVERTISED_1000baseT_Full)
3194 all_mask |= ADVERTISE_1000FULL;
3195
Matt Carlson221c5632011-06-13 13:39:01 +00003196 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return 0;
3198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 if ((tg3_ctrl & all_mask) != all_mask)
3200 return 0;
3201 }
3202 return 1;
3203}
3204
Matt Carlsonef167e22007-12-20 20:10:01 -08003205static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3206{
3207 u32 curadv, reqadv;
3208
3209 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3210 return 1;
3211
3212 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3213 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3214
3215 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3216 if (curadv != reqadv)
3217 return 0;
3218
Joe Perches63c3a662011-04-26 08:12:10 +00003219 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003220 tg3_readphy(tp, MII_LPA, rmtadv);
3221 } else {
3222 /* Reprogram the advertisement register, even if it
3223 * does not affect the current link. If the link
3224 * gets renegotiated in the future, we can save an
3225 * additional renegotiation cycle by advertising
3226 * it correctly in the first place.
3227 */
3228 if (curadv != reqadv) {
3229 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3230 ADVERTISE_PAUSE_ASYM);
3231 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3232 }
3233 }
3234
3235 return 1;
3236}
3237
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3239{
3240 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003241 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003242 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 u16 current_speed;
3244 u8 current_duplex;
3245 int i, err;
3246
3247 tw32(MAC_EVENT, 0);
3248
3249 tw32_f(MAC_STATUS,
3250 (MAC_STATUS_SYNC_CHANGED |
3251 MAC_STATUS_CFG_CHANGED |
3252 MAC_STATUS_MI_COMPLETION |
3253 MAC_STATUS_LNKSTATE_CHANGED));
3254 udelay(40);
3255
Matt Carlson8ef21422008-05-02 16:47:53 -07003256 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3257 tw32_f(MAC_MI_MODE,
3258 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3259 udelay(80);
3260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003262 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
3264 /* Some third-party PHYs need to be reset on link going
3265 * down.
3266 */
3267 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3268 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3269 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3270 netif_carrier_ok(tp->dev)) {
3271 tg3_readphy(tp, MII_BMSR, &bmsr);
3272 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3273 !(bmsr & BMSR_LSTATUS))
3274 force_reset = 1;
3275 }
3276 if (force_reset)
3277 tg3_phy_reset(tp);
3278
Matt Carlson79eb6902010-02-17 15:17:03 +00003279 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 tg3_readphy(tp, MII_BMSR, &bmsr);
3281 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003282 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 bmsr = 0;
3284
3285 if (!(bmsr & BMSR_LSTATUS)) {
3286 err = tg3_init_5401phy_dsp(tp);
3287 if (err)
3288 return err;
3289
3290 tg3_readphy(tp, MII_BMSR, &bmsr);
3291 for (i = 0; i < 1000; i++) {
3292 udelay(10);
3293 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3294 (bmsr & BMSR_LSTATUS)) {
3295 udelay(40);
3296 break;
3297 }
3298 }
3299
Matt Carlson79eb6902010-02-17 15:17:03 +00003300 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3301 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 !(bmsr & BMSR_LSTATUS) &&
3303 tp->link_config.active_speed == SPEED_1000) {
3304 err = tg3_phy_reset(tp);
3305 if (!err)
3306 err = tg3_init_5401phy_dsp(tp);
3307 if (err)
3308 return err;
3309 }
3310 }
3311 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3312 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3313 /* 5701 {A0,B0} CRC bug workaround */
3314 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003315 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3316 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3317 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 }
3319
3320 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003321 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3322 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003324 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003326 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3328
3329 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3330 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3331 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3332 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3333 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3334 else
3335 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3336 }
3337
3338 current_link_up = 0;
3339 current_speed = SPEED_INVALID;
3340 current_duplex = DUPLEX_INVALID;
3341
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003342 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003343 err = tg3_phy_auxctl_read(tp,
3344 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3345 &val);
3346 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003347 tg3_phy_auxctl_write(tp,
3348 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3349 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 goto relink;
3351 }
3352 }
3353
3354 bmsr = 0;
3355 for (i = 0; i < 100; i++) {
3356 tg3_readphy(tp, MII_BMSR, &bmsr);
3357 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3358 (bmsr & BMSR_LSTATUS))
3359 break;
3360 udelay(40);
3361 }
3362
3363 if (bmsr & BMSR_LSTATUS) {
3364 u32 aux_stat, bmcr;
3365
3366 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3367 for (i = 0; i < 2000; i++) {
3368 udelay(10);
3369 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3370 aux_stat)
3371 break;
3372 }
3373
3374 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3375 &current_speed,
3376 &current_duplex);
3377
3378 bmcr = 0;
3379 for (i = 0; i < 200; i++) {
3380 tg3_readphy(tp, MII_BMCR, &bmcr);
3381 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3382 continue;
3383 if (bmcr && bmcr != 0x7fff)
3384 break;
3385 udelay(10);
3386 }
3387
Matt Carlsonef167e22007-12-20 20:10:01 -08003388 lcl_adv = 0;
3389 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
Matt Carlsonef167e22007-12-20 20:10:01 -08003391 tp->link_config.active_speed = current_speed;
3392 tp->link_config.active_duplex = current_duplex;
3393
3394 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3395 if ((bmcr & BMCR_ANENABLE) &&
3396 tg3_copper_is_advertising_all(tp,
3397 tp->link_config.advertising)) {
3398 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3399 &rmt_adv))
3400 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 }
3402 } else {
3403 if (!(bmcr & BMCR_ANENABLE) &&
3404 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003405 tp->link_config.duplex == current_duplex &&
3406 tp->link_config.flowctrl ==
3407 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 }
3410 }
3411
Matt Carlsonef167e22007-12-20 20:10:01 -08003412 if (current_link_up == 1 &&
3413 tp->link_config.active_duplex == DUPLEX_FULL)
3414 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 }
3416
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417relink:
Matt Carlson80096062010-08-02 11:26:06 +00003418 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 tg3_phy_copper_begin(tp);
3420
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003421 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00003422 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
3423 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 current_link_up = 1;
3425 }
3426
3427 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3428 if (current_link_up == 1) {
3429 if (tp->link_config.active_speed == SPEED_100 ||
3430 tp->link_config.active_speed == SPEED_10)
3431 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3432 else
3433 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003434 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003435 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3436 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3438
3439 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3440 if (tp->link_config.active_duplex == DUPLEX_HALF)
3441 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3442
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003444 if (current_link_up == 1 &&
3445 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003447 else
3448 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 }
3450
3451 /* ??? Without this setting Netgear GA302T PHY does not
3452 * ??? send/receive packets...
3453 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003454 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3456 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3457 tw32_f(MAC_MI_MODE, tp->mi_mode);
3458 udelay(80);
3459 }
3460
3461 tw32_f(MAC_MODE, tp->mac_mode);
3462 udelay(40);
3463
Matt Carlson52b02d02010-10-14 10:37:41 +00003464 tg3_phy_eee_adjust(tp, current_link_up);
3465
Joe Perches63c3a662011-04-26 08:12:10 +00003466 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 /* Polled via timer. */
3468 tw32_f(MAC_EVENT, 0);
3469 } else {
3470 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3471 }
3472 udelay(40);
3473
3474 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3475 current_link_up == 1 &&
3476 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00003477 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 udelay(120);
3479 tw32_f(MAC_STATUS,
3480 (MAC_STATUS_SYNC_CHANGED |
3481 MAC_STATUS_CFG_CHANGED));
3482 udelay(40);
3483 tg3_write_mem(tp,
3484 NIC_SRAM_FIRMWARE_MBOX,
3485 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3486 }
3487
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003488 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00003489 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003490 u16 oldlnkctl, newlnkctl;
3491
3492 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003493 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003494 &oldlnkctl);
3495 if (tp->link_config.active_speed == SPEED_100 ||
3496 tp->link_config.active_speed == SPEED_10)
3497 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3498 else
3499 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3500 if (newlnkctl != oldlnkctl)
3501 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00003502 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003503 newlnkctl);
3504 }
3505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 if (current_link_up != netif_carrier_ok(tp->dev)) {
3507 if (current_link_up)
3508 netif_carrier_on(tp->dev);
3509 else
3510 netif_carrier_off(tp->dev);
3511 tg3_link_report(tp);
3512 }
3513
3514 return 0;
3515}
3516
3517struct tg3_fiber_aneginfo {
3518 int state;
3519#define ANEG_STATE_UNKNOWN 0
3520#define ANEG_STATE_AN_ENABLE 1
3521#define ANEG_STATE_RESTART_INIT 2
3522#define ANEG_STATE_RESTART 3
3523#define ANEG_STATE_DISABLE_LINK_OK 4
3524#define ANEG_STATE_ABILITY_DETECT_INIT 5
3525#define ANEG_STATE_ABILITY_DETECT 6
3526#define ANEG_STATE_ACK_DETECT_INIT 7
3527#define ANEG_STATE_ACK_DETECT 8
3528#define ANEG_STATE_COMPLETE_ACK_INIT 9
3529#define ANEG_STATE_COMPLETE_ACK 10
3530#define ANEG_STATE_IDLE_DETECT_INIT 11
3531#define ANEG_STATE_IDLE_DETECT 12
3532#define ANEG_STATE_LINK_OK 13
3533#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3534#define ANEG_STATE_NEXT_PAGE_WAIT 15
3535
3536 u32 flags;
3537#define MR_AN_ENABLE 0x00000001
3538#define MR_RESTART_AN 0x00000002
3539#define MR_AN_COMPLETE 0x00000004
3540#define MR_PAGE_RX 0x00000008
3541#define MR_NP_LOADED 0x00000010
3542#define MR_TOGGLE_TX 0x00000020
3543#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3544#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3545#define MR_LP_ADV_SYM_PAUSE 0x00000100
3546#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3547#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3548#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3549#define MR_LP_ADV_NEXT_PAGE 0x00001000
3550#define MR_TOGGLE_RX 0x00002000
3551#define MR_NP_RX 0x00004000
3552
3553#define MR_LINK_OK 0x80000000
3554
3555 unsigned long link_time, cur_time;
3556
3557 u32 ability_match_cfg;
3558 int ability_match_count;
3559
3560 char ability_match, idle_match, ack_match;
3561
3562 u32 txconfig, rxconfig;
3563#define ANEG_CFG_NP 0x00000080
3564#define ANEG_CFG_ACK 0x00000040
3565#define ANEG_CFG_RF2 0x00000020
3566#define ANEG_CFG_RF1 0x00000010
3567#define ANEG_CFG_PS2 0x00000001
3568#define ANEG_CFG_PS1 0x00008000
3569#define ANEG_CFG_HD 0x00004000
3570#define ANEG_CFG_FD 0x00002000
3571#define ANEG_CFG_INVAL 0x00001f06
3572
3573};
3574#define ANEG_OK 0
3575#define ANEG_DONE 1
3576#define ANEG_TIMER_ENAB 2
3577#define ANEG_FAILED -1
3578
3579#define ANEG_STATE_SETTLE_TIME 10000
3580
3581static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3582 struct tg3_fiber_aneginfo *ap)
3583{
Matt Carlson5be73b42007-12-20 20:09:29 -08003584 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 unsigned long delta;
3586 u32 rx_cfg_reg;
3587 int ret;
3588
3589 if (ap->state == ANEG_STATE_UNKNOWN) {
3590 ap->rxconfig = 0;
3591 ap->link_time = 0;
3592 ap->cur_time = 0;
3593 ap->ability_match_cfg = 0;
3594 ap->ability_match_count = 0;
3595 ap->ability_match = 0;
3596 ap->idle_match = 0;
3597 ap->ack_match = 0;
3598 }
3599 ap->cur_time++;
3600
3601 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3602 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3603
3604 if (rx_cfg_reg != ap->ability_match_cfg) {
3605 ap->ability_match_cfg = rx_cfg_reg;
3606 ap->ability_match = 0;
3607 ap->ability_match_count = 0;
3608 } else {
3609 if (++ap->ability_match_count > 1) {
3610 ap->ability_match = 1;
3611 ap->ability_match_cfg = rx_cfg_reg;
3612 }
3613 }
3614 if (rx_cfg_reg & ANEG_CFG_ACK)
3615 ap->ack_match = 1;
3616 else
3617 ap->ack_match = 0;
3618
3619 ap->idle_match = 0;
3620 } else {
3621 ap->idle_match = 1;
3622 ap->ability_match_cfg = 0;
3623 ap->ability_match_count = 0;
3624 ap->ability_match = 0;
3625 ap->ack_match = 0;
3626
3627 rx_cfg_reg = 0;
3628 }
3629
3630 ap->rxconfig = rx_cfg_reg;
3631 ret = ANEG_OK;
3632
Matt Carlson33f401a2010-04-05 10:19:27 +00003633 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 case ANEG_STATE_UNKNOWN:
3635 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3636 ap->state = ANEG_STATE_AN_ENABLE;
3637
3638 /* fallthru */
3639 case ANEG_STATE_AN_ENABLE:
3640 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3641 if (ap->flags & MR_AN_ENABLE) {
3642 ap->link_time = 0;
3643 ap->cur_time = 0;
3644 ap->ability_match_cfg = 0;
3645 ap->ability_match_count = 0;
3646 ap->ability_match = 0;
3647 ap->idle_match = 0;
3648 ap->ack_match = 0;
3649
3650 ap->state = ANEG_STATE_RESTART_INIT;
3651 } else {
3652 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3653 }
3654 break;
3655
3656 case ANEG_STATE_RESTART_INIT:
3657 ap->link_time = ap->cur_time;
3658 ap->flags &= ~(MR_NP_LOADED);
3659 ap->txconfig = 0;
3660 tw32(MAC_TX_AUTO_NEG, 0);
3661 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3662 tw32_f(MAC_MODE, tp->mac_mode);
3663 udelay(40);
3664
3665 ret = ANEG_TIMER_ENAB;
3666 ap->state = ANEG_STATE_RESTART;
3667
3668 /* fallthru */
3669 case ANEG_STATE_RESTART:
3670 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003671 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003673 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 break;
3676
3677 case ANEG_STATE_DISABLE_LINK_OK:
3678 ret = ANEG_DONE;
3679 break;
3680
3681 case ANEG_STATE_ABILITY_DETECT_INIT:
3682 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003683 ap->txconfig = ANEG_CFG_FD;
3684 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3685 if (flowctrl & ADVERTISE_1000XPAUSE)
3686 ap->txconfig |= ANEG_CFG_PS1;
3687 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3688 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3690 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3691 tw32_f(MAC_MODE, tp->mac_mode);
3692 udelay(40);
3693
3694 ap->state = ANEG_STATE_ABILITY_DETECT;
3695 break;
3696
3697 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003698 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 break;
3701
3702 case ANEG_STATE_ACK_DETECT_INIT:
3703 ap->txconfig |= ANEG_CFG_ACK;
3704 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3705 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3706 tw32_f(MAC_MODE, tp->mac_mode);
3707 udelay(40);
3708
3709 ap->state = ANEG_STATE_ACK_DETECT;
3710
3711 /* fallthru */
3712 case ANEG_STATE_ACK_DETECT:
3713 if (ap->ack_match != 0) {
3714 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3715 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3716 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3717 } else {
3718 ap->state = ANEG_STATE_AN_ENABLE;
3719 }
3720 } else if (ap->ability_match != 0 &&
3721 ap->rxconfig == 0) {
3722 ap->state = ANEG_STATE_AN_ENABLE;
3723 }
3724 break;
3725
3726 case ANEG_STATE_COMPLETE_ACK_INIT:
3727 if (ap->rxconfig & ANEG_CFG_INVAL) {
3728 ret = ANEG_FAILED;
3729 break;
3730 }
3731 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3732 MR_LP_ADV_HALF_DUPLEX |
3733 MR_LP_ADV_SYM_PAUSE |
3734 MR_LP_ADV_ASYM_PAUSE |
3735 MR_LP_ADV_REMOTE_FAULT1 |
3736 MR_LP_ADV_REMOTE_FAULT2 |
3737 MR_LP_ADV_NEXT_PAGE |
3738 MR_TOGGLE_RX |
3739 MR_NP_RX);
3740 if (ap->rxconfig & ANEG_CFG_FD)
3741 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3742 if (ap->rxconfig & ANEG_CFG_HD)
3743 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3744 if (ap->rxconfig & ANEG_CFG_PS1)
3745 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3746 if (ap->rxconfig & ANEG_CFG_PS2)
3747 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3748 if (ap->rxconfig & ANEG_CFG_RF1)
3749 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3750 if (ap->rxconfig & ANEG_CFG_RF2)
3751 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3752 if (ap->rxconfig & ANEG_CFG_NP)
3753 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3754
3755 ap->link_time = ap->cur_time;
3756
3757 ap->flags ^= (MR_TOGGLE_TX);
3758 if (ap->rxconfig & 0x0008)
3759 ap->flags |= MR_TOGGLE_RX;
3760 if (ap->rxconfig & ANEG_CFG_NP)
3761 ap->flags |= MR_NP_RX;
3762 ap->flags |= MR_PAGE_RX;
3763
3764 ap->state = ANEG_STATE_COMPLETE_ACK;
3765 ret = ANEG_TIMER_ENAB;
3766 break;
3767
3768 case ANEG_STATE_COMPLETE_ACK:
3769 if (ap->ability_match != 0 &&
3770 ap->rxconfig == 0) {
3771 ap->state = ANEG_STATE_AN_ENABLE;
3772 break;
3773 }
3774 delta = ap->cur_time - ap->link_time;
3775 if (delta > ANEG_STATE_SETTLE_TIME) {
3776 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3777 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3778 } else {
3779 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3780 !(ap->flags & MR_NP_RX)) {
3781 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3782 } else {
3783 ret = ANEG_FAILED;
3784 }
3785 }
3786 }
3787 break;
3788
3789 case ANEG_STATE_IDLE_DETECT_INIT:
3790 ap->link_time = ap->cur_time;
3791 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3792 tw32_f(MAC_MODE, tp->mac_mode);
3793 udelay(40);
3794
3795 ap->state = ANEG_STATE_IDLE_DETECT;
3796 ret = ANEG_TIMER_ENAB;
3797 break;
3798
3799 case ANEG_STATE_IDLE_DETECT:
3800 if (ap->ability_match != 0 &&
3801 ap->rxconfig == 0) {
3802 ap->state = ANEG_STATE_AN_ENABLE;
3803 break;
3804 }
3805 delta = ap->cur_time - ap->link_time;
3806 if (delta > ANEG_STATE_SETTLE_TIME) {
3807 /* XXX another gem from the Broadcom driver :( */
3808 ap->state = ANEG_STATE_LINK_OK;
3809 }
3810 break;
3811
3812 case ANEG_STATE_LINK_OK:
3813 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3814 ret = ANEG_DONE;
3815 break;
3816
3817 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3818 /* ??? unimplemented */
3819 break;
3820
3821 case ANEG_STATE_NEXT_PAGE_WAIT:
3822 /* ??? unimplemented */
3823 break;
3824
3825 default:
3826 ret = ANEG_FAILED;
3827 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
3830 return ret;
3831}
3832
Matt Carlson5be73b42007-12-20 20:09:29 -08003833static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834{
3835 int res = 0;
3836 struct tg3_fiber_aneginfo aninfo;
3837 int status = ANEG_FAILED;
3838 unsigned int tick;
3839 u32 tmp;
3840
3841 tw32_f(MAC_TX_AUTO_NEG, 0);
3842
3843 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3844 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3845 udelay(40);
3846
3847 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3848 udelay(40);
3849
3850 memset(&aninfo, 0, sizeof(aninfo));
3851 aninfo.flags |= MR_AN_ENABLE;
3852 aninfo.state = ANEG_STATE_UNKNOWN;
3853 aninfo.cur_time = 0;
3854 tick = 0;
3855 while (++tick < 195000) {
3856 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3857 if (status == ANEG_DONE || status == ANEG_FAILED)
3858 break;
3859
3860 udelay(1);
3861 }
3862
3863 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3864 tw32_f(MAC_MODE, tp->mac_mode);
3865 udelay(40);
3866
Matt Carlson5be73b42007-12-20 20:09:29 -08003867 *txflags = aninfo.txconfig;
3868 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869
3870 if (status == ANEG_DONE &&
3871 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3872 MR_LP_ADV_FULL_DUPLEX)))
3873 res = 1;
3874
3875 return res;
3876}
3877
3878static void tg3_init_bcm8002(struct tg3 *tp)
3879{
3880 u32 mac_status = tr32(MAC_STATUS);
3881 int i;
3882
3883 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00003884 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 !(mac_status & MAC_STATUS_PCS_SYNCED))
3886 return;
3887
3888 /* Set PLL lock range. */
3889 tg3_writephy(tp, 0x16, 0x8007);
3890
3891 /* SW reset */
3892 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3893
3894 /* Wait for reset to complete. */
3895 /* XXX schedule_timeout() ... */
3896 for (i = 0; i < 500; i++)
3897 udelay(10);
3898
3899 /* Config mode; select PMA/Ch 1 regs. */
3900 tg3_writephy(tp, 0x10, 0x8411);
3901
3902 /* Enable auto-lock and comdet, select txclk for tx. */
3903 tg3_writephy(tp, 0x11, 0x0a10);
3904
3905 tg3_writephy(tp, 0x18, 0x00a0);
3906 tg3_writephy(tp, 0x16, 0x41ff);
3907
3908 /* Assert and deassert POR. */
3909 tg3_writephy(tp, 0x13, 0x0400);
3910 udelay(40);
3911 tg3_writephy(tp, 0x13, 0x0000);
3912
3913 tg3_writephy(tp, 0x11, 0x0a50);
3914 udelay(40);
3915 tg3_writephy(tp, 0x11, 0x0a10);
3916
3917 /* Wait for signal to stabilize */
3918 /* XXX schedule_timeout() ... */
3919 for (i = 0; i < 15000; i++)
3920 udelay(10);
3921
3922 /* Deselect the channel register so we can read the PHYID
3923 * later.
3924 */
3925 tg3_writephy(tp, 0x10, 0x8011);
3926}
3927
3928static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3929{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003930 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 u32 sg_dig_ctrl, sg_dig_status;
3932 u32 serdes_cfg, expected_sg_dig_ctrl;
3933 int workaround, port_a;
3934 int current_link_up;
3935
3936 serdes_cfg = 0;
3937 expected_sg_dig_ctrl = 0;
3938 workaround = 0;
3939 port_a = 1;
3940 current_link_up = 0;
3941
3942 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3943 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3944 workaround = 1;
3945 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3946 port_a = 0;
3947
3948 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3949 /* preserve bits 20-23 for voltage regulator */
3950 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3951 }
3952
3953 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3954
3955 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003956 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 if (workaround) {
3958 u32 val = serdes_cfg;
3959
3960 if (port_a)
3961 val |= 0xc010000;
3962 else
3963 val |= 0x4010000;
3964 tw32_f(MAC_SERDES_CFG, val);
3965 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003966
3967 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 }
3969 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3970 tg3_setup_flow_control(tp, 0, 0);
3971 current_link_up = 1;
3972 }
3973 goto out;
3974 }
3975
3976 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003977 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
Matt Carlson82cd3d12007-12-20 20:09:00 -08003979 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3980 if (flowctrl & ADVERTISE_1000XPAUSE)
3981 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3982 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3983 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
3985 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003986 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003987 tp->serdes_counter &&
3988 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3989 MAC_STATUS_RCVD_CFG)) ==
3990 MAC_STATUS_PCS_SYNCED)) {
3991 tp->serdes_counter--;
3992 current_link_up = 1;
3993 goto out;
3994 }
3995restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 if (workaround)
3997 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003998 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 udelay(5);
4000 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4001
Michael Chan3d3ebe72006-09-27 15:59:15 -07004002 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004003 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4005 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004006 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 mac_status = tr32(MAC_STATUS);
4008
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004009 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004011 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
Matt Carlson82cd3d12007-12-20 20:09:00 -08004013 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4014 local_adv |= ADVERTISE_1000XPAUSE;
4015 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4016 local_adv |= ADVERTISE_1000XPSE_ASYM;
4017
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004018 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004019 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004020 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004021 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
4023 tg3_setup_flow_control(tp, local_adv, remote_adv);
4024 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004025 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004026 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004027 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004028 if (tp->serdes_counter)
4029 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 else {
4031 if (workaround) {
4032 u32 val = serdes_cfg;
4033
4034 if (port_a)
4035 val |= 0xc010000;
4036 else
4037 val |= 0x4010000;
4038
4039 tw32_f(MAC_SERDES_CFG, val);
4040 }
4041
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004042 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 udelay(40);
4044
4045 /* Link parallel detection - link is up */
4046 /* only if we have PCS_SYNC and not */
4047 /* receiving config code words */
4048 mac_status = tr32(MAC_STATUS);
4049 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4050 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4051 tg3_setup_flow_control(tp, 0, 0);
4052 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004053 tp->phy_flags |=
4054 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004055 tp->serdes_counter =
4056 SERDES_PARALLEL_DET_TIMEOUT;
4057 } else
4058 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 }
4060 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004061 } else {
4062 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004063 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 }
4065
4066out:
4067 return current_link_up;
4068}
4069
4070static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4071{
4072 int current_link_up = 0;
4073
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004074 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
4077 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004078 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004080
Matt Carlson5be73b42007-12-20 20:09:29 -08004081 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4082 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
Matt Carlson5be73b42007-12-20 20:09:29 -08004084 if (txflags & ANEG_CFG_PS1)
4085 local_adv |= ADVERTISE_1000XPAUSE;
4086 if (txflags & ANEG_CFG_PS2)
4087 local_adv |= ADVERTISE_1000XPSE_ASYM;
4088
4089 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4090 remote_adv |= LPA_1000XPAUSE;
4091 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4092 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
4094 tg3_setup_flow_control(tp, local_adv, remote_adv);
4095
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 current_link_up = 1;
4097 }
4098 for (i = 0; i < 30; i++) {
4099 udelay(20);
4100 tw32_f(MAC_STATUS,
4101 (MAC_STATUS_SYNC_CHANGED |
4102 MAC_STATUS_CFG_CHANGED));
4103 udelay(40);
4104 if ((tr32(MAC_STATUS) &
4105 (MAC_STATUS_SYNC_CHANGED |
4106 MAC_STATUS_CFG_CHANGED)) == 0)
4107 break;
4108 }
4109
4110 mac_status = tr32(MAC_STATUS);
4111 if (current_link_up == 0 &&
4112 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4113 !(mac_status & MAC_STATUS_RCVD_CFG))
4114 current_link_up = 1;
4115 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004116 tg3_setup_flow_control(tp, 0, 0);
4117
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 /* Forcing 1000FD link up. */
4119 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
4121 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4122 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004123
4124 tw32_f(MAC_MODE, tp->mac_mode);
4125 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 }
4127
4128out:
4129 return current_link_up;
4130}
4131
4132static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4133{
4134 u32 orig_pause_cfg;
4135 u16 orig_active_speed;
4136 u8 orig_active_duplex;
4137 u32 mac_status;
4138 int current_link_up;
4139 int i;
4140
Matt Carlson8d018622007-12-20 20:05:44 -08004141 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 orig_active_speed = tp->link_config.active_speed;
4143 orig_active_duplex = tp->link_config.active_duplex;
4144
Joe Perches63c3a662011-04-26 08:12:10 +00004145 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004147 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 mac_status = tr32(MAC_STATUS);
4149 mac_status &= (MAC_STATUS_PCS_SYNCED |
4150 MAC_STATUS_SIGNAL_DET |
4151 MAC_STATUS_CFG_CHANGED |
4152 MAC_STATUS_RCVD_CFG);
4153 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4154 MAC_STATUS_SIGNAL_DET)) {
4155 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4156 MAC_STATUS_CFG_CHANGED));
4157 return 0;
4158 }
4159 }
4160
4161 tw32_f(MAC_TX_AUTO_NEG, 0);
4162
4163 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4164 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4165 tw32_f(MAC_MODE, tp->mac_mode);
4166 udelay(40);
4167
Matt Carlson79eb6902010-02-17 15:17:03 +00004168 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 tg3_init_bcm8002(tp);
4170
4171 /* Enable link change event even when serdes polling. */
4172 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4173 udelay(40);
4174
4175 current_link_up = 0;
4176 mac_status = tr32(MAC_STATUS);
4177
Joe Perches63c3a662011-04-26 08:12:10 +00004178 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4180 else
4181 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4182
Matt Carlson898a56f2009-08-28 14:02:40 +00004183 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004185 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
4187 for (i = 0; i < 100; i++) {
4188 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4189 MAC_STATUS_CFG_CHANGED));
4190 udelay(5);
4191 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004192 MAC_STATUS_CFG_CHANGED |
4193 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 break;
4195 }
4196
4197 mac_status = tr32(MAC_STATUS);
4198 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4199 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004200 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4201 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 tw32_f(MAC_MODE, (tp->mac_mode |
4203 MAC_MODE_SEND_CONFIGS));
4204 udelay(1);
4205 tw32_f(MAC_MODE, tp->mac_mode);
4206 }
4207 }
4208
4209 if (current_link_up == 1) {
4210 tp->link_config.active_speed = SPEED_1000;
4211 tp->link_config.active_duplex = DUPLEX_FULL;
4212 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4213 LED_CTRL_LNKLED_OVERRIDE |
4214 LED_CTRL_1000MBPS_ON));
4215 } else {
4216 tp->link_config.active_speed = SPEED_INVALID;
4217 tp->link_config.active_duplex = DUPLEX_INVALID;
4218 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4219 LED_CTRL_LNKLED_OVERRIDE |
4220 LED_CTRL_TRAFFIC_OVERRIDE));
4221 }
4222
4223 if (current_link_up != netif_carrier_ok(tp->dev)) {
4224 if (current_link_up)
4225 netif_carrier_on(tp->dev);
4226 else
4227 netif_carrier_off(tp->dev);
4228 tg3_link_report(tp);
4229 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004230 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 if (orig_pause_cfg != now_pause_cfg ||
4232 orig_active_speed != tp->link_config.active_speed ||
4233 orig_active_duplex != tp->link_config.active_duplex)
4234 tg3_link_report(tp);
4235 }
4236
4237 return 0;
4238}
4239
Michael Chan747e8f82005-07-25 12:33:22 -07004240static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4241{
4242 int current_link_up, err = 0;
4243 u32 bmsr, bmcr;
4244 u16 current_speed;
4245 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004246 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004247
4248 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4249 tw32_f(MAC_MODE, tp->mac_mode);
4250 udelay(40);
4251
4252 tw32(MAC_EVENT, 0);
4253
4254 tw32_f(MAC_STATUS,
4255 (MAC_STATUS_SYNC_CHANGED |
4256 MAC_STATUS_CFG_CHANGED |
4257 MAC_STATUS_MI_COMPLETION |
4258 MAC_STATUS_LNKSTATE_CHANGED));
4259 udelay(40);
4260
4261 if (force_reset)
4262 tg3_phy_reset(tp);
4263
4264 current_link_up = 0;
4265 current_speed = SPEED_INVALID;
4266 current_duplex = DUPLEX_INVALID;
4267
4268 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4269 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004270 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4271 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4272 bmsr |= BMSR_LSTATUS;
4273 else
4274 bmsr &= ~BMSR_LSTATUS;
4275 }
Michael Chan747e8f82005-07-25 12:33:22 -07004276
4277 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4278
4279 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004280 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004281 /* do nothing, just check for link up at the end */
4282 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4283 u32 adv, new_adv;
4284
4285 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4286 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4287 ADVERTISE_1000XPAUSE |
4288 ADVERTISE_1000XPSE_ASYM |
4289 ADVERTISE_SLCT);
4290
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004291 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004292
4293 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4294 new_adv |= ADVERTISE_1000XHALF;
4295 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4296 new_adv |= ADVERTISE_1000XFULL;
4297
4298 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4299 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4300 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4301 tg3_writephy(tp, MII_BMCR, bmcr);
4302
4303 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004304 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004305 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004306
4307 return err;
4308 }
4309 } else {
4310 u32 new_bmcr;
4311
4312 bmcr &= ~BMCR_SPEED1000;
4313 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4314
4315 if (tp->link_config.duplex == DUPLEX_FULL)
4316 new_bmcr |= BMCR_FULLDPLX;
4317
4318 if (new_bmcr != bmcr) {
4319 /* BMCR_SPEED1000 is a reserved bit that needs
4320 * to be set on write.
4321 */
4322 new_bmcr |= BMCR_SPEED1000;
4323
4324 /* Force a linkdown */
4325 if (netif_carrier_ok(tp->dev)) {
4326 u32 adv;
4327
4328 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4329 adv &= ~(ADVERTISE_1000XFULL |
4330 ADVERTISE_1000XHALF |
4331 ADVERTISE_SLCT);
4332 tg3_writephy(tp, MII_ADVERTISE, adv);
4333 tg3_writephy(tp, MII_BMCR, bmcr |
4334 BMCR_ANRESTART |
4335 BMCR_ANENABLE);
4336 udelay(10);
4337 netif_carrier_off(tp->dev);
4338 }
4339 tg3_writephy(tp, MII_BMCR, new_bmcr);
4340 bmcr = new_bmcr;
4341 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4342 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004343 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4344 ASIC_REV_5714) {
4345 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4346 bmsr |= BMSR_LSTATUS;
4347 else
4348 bmsr &= ~BMSR_LSTATUS;
4349 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004350 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004351 }
4352 }
4353
4354 if (bmsr & BMSR_LSTATUS) {
4355 current_speed = SPEED_1000;
4356 current_link_up = 1;
4357 if (bmcr & BMCR_FULLDPLX)
4358 current_duplex = DUPLEX_FULL;
4359 else
4360 current_duplex = DUPLEX_HALF;
4361
Matt Carlsonef167e22007-12-20 20:10:01 -08004362 local_adv = 0;
4363 remote_adv = 0;
4364
Michael Chan747e8f82005-07-25 12:33:22 -07004365 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004366 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004367
4368 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4369 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4370 common = local_adv & remote_adv;
4371 if (common & (ADVERTISE_1000XHALF |
4372 ADVERTISE_1000XFULL)) {
4373 if (common & ADVERTISE_1000XFULL)
4374 current_duplex = DUPLEX_FULL;
4375 else
4376 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004377 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004378 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004379 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004380 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004381 }
Michael Chan747e8f82005-07-25 12:33:22 -07004382 }
4383 }
4384
Matt Carlsonef167e22007-12-20 20:10:01 -08004385 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4386 tg3_setup_flow_control(tp, local_adv, remote_adv);
4387
Michael Chan747e8f82005-07-25 12:33:22 -07004388 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4389 if (tp->link_config.active_duplex == DUPLEX_HALF)
4390 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4391
4392 tw32_f(MAC_MODE, tp->mac_mode);
4393 udelay(40);
4394
4395 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4396
4397 tp->link_config.active_speed = current_speed;
4398 tp->link_config.active_duplex = current_duplex;
4399
4400 if (current_link_up != netif_carrier_ok(tp->dev)) {
4401 if (current_link_up)
4402 netif_carrier_on(tp->dev);
4403 else {
4404 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004405 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004406 }
4407 tg3_link_report(tp);
4408 }
4409 return err;
4410}
4411
4412static void tg3_serdes_parallel_detect(struct tg3 *tp)
4413{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004414 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004415 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004416 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004417 return;
4418 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004419
Michael Chan747e8f82005-07-25 12:33:22 -07004420 if (!netif_carrier_ok(tp->dev) &&
4421 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4422 u32 bmcr;
4423
4424 tg3_readphy(tp, MII_BMCR, &bmcr);
4425 if (bmcr & BMCR_ANENABLE) {
4426 u32 phy1, phy2;
4427
4428 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004429 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4430 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004431
4432 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004433 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4434 MII_TG3_DSP_EXP1_INT_STAT);
4435 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4436 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004437
4438 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4439 /* We have signal detect and not receiving
4440 * config code words, link is up by parallel
4441 * detection.
4442 */
4443
4444 bmcr &= ~BMCR_ANENABLE;
4445 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4446 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004447 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004448 }
4449 }
Matt Carlson859a588792010-04-05 10:19:28 +00004450 } else if (netif_carrier_ok(tp->dev) &&
4451 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004452 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004453 u32 phy2;
4454
4455 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004456 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4457 MII_TG3_DSP_EXP1_INT_STAT);
4458 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004459 if (phy2 & 0x20) {
4460 u32 bmcr;
4461
4462 /* Config code words received, turn on autoneg. */
4463 tg3_readphy(tp, MII_BMCR, &bmcr);
4464 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4465
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004466 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004467
4468 }
4469 }
4470}
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4473{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004474 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 int err;
4476
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004477 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004479 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004480 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004481 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004484 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004485 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004486
4487 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4488 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4489 scale = 65;
4490 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4491 scale = 6;
4492 else
4493 scale = 12;
4494
4495 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4496 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4497 tw32(GRC_MISC_CFG, val);
4498 }
4499
Matt Carlsonf2096f92011-04-05 14:22:48 +00004500 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4501 (6 << TX_LENGTHS_IPG_SHIFT);
4502 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4503 val |= tr32(MAC_TX_LENGTHS) &
4504 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4505 TX_LENGTHS_CNT_DWN_VAL_MSK);
4506
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 if (tp->link_config.active_speed == SPEED_1000 &&
4508 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004509 tw32(MAC_TX_LENGTHS, val |
4510 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004512 tw32(MAC_TX_LENGTHS, val |
4513 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
Joe Perches63c3a662011-04-26 08:12:10 +00004515 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 if (netif_carrier_ok(tp->dev)) {
4517 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004518 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 } else {
4520 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4521 }
4522 }
4523
Joe Perches63c3a662011-04-26 08:12:10 +00004524 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004525 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004526 if (!netif_carrier_ok(tp->dev))
4527 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4528 tp->pwrmgmt_thresh;
4529 else
4530 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4531 tw32(PCIE_PWR_MGMT_THRESH, val);
4532 }
4533
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 return err;
4535}
4536
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004537static inline int tg3_irq_sync(struct tg3 *tp)
4538{
4539 return tp->irq_sync;
4540}
4541
Matt Carlson97bd8e42011-04-13 11:05:04 +00004542static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4543{
4544 int i;
4545
4546 dst = (u32 *)((u8 *)dst + off);
4547 for (i = 0; i < len; i += sizeof(u32))
4548 *dst++ = tr32(off + i);
4549}
4550
4551static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4552{
4553 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4554 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4555 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4556 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4557 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4558 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4559 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4560 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4561 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4562 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4563 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4564 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4565 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4566 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4567 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4568 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4569 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4570 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4571 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4572
Joe Perches63c3a662011-04-26 08:12:10 +00004573 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004574 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4575
4576 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4577 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4578 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4579 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4580 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4581 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4582 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4583 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4584
Joe Perches63c3a662011-04-26 08:12:10 +00004585 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004586 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4587 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4588 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4589 }
4590
4591 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4592 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4593 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4594 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4595 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4596
Joe Perches63c3a662011-04-26 08:12:10 +00004597 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004598 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4599}
4600
4601static void tg3_dump_state(struct tg3 *tp)
4602{
4603 int i;
4604 u32 *regs;
4605
4606 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4607 if (!regs) {
4608 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4609 return;
4610 }
4611
Joe Perches63c3a662011-04-26 08:12:10 +00004612 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004613 /* Read up to but not including private PCI registers */
4614 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4615 regs[i / sizeof(u32)] = tr32(i);
4616 } else
4617 tg3_dump_legacy_regs(tp, regs);
4618
4619 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4620 if (!regs[i + 0] && !regs[i + 1] &&
4621 !regs[i + 2] && !regs[i + 3])
4622 continue;
4623
4624 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4625 i * 4,
4626 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4627 }
4628
4629 kfree(regs);
4630
4631 for (i = 0; i < tp->irq_cnt; i++) {
4632 struct tg3_napi *tnapi = &tp->napi[i];
4633
4634 /* SW status block */
4635 netdev_err(tp->dev,
4636 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4637 i,
4638 tnapi->hw_status->status,
4639 tnapi->hw_status->status_tag,
4640 tnapi->hw_status->rx_jumbo_consumer,
4641 tnapi->hw_status->rx_consumer,
4642 tnapi->hw_status->rx_mini_consumer,
4643 tnapi->hw_status->idx[0].rx_producer,
4644 tnapi->hw_status->idx[0].tx_consumer);
4645
4646 netdev_err(tp->dev,
4647 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4648 i,
4649 tnapi->last_tag, tnapi->last_irq_tag,
4650 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4651 tnapi->rx_rcb_ptr,
4652 tnapi->prodring.rx_std_prod_idx,
4653 tnapi->prodring.rx_std_cons_idx,
4654 tnapi->prodring.rx_jmb_prod_idx,
4655 tnapi->prodring.rx_jmb_cons_idx);
4656 }
4657}
4658
Michael Chandf3e6542006-05-26 17:48:07 -07004659/* This is called whenever we suspect that the system chipset is re-
4660 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4661 * is bogus tx completions. We try to recover by setting the
4662 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4663 * in the workqueue.
4664 */
4665static void tg3_tx_recover(struct tg3 *tp)
4666{
Joe Perches63c3a662011-04-26 08:12:10 +00004667 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07004668 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4669
Matt Carlson5129c3a2010-04-05 10:19:23 +00004670 netdev_warn(tp->dev,
4671 "The system may be re-ordering memory-mapped I/O "
4672 "cycles to the network device, attempting to recover. "
4673 "Please report the problem to the driver maintainer "
4674 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004675
4676 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00004677 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07004678 spin_unlock(&tp->lock);
4679}
4680
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004681static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004682{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004683 /* Tell compiler to fetch tx indices from memory. */
4684 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004685 return tnapi->tx_pending -
4686 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004687}
4688
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689/* Tigon3 never reports partial packet sends. So we do not
4690 * need special logic to handle SKBs that have not had all
4691 * of their frags sent yet, like SunGEM does.
4692 */
Matt Carlson17375d22009-08-28 14:02:18 +00004693static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694{
Matt Carlson17375d22009-08-28 14:02:18 +00004695 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004696 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004697 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004698 struct netdev_queue *txq;
4699 int index = tnapi - tp->napi;
4700
Joe Perches63c3a662011-04-26 08:12:10 +00004701 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004702 index--;
4703
4704 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
4706 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004707 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004709 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
Michael Chandf3e6542006-05-26 17:48:07 -07004711 if (unlikely(skb == NULL)) {
4712 tg3_tx_recover(tp);
4713 return;
4714 }
4715
Alexander Duyckf4188d82009-12-02 16:48:38 +00004716 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004717 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004718 skb_headlen(skb),
4719 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
4721 ri->skb = NULL;
4722
4723 sw_idx = NEXT_TX(sw_idx);
4724
4725 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004726 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004727 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4728 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004729
4730 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004731 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004732 skb_shinfo(skb)->frags[i].size,
4733 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 sw_idx = NEXT_TX(sw_idx);
4735 }
4736
David S. Millerf47c11e2005-06-24 20:18:35 -07004737 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004738
4739 if (unlikely(tx_bug)) {
4740 tg3_tx_recover(tp);
4741 return;
4742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 }
4744
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004745 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
Michael Chan1b2a7202006-08-07 21:46:02 -07004747 /* Need to make the tx_cons update visible to tg3_start_xmit()
4748 * before checking for netif_queue_stopped(). Without the
4749 * memory barrier, there is a small possibility that tg3_start_xmit()
4750 * will miss it and cause the queue to be stopped forever.
4751 */
4752 smp_mb();
4753
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004754 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004755 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004756 __netif_tx_lock(txq, smp_processor_id());
4757 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004758 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004759 netif_tx_wake_queue(txq);
4760 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762}
4763
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004764static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4765{
4766 if (!ri->skb)
4767 return;
4768
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004769 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004770 map_sz, PCI_DMA_FROMDEVICE);
4771 dev_kfree_skb_any(ri->skb);
4772 ri->skb = NULL;
4773}
4774
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775/* Returns size of skb allocated or < 0 on error.
4776 *
4777 * We only need to fill in the address because the other members
4778 * of the RX descriptor are invariant, see tg3_init_rings.
4779 *
4780 * Note the purposeful assymetry of cpu vs. chip accesses. For
4781 * posting buffers we only dirty the first cache line of the RX
4782 * descriptor (containing the address). Whereas for the RX status
4783 * buffers the cpu only reads the last cacheline of the RX descriptor
4784 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4785 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004786static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004787 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788{
4789 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004790 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 struct sk_buff *skb;
4792 dma_addr_t mapping;
4793 int skb_size, dest_idx;
4794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 switch (opaque_key) {
4796 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004797 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004798 desc = &tpr->rx_std[dest_idx];
4799 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004800 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 break;
4802
4803 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004804 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004805 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004806 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004807 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 break;
4809
4810 default:
4811 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
4814 /* Do not overwrite any of the map or rp information
4815 * until we are sure we can commit to a new buffer.
4816 *
4817 * Callers depend upon this behavior and assume that
4818 * we leave everything unchanged if we fail.
4819 */
Matt Carlson287be122009-08-28 13:58:46 +00004820 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 if (skb == NULL)
4822 return -ENOMEM;
4823
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 skb_reserve(skb, tp->rx_offset);
4825
Matt Carlson287be122009-08-28 13:58:46 +00004826 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004828 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4829 dev_kfree_skb(skb);
4830 return -EIO;
4831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832
4833 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004834 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 desc->addr_hi = ((u64)mapping >> 32);
4837 desc->addr_lo = ((u64)mapping & 0xffffffff);
4838
4839 return skb_size;
4840}
4841
4842/* We only need to move over in the address because the other
4843 * members of the RX descriptor are invariant. See notes above
4844 * tg3_alloc_rx_skb for full details.
4845 */
Matt Carlsona3896162009-11-13 13:03:44 +00004846static void tg3_recycle_rx(struct tg3_napi *tnapi,
4847 struct tg3_rx_prodring_set *dpr,
4848 u32 opaque_key, int src_idx,
4849 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850{
Matt Carlson17375d22009-08-28 14:02:18 +00004851 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4853 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004854 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004855 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
4857 switch (opaque_key) {
4858 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004859 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004860 dest_desc = &dpr->rx_std[dest_idx];
4861 dest_map = &dpr->rx_std_buffers[dest_idx];
4862 src_desc = &spr->rx_std[src_idx];
4863 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 break;
4865
4866 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004867 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004868 dest_desc = &dpr->rx_jmb[dest_idx].std;
4869 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4870 src_desc = &spr->rx_jmb[src_idx].std;
4871 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 break;
4873
4874 default:
4875 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
4878 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004879 dma_unmap_addr_set(dest_map, mapping,
4880 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 dest_desc->addr_hi = src_desc->addr_hi;
4882 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004883
4884 /* Ensure that the update to the skb happens after the physical
4885 * addresses have been transferred to the new BD location.
4886 */
4887 smp_wmb();
4888
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 src_map->skb = NULL;
4890}
4891
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892/* The RX ring scheme is composed of multiple rings which post fresh
4893 * buffers to the chip, and one special ring the chip uses to report
4894 * status back to the host.
4895 *
4896 * The special ring reports the status of received packets to the
4897 * host. The chip does not write into the original descriptor the
4898 * RX buffer was obtained from. The chip simply takes the original
4899 * descriptor as provided by the host, updates the status and length
4900 * field, then writes this into the next status ring entry.
4901 *
4902 * Each ring the host uses to post buffers to the chip is described
4903 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4904 * it is first placed into the on-chip ram. When the packet's length
4905 * is known, it walks down the TG3_BDINFO entries to select the ring.
4906 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4907 * which is within the range of the new packet's length is chosen.
4908 *
4909 * The "separate ring for rx status" scheme may sound queer, but it makes
4910 * sense from a cache coherency perspective. If only the host writes
4911 * to the buffer post rings, and only the chip writes to the rx status
4912 * rings, then cache lines never move beyond shared-modified state.
4913 * If both the host and chip were to write into the same ring, cache line
4914 * eviction could occur since both entities want it in an exclusive state.
4915 */
Matt Carlson17375d22009-08-28 14:02:18 +00004916static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917{
Matt Carlson17375d22009-08-28 14:02:18 +00004918 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004919 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004920 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004921 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004922 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004924 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004926 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 /*
4928 * We need to order the read of hw_idx and the read of
4929 * the opaque cookie.
4930 */
4931 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 work_mask = 0;
4933 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004934 std_prod_idx = tpr->rx_std_prod_idx;
4935 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004937 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004938 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 unsigned int len;
4940 struct sk_buff *skb;
4941 dma_addr_t dma_addr;
4942 u32 opaque_key, desc_idx, *post_ptr;
4943
4944 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4945 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4946 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004947 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004948 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004949 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004950 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004951 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004953 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004954 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004955 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004956 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004957 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
4960 work_mask |= opaque_key;
4961
4962 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4963 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4964 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004965 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 desc_idx, *post_ptr);
4967 drop_it_no_recycle:
4968 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004969 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 goto next_pkt;
4971 }
4972
Matt Carlsonad829262008-11-21 17:16:16 -08004973 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4974 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975
Matt Carlsond2757fc2010-04-12 06:58:27 +00004976 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 int skb_size;
4978
Matt Carlson86b21e52009-11-13 13:03:45 +00004979 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004980 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 if (skb_size < 0)
4982 goto drop_it;
4983
Matt Carlson287be122009-08-28 13:58:46 +00004984 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 PCI_DMA_FROMDEVICE);
4986
Matt Carlson61e800c2010-02-17 15:16:54 +00004987 /* Ensure that the update to the skb happens
4988 * after the usage of the old DMA mapping.
4989 */
4990 smp_wmb();
4991
4992 ri->skb = NULL;
4993
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 skb_put(skb, len);
4995 } else {
4996 struct sk_buff *copy_skb;
4997
Matt Carlsona3896162009-11-13 13:03:44 +00004998 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 desc_idx, *post_ptr);
5000
Matt Carlsonbf933c82011-01-25 15:58:49 +00005001 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00005002 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 if (copy_skb == NULL)
5004 goto drop_it_no_recycle;
5005
Matt Carlsonbf933c82011-01-25 15:58:49 +00005006 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 skb_put(copy_skb, len);
5008 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03005009 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
5011
5012 /* We'll reuse the original ring buffer. */
5013 skb = copy_skb;
5014 }
5015
Michał Mirosławdc668912011-04-07 03:35:07 +00005016 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5018 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5019 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5020 skb->ip_summed = CHECKSUM_UNNECESSARY;
5021 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005022 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
5024 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005025
5026 if (len > (tp->dev->mtu + ETH_HLEN) &&
5027 skb->protocol != htons(ETH_P_8021Q)) {
5028 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005029 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005030 }
5031
Matt Carlson9dc7a112010-04-12 06:58:28 +00005032 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005033 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5034 __vlan_hwaccel_put_tag(skb,
5035 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005036
Matt Carlsonbf933c82011-01-25 15:58:49 +00005037 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 received++;
5040 budget--;
5041
5042next_pkt:
5043 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005044
5045 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005046 tpr->rx_std_prod_idx = std_prod_idx &
5047 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005048 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5049 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005050 work_mask &= ~RXD_OPAQUE_RING_STD;
5051 rx_std_posted = 0;
5052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005054 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005055 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005056
5057 /* Refresh hw_idx to see if there is new work */
5058 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005059 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005060 rmb();
5061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 }
5063
5064 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005065 tnapi->rx_rcb_ptr = sw_idx;
5066 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
5068 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005069 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005070 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005071 tpr->rx_std_prod_idx = std_prod_idx &
5072 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005073 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5074 tpr->rx_std_prod_idx);
5075 }
5076 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005077 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5078 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005079 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5080 tpr->rx_jmb_prod_idx);
5081 }
5082 mmiowb();
5083 } else if (work_mask) {
5084 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5085 * updated before the producer indices can be updated.
5086 */
5087 smp_wmb();
5088
Matt Carlson2c49a442010-09-30 10:34:35 +00005089 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5090 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005091
Matt Carlsone4af1af2010-02-12 14:47:05 +00005092 if (tnapi != &tp->napi[1])
5093 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
5096 return received;
5097}
5098
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005099static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005102 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005103 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5104
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 if (sblk->status & SD_STATUS_LINK_CHG) {
5106 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005107 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005108 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005109 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005110 tw32_f(MAC_STATUS,
5111 (MAC_STATUS_SYNC_CHANGED |
5112 MAC_STATUS_CFG_CHANGED |
5113 MAC_STATUS_MI_COMPLETION |
5114 MAC_STATUS_LNKSTATE_CHANGED));
5115 udelay(40);
5116 } else
5117 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005118 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 }
5120 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005121}
5122
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005123static int tg3_rx_prodring_xfer(struct tg3 *tp,
5124 struct tg3_rx_prodring_set *dpr,
5125 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005126{
5127 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005128 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005129
5130 while (1) {
5131 src_prod_idx = spr->rx_std_prod_idx;
5132
5133 /* Make sure updates to the rx_std_buffers[] entries and the
5134 * standard producer index are seen in the correct order.
5135 */
5136 smp_rmb();
5137
5138 if (spr->rx_std_cons_idx == src_prod_idx)
5139 break;
5140
5141 if (spr->rx_std_cons_idx < src_prod_idx)
5142 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5143 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005144 cpycnt = tp->rx_std_ring_mask + 1 -
5145 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005146
Matt Carlson2c49a442010-09-30 10:34:35 +00005147 cpycnt = min(cpycnt,
5148 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005149
5150 si = spr->rx_std_cons_idx;
5151 di = dpr->rx_std_prod_idx;
5152
Matt Carlsone92967b2010-02-12 14:47:06 +00005153 for (i = di; i < di + cpycnt; i++) {
5154 if (dpr->rx_std_buffers[i].skb) {
5155 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005156 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005157 break;
5158 }
5159 }
5160
5161 if (!cpycnt)
5162 break;
5163
5164 /* Ensure that updates to the rx_std_buffers ring and the
5165 * shadowed hardware producer ring from tg3_recycle_skb() are
5166 * ordered correctly WRT the skb check above.
5167 */
5168 smp_rmb();
5169
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005170 memcpy(&dpr->rx_std_buffers[di],
5171 &spr->rx_std_buffers[si],
5172 cpycnt * sizeof(struct ring_info));
5173
5174 for (i = 0; i < cpycnt; i++, di++, si++) {
5175 struct tg3_rx_buffer_desc *sbd, *dbd;
5176 sbd = &spr->rx_std[si];
5177 dbd = &dpr->rx_std[di];
5178 dbd->addr_hi = sbd->addr_hi;
5179 dbd->addr_lo = sbd->addr_lo;
5180 }
5181
Matt Carlson2c49a442010-09-30 10:34:35 +00005182 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5183 tp->rx_std_ring_mask;
5184 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5185 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005186 }
5187
5188 while (1) {
5189 src_prod_idx = spr->rx_jmb_prod_idx;
5190
5191 /* Make sure updates to the rx_jmb_buffers[] entries and
5192 * the jumbo producer index are seen in the correct order.
5193 */
5194 smp_rmb();
5195
5196 if (spr->rx_jmb_cons_idx == src_prod_idx)
5197 break;
5198
5199 if (spr->rx_jmb_cons_idx < src_prod_idx)
5200 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5201 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005202 cpycnt = tp->rx_jmb_ring_mask + 1 -
5203 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005204
5205 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005206 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005207
5208 si = spr->rx_jmb_cons_idx;
5209 di = dpr->rx_jmb_prod_idx;
5210
Matt Carlsone92967b2010-02-12 14:47:06 +00005211 for (i = di; i < di + cpycnt; i++) {
5212 if (dpr->rx_jmb_buffers[i].skb) {
5213 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005214 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005215 break;
5216 }
5217 }
5218
5219 if (!cpycnt)
5220 break;
5221
5222 /* Ensure that updates to the rx_jmb_buffers ring and the
5223 * shadowed hardware producer ring from tg3_recycle_skb() are
5224 * ordered correctly WRT the skb check above.
5225 */
5226 smp_rmb();
5227
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005228 memcpy(&dpr->rx_jmb_buffers[di],
5229 &spr->rx_jmb_buffers[si],
5230 cpycnt * sizeof(struct ring_info));
5231
5232 for (i = 0; i < cpycnt; i++, di++, si++) {
5233 struct tg3_rx_buffer_desc *sbd, *dbd;
5234 sbd = &spr->rx_jmb[si].std;
5235 dbd = &dpr->rx_jmb[di].std;
5236 dbd->addr_hi = sbd->addr_hi;
5237 dbd->addr_lo = sbd->addr_lo;
5238 }
5239
Matt Carlson2c49a442010-09-30 10:34:35 +00005240 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5241 tp->rx_jmb_ring_mask;
5242 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5243 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005244 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005245
5246 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005247}
5248
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005249static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5250{
5251 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
5253 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005254 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005255 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005256 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005257 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 }
5259
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 /* run RX thread, within the bounds set by NAPI.
5261 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005262 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005264 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005265 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266
Joe Perches63c3a662011-04-26 08:12:10 +00005267 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005268 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005269 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005270 u32 std_prod_idx = dpr->rx_std_prod_idx;
5271 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005272
Matt Carlsone4af1af2010-02-12 14:47:05 +00005273 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005274 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005275 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005276
5277 wmb();
5278
Matt Carlsone4af1af2010-02-12 14:47:05 +00005279 if (std_prod_idx != dpr->rx_std_prod_idx)
5280 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5281 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005282
Matt Carlsone4af1af2010-02-12 14:47:05 +00005283 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5284 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5285 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005286
5287 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005288
5289 if (err)
5290 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005291 }
5292
David S. Miller6f535762007-10-11 18:08:29 -07005293 return work_done;
5294}
David S. Millerf7383c22005-05-18 22:50:53 -07005295
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005296static int tg3_poll_msix(struct napi_struct *napi, int budget)
5297{
5298 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5299 struct tg3 *tp = tnapi->tp;
5300 int work_done = 0;
5301 struct tg3_hw_status *sblk = tnapi->hw_status;
5302
5303 while (1) {
5304 work_done = tg3_poll_work(tnapi, work_done, budget);
5305
Joe Perches63c3a662011-04-26 08:12:10 +00005306 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005307 goto tx_recovery;
5308
5309 if (unlikely(work_done >= budget))
5310 break;
5311
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005312 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005313 * to tell the hw how much work has been processed,
5314 * so we must read it before checking for more work.
5315 */
5316 tnapi->last_tag = sblk->status_tag;
5317 tnapi->last_irq_tag = tnapi->last_tag;
5318 rmb();
5319
5320 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005321 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5322 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005323 napi_complete(napi);
5324 /* Reenable interrupts. */
5325 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5326 mmiowb();
5327 break;
5328 }
5329 }
5330
5331 return work_done;
5332
5333tx_recovery:
5334 /* work_done is guaranteed to be less than budget. */
5335 napi_complete(napi);
5336 schedule_work(&tp->reset_task);
5337 return work_done;
5338}
5339
Matt Carlsone64de4e2011-04-13 11:05:05 +00005340static void tg3_process_error(struct tg3 *tp)
5341{
5342 u32 val;
5343 bool real_error = false;
5344
Joe Perches63c3a662011-04-26 08:12:10 +00005345 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005346 return;
5347
5348 /* Check Flow Attention register */
5349 val = tr32(HOSTCC_FLOW_ATTN);
5350 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5351 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5352 real_error = true;
5353 }
5354
5355 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5356 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5357 real_error = true;
5358 }
5359
5360 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5361 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5362 real_error = true;
5363 }
5364
5365 if (!real_error)
5366 return;
5367
5368 tg3_dump_state(tp);
5369
Joe Perches63c3a662011-04-26 08:12:10 +00005370 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005371 schedule_work(&tp->reset_task);
5372}
5373
David S. Miller6f535762007-10-11 18:08:29 -07005374static int tg3_poll(struct napi_struct *napi, int budget)
5375{
Matt Carlson8ef04422009-08-28 14:01:37 +00005376 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5377 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005378 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005379 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005380
5381 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005382 if (sblk->status & SD_STATUS_ERROR)
5383 tg3_process_error(tp);
5384
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005385 tg3_poll_link(tp);
5386
Matt Carlson17375d22009-08-28 14:02:18 +00005387 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005388
Joe Perches63c3a662011-04-26 08:12:10 +00005389 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07005390 goto tx_recovery;
5391
5392 if (unlikely(work_done >= budget))
5393 break;
5394
Joe Perches63c3a662011-04-26 08:12:10 +00005395 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00005396 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005397 * to tell the hw how much work has been processed,
5398 * so we must read it before checking for more work.
5399 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005400 tnapi->last_tag = sblk->status_tag;
5401 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005402 rmb();
5403 } else
5404 sblk->status &= ~SD_STATUS_UPDATED;
5405
Matt Carlson17375d22009-08-28 14:02:18 +00005406 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005407 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005408 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005409 break;
5410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 }
5412
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005413 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005414
5415tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005416 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005417 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005418 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005419 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420}
5421
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005422static void tg3_napi_disable(struct tg3 *tp)
5423{
5424 int i;
5425
5426 for (i = tp->irq_cnt - 1; i >= 0; i--)
5427 napi_disable(&tp->napi[i].napi);
5428}
5429
5430static void tg3_napi_enable(struct tg3 *tp)
5431{
5432 int i;
5433
5434 for (i = 0; i < tp->irq_cnt; i++)
5435 napi_enable(&tp->napi[i].napi);
5436}
5437
5438static void tg3_napi_init(struct tg3 *tp)
5439{
5440 int i;
5441
5442 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5443 for (i = 1; i < tp->irq_cnt; i++)
5444 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5445}
5446
5447static void tg3_napi_fini(struct tg3 *tp)
5448{
5449 int i;
5450
5451 for (i = 0; i < tp->irq_cnt; i++)
5452 netif_napi_del(&tp->napi[i].napi);
5453}
5454
5455static inline void tg3_netif_stop(struct tg3 *tp)
5456{
5457 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5458 tg3_napi_disable(tp);
5459 netif_tx_disable(tp->dev);
5460}
5461
5462static inline void tg3_netif_start(struct tg3 *tp)
5463{
5464 /* NOTE: unconditional netif_tx_wake_all_queues is only
5465 * appropriate so long as all callers are assured to
5466 * have free tx slots (such as after tg3_init_hw)
5467 */
5468 netif_tx_wake_all_queues(tp->dev);
5469
5470 tg3_napi_enable(tp);
5471 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5472 tg3_enable_ints(tp);
5473}
5474
David S. Millerf47c11e2005-06-24 20:18:35 -07005475static void tg3_irq_quiesce(struct tg3 *tp)
5476{
Matt Carlson4f125f42009-09-01 12:55:02 +00005477 int i;
5478
David S. Millerf47c11e2005-06-24 20:18:35 -07005479 BUG_ON(tp->irq_sync);
5480
5481 tp->irq_sync = 1;
5482 smp_mb();
5483
Matt Carlson4f125f42009-09-01 12:55:02 +00005484 for (i = 0; i < tp->irq_cnt; i++)
5485 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005486}
5487
David S. Millerf47c11e2005-06-24 20:18:35 -07005488/* Fully shutdown all tg3 driver activity elsewhere in the system.
5489 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5490 * with as well. Most of the time, this is not necessary except when
5491 * shutting down the device.
5492 */
5493static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5494{
Michael Chan46966542007-07-11 19:47:19 -07005495 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005496 if (irq_sync)
5497 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005498}
5499
5500static inline void tg3_full_unlock(struct tg3 *tp)
5501{
David S. Millerf47c11e2005-06-24 20:18:35 -07005502 spin_unlock_bh(&tp->lock);
5503}
5504
Michael Chanfcfa0a32006-03-20 22:28:41 -08005505/* One-shot MSI handler - Chip automatically disables interrupt
5506 * after sending MSI so driver doesn't have to do it.
5507 */
David Howells7d12e782006-10-05 14:55:46 +01005508static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005509{
Matt Carlson09943a12009-08-28 14:01:57 +00005510 struct tg3_napi *tnapi = dev_id;
5511 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005512
Matt Carlson898a56f2009-08-28 14:02:40 +00005513 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005514 if (tnapi->rx_rcb)
5515 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005516
5517 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005518 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005519
5520 return IRQ_HANDLED;
5521}
5522
Michael Chan88b06bc22005-04-21 17:13:25 -07005523/* MSI ISR - No need to check for interrupt sharing and no need to
5524 * flush status block and interrupt mailbox. PCI ordering rules
5525 * guarantee that MSI will arrive after the status block.
5526 */
David Howells7d12e782006-10-05 14:55:46 +01005527static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005528{
Matt Carlson09943a12009-08-28 14:01:57 +00005529 struct tg3_napi *tnapi = dev_id;
5530 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005531
Matt Carlson898a56f2009-08-28 14:02:40 +00005532 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005533 if (tnapi->rx_rcb)
5534 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005535 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005536 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005537 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005538 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005539 * NIC to stop sending us irqs, engaging "in-intr-handler"
5540 * event coalescing.
5541 */
5542 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005543 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005544 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005545
Michael Chan88b06bc22005-04-21 17:13:25 -07005546 return IRQ_RETVAL(1);
5547}
5548
David Howells7d12e782006-10-05 14:55:46 +01005549static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550{
Matt Carlson09943a12009-08-28 14:01:57 +00005551 struct tg3_napi *tnapi = dev_id;
5552 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005553 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 unsigned int handled = 1;
5555
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 /* In INTx mode, it is possible for the interrupt to arrive at
5557 * the CPU before the status block posted prior to the interrupt.
5558 * Reading the PCI State register will confirm whether the
5559 * interrupt is ours and will flush the status block.
5560 */
Michael Chand18edcb2007-03-24 20:57:11 -07005561 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00005562 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005563 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5564 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005565 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005566 }
Michael Chand18edcb2007-03-24 20:57:11 -07005567 }
5568
5569 /*
5570 * Writing any value to intr-mbox-0 clears PCI INTA# and
5571 * chip-internal interrupt pending events.
5572 * Writing non-zero to intr-mbox-0 additional tells the
5573 * NIC to stop sending us irqs, engaging "in-intr-handler"
5574 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005575 *
5576 * Flush the mailbox to de-assert the IRQ immediately to prevent
5577 * spurious interrupts. The flush impacts performance but
5578 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005579 */
Michael Chanc04cb342007-05-07 00:26:15 -07005580 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005581 if (tg3_irq_sync(tp))
5582 goto out;
5583 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005584 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005585 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005586 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005587 } else {
5588 /* No work, shared interrupt perhaps? re-enable
5589 * interrupts, and flush that PCI write
5590 */
5591 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5592 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005593 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005594out:
David S. Millerfac9b832005-05-18 22:46:34 -07005595 return IRQ_RETVAL(handled);
5596}
5597
David Howells7d12e782006-10-05 14:55:46 +01005598static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005599{
Matt Carlson09943a12009-08-28 14:01:57 +00005600 struct tg3_napi *tnapi = dev_id;
5601 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005602 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005603 unsigned int handled = 1;
5604
David S. Millerfac9b832005-05-18 22:46:34 -07005605 /* In INTx mode, it is possible for the interrupt to arrive at
5606 * the CPU before the status block posted prior to the interrupt.
5607 * Reading the PCI State register will confirm whether the
5608 * interrupt is ours and will flush the status block.
5609 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005610 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00005611 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005612 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5613 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005614 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 }
Michael Chand18edcb2007-03-24 20:57:11 -07005616 }
5617
5618 /*
5619 * writing any value to intr-mbox-0 clears PCI INTA# and
5620 * chip-internal interrupt pending events.
5621 * writing non-zero to intr-mbox-0 additional tells the
5622 * NIC to stop sending us irqs, engaging "in-intr-handler"
5623 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005624 *
5625 * Flush the mailbox to de-assert the IRQ immediately to prevent
5626 * spurious interrupts. The flush impacts performance but
5627 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005628 */
Michael Chanc04cb342007-05-07 00:26:15 -07005629 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005630
5631 /*
5632 * In a shared interrupt configuration, sometimes other devices'
5633 * interrupts will scream. We record the current status tag here
5634 * so that the above check can report that the screaming interrupts
5635 * are unhandled. Eventually they will be silenced.
5636 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005637 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005638
Michael Chand18edcb2007-03-24 20:57:11 -07005639 if (tg3_irq_sync(tp))
5640 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005641
Matt Carlson72334482009-08-28 14:03:01 +00005642 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005643
Matt Carlson09943a12009-08-28 14:01:57 +00005644 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005645
David S. Millerf47c11e2005-06-24 20:18:35 -07005646out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 return IRQ_RETVAL(handled);
5648}
5649
Michael Chan79381092005-04-21 17:13:59 -07005650/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005651static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005652{
Matt Carlson09943a12009-08-28 14:01:57 +00005653 struct tg3_napi *tnapi = dev_id;
5654 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005655 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005656
Michael Chanf9804dd2005-09-27 12:13:10 -07005657 if ((sblk->status & SD_STATUS_UPDATED) ||
5658 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005659 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005660 return IRQ_RETVAL(1);
5661 }
5662 return IRQ_RETVAL(0);
5663}
5664
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005665static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005666static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667
Michael Chanb9ec6c12006-07-25 16:37:27 -07005668/* Restart hardware after configuration changes, self-test, etc.
5669 * Invoked with tp->lock held.
5670 */
5671static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005672 __releases(tp->lock)
5673 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005674{
5675 int err;
5676
5677 err = tg3_init_hw(tp, reset_phy);
5678 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005679 netdev_err(tp->dev,
5680 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005681 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5682 tg3_full_unlock(tp);
5683 del_timer_sync(&tp->timer);
5684 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005685 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005686 dev_close(tp->dev);
5687 tg3_full_lock(tp, 0);
5688 }
5689 return err;
5690}
5691
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692#ifdef CONFIG_NET_POLL_CONTROLLER
5693static void tg3_poll_controller(struct net_device *dev)
5694{
Matt Carlson4f125f42009-09-01 12:55:02 +00005695 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005696 struct tg3 *tp = netdev_priv(dev);
5697
Matt Carlson4f125f42009-09-01 12:55:02 +00005698 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005699 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700}
5701#endif
5702
David Howellsc4028952006-11-22 14:57:56 +00005703static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704{
David Howellsc4028952006-11-22 14:57:56 +00005705 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005706 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 unsigned int restart_timer;
5708
Michael Chan7faa0062006-02-02 17:29:28 -08005709 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005710
5711 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005712 tg3_full_unlock(tp);
5713 return;
5714 }
5715
5716 tg3_full_unlock(tp);
5717
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005718 tg3_phy_stop(tp);
5719
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 tg3_netif_stop(tp);
5721
David S. Millerf47c11e2005-06-24 20:18:35 -07005722 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723
Joe Perches63c3a662011-04-26 08:12:10 +00005724 restart_timer = tg3_flag(tp, RESTART_TIMER);
5725 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
Joe Perches63c3a662011-04-26 08:12:10 +00005727 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07005728 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5729 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00005730 tg3_flag_set(tp, MBOX_WRITE_REORDER);
5731 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005732 }
5733
Michael Chan944d9802005-05-29 14:57:48 -07005734 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005735 err = tg3_init_hw(tp, 1);
5736 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005737 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738
5739 tg3_netif_start(tp);
5740
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 if (restart_timer)
5742 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005743
Michael Chanb9ec6c12006-07-25 16:37:27 -07005744out:
Michael Chan7faa0062006-02-02 17:29:28 -08005745 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005746
5747 if (!err)
5748 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749}
5750
5751static void tg3_tx_timeout(struct net_device *dev)
5752{
5753 struct tg3 *tp = netdev_priv(dev);
5754
Michael Chanb0408752007-02-13 12:18:30 -08005755 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005756 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005757 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759
5760 schedule_work(&tp->reset_task);
5761}
5762
Michael Chanc58ec932005-09-17 00:46:27 -07005763/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5764static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5765{
5766 u32 base = (u32) mapping & 0xffffffff;
5767
Eric Dumazet807540b2010-09-23 05:40:09 +00005768 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005769}
5770
Michael Chan72f2afb2006-03-06 19:28:35 -08005771/* Test for DMA addresses > 40-bit */
5772static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5773 int len)
5774{
5775#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00005776 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00005777 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005778 return 0;
5779#else
5780 return 0;
5781#endif
5782}
5783
Matt Carlson2ffcc982011-05-19 12:12:44 +00005784static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
5785 dma_addr_t mapping, int len, u32 flags,
5786 u32 mss_and_is_end)
5787{
5788 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
5789 int is_end = (mss_and_is_end & 0x1);
5790 u32 mss = (mss_and_is_end >> 1);
5791 u32 vlan_tag = 0;
5792
5793 if (is_end)
5794 flags |= TXD_FLAG_END;
5795 if (flags & TXD_FLAG_VLAN) {
5796 vlan_tag = flags >> 16;
5797 flags &= 0xffff;
5798 }
5799 vlan_tag |= (mss << TXD_MSS_SHIFT);
5800
5801 txd->addr_hi = ((u64) mapping >> 32);
5802 txd->addr_lo = ((u64) mapping & 0xffffffff);
5803 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5804 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5805}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
Matt Carlson432aa7e2011-05-19 12:12:45 +00005807static void tg3_skb_error_unmap(struct tg3_napi *tnapi,
5808 struct sk_buff *skb, int last)
5809{
5810 int i;
5811 u32 entry = tnapi->tx_prod;
5812 struct ring_info *txb = &tnapi->tx_buffers[entry];
5813
5814 pci_unmap_single(tnapi->tp->pdev,
5815 dma_unmap_addr(txb, mapping),
5816 skb_headlen(skb),
5817 PCI_DMA_TODEVICE);
Matt Carlson9a2e0fb2011-06-02 13:01:39 +00005818 for (i = 0; i < last; i++) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005819 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5820
5821 entry = NEXT_TX(entry);
5822 txb = &tnapi->tx_buffers[entry];
5823
5824 pci_unmap_page(tnapi->tp->pdev,
5825 dma_unmap_addr(txb, mapping),
5826 frag->size, PCI_DMA_TODEVICE);
5827 }
5828}
5829
Michael Chan72f2afb2006-03-06 19:28:35 -08005830/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005831static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
Matt Carlson432aa7e2011-05-19 12:12:45 +00005832 struct sk_buff *skb,
5833 u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005835 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005836 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005837 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005838 u32 entry = tnapi->tx_prod;
5839 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840
Matt Carlson41588ba2008-04-19 18:12:33 -07005841 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5842 new_skb = skb_copy(skb, GFP_ATOMIC);
5843 else {
5844 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5845
5846 new_skb = skb_copy_expand(skb,
5847 skb_headroom(skb) + more_headroom,
5848 skb_tailroom(skb), GFP_ATOMIC);
5849 }
5850
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005852 ret = -1;
5853 } else {
5854 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005855 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5856 PCI_DMA_TODEVICE);
5857 /* Make sure the mapping succeeded */
5858 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5859 ret = -1;
5860 dev_kfree_skb(new_skb);
David S. Miller90079ce2008-09-11 04:52:51 -07005861
Michael Chanc58ec932005-09-17 00:46:27 -07005862 /* Make sure new skb does not cross any 4G boundaries.
5863 * Drop the packet if it does.
5864 */
Matt Carlsoneb69d562011-06-13 13:38:57 +00005865 } else if (tg3_4g_overflow_test(new_addr, new_skb->len)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005866 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5867 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005868 ret = -1;
5869 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07005870 } else {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005871 tnapi->tx_buffers[entry].skb = new_skb;
5872 dma_unmap_addr_set(&tnapi->tx_buffers[entry],
5873 mapping, new_addr);
5874
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005875 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005876 base_flags, 1 | (mss << 1));
Michael Chanc58ec932005-09-17 00:46:27 -07005877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 }
5879
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 dev_kfree_skb(skb);
5881
Michael Chanc58ec932005-09-17 00:46:27 -07005882 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883}
5884
Matt Carlson2ffcc982011-05-19 12:12:44 +00005885static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005886
5887/* Use GSO to workaround a rare TSO bug that may be triggered when the
5888 * TSO header is greater than 80 bytes.
5889 */
5890static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5891{
5892 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005893 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005894
5895 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005896 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005897 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005898
5899 /* netif_tx_stop_queue() must be done before checking
5900 * checking tx index in tg3_tx_avail() below, because in
5901 * tg3_tx(), we update tx index before checking for
5902 * netif_tx_queue_stopped().
5903 */
5904 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005905 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005906 return NETDEV_TX_BUSY;
5907
5908 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005909 }
5910
5911 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005912 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005913 goto tg3_tso_bug_end;
5914
5915 do {
5916 nskb = segs;
5917 segs = segs->next;
5918 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00005919 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005920 } while (segs);
5921
5922tg3_tso_bug_end:
5923 dev_kfree_skb(skb);
5924
5925 return NETDEV_TX_OK;
5926}
Michael Chan52c0fd82006-06-29 20:15:54 -07005927
Michael Chan5a6f3072006-03-20 22:28:05 -08005928/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00005929 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005930 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00005931static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005932{
5933 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005934 u32 len, entry, base_flags, mss;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005935 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005936 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005937 struct tg3_napi *tnapi;
5938 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005939 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005940
Matt Carlson24f4efd2009-11-13 13:03:35 +00005941 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5942 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00005943 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005944 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945
Michael Chan00b70502006-06-17 21:58:45 -07005946 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005947 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005948 * interrupt. Furthermore, IRQ processing runs lockless so we have
5949 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005951 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005952 if (!netif_tx_queue_stopped(txq)) {
5953 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005954
5955 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005956 netdev_err(dev,
5957 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 return NETDEV_TX_BUSY;
5960 }
5961
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005962 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005964 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005966
Matt Carlsonbe98da62010-07-11 09:31:46 +00005967 mss = skb_shinfo(skb)->gso_size;
5968 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005969 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005970 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971
5972 if (skb_header_cloned(skb) &&
5973 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5974 dev_kfree_skb(skb);
5975 goto out_unlock;
5976 }
5977
Matt Carlson34195c32010-07-11 09:31:42 +00005978 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005979 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980
Matt Carlson02e96082010-09-15 08:59:59 +00005981 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00005982 hdr_len = skb_headlen(skb) - ETH_HLEN;
5983 } else {
5984 u32 ip_tcp_len;
5985
5986 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5987 hdr_len = ip_tcp_len + tcp_opt_len;
5988
5989 iph->check = 0;
5990 iph->tot_len = htons(mss + hdr_len);
5991 }
5992
Michael Chan52c0fd82006-06-29 20:15:54 -07005993 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00005994 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005995 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005996
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5998 TXD_FLAG_CPU_POST_DMA);
5999
Joe Perches63c3a662011-04-26 08:12:10 +00006000 if (tg3_flag(tp, HW_TSO_1) ||
6001 tg3_flag(tp, HW_TSO_2) ||
6002 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006003 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006005 } else
6006 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6007 iph->daddr, 0,
6008 IPPROTO_TCP,
6009 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
Joe Perches63c3a662011-04-26 08:12:10 +00006011 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006012 mss |= (hdr_len & 0xc) << 12;
6013 if (hdr_len & 0x10)
6014 base_flags |= 0x00000010;
6015 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006016 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006017 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006018 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006019 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006020 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 int tsflags;
6022
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006023 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 mss |= (tsflags << 11);
6025 }
6026 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006027 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 int tsflags;
6029
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006030 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 base_flags |= tsflags << 12;
6032 }
6033 }
6034 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006035
Jesse Grosseab6d182010-10-20 13:56:03 +00006036 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 base_flags |= (TXD_FLAG_VLAN |
6038 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
Joe Perches63c3a662011-04-26 08:12:10 +00006040 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006041 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006042 base_flags |= TXD_FLAG_JMB_PKT;
6043
Alexander Duyckf4188d82009-12-02 16:48:38 +00006044 len = skb_headlen(skb);
6045
6046 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6047 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006048 dev_kfree_skb(skb);
6049 goto out_unlock;
6050 }
6051
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006052 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006053 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054
6055 would_hit_hwbug = 0;
6056
Joe Perches63c3a662011-04-26 08:12:10 +00006057 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006058 would_hit_hwbug = 1;
6059
Matt Carlsoneb69d562011-06-13 13:38:57 +00006060 if (tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006061 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006062
Matt Carlsondaf9a552011-06-13 13:38:56 +00006063 if (tg3_40bit_overflow_test(tp, mapping, len))
Matt Carlson0e1406d2009-11-02 12:33:33 +00006064 would_hit_hwbug = 1;
6065
Joe Perches63c3a662011-04-26 08:12:10 +00006066 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006067 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006069 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6071
6072 entry = NEXT_TX(entry);
6073
6074 /* Now loop through additional data fragments, and queue them. */
6075 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 last = skb_shinfo(skb)->nr_frags - 1;
6077 for (i = 0; i <= last; i++) {
6078 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6079
6080 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006081 mapping = pci_map_page(tp->pdev,
6082 frag->page,
6083 frag->page_offset,
6084 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006086 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006087 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006088 mapping);
6089 if (pci_dma_mapping_error(tp->pdev, mapping))
6090 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091
Joe Perches63c3a662011-04-26 08:12:10 +00006092 if (tg3_flag(tp, SHORT_DMA_BUG) &&
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006093 len <= 8)
6094 would_hit_hwbug = 1;
6095
Matt Carlsoneb69d562011-06-13 13:38:57 +00006096 if (tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006097 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098
Matt Carlsondaf9a552011-06-13 13:38:56 +00006099 if (tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006100 would_hit_hwbug = 1;
6101
Joe Perches63c3a662011-04-26 08:12:10 +00006102 if (tg3_flag(tp, HW_TSO_1) ||
6103 tg3_flag(tp, HW_TSO_2) ||
6104 tg3_flag(tp, HW_TSO_3))
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006105 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 base_flags, (i == last)|(mss << 1));
6107 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006108 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 base_flags, (i == last));
6110
6111 entry = NEXT_TX(entry);
6112 }
6113 }
6114
6115 if (would_hit_hwbug) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00006116 tg3_skb_error_unmap(tnapi, skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117
6118 /* If the workaround fails due to memory/mapping
6119 * failure, silently drop this packet.
6120 */
Matt Carlson432aa7e2011-05-19 12:12:45 +00006121 if (tigon3_dma_hwbug_workaround(tnapi, skb, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 goto out_unlock;
6123
Matt Carlson432aa7e2011-05-19 12:12:45 +00006124 entry = NEXT_TX(tnapi->tx_prod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 }
6126
Richard Cochrand515b452011-06-19 03:31:41 +00006127 skb_tx_timestamp(skb);
6128
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006130 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006132 tnapi->tx_prod = entry;
6133 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006134 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006135
6136 /* netif_tx_stop_queue() must be done before checking
6137 * checking tx index in tg3_tx_avail() below, because in
6138 * tg3_tx(), we update tx index before checking for
6139 * netif_tx_queue_stopped().
6140 */
6141 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006142 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006143 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145
6146out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006147 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
6149 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006150
6151dma_error:
Matt Carlson432aa7e2011-05-19 12:12:45 +00006152 tg3_skb_error_unmap(tnapi, skb, i);
Alexander Duyckf4188d82009-12-02 16:48:38 +00006153 dev_kfree_skb(skb);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006154 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006155 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156}
6157
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006158static void tg3_set_loopback(struct net_device *dev, u32 features)
6159{
6160 struct tg3 *tp = netdev_priv(dev);
6161
6162 if (features & NETIF_F_LOOPBACK) {
6163 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6164 return;
6165
6166 /*
6167 * Clear MAC_MODE_HALF_DUPLEX or you won't get packets back in
6168 * loopback mode if Half-Duplex mode was negotiated earlier.
6169 */
6170 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
6171
6172 /* Enable internal MAC loopback mode */
6173 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6174 spin_lock_bh(&tp->lock);
6175 tw32(MAC_MODE, tp->mac_mode);
6176 netif_carrier_on(tp->dev);
6177 spin_unlock_bh(&tp->lock);
6178 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6179 } else {
6180 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6181 return;
6182
6183 /* Disable internal MAC loopback mode */
6184 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6185 spin_lock_bh(&tp->lock);
6186 tw32(MAC_MODE, tp->mac_mode);
6187 /* Force link status check */
6188 tg3_setup_phy(tp, 1);
6189 spin_unlock_bh(&tp->lock);
6190 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6191 }
6192}
6193
Michał Mirosławdc668912011-04-07 03:35:07 +00006194static u32 tg3_fix_features(struct net_device *dev, u32 features)
6195{
6196 struct tg3 *tp = netdev_priv(dev);
6197
Joe Perches63c3a662011-04-26 08:12:10 +00006198 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006199 features &= ~NETIF_F_ALL_TSO;
6200
6201 return features;
6202}
6203
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006204static int tg3_set_features(struct net_device *dev, u32 features)
6205{
6206 u32 changed = dev->features ^ features;
6207
6208 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6209 tg3_set_loopback(dev, features);
6210
6211 return 0;
6212}
6213
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6215 int new_mtu)
6216{
6217 dev->mtu = new_mtu;
6218
Michael Chanef7f5ec2005-07-25 12:32:25 -07006219 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006220 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006221 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006222 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00006223 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006224 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00006225 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006226 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006227 if (tg3_flag(tp, 5780_CLASS)) {
6228 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00006229 netdev_update_features(dev);
6230 }
Joe Perches63c3a662011-04-26 08:12:10 +00006231 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233}
6234
6235static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6236{
6237 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006238 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239
6240 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6241 return -EINVAL;
6242
6243 if (!netif_running(dev)) {
6244 /* We'll just catch it later when the
6245 * device is up'd.
6246 */
6247 tg3_set_mtu(dev, tp, new_mtu);
6248 return 0;
6249 }
6250
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006251 tg3_phy_stop(tp);
6252
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006254
6255 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256
Michael Chan944d9802005-05-29 14:57:48 -07006257 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258
6259 tg3_set_mtu(dev, tp, new_mtu);
6260
Michael Chanb9ec6c12006-07-25 16:37:27 -07006261 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262
Michael Chanb9ec6c12006-07-25 16:37:27 -07006263 if (!err)
6264 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265
David S. Millerf47c11e2005-06-24 20:18:35 -07006266 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006268 if (!err)
6269 tg3_phy_start(tp);
6270
Michael Chanb9ec6c12006-07-25 16:37:27 -07006271 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272}
6273
Matt Carlson21f581a2009-08-28 14:00:25 +00006274static void tg3_rx_prodring_free(struct tg3 *tp,
6275 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 int i;
6278
Matt Carlson8fea32b2010-09-15 08:59:58 +00006279 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006280 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006281 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006282 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6283 tp->rx_pkt_map_sz);
6284
Joe Perches63c3a662011-04-26 08:12:10 +00006285 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006286 for (i = tpr->rx_jmb_cons_idx;
6287 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006288 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006289 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6290 TG3_RX_JMB_MAP_SZ);
6291 }
6292 }
6293
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006294 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296
Matt Carlson2c49a442010-09-30 10:34:35 +00006297 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006298 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6299 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300
Joe Perches63c3a662011-04-26 08:12:10 +00006301 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006302 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006303 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6304 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 }
6306}
6307
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006308/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 *
6310 * The chip has been shut down and the driver detached from
6311 * the networking, so no interrupts or new tx packets will
6312 * end up in the driver. tp->{tx,}lock are held and thus
6313 * we may not sleep.
6314 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006315static int tg3_rx_prodring_alloc(struct tg3 *tp,
6316 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317{
Matt Carlson287be122009-08-28 13:58:46 +00006318 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006320 tpr->rx_std_cons_idx = 0;
6321 tpr->rx_std_prod_idx = 0;
6322 tpr->rx_jmb_cons_idx = 0;
6323 tpr->rx_jmb_prod_idx = 0;
6324
Matt Carlson8fea32b2010-09-15 08:59:58 +00006325 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006326 memset(&tpr->rx_std_buffers[0], 0,
6327 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006328 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006329 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006330 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006331 goto done;
6332 }
6333
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006335 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336
Matt Carlson287be122009-08-28 13:58:46 +00006337 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00006338 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006339 tp->dev->mtu > ETH_DATA_LEN)
6340 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6341 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006342
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 /* Initialize invariants of the rings, we only set this
6344 * stuff once. This works because the card does not
6345 * write into the rx buffer posting rings.
6346 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006347 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 struct tg3_rx_buffer_desc *rxd;
6349
Matt Carlson21f581a2009-08-28 14:00:25 +00006350 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006351 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6353 rxd->opaque = (RXD_OPAQUE_RING_STD |
6354 (i << RXD_OPAQUE_INDEX_SHIFT));
6355 }
6356
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006357 /* Now allocate fresh SKBs for each rx ring. */
6358 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006359 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006360 netdev_warn(tp->dev,
6361 "Using a smaller RX standard ring. Only "
6362 "%d out of %d buffers were allocated "
6363 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006364 if (i == 0)
6365 goto initfail;
6366 tp->rx_pending = i;
6367 break;
6368 }
6369 }
6370
Joe Perches63c3a662011-04-26 08:12:10 +00006371 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006372 goto done;
6373
Matt Carlson2c49a442010-09-30 10:34:35 +00006374 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006375
Joe Perches63c3a662011-04-26 08:12:10 +00006376 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00006377 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378
Matt Carlson2c49a442010-09-30 10:34:35 +00006379 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006380 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381
Matt Carlson0d86df82010-02-17 15:17:00 +00006382 rxd = &tpr->rx_jmb[i].std;
6383 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6384 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6385 RXD_FLAG_JUMBO;
6386 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6387 (i << RXD_OPAQUE_INDEX_SHIFT));
6388 }
6389
6390 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6391 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006392 netdev_warn(tp->dev,
6393 "Using a smaller RX jumbo ring. Only %d "
6394 "out of %d buffers were allocated "
6395 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006396 if (i == 0)
6397 goto initfail;
6398 tp->rx_jumbo_pending = i;
6399 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 }
6401 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006402
6403done:
Michael Chan32d8c572006-07-25 16:38:29 -07006404 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006405
6406initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006407 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006408 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409}
6410
Matt Carlson21f581a2009-08-28 14:00:25 +00006411static void tg3_rx_prodring_fini(struct tg3 *tp,
6412 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413{
Matt Carlson21f581a2009-08-28 14:00:25 +00006414 kfree(tpr->rx_std_buffers);
6415 tpr->rx_std_buffers = NULL;
6416 kfree(tpr->rx_jmb_buffers);
6417 tpr->rx_jmb_buffers = NULL;
6418 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006419 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6420 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006421 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006423 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006424 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6425 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006426 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006428}
6429
Matt Carlson21f581a2009-08-28 14:00:25 +00006430static int tg3_rx_prodring_init(struct tg3 *tp,
6431 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006432{
Matt Carlson2c49a442010-09-30 10:34:35 +00006433 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6434 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006435 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006436 return -ENOMEM;
6437
Matt Carlson4bae65c2010-11-24 08:31:52 +00006438 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6439 TG3_RX_STD_RING_BYTES(tp),
6440 &tpr->rx_std_mapping,
6441 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006442 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006443 goto err_out;
6444
Joe Perches63c3a662011-04-26 08:12:10 +00006445 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006446 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006447 GFP_KERNEL);
6448 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006449 goto err_out;
6450
Matt Carlson4bae65c2010-11-24 08:31:52 +00006451 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6452 TG3_RX_JMB_RING_BYTES(tp),
6453 &tpr->rx_jmb_mapping,
6454 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006455 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006456 goto err_out;
6457 }
6458
6459 return 0;
6460
6461err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006462 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006463 return -ENOMEM;
6464}
6465
6466/* Free up pending packets in all rx/tx rings.
6467 *
6468 * The chip has been shut down and the driver detached from
6469 * the networking, so no interrupts or new tx packets will
6470 * end up in the driver. tp->{tx,}lock is not held and we are not
6471 * in an interrupt context and thus may sleep.
6472 */
6473static void tg3_free_rings(struct tg3 *tp)
6474{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006475 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006476
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006477 for (j = 0; j < tp->irq_cnt; j++) {
6478 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006479
Matt Carlson8fea32b2010-09-15 08:59:58 +00006480 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006481
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006482 if (!tnapi->tx_buffers)
6483 continue;
6484
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006485 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006486 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006487 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006488 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006489
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006490 txp = &tnapi->tx_buffers[i];
6491 skb = txp->skb;
6492
6493 if (skb == NULL) {
6494 i++;
6495 continue;
6496 }
6497
Alexander Duyckf4188d82009-12-02 16:48:38 +00006498 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006499 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006500 skb_headlen(skb),
6501 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006502 txp->skb = NULL;
6503
Alexander Duyckf4188d82009-12-02 16:48:38 +00006504 i++;
6505
6506 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6507 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6508 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006509 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006510 skb_shinfo(skb)->frags[k].size,
6511 PCI_DMA_TODEVICE);
6512 i++;
6513 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006514
6515 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006516 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006517 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006518}
6519
6520/* Initialize tx/rx rings for packet processing.
6521 *
6522 * The chip has been shut down and the driver detached from
6523 * the networking, so no interrupts or new tx packets will
6524 * end up in the driver. tp->{tx,}lock are held and thus
6525 * we may not sleep.
6526 */
6527static int tg3_init_rings(struct tg3 *tp)
6528{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006529 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006530
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006531 /* Free up all the SKBs. */
6532 tg3_free_rings(tp);
6533
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006534 for (i = 0; i < tp->irq_cnt; i++) {
6535 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006536
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006537 tnapi->last_tag = 0;
6538 tnapi->last_irq_tag = 0;
6539 tnapi->hw_status->status = 0;
6540 tnapi->hw_status->status_tag = 0;
6541 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6542
6543 tnapi->tx_prod = 0;
6544 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006545 if (tnapi->tx_ring)
6546 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006547
6548 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006549 if (tnapi->rx_rcb)
6550 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006551
Matt Carlson8fea32b2010-09-15 08:59:58 +00006552 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006553 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006554 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006555 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006556 }
Matt Carlson72334482009-08-28 14:03:01 +00006557
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006558 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006559}
6560
6561/*
6562 * Must not be invoked with interrupt sources disabled and
6563 * the hardware shutdown down.
6564 */
6565static void tg3_free_consistent(struct tg3 *tp)
6566{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006567 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006568
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006569 for (i = 0; i < tp->irq_cnt; i++) {
6570 struct tg3_napi *tnapi = &tp->napi[i];
6571
6572 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006573 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006574 tnapi->tx_ring, tnapi->tx_desc_mapping);
6575 tnapi->tx_ring = NULL;
6576 }
6577
6578 kfree(tnapi->tx_buffers);
6579 tnapi->tx_buffers = NULL;
6580
6581 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006582 dma_free_coherent(&tp->pdev->dev,
6583 TG3_RX_RCB_RING_BYTES(tp),
6584 tnapi->rx_rcb,
6585 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006586 tnapi->rx_rcb = NULL;
6587 }
6588
Matt Carlson8fea32b2010-09-15 08:59:58 +00006589 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6590
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006591 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006592 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6593 tnapi->hw_status,
6594 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006595 tnapi->hw_status = NULL;
6596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006598
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006600 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6601 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 tp->hw_stats = NULL;
6603 }
6604}
6605
6606/*
6607 * Must not be invoked with interrupt sources disabled and
6608 * the hardware shutdown down. Can sleep.
6609 */
6610static int tg3_alloc_consistent(struct tg3 *tp)
6611{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006612 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006613
Matt Carlson4bae65c2010-11-24 08:31:52 +00006614 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6615 sizeof(struct tg3_hw_stats),
6616 &tp->stats_mapping,
6617 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 if (!tp->hw_stats)
6619 goto err_out;
6620
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6622
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006623 for (i = 0; i < tp->irq_cnt; i++) {
6624 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006625 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006626
Matt Carlson4bae65c2010-11-24 08:31:52 +00006627 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6628 TG3_HW_STATUS_SIZE,
6629 &tnapi->status_mapping,
6630 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006631 if (!tnapi->hw_status)
6632 goto err_out;
6633
6634 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006635 sblk = tnapi->hw_status;
6636
Matt Carlson8fea32b2010-09-15 08:59:58 +00006637 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6638 goto err_out;
6639
Matt Carlson19cfaec2009-12-03 08:36:20 +00006640 /* If multivector TSS is enabled, vector 0 does not handle
6641 * tx interrupts. Don't allocate any resources for it.
6642 */
Joe Perches63c3a662011-04-26 08:12:10 +00006643 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
6644 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00006645 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6646 TG3_TX_RING_SIZE,
6647 GFP_KERNEL);
6648 if (!tnapi->tx_buffers)
6649 goto err_out;
6650
Matt Carlson4bae65c2010-11-24 08:31:52 +00006651 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6652 TG3_TX_RING_BYTES,
6653 &tnapi->tx_desc_mapping,
6654 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006655 if (!tnapi->tx_ring)
6656 goto err_out;
6657 }
6658
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006659 /*
6660 * When RSS is enabled, the status block format changes
6661 * slightly. The "rx_jumbo_consumer", "reserved",
6662 * and "rx_mini_consumer" members get mapped to the
6663 * other three rx return ring producer indexes.
6664 */
6665 switch (i) {
6666 default:
6667 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6668 break;
6669 case 2:
6670 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6671 break;
6672 case 3:
6673 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6674 break;
6675 case 4:
6676 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6677 break;
6678 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006679
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006680 /*
6681 * If multivector RSS is enabled, vector 0 does not handle
6682 * rx or tx interrupts. Don't allocate any resources for it.
6683 */
Joe Perches63c3a662011-04-26 08:12:10 +00006684 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006685 continue;
6686
Matt Carlson4bae65c2010-11-24 08:31:52 +00006687 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6688 TG3_RX_RCB_RING_BYTES(tp),
6689 &tnapi->rx_rcb_mapping,
6690 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006691 if (!tnapi->rx_rcb)
6692 goto err_out;
6693
6694 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006695 }
6696
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 return 0;
6698
6699err_out:
6700 tg3_free_consistent(tp);
6701 return -ENOMEM;
6702}
6703
6704#define MAX_WAIT_CNT 1000
6705
6706/* To stop a block, clear the enable bit and poll till it
6707 * clears. tp->lock is held.
6708 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006709static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710{
6711 unsigned int i;
6712 u32 val;
6713
Joe Perches63c3a662011-04-26 08:12:10 +00006714 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 switch (ofs) {
6716 case RCVLSC_MODE:
6717 case DMAC_MODE:
6718 case MBFREE_MODE:
6719 case BUFMGR_MODE:
6720 case MEMARB_MODE:
6721 /* We can't enable/disable these bits of the
6722 * 5705/5750, just say success.
6723 */
6724 return 0;
6725
6726 default:
6727 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 }
6730
6731 val = tr32(ofs);
6732 val &= ~enable_bit;
6733 tw32_f(ofs, val);
6734
6735 for (i = 0; i < MAX_WAIT_CNT; i++) {
6736 udelay(100);
6737 val = tr32(ofs);
6738 if ((val & enable_bit) == 0)
6739 break;
6740 }
6741
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006742 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006743 dev_err(&tp->pdev->dev,
6744 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6745 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 return -ENODEV;
6747 }
6748
6749 return 0;
6750}
6751
6752/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006753static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754{
6755 int i, err;
6756
6757 tg3_disable_ints(tp);
6758
6759 tp->rx_mode &= ~RX_MODE_ENABLE;
6760 tw32_f(MAC_RX_MODE, tp->rx_mode);
6761 udelay(10);
6762
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006763 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6764 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6765 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6766 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6767 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6768 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006770 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6771 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6772 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6773 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6774 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6775 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6776 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777
6778 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6779 tw32_f(MAC_MODE, tp->mac_mode);
6780 udelay(40);
6781
6782 tp->tx_mode &= ~TX_MODE_ENABLE;
6783 tw32_f(MAC_TX_MODE, tp->tx_mode);
6784
6785 for (i = 0; i < MAX_WAIT_CNT; i++) {
6786 udelay(100);
6787 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6788 break;
6789 }
6790 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006791 dev_err(&tp->pdev->dev,
6792 "%s timed out, TX_MODE_ENABLE will not clear "
6793 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006794 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 }
6796
Michael Chane6de8ad2005-05-05 14:42:41 -07006797 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006798 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6799 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800
6801 tw32(FTQ_RESET, 0xffffffff);
6802 tw32(FTQ_RESET, 0x00000000);
6803
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006804 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6805 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006807 for (i = 0; i < tp->irq_cnt; i++) {
6808 struct tg3_napi *tnapi = &tp->napi[i];
6809 if (tnapi->hw_status)
6810 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 if (tp->hw_stats)
6813 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6814
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 return err;
6816}
6817
Matt Carlson0d3031d2007-10-10 18:02:43 -07006818static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6819{
6820 int i;
6821 u32 apedata;
6822
Matt Carlsondc6d0742010-09-15 08:59:55 +00006823 /* NCSI does not support APE events */
Joe Perches63c3a662011-04-26 08:12:10 +00006824 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsondc6d0742010-09-15 08:59:55 +00006825 return;
6826
Matt Carlson0d3031d2007-10-10 18:02:43 -07006827 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6828 if (apedata != APE_SEG_SIG_MAGIC)
6829 return;
6830
6831 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006832 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006833 return;
6834
6835 /* Wait for up to 1 millisecond for APE to service previous event. */
6836 for (i = 0; i < 10; i++) {
6837 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6838 return;
6839
6840 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6841
6842 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6843 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6844 event | APE_EVENT_STATUS_EVENT_PENDING);
6845
6846 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6847
6848 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6849 break;
6850
6851 udelay(100);
6852 }
6853
6854 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6855 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6856}
6857
6858static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6859{
6860 u32 event;
6861 u32 apedata;
6862
Joe Perches63c3a662011-04-26 08:12:10 +00006863 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006864 return;
6865
6866 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006867 case RESET_KIND_INIT:
6868 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6869 APE_HOST_SEG_SIG_MAGIC);
6870 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6871 APE_HOST_SEG_LEN_MAGIC);
6872 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6873 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6874 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006875 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006876 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6877 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006878 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6879 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006880
Matt Carlson33f401a2010-04-05 10:19:27 +00006881 event = APE_EVENT_STATUS_STATE_START;
6882 break;
6883 case RESET_KIND_SHUTDOWN:
6884 /* With the interface we are currently using,
6885 * APE does not track driver state. Wiping
6886 * out the HOST SEGMENT SIGNATURE forces
6887 * the APE to assume OS absent status.
6888 */
6889 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006890
Matt Carlsondc6d0742010-09-15 08:59:55 +00006891 if (device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006892 tg3_flag(tp, WOL_ENABLE)) {
Matt Carlsondc6d0742010-09-15 08:59:55 +00006893 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6894 TG3_APE_HOST_WOL_SPEED_AUTO);
6895 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6896 } else
6897 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6898
6899 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6900
Matt Carlson33f401a2010-04-05 10:19:27 +00006901 event = APE_EVENT_STATUS_STATE_UNLOAD;
6902 break;
6903 case RESET_KIND_SUSPEND:
6904 event = APE_EVENT_STATUS_STATE_SUSPEND;
6905 break;
6906 default:
6907 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006908 }
6909
6910 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6911
6912 tg3_ape_send_event(tp, event);
6913}
6914
Michael Chane6af3012005-04-21 17:12:05 -07006915/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6917{
David S. Millerf49639e2006-06-09 11:58:36 -07006918 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6919 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920
Joe Perches63c3a662011-04-26 08:12:10 +00006921 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922 switch (kind) {
6923 case RESET_KIND_INIT:
6924 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6925 DRV_STATE_START);
6926 break;
6927
6928 case RESET_KIND_SHUTDOWN:
6929 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6930 DRV_STATE_UNLOAD);
6931 break;
6932
6933 case RESET_KIND_SUSPEND:
6934 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6935 DRV_STATE_SUSPEND);
6936 break;
6937
6938 default:
6939 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006942
6943 if (kind == RESET_KIND_INIT ||
6944 kind == RESET_KIND_SUSPEND)
6945 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946}
6947
6948/* tp->lock is held. */
6949static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6950{
Joe Perches63c3a662011-04-26 08:12:10 +00006951 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952 switch (kind) {
6953 case RESET_KIND_INIT:
6954 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6955 DRV_STATE_START_DONE);
6956 break;
6957
6958 case RESET_KIND_SHUTDOWN:
6959 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6960 DRV_STATE_UNLOAD_DONE);
6961 break;
6962
6963 default:
6964 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006967
6968 if (kind == RESET_KIND_SHUTDOWN)
6969 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970}
6971
6972/* tp->lock is held. */
6973static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6974{
Joe Perches63c3a662011-04-26 08:12:10 +00006975 if (tg3_flag(tp, ENABLE_ASF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 switch (kind) {
6977 case RESET_KIND_INIT:
6978 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6979 DRV_STATE_START);
6980 break;
6981
6982 case RESET_KIND_SHUTDOWN:
6983 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6984 DRV_STATE_UNLOAD);
6985 break;
6986
6987 case RESET_KIND_SUSPEND:
6988 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6989 DRV_STATE_SUSPEND);
6990 break;
6991
6992 default:
6993 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 }
6996}
6997
Michael Chan7a6f4362006-09-27 16:03:31 -07006998static int tg3_poll_fw(struct tg3 *tp)
6999{
7000 int i;
7001 u32 val;
7002
Michael Chanb5d37722006-09-27 16:06:21 -07007003 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08007004 /* Wait up to 20ms for init done. */
7005 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07007006 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
7007 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08007008 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07007009 }
7010 return -ENODEV;
7011 }
7012
Michael Chan7a6f4362006-09-27 16:03:31 -07007013 /* Wait for firmware initialization to complete. */
7014 for (i = 0; i < 100000; i++) {
7015 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
7016 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
7017 break;
7018 udelay(10);
7019 }
7020
7021 /* Chip might not be fitted with firmware. Some Sun onboard
7022 * parts are configured like that. So don't signal the timeout
7023 * of the above loop as an error, but do report the lack of
7024 * running firmware once.
7025 */
Joe Perches63c3a662011-04-26 08:12:10 +00007026 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
7027 tg3_flag_set(tp, NO_FWARE_REPORTED);
Michael Chan7a6f4362006-09-27 16:03:31 -07007028
Joe Perches05dbe002010-02-17 19:44:19 +00007029 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07007030 }
7031
Matt Carlson6b10c162010-02-12 14:47:08 +00007032 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7033 /* The 57765 A0 needs a little more
7034 * time to do some important work.
7035 */
7036 mdelay(10);
7037 }
7038
Michael Chan7a6f4362006-09-27 16:03:31 -07007039 return 0;
7040}
7041
Michael Chanee6a99b2007-07-18 21:49:10 -07007042/* Save PCI command register before chip reset */
7043static void tg3_save_pci_state(struct tg3 *tp)
7044{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007045 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007046}
7047
7048/* Restore PCI state after chip reset */
7049static void tg3_restore_pci_state(struct tg3 *tp)
7050{
7051 u32 val;
7052
7053 /* Re-enable indirect register accesses. */
7054 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7055 tp->misc_host_ctrl);
7056
7057 /* Set MAX PCI retry to zero. */
7058 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7059 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007060 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007061 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007062 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007063 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007064 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007065 PCISTATE_ALLOW_APE_SHMEM_WR |
7066 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007067 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7068
Matt Carlson8a6eac92007-10-21 16:17:55 -07007069 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007070
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007071 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007072 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007073 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007074 else {
7075 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7076 tp->pci_cacheline_sz);
7077 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7078 tp->pci_lat_timer);
7079 }
Michael Chan114342f2007-10-15 02:12:26 -07007080 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007081
Michael Chanee6a99b2007-07-18 21:49:10 -07007082 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007083 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007084 u16 pcix_cmd;
7085
7086 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7087 &pcix_cmd);
7088 pcix_cmd &= ~PCI_X_CMD_ERO;
7089 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7090 pcix_cmd);
7091 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007092
Joe Perches63c3a662011-04-26 08:12:10 +00007093 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007094
7095 /* Chip reset on 5780 will reset MSI enable bit,
7096 * so need to restore it.
7097 */
Joe Perches63c3a662011-04-26 08:12:10 +00007098 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007099 u16 ctrl;
7100
7101 pci_read_config_word(tp->pdev,
7102 tp->msi_cap + PCI_MSI_FLAGS,
7103 &ctrl);
7104 pci_write_config_word(tp->pdev,
7105 tp->msi_cap + PCI_MSI_FLAGS,
7106 ctrl | PCI_MSI_FLAGS_ENABLE);
7107 val = tr32(MSGINT_MODE);
7108 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7109 }
7110 }
7111}
7112
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113static void tg3_stop_fw(struct tg3 *);
7114
7115/* tp->lock is held. */
7116static int tg3_chip_reset(struct tg3 *tp)
7117{
7118 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007119 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007120 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121
David S. Millerf49639e2006-06-09 11:58:36 -07007122 tg3_nvram_lock(tp);
7123
Matt Carlson77b483f2008-08-15 14:07:24 -07007124 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7125
David S. Millerf49639e2006-06-09 11:58:36 -07007126 /* No matching tg3_nvram_unlock() after this because
7127 * chip reset below will undo the nvram lock.
7128 */
7129 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130
Michael Chanee6a99b2007-07-18 21:49:10 -07007131 /* GRC_MISC_CFG core clock reset will clear the memory
7132 * enable bit in PCI register 4 and the MSI enable bit
7133 * on some chips, so we save relevant registers here.
7134 */
7135 tg3_save_pci_state(tp);
7136
Michael Chand9ab5ad12006-03-20 22:27:35 -08007137 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007138 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad12006-03-20 22:27:35 -08007139 tw32(GRC_FASTBOOT_PC, 0);
7140
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 /*
7142 * We must avoid the readl() that normally takes place.
7143 * It locks machines, causes machine checks, and other
7144 * fun things. So, temporarily disable the 5701
7145 * hardware workaround, while we do the reset.
7146 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007147 write_op = tp->write32;
7148 if (write_op == tg3_write_flush_reg32)
7149 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150
Michael Chand18edcb2007-03-24 20:57:11 -07007151 /* Prevent the irq handler from reading or writing PCI registers
7152 * during chip reset when the memory enable bit in the PCI command
7153 * register may be cleared. The chip does not generate interrupt
7154 * at this time, but the irq handler may still be called due to irq
7155 * sharing or irqpoll.
7156 */
Joe Perches63c3a662011-04-26 08:12:10 +00007157 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007158 for (i = 0; i < tp->irq_cnt; i++) {
7159 struct tg3_napi *tnapi = &tp->napi[i];
7160 if (tnapi->hw_status) {
7161 tnapi->hw_status->status = 0;
7162 tnapi->hw_status->status_tag = 0;
7163 }
7164 tnapi->last_tag = 0;
7165 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007166 }
Michael Chand18edcb2007-03-24 20:57:11 -07007167 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007168
7169 for (i = 0; i < tp->irq_cnt; i++)
7170 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007171
Matt Carlson255ca312009-08-25 10:07:27 +00007172 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7173 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7174 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7175 }
7176
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177 /* do the reset */
7178 val = GRC_MISC_CFG_CORECLK_RESET;
7179
Joe Perches63c3a662011-04-26 08:12:10 +00007180 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007181 /* Force PCIe 1.0a mode */
7182 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007183 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007184 tr32(TG3_PCIE_PHY_TSTCTL) ==
7185 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7186 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7187
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7189 tw32(GRC_MISC_CFG, (1 << 29));
7190 val |= (1 << 29);
7191 }
7192 }
7193
Michael Chanb5d37722006-09-27 16:06:21 -07007194 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7195 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7196 tw32(GRC_VCPU_EXT_CTRL,
7197 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7198 }
7199
Matt Carlsonf37500d2010-08-02 11:25:59 +00007200 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007201 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007203
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204 tw32(GRC_MISC_CFG, val);
7205
Michael Chan1ee582d2005-08-09 20:16:46 -07007206 /* restore 5701 hardware bug workaround write method */
7207 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208
7209 /* Unfortunately, we have to delay before the PCI read back.
7210 * Some 575X chips even will not respond to a PCI cfg access
7211 * when the reset command is given to the chip.
7212 *
7213 * How do these hardware designers expect things to work
7214 * properly if the PCI write is posted for a long period
7215 * of time? It is always necessary to have some method by
7216 * which a register read back can occur to push the write
7217 * out which does the reset.
7218 *
7219 * For most tg3 variants the trick below was working.
7220 * Ho hum...
7221 */
7222 udelay(120);
7223
7224 /* Flush PCI posted writes. The normal MMIO registers
7225 * are inaccessible at this time so this is the only
7226 * way to make this reliably (actually, this is no longer
7227 * the case, see above). I tried to use indirect
7228 * register read/write but this upset some 5701 variants.
7229 */
7230 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7231
7232 udelay(120);
7233
Jon Mason708ebb3a2011-06-27 12:56:50 +00007234 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007235 u16 val16;
7236
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7238 int i;
7239 u32 cfg_val;
7240
7241 /* Wait for link training to complete. */
7242 for (i = 0; i < 5000; i++)
7243 udelay(100);
7244
7245 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7246 pci_write_config_dword(tp->pdev, 0xc4,
7247 cfg_val | (1 << 15));
7248 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007249
Matt Carlsone7126992009-08-25 10:08:16 +00007250 /* Clear the "no snoop" and "relaxed ordering" bits. */
7251 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007252 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007253 &val16);
7254 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7255 PCI_EXP_DEVCTL_NOSNOOP_EN);
7256 /*
7257 * Older PCIe devices only support the 128 byte
7258 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007259 */
Joe Perches63c3a662011-04-26 08:12:10 +00007260 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007261 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007262 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007263 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007264 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007265
Matt Carlsoncf790032010-11-24 08:31:48 +00007266 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007267
7268 /* Clear error status */
7269 pci_write_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +00007270 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007271 PCI_EXP_DEVSTA_CED |
7272 PCI_EXP_DEVSTA_NFED |
7273 PCI_EXP_DEVSTA_FED |
7274 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275 }
7276
Michael Chanee6a99b2007-07-18 21:49:10 -07007277 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278
Joe Perches63c3a662011-04-26 08:12:10 +00007279 tg3_flag_clear(tp, CHIP_RESETTING);
7280 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007281
Michael Chanee6a99b2007-07-18 21:49:10 -07007282 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007283 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007284 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007285 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286
7287 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7288 tg3_stop_fw(tp);
7289 tw32(0x5000, 0x400);
7290 }
7291
7292 tw32(GRC_MODE, tp->grc_mode);
7293
7294 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007295 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296
7297 tw32(0xc4, val | (1 << 15));
7298 }
7299
7300 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7301 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7302 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7303 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7304 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7305 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7306 }
7307
Joe Perches63c3a662011-04-26 08:12:10 +00007308 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007309 tp->mac_mode = MAC_MODE_APE_TX_EN |
7310 MAC_MODE_APE_RX_EN |
7311 MAC_MODE_TDE_ENABLE;
7312
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007313 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007314 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7315 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007316 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007317 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7318 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007320 val = 0;
7321
7322 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323 udelay(40);
7324
Matt Carlson77b483f2008-08-15 14:07:24 -07007325 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7326
Michael Chan7a6f4362006-09-27 16:03:31 -07007327 err = tg3_poll_fw(tp);
7328 if (err)
7329 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007330
Matt Carlson0a9140c2009-08-28 12:27:50 +00007331 tg3_mdio_start(tp);
7332
Joe Perches63c3a662011-04-26 08:12:10 +00007333 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007334 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7335 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007336 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007337 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338
7339 tw32(0x7c00, val | (1 << 25));
7340 }
7341
Matt Carlsond78b59f2011-04-05 14:22:46 +00007342 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7343 val = tr32(TG3_CPMU_CLCK_ORIDE);
7344 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7345 }
7346
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007348 tg3_flag_clear(tp, ENABLE_ASF);
7349 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7351 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7352 u32 nic_cfg;
7353
7354 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7355 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007356 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007357 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007358 if (tg3_flag(tp, 5750_PLUS))
7359 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 }
7361 }
7362
7363 return 0;
7364}
7365
7366/* tp->lock is held. */
7367static void tg3_stop_fw(struct tg3 *tp)
7368{
Joe Perches63c3a662011-04-26 08:12:10 +00007369 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007370 /* Wait for RX cpu to ACK the previous event. */
7371 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372
7373 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007374
7375 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376
Matt Carlson7c5026a2008-05-02 16:49:29 -07007377 /* Wait for RX cpu to ACK this event. */
7378 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379 }
7380}
7381
7382/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007383static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384{
7385 int err;
7386
7387 tg3_stop_fw(tp);
7388
Michael Chan944d9802005-05-29 14:57:48 -07007389 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007391 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392 err = tg3_chip_reset(tp);
7393
Matt Carlsondaba2a62009-04-20 06:58:52 +00007394 __tg3_set_mac_addr(tp, 0);
7395
Michael Chan944d9802005-05-29 14:57:48 -07007396 tg3_write_sig_legacy(tp, kind);
7397 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398
7399 if (err)
7400 return err;
7401
7402 return 0;
7403}
7404
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405#define RX_CPU_SCRATCH_BASE 0x30000
7406#define RX_CPU_SCRATCH_SIZE 0x04000
7407#define TX_CPU_SCRATCH_BASE 0x34000
7408#define TX_CPU_SCRATCH_SIZE 0x04000
7409
7410/* tp->lock is held. */
7411static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7412{
7413 int i;
7414
Joe Perches63c3a662011-04-26 08:12:10 +00007415 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416
Michael Chanb5d37722006-09-27 16:06:21 -07007417 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7418 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7419
7420 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7421 return 0;
7422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 if (offset == RX_CPU_BASE) {
7424 for (i = 0; i < 10000; i++) {
7425 tw32(offset + CPU_STATE, 0xffffffff);
7426 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7427 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7428 break;
7429 }
7430
7431 tw32(offset + CPU_STATE, 0xffffffff);
7432 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7433 udelay(10);
7434 } else {
7435 for (i = 0; i < 10000; i++) {
7436 tw32(offset + CPU_STATE, 0xffffffff);
7437 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7438 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7439 break;
7440 }
7441 }
7442
7443 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007444 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7445 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446 return -ENODEV;
7447 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007448
7449 /* Clear firmware's nvram arbitration. */
Joe Perches63c3a662011-04-26 08:12:10 +00007450 if (tg3_flag(tp, NVRAM))
Michael Chanec41c7d2006-01-17 02:40:55 -08007451 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452 return 0;
7453}
7454
7455struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007456 unsigned int fw_base;
7457 unsigned int fw_len;
7458 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459};
7460
7461/* tp->lock is held. */
7462static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7463 int cpu_scratch_size, struct fw_info *info)
7464{
Michael Chanec41c7d2006-01-17 02:40:55 -08007465 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466 void (*write_op)(struct tg3 *, u32, u32);
7467
Joe Perches63c3a662011-04-26 08:12:10 +00007468 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007469 netdev_err(tp->dev,
7470 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007471 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 return -EINVAL;
7473 }
7474
Joe Perches63c3a662011-04-26 08:12:10 +00007475 if (tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 write_op = tg3_write_mem;
7477 else
7478 write_op = tg3_write_indirect_reg32;
7479
Michael Chan1b628152005-05-29 14:59:49 -07007480 /* It is possible that bootcode is still loading at this point.
7481 * Get the nvram lock first before halting the cpu.
7482 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007483 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007485 if (!lock_err)
7486 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487 if (err)
7488 goto out;
7489
7490 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7491 write_op(tp, cpu_scratch_base + i, 0);
7492 tw32(cpu_base + CPU_STATE, 0xffffffff);
7493 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007494 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007496 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007498 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499
7500 err = 0;
7501
7502out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 return err;
7504}
7505
7506/* tp->lock is held. */
7507static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7508{
7509 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007510 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511 int err, i;
7512
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007513 fw_data = (void *)tp->fw->data;
7514
7515 /* Firmware blob starts with version numbers, followed by
7516 start address and length. We are setting complete length.
7517 length = end_address_of_bss - start_address_of_text.
7518 Remainder is the blob to be loaded contiguously
7519 from start address. */
7520
7521 info.fw_base = be32_to_cpu(fw_data[1]);
7522 info.fw_len = tp->fw->size - 12;
7523 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524
7525 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7526 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7527 &info);
7528 if (err)
7529 return err;
7530
7531 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7532 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7533 &info);
7534 if (err)
7535 return err;
7536
7537 /* Now startup only the RX cpu. */
7538 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007539 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007540
7541 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007542 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007543 break;
7544 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7545 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007546 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 udelay(1000);
7548 }
7549 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007550 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7551 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007552 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553 return -ENODEV;
7554 }
7555 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7556 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7557
7558 return 0;
7559}
7560
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561/* tp->lock is held. */
7562static int tg3_load_tso_firmware(struct tg3 *tp)
7563{
7564 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007565 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7567 int err, i;
7568
Joe Perches63c3a662011-04-26 08:12:10 +00007569 if (tg3_flag(tp, HW_TSO_1) ||
7570 tg3_flag(tp, HW_TSO_2) ||
7571 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007572 return 0;
7573
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007574 fw_data = (void *)tp->fw->data;
7575
7576 /* Firmware blob starts with version numbers, followed by
7577 start address and length. We are setting complete length.
7578 length = end_address_of_bss - start_address_of_text.
7579 Remainder is the blob to be loaded contiguously
7580 from start address. */
7581
7582 info.fw_base = be32_to_cpu(fw_data[1]);
7583 cpu_scratch_size = tp->fw_len;
7584 info.fw_len = tp->fw->size - 12;
7585 info.fw_data = &fw_data[3];
7586
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588 cpu_base = RX_CPU_BASE;
7589 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007590 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 cpu_base = TX_CPU_BASE;
7592 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7593 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7594 }
7595
7596 err = tg3_load_firmware_cpu(tp, cpu_base,
7597 cpu_scratch_base, cpu_scratch_size,
7598 &info);
7599 if (err)
7600 return err;
7601
7602 /* Now startup the cpu. */
7603 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007604 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007605
7606 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007607 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007608 break;
7609 tw32(cpu_base + CPU_STATE, 0xffffffff);
7610 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007611 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612 udelay(1000);
7613 }
7614 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007615 netdev_err(tp->dev,
7616 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007617 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007618 return -ENODEV;
7619 }
7620 tw32(cpu_base + CPU_STATE, 0xffffffff);
7621 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7622 return 0;
7623}
7624
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626static int tg3_set_mac_addr(struct net_device *dev, void *p)
7627{
7628 struct tg3 *tp = netdev_priv(dev);
7629 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007630 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631
Michael Chanf9804dd2005-09-27 12:13:10 -07007632 if (!is_valid_ether_addr(addr->sa_data))
7633 return -EINVAL;
7634
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7636
Michael Chane75f7c92006-03-20 21:33:26 -08007637 if (!netif_running(dev))
7638 return 0;
7639
Joe Perches63c3a662011-04-26 08:12:10 +00007640 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007641 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007642
Michael Chan986e0ae2007-05-05 12:10:20 -07007643 addr0_high = tr32(MAC_ADDR_0_HIGH);
7644 addr0_low = tr32(MAC_ADDR_0_LOW);
7645 addr1_high = tr32(MAC_ADDR_1_HIGH);
7646 addr1_low = tr32(MAC_ADDR_1_LOW);
7647
7648 /* Skip MAC addr 1 if ASF is using it. */
7649 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7650 !(addr1_high == 0 && addr1_low == 0))
7651 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007652 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007653 spin_lock_bh(&tp->lock);
7654 __tg3_set_mac_addr(tp, skip_mac_1);
7655 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656
Michael Chanb9ec6c12006-07-25 16:37:27 -07007657 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007658}
7659
7660/* tp->lock is held. */
7661static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7662 dma_addr_t mapping, u32 maxlen_flags,
7663 u32 nic_addr)
7664{
7665 tg3_write_mem(tp,
7666 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7667 ((u64) mapping >> 32));
7668 tg3_write_mem(tp,
7669 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7670 ((u64) mapping & 0xffffffff));
7671 tg3_write_mem(tp,
7672 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7673 maxlen_flags);
7674
Joe Perches63c3a662011-04-26 08:12:10 +00007675 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676 tg3_write_mem(tp,
7677 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7678 nic_addr);
7679}
7680
7681static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007682static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007683{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007684 int i;
7685
Joe Perches63c3a662011-04-26 08:12:10 +00007686 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007687 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7688 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7689 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007690 } else {
7691 tw32(HOSTCC_TXCOL_TICKS, 0);
7692 tw32(HOSTCC_TXMAX_FRAMES, 0);
7693 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007694 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007695
Joe Perches63c3a662011-04-26 08:12:10 +00007696 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007697 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7698 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7699 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7700 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007701 tw32(HOSTCC_RXCOL_TICKS, 0);
7702 tw32(HOSTCC_RXMAX_FRAMES, 0);
7703 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007704 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007705
Joe Perches63c3a662011-04-26 08:12:10 +00007706 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07007707 u32 val = ec->stats_block_coalesce_usecs;
7708
Matt Carlsonb6080e12009-09-01 13:12:00 +00007709 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7710 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7711
David S. Miller15f98502005-05-18 22:49:26 -07007712 if (!netif_carrier_ok(tp->dev))
7713 val = 0;
7714
7715 tw32(HOSTCC_STAT_COAL_TICKS, val);
7716 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007717
7718 for (i = 0; i < tp->irq_cnt - 1; i++) {
7719 u32 reg;
7720
7721 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7722 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007723 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7724 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007725 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7726 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007727
Joe Perches63c3a662011-04-26 08:12:10 +00007728 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007729 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7730 tw32(reg, ec->tx_coalesce_usecs);
7731 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7732 tw32(reg, ec->tx_max_coalesced_frames);
7733 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7734 tw32(reg, ec->tx_max_coalesced_frames_irq);
7735 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007736 }
7737
7738 for (; i < tp->irq_max - 1; i++) {
7739 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007740 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007741 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007742
Joe Perches63c3a662011-04-26 08:12:10 +00007743 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007744 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7745 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7746 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7747 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007748 }
David S. Miller15f98502005-05-18 22:49:26 -07007749}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007750
7751/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007752static void tg3_rings_reset(struct tg3 *tp)
7753{
7754 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007755 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007756 struct tg3_napi *tnapi = &tp->napi[0];
7757
7758 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007759 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007760 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00007761 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00007762 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007763 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7764 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007765 else
7766 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7767
7768 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7769 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7770 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7771 BDINFO_FLAGS_DISABLED);
7772
7773
7774 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007775 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007776 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00007777 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007778 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007779 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7780 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007781 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7782 else
7783 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7784
7785 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7786 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7787 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7788 BDINFO_FLAGS_DISABLED);
7789
7790 /* Disable interrupts */
7791 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00007792 tp->napi[0].chk_msi_cnt = 0;
7793 tp->napi[0].last_rx_cons = 0;
7794 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007795
7796 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00007797 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007798 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007799 tp->napi[i].tx_prod = 0;
7800 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007801 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007802 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007803 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7804 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00007805 tp->napi[0].chk_msi_cnt = 0;
7806 tp->napi[i].last_rx_cons = 0;
7807 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007808 }
Joe Perches63c3a662011-04-26 08:12:10 +00007809 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007810 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007811 } else {
7812 tp->napi[0].tx_prod = 0;
7813 tp->napi[0].tx_cons = 0;
7814 tw32_mailbox(tp->napi[0].prodmbox, 0);
7815 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7816 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007817
7818 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00007819 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00007820 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7821 for (i = 0; i < 16; i++)
7822 tw32_tx_mbox(mbox + i * 8, 0);
7823 }
7824
7825 txrcb = NIC_SRAM_SEND_RCB;
7826 rxrcb = NIC_SRAM_RCV_RET_RCB;
7827
7828 /* Clear status block in ram. */
7829 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7830
7831 /* Set status block DMA address */
7832 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7833 ((u64) tnapi->status_mapping >> 32));
7834 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7835 ((u64) tnapi->status_mapping & 0xffffffff));
7836
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007837 if (tnapi->tx_ring) {
7838 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7839 (TG3_TX_RING_SIZE <<
7840 BDINFO_FLAGS_MAXLEN_SHIFT),
7841 NIC_SRAM_TX_BUFFER_DESC);
7842 txrcb += TG3_BDINFO_SIZE;
7843 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007844
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007845 if (tnapi->rx_rcb) {
7846 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007847 (tp->rx_ret_ring_mask + 1) <<
7848 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007849 rxrcb += TG3_BDINFO_SIZE;
7850 }
7851
7852 stblk = HOSTCC_STATBLCK_RING1;
7853
7854 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7855 u64 mapping = (u64)tnapi->status_mapping;
7856 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7857 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7858
7859 /* Clear status block in ram. */
7860 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7861
Matt Carlson19cfaec2009-12-03 08:36:20 +00007862 if (tnapi->tx_ring) {
7863 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7864 (TG3_TX_RING_SIZE <<
7865 BDINFO_FLAGS_MAXLEN_SHIFT),
7866 NIC_SRAM_TX_BUFFER_DESC);
7867 txrcb += TG3_BDINFO_SIZE;
7868 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007869
7870 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007871 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007872 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7873
7874 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007875 rxrcb += TG3_BDINFO_SIZE;
7876 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007877}
7878
Matt Carlsoneb07a942011-04-20 07:57:36 +00007879static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
7880{
7881 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
7882
Joe Perches63c3a662011-04-26 08:12:10 +00007883 if (!tg3_flag(tp, 5750_PLUS) ||
7884 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00007885 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
7886 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7887 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
7888 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7889 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
7890 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
7891 else
7892 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
7893
7894 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
7895 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
7896
7897 val = min(nic_rep_thresh, host_rep_thresh);
7898 tw32(RCVBDI_STD_THRESH, val);
7899
Joe Perches63c3a662011-04-26 08:12:10 +00007900 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007901 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
7902
Joe Perches63c3a662011-04-26 08:12:10 +00007903 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007904 return;
7905
Joe Perches63c3a662011-04-26 08:12:10 +00007906 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007907 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
7908 else
7909 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
7910
7911 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
7912
7913 val = min(bdcache_maxcnt / 2, host_rep_thresh);
7914 tw32(RCVBDI_JUMBO_THRESH, val);
7915
Joe Perches63c3a662011-04-26 08:12:10 +00007916 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007917 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
7918}
7919
Matt Carlson2d31eca2009-09-01 12:53:31 +00007920/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007921static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007922{
7923 u32 val, rdmac_mode;
7924 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007925 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926
7927 tg3_disable_ints(tp);
7928
7929 tg3_stop_fw(tp);
7930
7931 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7932
Joe Perches63c3a662011-04-26 08:12:10 +00007933 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07007934 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935
Matt Carlson699c0192010-12-06 08:28:51 +00007936 /* Enable MAC control of LPI */
7937 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7938 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7939 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7940 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7941
7942 tw32_f(TG3_CPMU_EEE_CTRL,
7943 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7944
Matt Carlsona386b902010-12-06 08:28:53 +00007945 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7946 TG3_CPMU_EEEMD_LPI_IN_TX |
7947 TG3_CPMU_EEEMD_LPI_IN_RX |
7948 TG3_CPMU_EEEMD_EEE_ENABLE;
7949
7950 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
7951 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
7952
Joe Perches63c3a662011-04-26 08:12:10 +00007953 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00007954 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
7955
7956 tw32_f(TG3_CPMU_EEE_MODE, val);
7957
7958 tw32_f(TG3_CPMU_EEE_DBTMR1,
7959 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
7960 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
7961
7962 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00007963 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00007964 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00007965 }
7966
Matt Carlson603f1172010-02-12 14:47:10 +00007967 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007968 tg3_phy_reset(tp);
7969
Linus Torvalds1da177e2005-04-16 15:20:36 -07007970 err = tg3_chip_reset(tp);
7971 if (err)
7972 return err;
7973
7974 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7975
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007976 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007977 val = tr32(TG3_CPMU_CTRL);
7978 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7979 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007980
7981 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7982 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7983 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7984 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7985
7986 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7987 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7988 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7989 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7990
7991 val = tr32(TG3_CPMU_HST_ACC);
7992 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7993 val |= CPMU_HST_ACC_MACCLK_6_25;
7994 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007995 }
7996
Matt Carlson33466d932009-04-20 06:57:41 +00007997 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7998 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7999 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8000 PCIE_PWR_MGMT_L1_THRESH_4MS;
8001 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008002
8003 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8004 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8005
8006 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d932009-04-20 06:57:41 +00008007
Matt Carlsonf40386c2009-11-02 14:24:02 +00008008 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8009 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008010 }
8011
Joe Perches63c3a662011-04-26 08:12:10 +00008012 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008013 u32 grc_mode = tr32(GRC_MODE);
8014
8015 /* Access the lower 1K of PL PCIE block registers. */
8016 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8017 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8018
8019 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8020 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8021 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8022
8023 tw32(GRC_MODE, grc_mode);
8024 }
8025
Matt Carlson5093eed2010-11-24 08:31:45 +00008026 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8027 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8028 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008029
Matt Carlson5093eed2010-11-24 08:31:45 +00008030 /* Access the lower 1K of PL PCIE block registers. */
8031 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8032 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008033
Matt Carlson5093eed2010-11-24 08:31:45 +00008034 val = tr32(TG3_PCIE_TLDLPL_PORT +
8035 TG3_PCIE_PL_LO_PHYCTL5);
8036 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8037 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008038
Matt Carlson5093eed2010-11-24 08:31:45 +00008039 tw32(GRC_MODE, grc_mode);
8040 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008041
Matt Carlson1ff30a52011-05-19 12:12:46 +00008042 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8043 u32 grc_mode = tr32(GRC_MODE);
8044
8045 /* Access the lower 1K of DL PCIE block registers. */
8046 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8047 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8048
8049 val = tr32(TG3_PCIE_TLDLPL_PORT +
8050 TG3_PCIE_DL_LO_FTSMAX);
8051 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8052 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8053 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8054
8055 tw32(GRC_MODE, grc_mode);
8056 }
8057
Matt Carlsona977dbe2010-04-12 06:58:26 +00008058 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8059 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8060 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8061 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008062 }
8063
Linus Torvalds1da177e2005-04-16 15:20:36 -07008064 /* This works around an issue with Athlon chipsets on
8065 * B3 tigon3 silicon. This bit has no effect on any
8066 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008067 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068 */
Joe Perches63c3a662011-04-26 08:12:10 +00008069 if (!tg3_flag(tp, CPMU_PRESENT)) {
8070 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008071 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8072 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008074
8075 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008076 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008077 val = tr32(TG3PCI_PCISTATE);
8078 val |= PCISTATE_RETRY_SAME_DMA;
8079 tw32(TG3PCI_PCISTATE, val);
8080 }
8081
Joe Perches63c3a662011-04-26 08:12:10 +00008082 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008083 /* Allow reads and writes to the
8084 * APE register and memory space.
8085 */
8086 val = tr32(TG3PCI_PCISTATE);
8087 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008088 PCISTATE_ALLOW_APE_SHMEM_WR |
8089 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008090 tw32(TG3PCI_PCISTATE, val);
8091 }
8092
Linus Torvalds1da177e2005-04-16 15:20:36 -07008093 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8094 /* Enable some hw fixes. */
8095 val = tr32(TG3PCI_MSI_DATA);
8096 val |= (1 << 26) | (1 << 28) | (1 << 29);
8097 tw32(TG3PCI_MSI_DATA, val);
8098 }
8099
8100 /* Descriptor ring init may make accesses to the
8101 * NIC SRAM area to setup the TX descriptors, so we
8102 * can only do this after the hardware has been
8103 * successfully reset.
8104 */
Michael Chan32d8c572006-07-25 16:38:29 -07008105 err = tg3_init_rings(tp);
8106 if (err)
8107 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008108
Joe Perches63c3a662011-04-26 08:12:10 +00008109 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008110 val = tr32(TG3PCI_DMA_RW_CTRL) &
8111 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008112 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8113 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008114 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8115 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8116 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008117 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8118 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8119 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008120 /* This value is determined during the probe time DMA
8121 * engine test, tg3_test_dma.
8122 */
8123 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008125
8126 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8127 GRC_MODE_4X_NIC_SEND_RINGS |
8128 GRC_MODE_NO_TX_PHDR_CSUM |
8129 GRC_MODE_NO_RX_PHDR_CSUM);
8130 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008131
8132 /* Pseudo-header checksum is done by hardware logic and not
8133 * the offload processers, so make the chip do the pseudo-
8134 * header checksums on receive. For transmit it is more
8135 * convenient to do the pseudo-header checksum in software
8136 * as Linux does that on transmit for us in all cases.
8137 */
8138 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139
8140 tw32(GRC_MODE,
8141 tp->grc_mode |
8142 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8143
8144 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8145 val = tr32(GRC_MISC_CFG);
8146 val &= ~0xff;
8147 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8148 tw32(GRC_MISC_CFG, val);
8149
8150 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008151 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008152 /* Do nothing. */
8153 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8154 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8155 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8156 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8157 else
8158 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8159 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8160 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008161 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008162 int fw_len;
8163
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008164 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008165 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8166 tw32(BUFMGR_MB_POOL_ADDR,
8167 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8168 tw32(BUFMGR_MB_POOL_SIZE,
8169 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008171
Michael Chan0f893dc2005-07-25 12:30:38 -07008172 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008173 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8174 tp->bufmgr_config.mbuf_read_dma_low_water);
8175 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8176 tp->bufmgr_config.mbuf_mac_rx_low_water);
8177 tw32(BUFMGR_MB_HIGH_WATER,
8178 tp->bufmgr_config.mbuf_high_water);
8179 } else {
8180 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8181 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8182 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8183 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8184 tw32(BUFMGR_MB_HIGH_WATER,
8185 tp->bufmgr_config.mbuf_high_water_jumbo);
8186 }
8187 tw32(BUFMGR_DMA_LOW_WATER,
8188 tp->bufmgr_config.dma_low_water);
8189 tw32(BUFMGR_DMA_HIGH_WATER,
8190 tp->bufmgr_config.dma_high_water);
8191
Matt Carlsond309a462010-09-30 10:34:31 +00008192 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8193 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8194 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008195 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8196 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8197 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8198 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008199 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008200 for (i = 0; i < 2000; i++) {
8201 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8202 break;
8203 udelay(10);
8204 }
8205 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008206 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008207 return -ENODEV;
8208 }
8209
Matt Carlsoneb07a942011-04-20 07:57:36 +00008210 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8211 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008212
Matt Carlsoneb07a942011-04-20 07:57:36 +00008213 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214
8215 /* Initialize TG3_BDINFO's at:
8216 * RCVDBDI_STD_BD: standard eth size rx ring
8217 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8218 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8219 *
8220 * like so:
8221 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8222 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8223 * ring attribute flags
8224 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8225 *
8226 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8227 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8228 *
8229 * The size of each ring is fixed in the firmware, but the location is
8230 * configurable.
8231 */
8232 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008233 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008234 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008235 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008236 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008237 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8238 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008239
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008240 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008241 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8243 BDINFO_FLAGS_DISABLED);
8244
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008245 /* Program the jumbo buffer descriptor ring control
8246 * blocks on those devices that have them.
8247 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008248 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008249 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008250
Joe Perches63c3a662011-04-26 08:12:10 +00008251 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008252 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008253 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008254 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008255 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008256 val = TG3_RX_JMB_RING_SIZE(tp) <<
8257 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008258 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008259 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008260 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008261 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008262 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8263 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264 } else {
8265 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8266 BDINFO_FLAGS_DISABLED);
8267 }
8268
Joe Perches63c3a662011-04-26 08:12:10 +00008269 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008270 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008271 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008272 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008273 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008274 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8275 val |= (TG3_RX_STD_DMA_SZ << 2);
8276 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008277 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008278 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008279 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008280
8281 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008282
Matt Carlson411da642009-11-13 13:03:46 +00008283 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e662009-11-13 13:03:49 +00008284 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008285
Joe Perches63c3a662011-04-26 08:12:10 +00008286 tpr->rx_jmb_prod_idx =
8287 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e662009-11-13 13:03:49 +00008288 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008289
Matt Carlson2d31eca2009-09-01 12:53:31 +00008290 tg3_rings_reset(tp);
8291
Linus Torvalds1da177e2005-04-16 15:20:36 -07008292 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008293 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294
8295 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008296 tw32(MAC_RX_MTU_SIZE,
8297 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008298
8299 /* The slot time is changed by tg3_setup_phy if we
8300 * run at gigabit with half duplex.
8301 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008302 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8303 (6 << TX_LENGTHS_IPG_SHIFT) |
8304 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8305
8306 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8307 val |= tr32(MAC_TX_LENGTHS) &
8308 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8309 TX_LENGTHS_CNT_DWN_VAL_MSK);
8310
8311 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008312
8313 /* Receive rules. */
8314 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8315 tw32(RCVLPC_CONFIG, 0x0181);
8316
8317 /* Calculate RDMAC_MODE setting early, we need it to determine
8318 * the RCVLPC_STATE_ENABLE mask.
8319 */
8320 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8321 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8322 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8323 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8324 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008325
Matt Carlsondeabaac2010-11-24 08:31:50 +00008326 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008327 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8328
Matt Carlson57e69832008-05-25 23:48:31 -07008329 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008330 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8331 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008332 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8333 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8334 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8335
Matt Carlsonc5908932011-03-09 16:58:25 +00008336 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8337 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008338 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008339 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008340 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8341 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008342 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008343 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8344 }
8345 }
8346
Joe Perches63c3a662011-04-26 08:12:10 +00008347 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008348 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8349
Joe Perches63c3a662011-04-26 08:12:10 +00008350 if (tg3_flag(tp, HW_TSO_1) ||
8351 tg3_flag(tp, HW_TSO_2) ||
8352 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008353 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8354
Matt Carlson108a6c12011-05-19 12:12:47 +00008355 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008356 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008357 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8358 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008359
Matt Carlsonf2096f92011-04-05 14:22:48 +00008360 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8361 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8362
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008363 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8364 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8365 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8366 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008367 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008368 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008369 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8370 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008371 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8372 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8373 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8374 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8375 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8376 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008377 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008378 tw32(TG3_RDMA_RSRVCTRL_REG,
8379 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8380 }
8381
Matt Carlsond78b59f2011-04-05 14:22:46 +00008382 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8383 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008384 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8385 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8386 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8387 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8388 }
8389
Linus Torvalds1da177e2005-04-16 15:20:36 -07008390 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008391 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008392 val = tr32(RCVLPC_STATS_ENABLE);
8393 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8394 tw32(RCVLPC_STATS_ENABLE, val);
8395 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008396 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008397 val = tr32(RCVLPC_STATS_ENABLE);
8398 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8399 tw32(RCVLPC_STATS_ENABLE, val);
8400 } else {
8401 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8402 }
8403 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8404 tw32(SNDDATAI_STATSENAB, 0xffffff);
8405 tw32(SNDDATAI_STATSCTRL,
8406 (SNDDATAI_SCTRL_ENABLE |
8407 SNDDATAI_SCTRL_FASTUPD));
8408
8409 /* Setup host coalescing engine. */
8410 tw32(HOSTCC_MODE, 0);
8411 for (i = 0; i < 2000; i++) {
8412 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8413 break;
8414 udelay(10);
8415 }
8416
Michael Chand244c892005-07-05 14:42:33 -07008417 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008418
Joe Perches63c3a662011-04-26 08:12:10 +00008419 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008420 /* Status/statistics block address. See tg3_timer,
8421 * the tg3_periodic_fetch_stats call there, and
8422 * tg3_get_stats to see how this works for 5705/5750 chips.
8423 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008424 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8425 ((u64) tp->stats_mapping >> 32));
8426 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8427 ((u64) tp->stats_mapping & 0xffffffff));
8428 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008429
Linus Torvalds1da177e2005-04-16 15:20:36 -07008430 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008431
8432 /* Clear statistics and status block memory areas */
8433 for (i = NIC_SRAM_STATS_BLK;
8434 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8435 i += sizeof(u32)) {
8436 tg3_write_mem(tp, i, 0);
8437 udelay(40);
8438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439 }
8440
8441 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8442
8443 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8444 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008445 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8447
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008448 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8449 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008450 /* reset to prevent losing 1st rx packet intermittently */
8451 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8452 udelay(10);
8453 }
8454
Joe Perches63c3a662011-04-26 08:12:10 +00008455 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008456 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008457 else
8458 tp->mac_mode = 0;
8459 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008460 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008461 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008462 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008463 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8464 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008465 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8466 udelay(40);
8467
Michael Chan314fba32005-04-21 17:07:04 -07008468 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008469 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008470 * register to preserve the GPIO settings for LOMs. The GPIOs,
8471 * whether used as inputs or outputs, are set by boot code after
8472 * reset.
8473 */
Joe Perches63c3a662011-04-26 08:12:10 +00008474 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008475 u32 gpio_mask;
8476
Michael Chan9d26e212006-12-07 00:21:14 -08008477 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8478 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8479 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008480
8481 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8482 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8483 GRC_LCLCTRL_GPIO_OUTPUT3;
8484
Michael Chanaf36e6b2006-03-23 01:28:06 -08008485 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8486 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8487
Gary Zambranoaaf84462007-05-05 11:51:45 -07008488 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008489 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8490
8491 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008492 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008493 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8494 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008496 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8497 udelay(100);
8498
Joe Perches63c3a662011-04-26 08:12:10 +00008499 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008500 val = tr32(MSGINT_MODE);
8501 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8502 tw32(MSGINT_MODE, val);
8503 }
8504
Joe Perches63c3a662011-04-26 08:12:10 +00008505 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008506 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8507 udelay(40);
8508 }
8509
8510 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8511 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8512 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8513 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8514 WDMAC_MODE_LNGREAD_ENAB);
8515
Matt Carlsonc5908932011-03-09 16:58:25 +00008516 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8517 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008518 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008519 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8520 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8521 /* nothing */
8522 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008523 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008524 val |= WDMAC_MODE_RX_ACCEL;
8525 }
8526 }
8527
Michael Chand9ab5ad12006-03-20 22:27:35 -08008528 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008529 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008530 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad12006-03-20 22:27:35 -08008531
Matt Carlson788a0352009-11-02 14:26:03 +00008532 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8533 val |= WDMAC_MODE_BURST_ALL_DATA;
8534
Linus Torvalds1da177e2005-04-16 15:20:36 -07008535 tw32_f(WDMAC_MODE, val);
8536 udelay(40);
8537
Joe Perches63c3a662011-04-26 08:12:10 +00008538 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008539 u16 pcix_cmd;
8540
8541 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8542 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008544 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8545 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008547 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8548 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008549 }
Matt Carlson9974a352007-10-07 23:27:28 -07008550 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8551 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008552 }
8553
8554 tw32_f(RDMAC_MODE, rdmac_mode);
8555 udelay(40);
8556
8557 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008558 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008559 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008560
8561 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8562 tw32(SNDDATAC_MODE,
8563 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8564 else
8565 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8566
Linus Torvalds1da177e2005-04-16 15:20:36 -07008567 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8568 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008569 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008570 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008571 val |= RCVDBDI_MODE_LRG_RING_SZ;
8572 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008573 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008574 if (tg3_flag(tp, HW_TSO_1) ||
8575 tg3_flag(tp, HW_TSO_2) ||
8576 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008577 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008578 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008579 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008580 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8581 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008582 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8583
8584 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8585 err = tg3_load_5701_a0_firmware_fix(tp);
8586 if (err)
8587 return err;
8588 }
8589
Joe Perches63c3a662011-04-26 08:12:10 +00008590 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008591 err = tg3_load_tso_firmware(tp);
8592 if (err)
8593 return err;
8594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595
8596 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008597
Joe Perches63c3a662011-04-26 08:12:10 +00008598 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008599 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8600 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008601
8602 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8603 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8604 tp->tx_mode &= ~val;
8605 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8606 }
8607
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608 tw32_f(MAC_TX_MODE, tp->tx_mode);
8609 udelay(100);
8610
Joe Perches63c3a662011-04-26 08:12:10 +00008611 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008612 u32 reg = MAC_RSS_INDIR_TBL_0;
8613 u8 *ent = (u8 *)&val;
8614
8615 /* Setup the indirection table */
8616 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8617 int idx = i % sizeof(val);
8618
Matt Carlson5efeeea2010-07-11 09:31:40 +00008619 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008620 if (idx == sizeof(val) - 1) {
8621 tw32(reg, val);
8622 reg += 4;
8623 }
8624 }
8625
8626 /* Setup the "secret" hash key. */
8627 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8628 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8629 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8630 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8631 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8632 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8633 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8634 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8635 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8636 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8637 }
8638
Linus Torvalds1da177e2005-04-16 15:20:36 -07008639 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008640 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008641 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8642
Joe Perches63c3a662011-04-26 08:12:10 +00008643 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008644 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8645 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8646 RX_MODE_RSS_IPV6_HASH_EN |
8647 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8648 RX_MODE_RSS_IPV4_HASH_EN |
8649 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8650
Linus Torvalds1da177e2005-04-16 15:20:36 -07008651 tw32_f(MAC_RX_MODE, tp->rx_mode);
8652 udelay(10);
8653
Linus Torvalds1da177e2005-04-16 15:20:36 -07008654 tw32(MAC_LED_CTRL, tp->led_ctrl);
8655
8656 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008657 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008658 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8659 udelay(10);
8660 }
8661 tw32_f(MAC_RX_MODE, tp->rx_mode);
8662 udelay(10);
8663
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008664 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008665 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008666 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008667 /* Set drive transmission level to 1.2V */
8668 /* only if the signal pre-emphasis bit is not set */
8669 val = tr32(MAC_SERDES_CFG);
8670 val &= 0xfffff000;
8671 val |= 0x880;
8672 tw32(MAC_SERDES_CFG, val);
8673 }
8674 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8675 tw32(MAC_SERDES_CFG, 0x616000);
8676 }
8677
8678 /* Prevent chip from dropping frames when flow control
8679 * is enabled.
8680 */
Matt Carlson666bc832010-01-20 16:58:03 +00008681 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8682 val = 1;
8683 else
8684 val = 2;
8685 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008686
8687 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008688 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008689 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00008690 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008691 }
8692
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008693 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00008694 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08008695 u32 tmp;
8696
8697 tmp = tr32(SERDES_RX_CTRL);
8698 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8699 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8700 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8701 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8702 }
8703
Joe Perches63c3a662011-04-26 08:12:10 +00008704 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008705 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8706 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008707 tp->link_config.speed = tp->link_config.orig_speed;
8708 tp->link_config.duplex = tp->link_config.orig_duplex;
8709 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008711
Matt Carlsondd477002008-05-25 23:45:58 -07008712 err = tg3_setup_phy(tp, 0);
8713 if (err)
8714 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008715
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008716 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8717 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008718 u32 tmp;
8719
8720 /* Clear CRC stats. */
8721 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8722 tg3_writephy(tp, MII_TG3_TEST1,
8723 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008724 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008726 }
8727 }
8728
8729 __tg3_set_rx_mode(tp->dev);
8730
8731 /* Initialize receive rules. */
8732 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8733 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8734 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8735 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8736
Joe Perches63c3a662011-04-26 08:12:10 +00008737 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008738 limit = 8;
8739 else
8740 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008741 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008742 limit -= 4;
8743 switch (limit) {
8744 case 16:
8745 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8746 case 15:
8747 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8748 case 14:
8749 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8750 case 13:
8751 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8752 case 12:
8753 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8754 case 11:
8755 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8756 case 10:
8757 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8758 case 9:
8759 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8760 case 8:
8761 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8762 case 7:
8763 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8764 case 6:
8765 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8766 case 5:
8767 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8768 case 4:
8769 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8770 case 3:
8771 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8772 case 2:
8773 case 1:
8774
8775 default:
8776 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008778
Joe Perches63c3a662011-04-26 08:12:10 +00008779 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07008780 /* Write our heartbeat update interval to APE. */
8781 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8782 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008783
Linus Torvalds1da177e2005-04-16 15:20:36 -07008784 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8785
Linus Torvalds1da177e2005-04-16 15:20:36 -07008786 return 0;
8787}
8788
8789/* Called at device open time to get the chip ready for
8790 * packet processing. Invoked with tp->lock held.
8791 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008792static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008793{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008794 tg3_switch_clocks(tp);
8795
8796 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8797
Matt Carlson2f751b62008-08-04 23:17:34 -07008798 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008799}
8800
8801#define TG3_STAT_ADD32(PSTAT, REG) \
8802do { u32 __val = tr32(REG); \
8803 (PSTAT)->low += __val; \
8804 if ((PSTAT)->low < __val) \
8805 (PSTAT)->high += 1; \
8806} while (0)
8807
8808static void tg3_periodic_fetch_stats(struct tg3 *tp)
8809{
8810 struct tg3_hw_stats *sp = tp->hw_stats;
8811
8812 if (!netif_carrier_ok(tp->dev))
8813 return;
8814
8815 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8816 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8817 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8818 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8819 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8820 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8821 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8822 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8823 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8824 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8825 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8826 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8827 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8828
8829 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8830 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8831 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8832 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8833 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8834 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8835 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8836 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8837 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8838 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8839 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8840 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8841 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8842 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008843
8844 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00008845 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8846 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
8847 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00008848 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8849 } else {
8850 u32 val = tr32(HOSTCC_FLOW_ATTN);
8851 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
8852 if (val) {
8853 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
8854 sp->rx_discards.low += val;
8855 if (sp->rx_discards.low < val)
8856 sp->rx_discards.high += 1;
8857 }
8858 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
8859 }
Michael Chan463d3052006-05-22 16:36:27 -07008860 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008861}
8862
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008863static void tg3_chk_missed_msi(struct tg3 *tp)
8864{
8865 u32 i;
8866
8867 for (i = 0; i < tp->irq_cnt; i++) {
8868 struct tg3_napi *tnapi = &tp->napi[i];
8869
8870 if (tg3_has_work(tnapi)) {
8871 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
8872 tnapi->last_tx_cons == tnapi->tx_cons) {
8873 if (tnapi->chk_msi_cnt < 1) {
8874 tnapi->chk_msi_cnt++;
8875 return;
8876 }
8877 tw32_mailbox(tnapi->int_mbox,
8878 tnapi->last_tag << 24);
8879 }
8880 }
8881 tnapi->chk_msi_cnt = 0;
8882 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
8883 tnapi->last_tx_cons = tnapi->tx_cons;
8884 }
8885}
8886
Linus Torvalds1da177e2005-04-16 15:20:36 -07008887static void tg3_timer(unsigned long __opaque)
8888{
8889 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890
Michael Chanf475f162006-03-27 23:20:14 -08008891 if (tp->irq_sync)
8892 goto restart_timer;
8893
David S. Millerf47c11e2005-06-24 20:18:35 -07008894 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008895
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008896 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8897 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8898 tg3_chk_missed_msi(tp);
8899
Joe Perches63c3a662011-04-26 08:12:10 +00008900 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07008901 /* All of this garbage is because when using non-tagged
8902 * IRQ status the mailbox/status_block protocol the chip
8903 * uses with the cpu is race prone.
8904 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008905 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008906 tw32(GRC_LOCAL_CTRL,
8907 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8908 } else {
8909 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008910 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008912
David S. Millerfac9b832005-05-18 22:46:34 -07008913 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +00008914 tg3_flag_set(tp, RESTART_TIMER);
David S. Millerf47c11e2005-06-24 20:18:35 -07008915 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008916 schedule_work(&tp->reset_task);
8917 return;
8918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008919 }
8920
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921 /* This part only runs once per second. */
8922 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008923 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07008924 tg3_periodic_fetch_stats(tp);
8925
Matt Carlsonb0c59432011-05-19 12:12:48 +00008926 if (tp->setlpicnt && !--tp->setlpicnt)
8927 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00008928
Joe Perches63c3a662011-04-26 08:12:10 +00008929 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008930 u32 mac_stat;
8931 int phy_event;
8932
8933 mac_stat = tr32(MAC_STATUS);
8934
8935 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008936 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008937 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8938 phy_event = 1;
8939 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8940 phy_event = 1;
8941
8942 if (phy_event)
8943 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00008944 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945 u32 mac_stat = tr32(MAC_STATUS);
8946 int need_setup = 0;
8947
8948 if (netif_carrier_ok(tp->dev) &&
8949 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8950 need_setup = 1;
8951 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008952 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008953 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8954 MAC_STATUS_SIGNAL_DET))) {
8955 need_setup = 1;
8956 }
8957 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008958 if (!tp->serdes_counter) {
8959 tw32_f(MAC_MODE,
8960 (tp->mac_mode &
8961 ~MAC_MODE_PORT_MODE_MASK));
8962 udelay(40);
8963 tw32_f(MAC_MODE, tp->mac_mode);
8964 udelay(40);
8965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008966 tg3_setup_phy(tp, 0);
8967 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008968 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008969 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008970 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008972
8973 tp->timer_counter = tp->timer_multiplier;
8974 }
8975
Michael Chan130b8e42006-09-27 16:00:40 -07008976 /* Heartbeat is only sent once every 2 seconds.
8977 *
8978 * The heartbeat is to tell the ASF firmware that the host
8979 * driver is still alive. In the event that the OS crashes,
8980 * ASF needs to reset the hardware to free up the FIFO space
8981 * that may be filled with rx packets destined for the host.
8982 * If the FIFO is full, ASF will no longer function properly.
8983 *
8984 * Unintended resets have been reported on real time kernels
8985 * where the timer doesn't run on time. Netpoll will also have
8986 * same problem.
8987 *
8988 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8989 * to check the ring condition when the heartbeat is expiring
8990 * before doing the reset. This will prevent most unintended
8991 * resets.
8992 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008993 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008994 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008995 tg3_wait_for_event_ack(tp);
8996
Michael Chanbbadf502006-04-06 21:46:34 -07008997 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008998 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008999 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009000 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9001 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009002
9003 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009004 }
9005 tp->asf_counter = tp->asf_multiplier;
9006 }
9007
David S. Millerf47c11e2005-06-24 20:18:35 -07009008 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009009
Michael Chanf475f162006-03-27 23:20:14 -08009010restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009011 tp->timer.expires = jiffies + tp->timer_offset;
9012 add_timer(&tp->timer);
9013}
9014
Matt Carlson4f125f42009-09-01 12:55:02 +00009015static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009016{
David Howells7d12e782006-10-05 14:55:46 +01009017 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009018 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009019 char *name;
9020 struct tg3_napi *tnapi = &tp->napi[irq_num];
9021
9022 if (tp->irq_cnt == 1)
9023 name = tp->dev->name;
9024 else {
9025 name = &tnapi->irq_lbl[0];
9026 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9027 name[IFNAMSIZ-1] = 0;
9028 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009029
Joe Perches63c3a662011-04-26 08:12:10 +00009030 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009031 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009032 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009033 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009034 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009035 } else {
9036 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009037 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009038 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009039 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009040 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009041
9042 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009043}
9044
Michael Chan79381092005-04-21 17:13:59 -07009045static int tg3_test_interrupt(struct tg3 *tp)
9046{
Matt Carlson09943a12009-08-28 14:01:57 +00009047 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009048 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009049 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009050 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009051
Michael Chand4bc3922005-05-29 14:59:20 -07009052 if (!netif_running(dev))
9053 return -ENODEV;
9054
Michael Chan79381092005-04-21 17:13:59 -07009055 tg3_disable_ints(tp);
9056
Matt Carlson4f125f42009-09-01 12:55:02 +00009057 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009058
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009059 /*
9060 * Turn off MSI one shot mode. Otherwise this test has no
9061 * observable way to know whether the interrupt was delivered.
9062 */
Joe Perches63c3a662011-04-26 08:12:10 +00009063 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009064 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9065 tw32(MSGINT_MODE, val);
9066 }
9067
Matt Carlson4f125f42009-09-01 12:55:02 +00009068 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009069 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009070 if (err)
9071 return err;
9072
Matt Carlson898a56f2009-08-28 14:02:40 +00009073 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009074 tg3_enable_ints(tp);
9075
9076 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009077 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009078
9079 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009080 u32 int_mbox, misc_host_ctrl;
9081
Matt Carlson898a56f2009-08-28 14:02:40 +00009082 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009083 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9084
9085 if ((int_mbox != 0) ||
9086 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9087 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009088 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009089 }
9090
Michael Chan79381092005-04-21 17:13:59 -07009091 msleep(10);
9092 }
9093
9094 tg3_disable_ints(tp);
9095
Matt Carlson4f125f42009-09-01 12:55:02 +00009096 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009097
Matt Carlson4f125f42009-09-01 12:55:02 +00009098 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009099
9100 if (err)
9101 return err;
9102
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009103 if (intr_ok) {
9104 /* Reenable MSI one shot mode. */
Joe Perches63c3a662011-04-26 08:12:10 +00009105 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009106 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9107 tw32(MSGINT_MODE, val);
9108 }
Michael Chan79381092005-04-21 17:13:59 -07009109 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009110 }
Michael Chan79381092005-04-21 17:13:59 -07009111
9112 return -EIO;
9113}
9114
9115/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9116 * successfully restored
9117 */
9118static int tg3_test_msi(struct tg3 *tp)
9119{
Michael Chan79381092005-04-21 17:13:59 -07009120 int err;
9121 u16 pci_cmd;
9122
Joe Perches63c3a662011-04-26 08:12:10 +00009123 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009124 return 0;
9125
9126 /* Turn off SERR reporting in case MSI terminates with Master
9127 * Abort.
9128 */
9129 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9130 pci_write_config_word(tp->pdev, PCI_COMMAND,
9131 pci_cmd & ~PCI_COMMAND_SERR);
9132
9133 err = tg3_test_interrupt(tp);
9134
9135 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9136
9137 if (!err)
9138 return 0;
9139
9140 /* other failures */
9141 if (err != -EIO)
9142 return err;
9143
9144 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009145 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9146 "to INTx mode. Please report this failure to the PCI "
9147 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009148
Matt Carlson4f125f42009-09-01 12:55:02 +00009149 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009150
Michael Chan79381092005-04-21 17:13:59 -07009151 pci_disable_msi(tp->pdev);
9152
Joe Perches63c3a662011-04-26 08:12:10 +00009153 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009154 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009155
Matt Carlson4f125f42009-09-01 12:55:02 +00009156 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009157 if (err)
9158 return err;
9159
9160 /* Need to reset the chip because the MSI cycle may have terminated
9161 * with Master Abort.
9162 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009163 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009164
Michael Chan944d9802005-05-29 14:57:48 -07009165 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009166 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009167
David S. Millerf47c11e2005-06-24 20:18:35 -07009168 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009169
9170 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009171 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009172
9173 return err;
9174}
9175
Matt Carlson9e9fd122009-01-19 16:57:45 -08009176static int tg3_request_firmware(struct tg3 *tp)
9177{
9178 const __be32 *fw_data;
9179
9180 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009181 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9182 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009183 return -ENOENT;
9184 }
9185
9186 fw_data = (void *)tp->fw->data;
9187
9188 /* Firmware blob starts with version numbers, followed by
9189 * start address and _full_ length including BSS sections
9190 * (which must be longer than the actual data, of course
9191 */
9192
9193 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9194 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009195 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9196 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009197 release_firmware(tp->fw);
9198 tp->fw = NULL;
9199 return -EINVAL;
9200 }
9201
9202 /* We no longer need firmware; we have it. */
9203 tp->fw_needed = NULL;
9204 return 0;
9205}
9206
Matt Carlson679563f2009-09-01 12:55:46 +00009207static bool tg3_enable_msix(struct tg3 *tp)
9208{
9209 int i, rc, cpus = num_online_cpus();
9210 struct msix_entry msix_ent[tp->irq_max];
9211
9212 if (cpus == 1)
9213 /* Just fallback to the simpler MSI mode. */
9214 return false;
9215
9216 /*
9217 * We want as many rx rings enabled as there are cpus.
9218 * The first MSIX vector only deals with link interrupts, etc,
9219 * so we add one to the number of vectors we are requesting.
9220 */
9221 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9222
9223 for (i = 0; i < tp->irq_max; i++) {
9224 msix_ent[i].entry = i;
9225 msix_ent[i].vector = 0;
9226 }
9227
9228 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009229 if (rc < 0) {
9230 return false;
9231 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009232 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9233 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009234 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9235 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009236 tp->irq_cnt = rc;
9237 }
9238
9239 for (i = 0; i < tp->irq_max; i++)
9240 tp->napi[i].irq_vec = msix_ent[i].vector;
9241
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009242 netif_set_real_num_tx_queues(tp->dev, 1);
9243 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9244 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9245 pci_disable_msix(tp->pdev);
9246 return false;
9247 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009248
9249 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009250 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009251
9252 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9253 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009254 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009255 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9256 }
9257 }
Matt Carlson2430b032010-06-05 17:24:34 +00009258
Matt Carlson679563f2009-09-01 12:55:46 +00009259 return true;
9260}
9261
Matt Carlson07b01732009-08-28 14:01:15 +00009262static void tg3_ints_init(struct tg3 *tp)
9263{
Joe Perches63c3a662011-04-26 08:12:10 +00009264 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9265 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009266 /* All MSI supporting chips should support tagged
9267 * status. Assert that this is the case.
9268 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009269 netdev_warn(tp->dev,
9270 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009271 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009272 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009273
Joe Perches63c3a662011-04-26 08:12:10 +00009274 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9275 tg3_flag_set(tp, USING_MSIX);
9276 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9277 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009278
Joe Perches63c3a662011-04-26 08:12:10 +00009279 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009280 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009281 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009282 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009283 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9284 }
9285defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009286 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009287 tp->irq_cnt = 1;
9288 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009289 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009290 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009291 }
Matt Carlson07b01732009-08-28 14:01:15 +00009292}
9293
9294static void tg3_ints_fini(struct tg3 *tp)
9295{
Joe Perches63c3a662011-04-26 08:12:10 +00009296 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009297 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009298 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009299 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009300 tg3_flag_clear(tp, USING_MSI);
9301 tg3_flag_clear(tp, USING_MSIX);
9302 tg3_flag_clear(tp, ENABLE_RSS);
9303 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009304}
9305
Linus Torvalds1da177e2005-04-16 15:20:36 -07009306static int tg3_open(struct net_device *dev)
9307{
9308 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009309 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009310
Matt Carlson9e9fd122009-01-19 16:57:45 -08009311 if (tp->fw_needed) {
9312 err = tg3_request_firmware(tp);
9313 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9314 if (err)
9315 return err;
9316 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009317 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009318 tg3_flag_clear(tp, TSO_CAPABLE);
9319 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009320 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009321 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009322 }
9323 }
9324
Michael Chanc49a1562006-12-17 17:07:29 -08009325 netif_carrier_off(tp->dev);
9326
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009327 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009328 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009329 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009330
9331 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009332
Linus Torvalds1da177e2005-04-16 15:20:36 -07009333 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009334 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335
David S. Millerf47c11e2005-06-24 20:18:35 -07009336 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009337
Matt Carlson679563f2009-09-01 12:55:46 +00009338 /*
9339 * Setup interrupts first so we know how
9340 * many NAPI resources to allocate
9341 */
9342 tg3_ints_init(tp);
9343
Linus Torvalds1da177e2005-04-16 15:20:36 -07009344 /* The placement of this call is tied
9345 * to the setup and use of Host TX descriptors.
9346 */
9347 err = tg3_alloc_consistent(tp);
9348 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009349 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009350
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009351 tg3_napi_init(tp);
9352
Matt Carlsonfed97812009-09-01 13:10:19 +00009353 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009354
Matt Carlson4f125f42009-09-01 12:55:02 +00009355 for (i = 0; i < tp->irq_cnt; i++) {
9356 struct tg3_napi *tnapi = &tp->napi[i];
9357 err = tg3_request_irq(tp, i);
9358 if (err) {
9359 for (i--; i >= 0; i--)
9360 free_irq(tnapi->irq_vec, tnapi);
9361 break;
9362 }
9363 }
Matt Carlson07b01732009-08-28 14:01:15 +00009364
9365 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009366 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009367
David S. Millerf47c11e2005-06-24 20:18:35 -07009368 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009369
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009370 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009371 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009372 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009373 tg3_free_rings(tp);
9374 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009375 if (tg3_flag(tp, TAGGED_STATUS) &&
9376 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9377 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765)
David S. Millerfac9b832005-05-18 22:46:34 -07009378 tp->timer_offset = HZ;
9379 else
9380 tp->timer_offset = HZ / 10;
9381
9382 BUG_ON(tp->timer_offset > HZ);
9383 tp->timer_counter = tp->timer_multiplier =
9384 (HZ / tp->timer_offset);
9385 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009386 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387
9388 init_timer(&tp->timer);
9389 tp->timer.expires = jiffies + tp->timer_offset;
9390 tp->timer.data = (unsigned long) tp;
9391 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009392 }
9393
David S. Millerf47c11e2005-06-24 20:18:35 -07009394 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009395
Matt Carlson07b01732009-08-28 14:01:15 +00009396 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009397 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009398
Joe Perches63c3a662011-04-26 08:12:10 +00009399 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009400 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009401
Michael Chan79381092005-04-21 17:13:59 -07009402 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009403 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009404 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009405 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009406 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009407
Matt Carlson679563f2009-09-01 12:55:46 +00009408 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009409 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009410
Joe Perches63c3a662011-04-26 08:12:10 +00009411 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009412 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009413
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009414 tw32(PCIE_TRANSACTION_CFG,
9415 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009416 }
Michael Chan79381092005-04-21 17:13:59 -07009417 }
9418
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009419 tg3_phy_start(tp);
9420
David S. Millerf47c11e2005-06-24 20:18:35 -07009421 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009422
Michael Chan79381092005-04-21 17:13:59 -07009423 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009424 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009425 tg3_enable_ints(tp);
9426
David S. Millerf47c11e2005-06-24 20:18:35 -07009427 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009429 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009430
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009431 /*
9432 * Reset loopback feature if it was turned on while the device was down
9433 * make sure that it's installed properly now.
9434 */
9435 if (dev->features & NETIF_F_LOOPBACK)
9436 tg3_set_loopback(dev, dev->features);
9437
Linus Torvalds1da177e2005-04-16 15:20:36 -07009438 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009439
Matt Carlson679563f2009-09-01 12:55:46 +00009440err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009441 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9442 struct tg3_napi *tnapi = &tp->napi[i];
9443 free_irq(tnapi->irq_vec, tnapi);
9444 }
Matt Carlson07b01732009-08-28 14:01:15 +00009445
Matt Carlson679563f2009-09-01 12:55:46 +00009446err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009447 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009448 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009449 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009450
9451err_out1:
9452 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009453 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009454}
9455
Eric Dumazet511d2222010-07-07 20:44:24 +00009456static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9457 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009458static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9459
9460static int tg3_close(struct net_device *dev)
9461{
Matt Carlson4f125f42009-09-01 12:55:02 +00009462 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463 struct tg3 *tp = netdev_priv(dev);
9464
Matt Carlsonfed97812009-09-01 13:10:19 +00009465 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009466 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009467
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009468 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009469
9470 del_timer_sync(&tp->timer);
9471
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009472 tg3_phy_stop(tp);
9473
David S. Millerf47c11e2005-06-24 20:18:35 -07009474 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009475
9476 tg3_disable_ints(tp);
9477
Michael Chan944d9802005-05-29 14:57:48 -07009478 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009479 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009480 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009481
David S. Millerf47c11e2005-06-24 20:18:35 -07009482 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009483
Matt Carlson4f125f42009-09-01 12:55:02 +00009484 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9485 struct tg3_napi *tnapi = &tp->napi[i];
9486 free_irq(tnapi->irq_vec, tnapi);
9487 }
Matt Carlson07b01732009-08-28 14:01:15 +00009488
9489 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009490
Eric Dumazet511d2222010-07-07 20:44:24 +00009491 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9492
Linus Torvalds1da177e2005-04-16 15:20:36 -07009493 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9494 sizeof(tp->estats_prev));
9495
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009496 tg3_napi_fini(tp);
9497
Linus Torvalds1da177e2005-04-16 15:20:36 -07009498 tg3_free_consistent(tp);
9499
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009500 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009501
9502 netif_carrier_off(tp->dev);
9503
Linus Torvalds1da177e2005-04-16 15:20:36 -07009504 return 0;
9505}
9506
Eric Dumazet511d2222010-07-07 20:44:24 +00009507static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009508{
9509 return ((u64)val->high << 32) | ((u64)val->low);
9510}
9511
Eric Dumazet511d2222010-07-07 20:44:24 +00009512static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009513{
9514 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9515
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009516 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009517 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9518 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009519 u32 val;
9520
David S. Millerf47c11e2005-06-24 20:18:35 -07009521 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009522 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9523 tg3_writephy(tp, MII_TG3_TEST1,
9524 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009525 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009526 } else
9527 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009528 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009529
9530 tp->phy_crc_errors += val;
9531
9532 return tp->phy_crc_errors;
9533 }
9534
9535 return get_stat64(&hw_stats->rx_fcs_errors);
9536}
9537
9538#define ESTAT_ADD(member) \
9539 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009540 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009541
9542static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9543{
9544 struct tg3_ethtool_stats *estats = &tp->estats;
9545 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9546 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9547
9548 if (!hw_stats)
9549 return old_estats;
9550
9551 ESTAT_ADD(rx_octets);
9552 ESTAT_ADD(rx_fragments);
9553 ESTAT_ADD(rx_ucast_packets);
9554 ESTAT_ADD(rx_mcast_packets);
9555 ESTAT_ADD(rx_bcast_packets);
9556 ESTAT_ADD(rx_fcs_errors);
9557 ESTAT_ADD(rx_align_errors);
9558 ESTAT_ADD(rx_xon_pause_rcvd);
9559 ESTAT_ADD(rx_xoff_pause_rcvd);
9560 ESTAT_ADD(rx_mac_ctrl_rcvd);
9561 ESTAT_ADD(rx_xoff_entered);
9562 ESTAT_ADD(rx_frame_too_long_errors);
9563 ESTAT_ADD(rx_jabbers);
9564 ESTAT_ADD(rx_undersize_packets);
9565 ESTAT_ADD(rx_in_length_errors);
9566 ESTAT_ADD(rx_out_length_errors);
9567 ESTAT_ADD(rx_64_or_less_octet_packets);
9568 ESTAT_ADD(rx_65_to_127_octet_packets);
9569 ESTAT_ADD(rx_128_to_255_octet_packets);
9570 ESTAT_ADD(rx_256_to_511_octet_packets);
9571 ESTAT_ADD(rx_512_to_1023_octet_packets);
9572 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9573 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9574 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9575 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9576 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9577
9578 ESTAT_ADD(tx_octets);
9579 ESTAT_ADD(tx_collisions);
9580 ESTAT_ADD(tx_xon_sent);
9581 ESTAT_ADD(tx_xoff_sent);
9582 ESTAT_ADD(tx_flow_control);
9583 ESTAT_ADD(tx_mac_errors);
9584 ESTAT_ADD(tx_single_collisions);
9585 ESTAT_ADD(tx_mult_collisions);
9586 ESTAT_ADD(tx_deferred);
9587 ESTAT_ADD(tx_excessive_collisions);
9588 ESTAT_ADD(tx_late_collisions);
9589 ESTAT_ADD(tx_collide_2times);
9590 ESTAT_ADD(tx_collide_3times);
9591 ESTAT_ADD(tx_collide_4times);
9592 ESTAT_ADD(tx_collide_5times);
9593 ESTAT_ADD(tx_collide_6times);
9594 ESTAT_ADD(tx_collide_7times);
9595 ESTAT_ADD(tx_collide_8times);
9596 ESTAT_ADD(tx_collide_9times);
9597 ESTAT_ADD(tx_collide_10times);
9598 ESTAT_ADD(tx_collide_11times);
9599 ESTAT_ADD(tx_collide_12times);
9600 ESTAT_ADD(tx_collide_13times);
9601 ESTAT_ADD(tx_collide_14times);
9602 ESTAT_ADD(tx_collide_15times);
9603 ESTAT_ADD(tx_ucast_packets);
9604 ESTAT_ADD(tx_mcast_packets);
9605 ESTAT_ADD(tx_bcast_packets);
9606 ESTAT_ADD(tx_carrier_sense_errors);
9607 ESTAT_ADD(tx_discards);
9608 ESTAT_ADD(tx_errors);
9609
9610 ESTAT_ADD(dma_writeq_full);
9611 ESTAT_ADD(dma_write_prioq_full);
9612 ESTAT_ADD(rxbds_empty);
9613 ESTAT_ADD(rx_discards);
9614 ESTAT_ADD(rx_errors);
9615 ESTAT_ADD(rx_threshold_hit);
9616
9617 ESTAT_ADD(dma_readq_full);
9618 ESTAT_ADD(dma_read_prioq_full);
9619 ESTAT_ADD(tx_comp_queue_full);
9620
9621 ESTAT_ADD(ring_set_send_prod_index);
9622 ESTAT_ADD(ring_status_update);
9623 ESTAT_ADD(nic_irqs);
9624 ESTAT_ADD(nic_avoided_irqs);
9625 ESTAT_ADD(nic_tx_threshold_hit);
9626
Matt Carlson4452d092011-05-19 12:12:51 +00009627 ESTAT_ADD(mbuf_lwm_thresh_hit);
9628
Linus Torvalds1da177e2005-04-16 15:20:36 -07009629 return estats;
9630}
9631
Eric Dumazet511d2222010-07-07 20:44:24 +00009632static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9633 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009634{
9635 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009636 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009637 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9638
9639 if (!hw_stats)
9640 return old_stats;
9641
9642 stats->rx_packets = old_stats->rx_packets +
9643 get_stat64(&hw_stats->rx_ucast_packets) +
9644 get_stat64(&hw_stats->rx_mcast_packets) +
9645 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009646
Linus Torvalds1da177e2005-04-16 15:20:36 -07009647 stats->tx_packets = old_stats->tx_packets +
9648 get_stat64(&hw_stats->tx_ucast_packets) +
9649 get_stat64(&hw_stats->tx_mcast_packets) +
9650 get_stat64(&hw_stats->tx_bcast_packets);
9651
9652 stats->rx_bytes = old_stats->rx_bytes +
9653 get_stat64(&hw_stats->rx_octets);
9654 stats->tx_bytes = old_stats->tx_bytes +
9655 get_stat64(&hw_stats->tx_octets);
9656
9657 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009658 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009659 stats->tx_errors = old_stats->tx_errors +
9660 get_stat64(&hw_stats->tx_errors) +
9661 get_stat64(&hw_stats->tx_mac_errors) +
9662 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9663 get_stat64(&hw_stats->tx_discards);
9664
9665 stats->multicast = old_stats->multicast +
9666 get_stat64(&hw_stats->rx_mcast_packets);
9667 stats->collisions = old_stats->collisions +
9668 get_stat64(&hw_stats->tx_collisions);
9669
9670 stats->rx_length_errors = old_stats->rx_length_errors +
9671 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9672 get_stat64(&hw_stats->rx_undersize_packets);
9673
9674 stats->rx_over_errors = old_stats->rx_over_errors +
9675 get_stat64(&hw_stats->rxbds_empty);
9676 stats->rx_frame_errors = old_stats->rx_frame_errors +
9677 get_stat64(&hw_stats->rx_align_errors);
9678 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9679 get_stat64(&hw_stats->tx_discards);
9680 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9681 get_stat64(&hw_stats->tx_carrier_sense_errors);
9682
9683 stats->rx_crc_errors = old_stats->rx_crc_errors +
9684 calc_crc_errors(tp);
9685
John W. Linville4f63b872005-09-12 14:43:18 -07009686 stats->rx_missed_errors = old_stats->rx_missed_errors +
9687 get_stat64(&hw_stats->rx_discards);
9688
Eric Dumazetb0057c52010-10-10 19:55:52 +00009689 stats->rx_dropped = tp->rx_dropped;
9690
Linus Torvalds1da177e2005-04-16 15:20:36 -07009691 return stats;
9692}
9693
9694static inline u32 calc_crc(unsigned char *buf, int len)
9695{
9696 u32 reg;
9697 u32 tmp;
9698 int j, k;
9699
9700 reg = 0xffffffff;
9701
9702 for (j = 0; j < len; j++) {
9703 reg ^= buf[j];
9704
9705 for (k = 0; k < 8; k++) {
9706 tmp = reg & 0x01;
9707
9708 reg >>= 1;
9709
Matt Carlson859a588792010-04-05 10:19:28 +00009710 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009711 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009712 }
9713 }
9714
9715 return ~reg;
9716}
9717
9718static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9719{
9720 /* accept or reject all multicast frames */
9721 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9722 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9723 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9724 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9725}
9726
9727static void __tg3_set_rx_mode(struct net_device *dev)
9728{
9729 struct tg3 *tp = netdev_priv(dev);
9730 u32 rx_mode;
9731
9732 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9733 RX_MODE_KEEP_VLAN_TAG);
9734
Matt Carlsonbf933c82011-01-25 15:58:49 +00009735#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009736 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9737 * flag clear.
9738 */
Joe Perches63c3a662011-04-26 08:12:10 +00009739 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009740 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9741#endif
9742
9743 if (dev->flags & IFF_PROMISC) {
9744 /* Promiscuous mode. */
9745 rx_mode |= RX_MODE_PROMISC;
9746 } else if (dev->flags & IFF_ALLMULTI) {
9747 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009748 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009749 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009750 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009751 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009752 } else {
9753 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009754 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009755 u32 mc_filter[4] = { 0, };
9756 u32 regidx;
9757 u32 bit;
9758 u32 crc;
9759
Jiri Pirko22bedad32010-04-01 21:22:57 +00009760 netdev_for_each_mc_addr(ha, dev) {
9761 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009762 bit = ~crc & 0x7f;
9763 regidx = (bit & 0x60) >> 5;
9764 bit &= 0x1f;
9765 mc_filter[regidx] |= (1 << bit);
9766 }
9767
9768 tw32(MAC_HASH_REG_0, mc_filter[0]);
9769 tw32(MAC_HASH_REG_1, mc_filter[1]);
9770 tw32(MAC_HASH_REG_2, mc_filter[2]);
9771 tw32(MAC_HASH_REG_3, mc_filter[3]);
9772 }
9773
9774 if (rx_mode != tp->rx_mode) {
9775 tp->rx_mode = rx_mode;
9776 tw32_f(MAC_RX_MODE, rx_mode);
9777 udelay(10);
9778 }
9779}
9780
9781static void tg3_set_rx_mode(struct net_device *dev)
9782{
9783 struct tg3 *tp = netdev_priv(dev);
9784
Michael Chane75f7c92006-03-20 21:33:26 -08009785 if (!netif_running(dev))
9786 return;
9787
David S. Millerf47c11e2005-06-24 20:18:35 -07009788 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009789 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009790 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009791}
9792
Linus Torvalds1da177e2005-04-16 15:20:36 -07009793static int tg3_get_regs_len(struct net_device *dev)
9794{
Matt Carlson97bd8e42011-04-13 11:05:04 +00009795 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796}
9797
9798static void tg3_get_regs(struct net_device *dev,
9799 struct ethtool_regs *regs, void *_p)
9800{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009801 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009802
9803 regs->version = 0;
9804
Matt Carlson97bd8e42011-04-13 11:05:04 +00009805 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806
Matt Carlson80096062010-08-02 11:26:06 +00009807 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009808 return;
9809
David S. Millerf47c11e2005-06-24 20:18:35 -07009810 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009811
Matt Carlson97bd8e42011-04-13 11:05:04 +00009812 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009813
David S. Millerf47c11e2005-06-24 20:18:35 -07009814 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009815}
9816
9817static int tg3_get_eeprom_len(struct net_device *dev)
9818{
9819 struct tg3 *tp = netdev_priv(dev);
9820
9821 return tp->nvram_size;
9822}
9823
Linus Torvalds1da177e2005-04-16 15:20:36 -07009824static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9825{
9826 struct tg3 *tp = netdev_priv(dev);
9827 int ret;
9828 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009829 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009830 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009831
Joe Perches63c3a662011-04-26 08:12:10 +00009832 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +00009833 return -EINVAL;
9834
Matt Carlson80096062010-08-02 11:26:06 +00009835 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009836 return -EAGAIN;
9837
Linus Torvalds1da177e2005-04-16 15:20:36 -07009838 offset = eeprom->offset;
9839 len = eeprom->len;
9840 eeprom->len = 0;
9841
9842 eeprom->magic = TG3_EEPROM_MAGIC;
9843
9844 if (offset & 3) {
9845 /* adjustments to start on required 4 byte boundary */
9846 b_offset = offset & 3;
9847 b_count = 4 - b_offset;
9848 if (b_count > len) {
9849 /* i.e. offset=1 len=2 */
9850 b_count = len;
9851 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009852 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009853 if (ret)
9854 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009855 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009856 len -= b_count;
9857 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009858 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009859 }
9860
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009861 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009862 pd = &data[eeprom->len];
9863 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009864 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009865 if (ret) {
9866 eeprom->len += i;
9867 return ret;
9868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009869 memcpy(pd + i, &val, 4);
9870 }
9871 eeprom->len += i;
9872
9873 if (len & 3) {
9874 /* read last bytes not ending on 4 byte boundary */
9875 pd = &data[eeprom->len];
9876 b_count = len & 3;
9877 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009878 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009879 if (ret)
9880 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009881 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009882 eeprom->len += b_count;
9883 }
9884 return 0;
9885}
9886
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009887static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009888
9889static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9890{
9891 struct tg3 *tp = netdev_priv(dev);
9892 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009893 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009894 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009895 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009896
Matt Carlson80096062010-08-02 11:26:06 +00009897 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009898 return -EAGAIN;
9899
Joe Perches63c3a662011-04-26 08:12:10 +00009900 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +00009901 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009902 return -EINVAL;
9903
9904 offset = eeprom->offset;
9905 len = eeprom->len;
9906
9907 if ((b_offset = (offset & 3))) {
9908 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009909 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009910 if (ret)
9911 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009912 len += b_offset;
9913 offset &= ~3;
Michael Chan1c8594b42005-04-21 17:12:46 -07009914 if (len < 4)
9915 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009916 }
9917
9918 odd_len = 0;
Michael Chan1c8594b42005-04-21 17:12:46 -07009919 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009920 /* adjustments to end on required 4 byte boundary */
9921 odd_len = 1;
9922 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009923 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009924 if (ret)
9925 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009926 }
9927
9928 buf = data;
9929 if (b_offset || odd_len) {
9930 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009931 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009932 return -ENOMEM;
9933 if (b_offset)
9934 memcpy(buf, &start, 4);
9935 if (odd_len)
9936 memcpy(buf+len-4, &end, 4);
9937 memcpy(buf + b_offset, data, eeprom->len);
9938 }
9939
9940 ret = tg3_nvram_write_block(tp, offset, len, buf);
9941
9942 if (buf != data)
9943 kfree(buf);
9944
9945 return ret;
9946}
9947
9948static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9949{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009950 struct tg3 *tp = netdev_priv(dev);
9951
Joe Perches63c3a662011-04-26 08:12:10 +00009952 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009953 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009954 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009955 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009956 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9957 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009958 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009959
Linus Torvalds1da177e2005-04-16 15:20:36 -07009960 cmd->supported = (SUPPORTED_Autoneg);
9961
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009962 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009963 cmd->supported |= (SUPPORTED_1000baseT_Half |
9964 SUPPORTED_1000baseT_Full);
9965
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009966 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009967 cmd->supported |= (SUPPORTED_100baseT_Half |
9968 SUPPORTED_100baseT_Full |
9969 SUPPORTED_10baseT_Half |
9970 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009971 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009972 cmd->port = PORT_TP;
9973 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009974 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009975 cmd->port = PORT_FIBRE;
9976 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009977
Linus Torvalds1da177e2005-04-16 15:20:36 -07009978 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +00009979 if (tg3_flag(tp, PAUSE_AUTONEG)) {
9980 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
9981 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
9982 cmd->advertising |= ADVERTISED_Pause;
9983 } else {
9984 cmd->advertising |= ADVERTISED_Pause |
9985 ADVERTISED_Asym_Pause;
9986 }
9987 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
9988 cmd->advertising |= ADVERTISED_Asym_Pause;
9989 }
9990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009991 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +00009992 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009993 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +00009994 } else {
David Decotigny70739492011-04-27 18:32:40 +00009995 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +00009996 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009997 }
Matt Carlson882e9792009-09-01 13:21:36 +00009998 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009999 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010000 cmd->autoneg = tp->link_config.autoneg;
10001 cmd->maxtxpkt = 0;
10002 cmd->maxrxpkt = 0;
10003 return 0;
10004}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010005
Linus Torvalds1da177e2005-04-16 15:20:36 -070010006static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10007{
10008 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010009 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010010
Joe Perches63c3a662011-04-26 08:12:10 +000010011 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010012 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010013 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010014 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010015 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10016 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010017 }
10018
Matt Carlson7e5856b2009-02-25 14:23:01 +000010019 if (cmd->autoneg != AUTONEG_ENABLE &&
10020 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010021 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010022
10023 if (cmd->autoneg == AUTONEG_DISABLE &&
10024 cmd->duplex != DUPLEX_FULL &&
10025 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010026 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010027
Matt Carlson7e5856b2009-02-25 14:23:01 +000010028 if (cmd->autoneg == AUTONEG_ENABLE) {
10029 u32 mask = ADVERTISED_Autoneg |
10030 ADVERTISED_Pause |
10031 ADVERTISED_Asym_Pause;
10032
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010033 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010034 mask |= ADVERTISED_1000baseT_Half |
10035 ADVERTISED_1000baseT_Full;
10036
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010037 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010038 mask |= ADVERTISED_100baseT_Half |
10039 ADVERTISED_100baseT_Full |
10040 ADVERTISED_10baseT_Half |
10041 ADVERTISED_10baseT_Full |
10042 ADVERTISED_TP;
10043 else
10044 mask |= ADVERTISED_FIBRE;
10045
10046 if (cmd->advertising & ~mask)
10047 return -EINVAL;
10048
10049 mask &= (ADVERTISED_1000baseT_Half |
10050 ADVERTISED_1000baseT_Full |
10051 ADVERTISED_100baseT_Half |
10052 ADVERTISED_100baseT_Full |
10053 ADVERTISED_10baseT_Half |
10054 ADVERTISED_10baseT_Full);
10055
10056 cmd->advertising &= mask;
10057 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010058 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010059 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010060 return -EINVAL;
10061
10062 if (cmd->duplex != DUPLEX_FULL)
10063 return -EINVAL;
10064 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010065 if (speed != SPEED_100 &&
10066 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010067 return -EINVAL;
10068 }
10069 }
10070
David S. Millerf47c11e2005-06-24 20:18:35 -070010071 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010072
10073 tp->link_config.autoneg = cmd->autoneg;
10074 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010075 tp->link_config.advertising = (cmd->advertising |
10076 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010077 tp->link_config.speed = SPEED_INVALID;
10078 tp->link_config.duplex = DUPLEX_INVALID;
10079 } else {
10080 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010081 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010082 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010083 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010084
Michael Chan24fcad62006-12-17 17:06:46 -080010085 tp->link_config.orig_speed = tp->link_config.speed;
10086 tp->link_config.orig_duplex = tp->link_config.duplex;
10087 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10088
Linus Torvalds1da177e2005-04-16 15:20:36 -070010089 if (netif_running(dev))
10090 tg3_setup_phy(tp, 1);
10091
David S. Millerf47c11e2005-06-24 20:18:35 -070010092 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010093
Linus Torvalds1da177e2005-04-16 15:20:36 -070010094 return 0;
10095}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010096
Linus Torvalds1da177e2005-04-16 15:20:36 -070010097static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10098{
10099 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010100
Linus Torvalds1da177e2005-04-16 15:20:36 -070010101 strcpy(info->driver, DRV_MODULE_NAME);
10102 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010103 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010104 strcpy(info->bus_info, pci_name(tp->pdev));
10105}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010106
Linus Torvalds1da177e2005-04-16 15:20:36 -070010107static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10108{
10109 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010110
Joe Perches63c3a662011-04-26 08:12:10 +000010111 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010112 wol->supported = WAKE_MAGIC;
10113 else
10114 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010115 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010116 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010117 wol->wolopts = WAKE_MAGIC;
10118 memset(&wol->sopass, 0, sizeof(wol->sopass));
10119}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010120
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10122{
10123 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010124 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010125
Linus Torvalds1da177e2005-04-16 15:20:36 -070010126 if (wol->wolopts & ~WAKE_MAGIC)
10127 return -EINVAL;
10128 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010129 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010130 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010131
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010132 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10133
David S. Millerf47c11e2005-06-24 20:18:35 -070010134 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010135 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010136 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010137 else
Joe Perches63c3a662011-04-26 08:12:10 +000010138 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010139 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010140
Linus Torvalds1da177e2005-04-16 15:20:36 -070010141 return 0;
10142}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010143
Linus Torvalds1da177e2005-04-16 15:20:36 -070010144static u32 tg3_get_msglevel(struct net_device *dev)
10145{
10146 struct tg3 *tp = netdev_priv(dev);
10147 return tp->msg_enable;
10148}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010149
Linus Torvalds1da177e2005-04-16 15:20:36 -070010150static void tg3_set_msglevel(struct net_device *dev, u32 value)
10151{
10152 struct tg3 *tp = netdev_priv(dev);
10153 tp->msg_enable = value;
10154}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010155
Linus Torvalds1da177e2005-04-16 15:20:36 -070010156static int tg3_nway_reset(struct net_device *dev)
10157{
10158 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010159 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010160
Linus Torvalds1da177e2005-04-16 15:20:36 -070010161 if (!netif_running(dev))
10162 return -EAGAIN;
10163
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010164 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010165 return -EINVAL;
10166
Joe Perches63c3a662011-04-26 08:12:10 +000010167 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010168 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010169 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010170 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010171 } else {
10172 u32 bmcr;
10173
10174 spin_lock_bh(&tp->lock);
10175 r = -EINVAL;
10176 tg3_readphy(tp, MII_BMCR, &bmcr);
10177 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10178 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010179 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010180 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10181 BMCR_ANENABLE);
10182 r = 0;
10183 }
10184 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010185 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010186
Linus Torvalds1da177e2005-04-16 15:20:36 -070010187 return r;
10188}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010189
Linus Torvalds1da177e2005-04-16 15:20:36 -070010190static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10191{
10192 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010193
Matt Carlson2c49a442010-09-30 10:34:35 +000010194 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010195 ering->rx_mini_max_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010196 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010197 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010198 else
10199 ering->rx_jumbo_max_pending = 0;
10200
10201 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010202
10203 ering->rx_pending = tp->rx_pending;
10204 ering->rx_mini_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010205 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010206 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10207 else
10208 ering->rx_jumbo_pending = 0;
10209
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010210 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010211}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010212
Linus Torvalds1da177e2005-04-16 15:20:36 -070010213static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10214{
10215 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010216 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010217
Matt Carlson2c49a442010-09-30 10:34:35 +000010218 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10219 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010220 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10221 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010222 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010223 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010224 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010225
Michael Chanbbe832c2005-06-24 20:20:04 -070010226 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010227 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010228 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010229 irq_sync = 1;
10230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010231
Michael Chanbbe832c2005-06-24 20:20:04 -070010232 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010233
Linus Torvalds1da177e2005-04-16 15:20:36 -070010234 tp->rx_pending = ering->rx_pending;
10235
Joe Perches63c3a662011-04-26 08:12:10 +000010236 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010237 tp->rx_pending > 63)
10238 tp->rx_pending = 63;
10239 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010240
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010241 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010242 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010243
10244 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010245 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010246 err = tg3_restart_hw(tp, 1);
10247 if (!err)
10248 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010249 }
10250
David S. Millerf47c11e2005-06-24 20:18:35 -070010251 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010252
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010253 if (irq_sync && !err)
10254 tg3_phy_start(tp);
10255
Michael Chanb9ec6c12006-07-25 16:37:27 -070010256 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010257}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010258
Linus Torvalds1da177e2005-04-16 15:20:36 -070010259static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10260{
10261 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010262
Joe Perches63c3a662011-04-26 08:12:10 +000010263 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010264
Steve Glendinninge18ce342008-12-16 02:00:00 -080010265 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010266 epause->rx_pause = 1;
10267 else
10268 epause->rx_pause = 0;
10269
Steve Glendinninge18ce342008-12-16 02:00:00 -080010270 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010271 epause->tx_pause = 1;
10272 else
10273 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010274}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010275
Linus Torvalds1da177e2005-04-16 15:20:36 -070010276static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10277{
10278 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010279 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010280
Joe Perches63c3a662011-04-26 08:12:10 +000010281 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010282 u32 newadv;
10283 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010284
Matt Carlson27121682010-02-17 15:16:57 +000010285 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010286
Matt Carlson27121682010-02-17 15:16:57 +000010287 if (!(phydev->supported & SUPPORTED_Pause) ||
10288 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010289 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010290 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010291
Matt Carlson27121682010-02-17 15:16:57 +000010292 tp->link_config.flowctrl = 0;
10293 if (epause->rx_pause) {
10294 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010295
Matt Carlson27121682010-02-17 15:16:57 +000010296 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010297 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010298 newadv = ADVERTISED_Pause;
10299 } else
10300 newadv = ADVERTISED_Pause |
10301 ADVERTISED_Asym_Pause;
10302 } else if (epause->tx_pause) {
10303 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10304 newadv = ADVERTISED_Asym_Pause;
10305 } else
10306 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010307
Matt Carlson27121682010-02-17 15:16:57 +000010308 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010309 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010310 else
Joe Perches63c3a662011-04-26 08:12:10 +000010311 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010312
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010313 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010314 u32 oldadv = phydev->advertising &
10315 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10316 if (oldadv != newadv) {
10317 phydev->advertising &=
10318 ~(ADVERTISED_Pause |
10319 ADVERTISED_Asym_Pause);
10320 phydev->advertising |= newadv;
10321 if (phydev->autoneg) {
10322 /*
10323 * Always renegotiate the link to
10324 * inform our link partner of our
10325 * flow control settings, even if the
10326 * flow control is forced. Let
10327 * tg3_adjust_link() do the final
10328 * flow control setup.
10329 */
10330 return phy_start_aneg(phydev);
10331 }
10332 }
10333
10334 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010335 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010336 } else {
10337 tp->link_config.orig_advertising &=
10338 ~(ADVERTISED_Pause |
10339 ADVERTISED_Asym_Pause);
10340 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010341 }
10342 } else {
10343 int irq_sync = 0;
10344
10345 if (netif_running(dev)) {
10346 tg3_netif_stop(tp);
10347 irq_sync = 1;
10348 }
10349
10350 tg3_full_lock(tp, irq_sync);
10351
10352 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010353 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010354 else
Joe Perches63c3a662011-04-26 08:12:10 +000010355 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010356 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010357 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010358 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010359 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010360 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010361 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010362 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010363 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010364
10365 if (netif_running(dev)) {
10366 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10367 err = tg3_restart_hw(tp, 1);
10368 if (!err)
10369 tg3_netif_start(tp);
10370 }
10371
10372 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010374
Michael Chanb9ec6c12006-07-25 16:37:27 -070010375 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010376}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010377
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010378static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010379{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010380 switch (sset) {
10381 case ETH_SS_TEST:
10382 return TG3_NUM_TEST;
10383 case ETH_SS_STATS:
10384 return TG3_NUM_STATS;
10385 default:
10386 return -EOPNOTSUPP;
10387 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010388}
10389
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010390static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010391{
10392 switch (stringset) {
10393 case ETH_SS_STATS:
10394 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10395 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010396 case ETH_SS_TEST:
10397 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10398 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010399 default:
10400 WARN_ON(1); /* we need a WARN() */
10401 break;
10402 }
10403}
10404
stephen hemminger81b87092011-04-04 08:43:50 +000010405static int tg3_set_phys_id(struct net_device *dev,
10406 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010407{
10408 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010409
10410 if (!netif_running(tp->dev))
10411 return -EAGAIN;
10412
stephen hemminger81b87092011-04-04 08:43:50 +000010413 switch (state) {
10414 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010415 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010416
stephen hemminger81b87092011-04-04 08:43:50 +000010417 case ETHTOOL_ID_ON:
10418 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10419 LED_CTRL_1000MBPS_ON |
10420 LED_CTRL_100MBPS_ON |
10421 LED_CTRL_10MBPS_ON |
10422 LED_CTRL_TRAFFIC_OVERRIDE |
10423 LED_CTRL_TRAFFIC_BLINK |
10424 LED_CTRL_TRAFFIC_LED);
10425 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010426
stephen hemminger81b87092011-04-04 08:43:50 +000010427 case ETHTOOL_ID_OFF:
10428 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10429 LED_CTRL_TRAFFIC_OVERRIDE);
10430 break;
Michael Chan4009a932005-09-05 17:52:54 -070010431
stephen hemminger81b87092011-04-04 08:43:50 +000010432 case ETHTOOL_ID_INACTIVE:
10433 tw32(MAC_LED_CTRL, tp->led_ctrl);
10434 break;
Michael Chan4009a932005-09-05 17:52:54 -070010435 }
stephen hemminger81b87092011-04-04 08:43:50 +000010436
Michael Chan4009a932005-09-05 17:52:54 -070010437 return 0;
10438}
10439
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010440static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010441 struct ethtool_stats *estats, u64 *tmp_stats)
10442{
10443 struct tg3 *tp = netdev_priv(dev);
10444 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10445}
10446
Matt Carlsonc3e94502011-04-13 11:05:08 +000010447static __be32 * tg3_vpd_readblock(struct tg3 *tp)
10448{
10449 int i;
10450 __be32 *buf;
10451 u32 offset = 0, len = 0;
10452 u32 magic, val;
10453
Joe Perches63c3a662011-04-26 08:12:10 +000010454 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010455 return NULL;
10456
10457 if (magic == TG3_EEPROM_MAGIC) {
10458 for (offset = TG3_NVM_DIR_START;
10459 offset < TG3_NVM_DIR_END;
10460 offset += TG3_NVM_DIRENT_SIZE) {
10461 if (tg3_nvram_read(tp, offset, &val))
10462 return NULL;
10463
10464 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10465 TG3_NVM_DIRTYPE_EXTVPD)
10466 break;
10467 }
10468
10469 if (offset != TG3_NVM_DIR_END) {
10470 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10471 if (tg3_nvram_read(tp, offset + 4, &offset))
10472 return NULL;
10473
10474 offset = tg3_nvram_logical_addr(tp, offset);
10475 }
10476 }
10477
10478 if (!offset || !len) {
10479 offset = TG3_NVM_VPD_OFF;
10480 len = TG3_NVM_VPD_LEN;
10481 }
10482
10483 buf = kmalloc(len, GFP_KERNEL);
10484 if (buf == NULL)
10485 return NULL;
10486
10487 if (magic == TG3_EEPROM_MAGIC) {
10488 for (i = 0; i < len; i += 4) {
10489 /* The data is in little-endian format in NVRAM.
10490 * Use the big-endian read routines to preserve
10491 * the byte order as it exists in NVRAM.
10492 */
10493 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10494 goto error;
10495 }
10496 } else {
10497 u8 *ptr;
10498 ssize_t cnt;
10499 unsigned int pos = 0;
10500
10501 ptr = (u8 *)&buf[0];
10502 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10503 cnt = pci_read_vpd(tp->pdev, pos,
10504 len - pos, ptr);
10505 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10506 cnt = 0;
10507 else if (cnt < 0)
10508 goto error;
10509 }
10510 if (pos != len)
10511 goto error;
10512 }
10513
10514 return buf;
10515
10516error:
10517 kfree(buf);
10518 return NULL;
10519}
10520
Michael Chan566f86a2005-05-29 14:56:58 -070010521#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010522#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10523#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10524#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010525#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10526#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
10527#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x4c
Michael Chanb16250e2006-09-27 16:10:14 -070010528#define NVRAM_SELFBOOT_HW_SIZE 0x20
10529#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010530
10531static int tg3_test_nvram(struct tg3 *tp)
10532{
Al Virob9fc7dc2007-12-17 22:59:57 -080010533 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010534 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010535 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010536
Joe Perches63c3a662011-04-26 08:12:10 +000010537 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010538 return 0;
10539
Matt Carlsone4f34112009-02-25 14:25:00 +000010540 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010541 return -EIO;
10542
Michael Chan1b277772006-03-20 22:27:48 -080010543 if (magic == TG3_EEPROM_MAGIC)
10544 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010545 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010546 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10547 TG3_EEPROM_SB_FORMAT_1) {
10548 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10549 case TG3_EEPROM_SB_REVISION_0:
10550 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10551 break;
10552 case TG3_EEPROM_SB_REVISION_2:
10553 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10554 break;
10555 case TG3_EEPROM_SB_REVISION_3:
10556 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10557 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010558 case TG3_EEPROM_SB_REVISION_4:
10559 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10560 break;
10561 case TG3_EEPROM_SB_REVISION_5:
10562 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10563 break;
10564 case TG3_EEPROM_SB_REVISION_6:
10565 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10566 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010567 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010568 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010569 }
10570 } else
Michael Chan1b277772006-03-20 22:27:48 -080010571 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010572 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10573 size = NVRAM_SELFBOOT_HW_SIZE;
10574 else
Michael Chan1b277772006-03-20 22:27:48 -080010575 return -EIO;
10576
10577 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010578 if (buf == NULL)
10579 return -ENOMEM;
10580
Michael Chan1b277772006-03-20 22:27:48 -080010581 err = -EIO;
10582 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010583 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10584 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010585 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010586 }
Michael Chan1b277772006-03-20 22:27:48 -080010587 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010588 goto out;
10589
Michael Chan1b277772006-03-20 22:27:48 -080010590 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010591 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010592 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010593 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010594 u8 *buf8 = (u8 *) buf, csum8 = 0;
10595
Al Virob9fc7dc2007-12-17 22:59:57 -080010596 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010597 TG3_EEPROM_SB_REVISION_2) {
10598 /* For rev 2, the csum doesn't include the MBA. */
10599 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10600 csum8 += buf8[i];
10601 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10602 csum8 += buf8[i];
10603 } else {
10604 for (i = 0; i < size; i++)
10605 csum8 += buf8[i];
10606 }
Michael Chan1b277772006-03-20 22:27:48 -080010607
Adrian Bunkad96b482006-04-05 22:21:04 -070010608 if (csum8 == 0) {
10609 err = 0;
10610 goto out;
10611 }
10612
10613 err = -EIO;
10614 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010615 }
Michael Chan566f86a2005-05-29 14:56:58 -070010616
Al Virob9fc7dc2007-12-17 22:59:57 -080010617 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010618 TG3_EEPROM_MAGIC_HW) {
10619 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010620 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010621 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010622
10623 /* Separate the parity bits and the data bytes. */
10624 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10625 if ((i == 0) || (i == 8)) {
10626 int l;
10627 u8 msk;
10628
10629 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10630 parity[k++] = buf8[i] & msk;
10631 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010632 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010633 int l;
10634 u8 msk;
10635
10636 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10637 parity[k++] = buf8[i] & msk;
10638 i++;
10639
10640 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10641 parity[k++] = buf8[i] & msk;
10642 i++;
10643 }
10644 data[j++] = buf8[i];
10645 }
10646
10647 err = -EIO;
10648 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10649 u8 hw8 = hweight8(data[i]);
10650
10651 if ((hw8 & 0x1) && parity[i])
10652 goto out;
10653 else if (!(hw8 & 0x1) && !parity[i])
10654 goto out;
10655 }
10656 err = 0;
10657 goto out;
10658 }
10659
Matt Carlson01c3a392011-03-09 16:58:20 +000010660 err = -EIO;
10661
Michael Chan566f86a2005-05-29 14:56:58 -070010662 /* Bootstrap checksum at offset 0x10 */
10663 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010664 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010665 goto out;
10666
10667 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10668 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010669 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010670 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010671
Matt Carlsonc3e94502011-04-13 11:05:08 +000010672 kfree(buf);
10673
10674 buf = tg3_vpd_readblock(tp);
10675 if (!buf)
10676 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010677
10678 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10679 PCI_VPD_LRDT_RO_DATA);
10680 if (i > 0) {
10681 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10682 if (j < 0)
10683 goto out;
10684
10685 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10686 goto out;
10687
10688 i += PCI_VPD_LRDT_TAG_SIZE;
10689 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10690 PCI_VPD_RO_KEYWORD_CHKSUM);
10691 if (j > 0) {
10692 u8 csum8 = 0;
10693
10694 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10695
10696 for (i = 0; i <= j; i++)
10697 csum8 += ((u8 *)buf)[i];
10698
10699 if (csum8)
10700 goto out;
10701 }
10702 }
10703
Michael Chan566f86a2005-05-29 14:56:58 -070010704 err = 0;
10705
10706out:
10707 kfree(buf);
10708 return err;
10709}
10710
Michael Chanca430072005-05-29 14:57:23 -070010711#define TG3_SERDES_TIMEOUT_SEC 2
10712#define TG3_COPPER_TIMEOUT_SEC 6
10713
10714static int tg3_test_link(struct tg3 *tp)
10715{
10716 int i, max;
10717
10718 if (!netif_running(tp->dev))
10719 return -ENODEV;
10720
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010721 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010722 max = TG3_SERDES_TIMEOUT_SEC;
10723 else
10724 max = TG3_COPPER_TIMEOUT_SEC;
10725
10726 for (i = 0; i < max; i++) {
10727 if (netif_carrier_ok(tp->dev))
10728 return 0;
10729
10730 if (msleep_interruptible(1000))
10731 break;
10732 }
10733
10734 return -EIO;
10735}
10736
Michael Chana71116d2005-05-29 14:58:11 -070010737/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010738static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010739{
Michael Chanb16250e2006-09-27 16:10:14 -070010740 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010741 u32 offset, read_mask, write_mask, val, save_val, read_val;
10742 static struct {
10743 u16 offset;
10744 u16 flags;
10745#define TG3_FL_5705 0x1
10746#define TG3_FL_NOT_5705 0x2
10747#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010748#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010749 u32 read_mask;
10750 u32 write_mask;
10751 } reg_tbl[] = {
10752 /* MAC Control Registers */
10753 { MAC_MODE, TG3_FL_NOT_5705,
10754 0x00000000, 0x00ef6f8c },
10755 { MAC_MODE, TG3_FL_5705,
10756 0x00000000, 0x01ef6b8c },
10757 { MAC_STATUS, TG3_FL_NOT_5705,
10758 0x03800107, 0x00000000 },
10759 { MAC_STATUS, TG3_FL_5705,
10760 0x03800100, 0x00000000 },
10761 { MAC_ADDR_0_HIGH, 0x0000,
10762 0x00000000, 0x0000ffff },
10763 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010764 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010765 { MAC_RX_MTU_SIZE, 0x0000,
10766 0x00000000, 0x0000ffff },
10767 { MAC_TX_MODE, 0x0000,
10768 0x00000000, 0x00000070 },
10769 { MAC_TX_LENGTHS, 0x0000,
10770 0x00000000, 0x00003fff },
10771 { MAC_RX_MODE, TG3_FL_NOT_5705,
10772 0x00000000, 0x000007fc },
10773 { MAC_RX_MODE, TG3_FL_5705,
10774 0x00000000, 0x000007dc },
10775 { MAC_HASH_REG_0, 0x0000,
10776 0x00000000, 0xffffffff },
10777 { MAC_HASH_REG_1, 0x0000,
10778 0x00000000, 0xffffffff },
10779 { MAC_HASH_REG_2, 0x0000,
10780 0x00000000, 0xffffffff },
10781 { MAC_HASH_REG_3, 0x0000,
10782 0x00000000, 0xffffffff },
10783
10784 /* Receive Data and Receive BD Initiator Control Registers. */
10785 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10786 0x00000000, 0xffffffff },
10787 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10788 0x00000000, 0xffffffff },
10789 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10790 0x00000000, 0x00000003 },
10791 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10792 0x00000000, 0xffffffff },
10793 { RCVDBDI_STD_BD+0, 0x0000,
10794 0x00000000, 0xffffffff },
10795 { RCVDBDI_STD_BD+4, 0x0000,
10796 0x00000000, 0xffffffff },
10797 { RCVDBDI_STD_BD+8, 0x0000,
10798 0x00000000, 0xffff0002 },
10799 { RCVDBDI_STD_BD+0xc, 0x0000,
10800 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010801
Michael Chana71116d2005-05-29 14:58:11 -070010802 /* Receive BD Initiator Control Registers. */
10803 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10804 0x00000000, 0xffffffff },
10805 { RCVBDI_STD_THRESH, TG3_FL_5705,
10806 0x00000000, 0x000003ff },
10807 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10808 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010809
Michael Chana71116d2005-05-29 14:58:11 -070010810 /* Host Coalescing Control Registers. */
10811 { HOSTCC_MODE, TG3_FL_NOT_5705,
10812 0x00000000, 0x00000004 },
10813 { HOSTCC_MODE, TG3_FL_5705,
10814 0x00000000, 0x000000f6 },
10815 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10816 0x00000000, 0xffffffff },
10817 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10818 0x00000000, 0x000003ff },
10819 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10820 0x00000000, 0xffffffff },
10821 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10822 0x00000000, 0x000003ff },
10823 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10824 0x00000000, 0xffffffff },
10825 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10826 0x00000000, 0x000000ff },
10827 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10828 0x00000000, 0xffffffff },
10829 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10830 0x00000000, 0x000000ff },
10831 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10832 0x00000000, 0xffffffff },
10833 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10834 0x00000000, 0xffffffff },
10835 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10836 0x00000000, 0xffffffff },
10837 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10838 0x00000000, 0x000000ff },
10839 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10840 0x00000000, 0xffffffff },
10841 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10842 0x00000000, 0x000000ff },
10843 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10844 0x00000000, 0xffffffff },
10845 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10846 0x00000000, 0xffffffff },
10847 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10848 0x00000000, 0xffffffff },
10849 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10850 0x00000000, 0xffffffff },
10851 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10852 0x00000000, 0xffffffff },
10853 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10854 0xffffffff, 0x00000000 },
10855 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10856 0xffffffff, 0x00000000 },
10857
10858 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010859 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010860 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010861 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010862 0x00000000, 0x007fffff },
10863 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10864 0x00000000, 0x0000003f },
10865 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10866 0x00000000, 0x000001ff },
10867 { BUFMGR_MB_HIGH_WATER, 0x0000,
10868 0x00000000, 0x000001ff },
10869 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10870 0xffffffff, 0x00000000 },
10871 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10872 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010873
Michael Chana71116d2005-05-29 14:58:11 -070010874 /* Mailbox Registers */
10875 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10876 0x00000000, 0x000001ff },
10877 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10878 0x00000000, 0x000001ff },
10879 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10880 0x00000000, 0x000007ff },
10881 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10882 0x00000000, 0x000001ff },
10883
10884 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10885 };
10886
Michael Chanb16250e2006-09-27 16:10:14 -070010887 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010888 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070010889 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000010890 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070010891 is_5750 = 1;
10892 }
Michael Chana71116d2005-05-29 14:58:11 -070010893
10894 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10895 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10896 continue;
10897
10898 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10899 continue;
10900
Joe Perches63c3a662011-04-26 08:12:10 +000010901 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070010902 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10903 continue;
10904
Michael Chanb16250e2006-09-27 16:10:14 -070010905 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10906 continue;
10907
Michael Chana71116d2005-05-29 14:58:11 -070010908 offset = (u32) reg_tbl[i].offset;
10909 read_mask = reg_tbl[i].read_mask;
10910 write_mask = reg_tbl[i].write_mask;
10911
10912 /* Save the original register content */
10913 save_val = tr32(offset);
10914
10915 /* Determine the read-only value. */
10916 read_val = save_val & read_mask;
10917
10918 /* Write zero to the register, then make sure the read-only bits
10919 * are not changed and the read/write bits are all zeros.
10920 */
10921 tw32(offset, 0);
10922
10923 val = tr32(offset);
10924
10925 /* Test the read-only and read/write bits. */
10926 if (((val & read_mask) != read_val) || (val & write_mask))
10927 goto out;
10928
10929 /* Write ones to all the bits defined by RdMask and WrMask, then
10930 * make sure the read-only bits are not changed and the
10931 * read/write bits are all ones.
10932 */
10933 tw32(offset, read_mask | write_mask);
10934
10935 val = tr32(offset);
10936
10937 /* Test the read-only bits. */
10938 if ((val & read_mask) != read_val)
10939 goto out;
10940
10941 /* Test the read/write bits. */
10942 if ((val & write_mask) != write_mask)
10943 goto out;
10944
10945 tw32(offset, save_val);
10946 }
10947
10948 return 0;
10949
10950out:
Michael Chan9f88f292006-12-07 00:22:54 -080010951 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010952 netdev_err(tp->dev,
10953 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010954 tw32(offset, save_val);
10955 return -EIO;
10956}
10957
Michael Chan7942e1d2005-05-29 14:58:36 -070010958static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10959{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010960 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010961 int i;
10962 u32 j;
10963
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010964 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010965 for (j = 0; j < len; j += 4) {
10966 u32 val;
10967
10968 tg3_write_mem(tp, offset + j, test_pattern[i]);
10969 tg3_read_mem(tp, offset + j, &val);
10970 if (val != test_pattern[i])
10971 return -EIO;
10972 }
10973 }
10974 return 0;
10975}
10976
10977static int tg3_test_memory(struct tg3 *tp)
10978{
10979 static struct mem_entry {
10980 u32 offset;
10981 u32 len;
10982 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010983 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010984 { 0x00002000, 0x1c000},
10985 { 0xffffffff, 0x00000}
10986 }, mem_tbl_5705[] = {
10987 { 0x00000100, 0x0000c},
10988 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010989 { 0x00004000, 0x00800},
10990 { 0x00006000, 0x01000},
10991 { 0x00008000, 0x02000},
10992 { 0x00010000, 0x0e000},
10993 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010994 }, mem_tbl_5755[] = {
10995 { 0x00000200, 0x00008},
10996 { 0x00004000, 0x00800},
10997 { 0x00006000, 0x00800},
10998 { 0x00008000, 0x02000},
10999 { 0x00010000, 0x0c000},
11000 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011001 }, mem_tbl_5906[] = {
11002 { 0x00000200, 0x00008},
11003 { 0x00004000, 0x00400},
11004 { 0x00006000, 0x00400},
11005 { 0x00008000, 0x01000},
11006 { 0x00010000, 0x01000},
11007 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011008 }, mem_tbl_5717[] = {
11009 { 0x00000200, 0x00008},
11010 { 0x00010000, 0x0a000},
11011 { 0x00020000, 0x13c00},
11012 { 0xffffffff, 0x00000}
11013 }, mem_tbl_57765[] = {
11014 { 0x00000200, 0x00008},
11015 { 0x00004000, 0x00800},
11016 { 0x00006000, 0x09800},
11017 { 0x00010000, 0x0a000},
11018 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011019 };
11020 struct mem_entry *mem_tbl;
11021 int err = 0;
11022 int i;
11023
Joe Perches63c3a662011-04-26 08:12:10 +000011024 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011025 mem_tbl = mem_tbl_5717;
11026 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11027 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011028 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011029 mem_tbl = mem_tbl_5755;
11030 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11031 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011032 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011033 mem_tbl = mem_tbl_5705;
11034 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011035 mem_tbl = mem_tbl_570x;
11036
11037 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011038 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11039 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011040 break;
11041 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011042
Michael Chan7942e1d2005-05-29 14:58:36 -070011043 return err;
11044}
11045
Michael Chan9f40dea2005-09-05 17:53:06 -070011046#define TG3_MAC_LOOPBACK 0
11047#define TG3_PHY_LOOPBACK 1
Matt Carlsonbb158d62011-04-25 12:42:47 +000011048#define TG3_TSO_LOOPBACK 2
11049
11050#define TG3_TSO_MSS 500
11051
11052#define TG3_TSO_IP_HDR_LEN 20
11053#define TG3_TSO_TCP_HDR_LEN 20
11054#define TG3_TSO_TCP_OPT_LEN 12
11055
11056static const u8 tg3_tso_header[] = {
110570x08, 0x00,
110580x45, 0x00, 0x00, 0x00,
110590x00, 0x00, 0x40, 0x00,
110600x40, 0x06, 0x00, 0x00,
110610x0a, 0x00, 0x00, 0x01,
110620x0a, 0x00, 0x00, 0x02,
110630x0d, 0x00, 0xe0, 0x00,
110640x00, 0x00, 0x01, 0x00,
110650x00, 0x00, 0x02, 0x00,
110660x80, 0x10, 0x10, 0x00,
110670x14, 0x09, 0x00, 0x00,
110680x01, 0x01, 0x08, 0x0a,
110690x11, 0x11, 0x11, 0x11,
110700x11, 0x11, 0x11, 0x11,
11071};
Michael Chan9f40dea2005-09-05 17:53:06 -070011072
Matt Carlson4852a862011-04-13 11:05:07 +000011073static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070011074{
Michael Chan9f40dea2005-09-05 17:53:06 -070011075 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011076 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Michael Chanc76949a2005-05-29 14:58:59 -070011077 struct sk_buff *skb, *rx_skb;
11078 u8 *tx_data;
11079 dma_addr_t map;
11080 int num_pkts, tx_len, rx_len, i, err;
11081 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011082 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011083 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011084
Matt Carlsonc8873402010-02-12 14:47:11 +000011085 tnapi = &tp->napi[0];
11086 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011087 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011088 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011089 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011090 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011091 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011092 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011093 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011094
Michael Chan9f40dea2005-09-05 17:53:06 -070011095 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070011096 /* HW errata - mac loopback fails in some cases on 5780.
11097 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000011098 * errata. Also, the MAC loopback test is deprecated for
11099 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070011100 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000011101 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011102 tg3_flag(tp, CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070011103 return 0;
11104
Matt Carlson49692ca2011-01-25 15:58:52 +000011105 mac_mode = tp->mac_mode &
11106 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
11107 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Joe Perches63c3a662011-04-26 08:12:10 +000011108 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011109 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011110 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070011111 mac_mode |= MAC_MODE_PORT_MODE_MII;
11112 else
11113 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070011114 tw32(MAC_MODE, mac_mode);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011115 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011116 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000011117 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080011118 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
11119 } else
11120 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070011121
Matt Carlson9ef8ca92007-07-11 19:48:29 -070011122 tg3_phy_toggle_automdix(tp, 0);
11123
Michael Chan3f7045c2006-09-27 16:02:29 -070011124 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070011125 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080011126
Matt Carlson49692ca2011-01-25 15:58:52 +000011127 mac_mode = tp->mac_mode &
11128 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011129 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000011130 tg3_writephy(tp, MII_TG3_FET_PTEST,
11131 MII_TG3_FET_PTEST_FRC_TX_LINK |
11132 MII_TG3_FET_PTEST_FRC_TX_LOCK);
11133 /* The write needs to be flushed for the AC131 */
11134 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11135 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080011136 mac_mode |= MAC_MODE_PORT_MODE_MII;
11137 } else
11138 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070011139
Michael Chanc94e3942005-09-27 12:12:42 -070011140 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011141 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070011142 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
11143 udelay(10);
11144 tw32_f(MAC_RX_MODE, tp->rx_mode);
11145 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011146 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000011147 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
11148 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011149 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000011150 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011151 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080011152 tg3_writephy(tp, MII_TG3_EXT_CTRL,
11153 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
11154 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011155 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000011156
11157 /* Wait for link */
11158 for (i = 0; i < 100; i++) {
11159 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11160 break;
11161 mdelay(1);
11162 }
Matt Carlson859a588792010-04-05 10:19:28 +000011163 }
Michael Chanc76949a2005-05-29 14:58:59 -070011164
11165 err = -EIO;
11166
Matt Carlson4852a862011-04-13 11:05:07 +000011167 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011168 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011169 if (!skb)
11170 return -ENOMEM;
11171
Michael Chanc76949a2005-05-29 14:58:59 -070011172 tx_data = skb_put(skb, tx_len);
11173 memcpy(tx_data, tp->dev->dev_addr, 6);
11174 memset(tx_data + 6, 0x0, 8);
11175
Matt Carlson4852a862011-04-13 11:05:07 +000011176 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011177
Matt Carlsonbb158d62011-04-25 12:42:47 +000011178 if (loopback_mode == TG3_TSO_LOOPBACK) {
11179 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11180
11181 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11182 TG3_TSO_TCP_OPT_LEN;
11183
11184 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11185 sizeof(tg3_tso_header));
11186 mss = TG3_TSO_MSS;
11187
11188 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11189 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11190
11191 /* Set the total length field in the IP header */
11192 iph->tot_len = htons((u16)(mss + hdr_len));
11193
11194 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11195 TXD_FLAG_CPU_POST_DMA);
11196
Joe Perches63c3a662011-04-26 08:12:10 +000011197 if (tg3_flag(tp, HW_TSO_1) ||
11198 tg3_flag(tp, HW_TSO_2) ||
11199 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011200 struct tcphdr *th;
11201 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11202 th = (struct tcphdr *)&tx_data[val];
11203 th->check = 0;
11204 } else
11205 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11206
Joe Perches63c3a662011-04-26 08:12:10 +000011207 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011208 mss |= (hdr_len & 0xc) << 12;
11209 if (hdr_len & 0x10)
11210 base_flags |= 0x00000010;
11211 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011212 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011213 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011214 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011215 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11216 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11217 } else {
11218 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11219 }
11220
11221 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11222 } else {
11223 num_pkts = 1;
11224 data_off = ETH_HLEN;
11225 }
11226
11227 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011228 tx_data[i] = (u8) (i & 0xff);
11229
Alexander Duyckf4188d82009-12-02 16:48:38 +000011230 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11231 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011232 dev_kfree_skb(skb);
11233 return -EIO;
11234 }
Michael Chanc76949a2005-05-29 14:58:59 -070011235
11236 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011237 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011238
11239 udelay(10);
11240
Matt Carlson898a56f2009-08-28 14:02:40 +000011241 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011242
Matt Carlsonbb158d62011-04-25 12:42:47 +000011243 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len,
11244 base_flags, (mss << 1) | 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011245
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011246 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011247
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011248 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11249 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011250
11251 udelay(10);
11252
Matt Carlson303fc922009-11-02 14:27:34 +000011253 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11254 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011255 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011256 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011257
11258 udelay(10);
11259
Matt Carlson898a56f2009-08-28 14:02:40 +000011260 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11261 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011262 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011263 (rx_idx == (rx_start_idx + num_pkts)))
11264 break;
11265 }
11266
Alexander Duyckf4188d82009-12-02 16:48:38 +000011267 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011268 dev_kfree_skb(skb);
11269
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011270 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011271 goto out;
11272
11273 if (rx_idx != rx_start_idx + num_pkts)
11274 goto out;
11275
Matt Carlsonbb158d62011-04-25 12:42:47 +000011276 val = data_off;
11277 while (rx_idx != rx_start_idx) {
11278 desc = &rnapi->rx_rcb[rx_start_idx++];
11279 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11280 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011281
Matt Carlsonbb158d62011-04-25 12:42:47 +000011282 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11283 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011284 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011285
Matt Carlsonbb158d62011-04-25 12:42:47 +000011286 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11287 - ETH_FCS_LEN;
11288
11289 if (loopback_mode != TG3_TSO_LOOPBACK) {
11290 if (rx_len != tx_len)
11291 goto out;
11292
11293 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11294 if (opaque_key != RXD_OPAQUE_RING_STD)
11295 goto out;
11296 } else {
11297 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11298 goto out;
11299 }
11300 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11301 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011302 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011303 goto out;
11304 }
11305
11306 if (opaque_key == RXD_OPAQUE_RING_STD) {
11307 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11308 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11309 mapping);
11310 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
11311 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11312 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11313 mapping);
11314 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011315 goto out;
11316
Matt Carlsonbb158d62011-04-25 12:42:47 +000011317 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11318 PCI_DMA_FROMDEVICE);
11319
11320 for (i = data_off; i < rx_len; i++, val++) {
11321 if (*(rx_skb->data + i) != (u8) (val & 0xff))
11322 goto out;
11323 }
Matt Carlson4852a862011-04-13 11:05:07 +000011324 }
11325
Michael Chanc76949a2005-05-29 14:58:59 -070011326 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011327
Michael Chanc76949a2005-05-29 14:58:59 -070011328 /* tg3_free_rings will unmap and free the rx_skb */
11329out:
11330 return err;
11331}
11332
Matt Carlson00c266b2011-04-25 12:42:46 +000011333#define TG3_STD_LOOPBACK_FAILED 1
11334#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011335#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson00c266b2011-04-25 12:42:46 +000011336
11337#define TG3_MAC_LOOPBACK_SHIFT 0
11338#define TG3_PHY_LOOPBACK_SHIFT 4
Matt Carlsonbb158d62011-04-25 12:42:47 +000011339#define TG3_LOOPBACK_FAILED 0x00000077
Michael Chan9f40dea2005-09-05 17:53:06 -070011340
11341static int tg3_test_loopback(struct tg3 *tp)
11342{
11343 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011344 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011345
11346 if (!netif_running(tp->dev))
11347 return TG3_LOOPBACK_FAILED;
11348
Matt Carlsonab789042011-01-25 15:58:54 +000011349 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11350 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11351
Michael Chanb9ec6c12006-07-25 16:37:27 -070011352 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011353 if (err) {
11354 err = TG3_LOOPBACK_FAILED;
11355 goto done;
11356 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011357
Joe Perches63c3a662011-04-26 08:12:10 +000011358 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011359 int i;
11360
11361 /* Reroute all rx packets to the 1st queue */
11362 for (i = MAC_RSS_INDIR_TBL_0;
11363 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11364 tw32(i, 0x0);
11365 }
11366
Matt Carlson6833c042008-11-21 17:18:59 -080011367 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011368 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011369 tg3_phy_toggle_apd(tp, false);
11370
Joe Perches63c3a662011-04-26 08:12:10 +000011371 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011372 int i;
11373 u32 status;
11374
11375 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11376
11377 /* Wait for up to 40 microseconds to acquire lock. */
11378 for (i = 0; i < 4; i++) {
11379 status = tr32(TG3_CPMU_MUTEX_GNT);
11380 if (status == CPMU_MUTEX_GNT_DRIVER)
11381 break;
11382 udelay(10);
11383 }
11384
Matt Carlsonab789042011-01-25 15:58:54 +000011385 if (status != CPMU_MUTEX_GNT_DRIVER) {
11386 err = TG3_LOOPBACK_FAILED;
11387 goto done;
11388 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011389
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011390 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011391 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011392 tw32(TG3_CPMU_CTRL,
11393 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11394 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011395 }
11396
Matt Carlson4852a862011-04-13 11:05:07 +000011397 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011398 err |= TG3_STD_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011399
Joe Perches63c3a662011-04-26 08:12:10 +000011400 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011401 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011402 err |= TG3_JMB_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson4852a862011-04-13 11:05:07 +000011403
Joe Perches63c3a662011-04-26 08:12:10 +000011404 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011405 tw32(TG3_CPMU_CTRL, cpmuctrl);
11406
11407 /* Release the mutex */
11408 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11409 }
11410
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011411 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011412 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson4852a862011-04-13 11:05:07 +000011413 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011414 err |= TG3_STD_LOOPBACK_FAILED <<
11415 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011416 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonbb158d62011-04-25 12:42:47 +000011417 tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_TSO_LOOPBACK))
11418 err |= TG3_TSO_LOOPBACK_FAILED <<
11419 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011420 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011421 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011422 err |= TG3_JMB_LOOPBACK_FAILED <<
11423 TG3_PHY_LOOPBACK_SHIFT;
Michael Chan9f40dea2005-09-05 17:53:06 -070011424 }
11425
Matt Carlson6833c042008-11-21 17:18:59 -080011426 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011427 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011428 tg3_phy_toggle_apd(tp, true);
11429
Matt Carlsonab789042011-01-25 15:58:54 +000011430done:
11431 tp->phy_flags |= eee_cap;
11432
Michael Chan9f40dea2005-09-05 17:53:06 -070011433 return err;
11434}
11435
Michael Chan4cafd3f2005-05-29 14:56:34 -070011436static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11437 u64 *data)
11438{
Michael Chan566f86a2005-05-29 14:56:58 -070011439 struct tg3 *tp = netdev_priv(dev);
11440
Matt Carlsonbed98292011-07-13 09:27:29 +000011441 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11442 tg3_power_up(tp)) {
11443 etest->flags |= ETH_TEST_FL_FAILED;
11444 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11445 return;
11446 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011447
Michael Chan566f86a2005-05-29 14:56:58 -070011448 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11449
11450 if (tg3_test_nvram(tp) != 0) {
11451 etest->flags |= ETH_TEST_FL_FAILED;
11452 data[0] = 1;
11453 }
Michael Chanca430072005-05-29 14:57:23 -070011454 if (tg3_test_link(tp) != 0) {
11455 etest->flags |= ETH_TEST_FL_FAILED;
11456 data[1] = 1;
11457 }
Michael Chana71116d2005-05-29 14:58:11 -070011458 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011459 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011460
Michael Chanbbe832c2005-06-24 20:20:04 -070011461 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011462 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011463 tg3_netif_stop(tp);
11464 irq_sync = 1;
11465 }
11466
11467 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011468
11469 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011470 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011471 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011472 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011473 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011474 if (!err)
11475 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011476
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011477 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad12006-03-20 22:27:35 -080011478 tg3_phy_reset(tp);
11479
Michael Chana71116d2005-05-29 14:58:11 -070011480 if (tg3_test_registers(tp) != 0) {
11481 etest->flags |= ETH_TEST_FL_FAILED;
11482 data[2] = 1;
11483 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011484 if (tg3_test_memory(tp) != 0) {
11485 etest->flags |= ETH_TEST_FL_FAILED;
11486 data[3] = 1;
11487 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011488 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011489 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011490
David S. Millerf47c11e2005-06-24 20:18:35 -070011491 tg3_full_unlock(tp);
11492
Michael Chand4bc3922005-05-29 14:59:20 -070011493 if (tg3_test_interrupt(tp) != 0) {
11494 etest->flags |= ETH_TEST_FL_FAILED;
11495 data[5] = 1;
11496 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011497
11498 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011499
Michael Chana71116d2005-05-29 14:58:11 -070011500 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11501 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011502 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011503 err2 = tg3_restart_hw(tp, 1);
11504 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011505 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011506 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011507
11508 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011509
11510 if (irq_sync && !err2)
11511 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011512 }
Matt Carlson80096062010-08-02 11:26:06 +000011513 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011514 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011515
Michael Chan4cafd3f2005-05-29 14:56:34 -070011516}
11517
Linus Torvalds1da177e2005-04-16 15:20:36 -070011518static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11519{
11520 struct mii_ioctl_data *data = if_mii(ifr);
11521 struct tg3 *tp = netdev_priv(dev);
11522 int err;
11523
Joe Perches63c3a662011-04-26 08:12:10 +000011524 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011525 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011526 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011527 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011528 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011529 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011530 }
11531
Matt Carlson33f401a2010-04-05 10:19:27 +000011532 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011533 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011534 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011535
11536 /* fallthru */
11537 case SIOCGMIIREG: {
11538 u32 mii_regval;
11539
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011540 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011541 break; /* We have no PHY */
11542
Matt Carlson34eea5a2011-04-20 07:57:38 +000011543 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011544 return -EAGAIN;
11545
David S. Millerf47c11e2005-06-24 20:18:35 -070011546 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011547 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011548 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011549
11550 data->val_out = mii_regval;
11551
11552 return err;
11553 }
11554
11555 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011556 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011557 break; /* We have no PHY */
11558
Matt Carlson34eea5a2011-04-20 07:57:38 +000011559 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011560 return -EAGAIN;
11561
David S. Millerf47c11e2005-06-24 20:18:35 -070011562 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011563 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011564 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011565
11566 return err;
11567
11568 default:
11569 /* do nothing */
11570 break;
11571 }
11572 return -EOPNOTSUPP;
11573}
11574
David S. Miller15f98502005-05-18 22:49:26 -070011575static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11576{
11577 struct tg3 *tp = netdev_priv(dev);
11578
11579 memcpy(ec, &tp->coal, sizeof(*ec));
11580 return 0;
11581}
11582
Michael Chand244c892005-07-05 14:42:33 -070011583static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11584{
11585 struct tg3 *tp = netdev_priv(dev);
11586 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11587 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11588
Joe Perches63c3a662011-04-26 08:12:10 +000011589 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011590 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11591 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11592 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11593 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11594 }
11595
11596 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11597 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11598 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11599 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11600 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11601 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11602 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11603 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11604 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11605 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11606 return -EINVAL;
11607
11608 /* No rx interrupts will be generated if both are zero */
11609 if ((ec->rx_coalesce_usecs == 0) &&
11610 (ec->rx_max_coalesced_frames == 0))
11611 return -EINVAL;
11612
11613 /* No tx interrupts will be generated if both are zero */
11614 if ((ec->tx_coalesce_usecs == 0) &&
11615 (ec->tx_max_coalesced_frames == 0))
11616 return -EINVAL;
11617
11618 /* Only copy relevant parameters, ignore all others. */
11619 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11620 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11621 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11622 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11623 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11624 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11625 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11626 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11627 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11628
11629 if (netif_running(dev)) {
11630 tg3_full_lock(tp, 0);
11631 __tg3_set_coalesce(tp, &tp->coal);
11632 tg3_full_unlock(tp);
11633 }
11634 return 0;
11635}
11636
Jeff Garzik7282d492006-09-13 14:30:00 -040011637static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011638 .get_settings = tg3_get_settings,
11639 .set_settings = tg3_set_settings,
11640 .get_drvinfo = tg3_get_drvinfo,
11641 .get_regs_len = tg3_get_regs_len,
11642 .get_regs = tg3_get_regs,
11643 .get_wol = tg3_get_wol,
11644 .set_wol = tg3_set_wol,
11645 .get_msglevel = tg3_get_msglevel,
11646 .set_msglevel = tg3_set_msglevel,
11647 .nway_reset = tg3_nway_reset,
11648 .get_link = ethtool_op_get_link,
11649 .get_eeprom_len = tg3_get_eeprom_len,
11650 .get_eeprom = tg3_get_eeprom,
11651 .set_eeprom = tg3_set_eeprom,
11652 .get_ringparam = tg3_get_ringparam,
11653 .set_ringparam = tg3_set_ringparam,
11654 .get_pauseparam = tg3_get_pauseparam,
11655 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011656 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011657 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011658 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011659 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011660 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011661 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011662 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011663};
11664
11665static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11666{
Michael Chan1b277772006-03-20 22:27:48 -080011667 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011668
11669 tp->nvram_size = EEPROM_CHIP_SIZE;
11670
Matt Carlsone4f34112009-02-25 14:25:00 +000011671 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011672 return;
11673
Michael Chanb16250e2006-09-27 16:10:14 -070011674 if ((magic != TG3_EEPROM_MAGIC) &&
11675 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11676 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011677 return;
11678
11679 /*
11680 * Size the chip by reading offsets at increasing powers of two.
11681 * When we encounter our validation signature, we know the addressing
11682 * has wrapped around, and thus have our chip size.
11683 */
Michael Chan1b277772006-03-20 22:27:48 -080011684 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011685
11686 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011687 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011688 return;
11689
Michael Chan18201802006-03-20 22:29:15 -080011690 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011691 break;
11692
11693 cursize <<= 1;
11694 }
11695
11696 tp->nvram_size = cursize;
11697}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011698
Linus Torvalds1da177e2005-04-16 15:20:36 -070011699static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11700{
11701 u32 val;
11702
Joe Perches63c3a662011-04-26 08:12:10 +000011703 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011704 return;
11705
11706 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011707 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011708 tg3_get_eeprom_size(tp);
11709 return;
11710 }
11711
Matt Carlson6d348f22009-02-25 14:25:52 +000011712 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011713 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011714 /* This is confusing. We want to operate on the
11715 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11716 * call will read from NVRAM and byteswap the data
11717 * according to the byteswapping settings for all
11718 * other register accesses. This ensures the data we
11719 * want will always reside in the lower 16-bits.
11720 * However, the data in NVRAM is in LE format, which
11721 * means the data from the NVRAM read will always be
11722 * opposite the endianness of the CPU. The 16-bit
11723 * byteswap then brings the data to CPU endianness.
11724 */
11725 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011726 return;
11727 }
11728 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011729 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011730}
11731
11732static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11733{
11734 u32 nvcfg1;
11735
11736 nvcfg1 = tr32(NVRAM_CFG1);
11737 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000011738 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011739 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011740 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11741 tw32(NVRAM_CFG1, nvcfg1);
11742 }
11743
Matt Carlson6ff6f812011-05-19 12:12:54 +000011744 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011745 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011746 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011747 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11748 tp->nvram_jedecnum = JEDEC_ATMEL;
11749 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011750 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011751 break;
11752 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11753 tp->nvram_jedecnum = JEDEC_ATMEL;
11754 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11755 break;
11756 case FLASH_VENDOR_ATMEL_EEPROM:
11757 tp->nvram_jedecnum = JEDEC_ATMEL;
11758 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011759 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011760 break;
11761 case FLASH_VENDOR_ST:
11762 tp->nvram_jedecnum = JEDEC_ST;
11763 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011764 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011765 break;
11766 case FLASH_VENDOR_SAIFUN:
11767 tp->nvram_jedecnum = JEDEC_SAIFUN;
11768 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11769 break;
11770 case FLASH_VENDOR_SST_SMALL:
11771 case FLASH_VENDOR_SST_LARGE:
11772 tp->nvram_jedecnum = JEDEC_SST;
11773 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011775 }
Matt Carlson8590a602009-08-28 12:29:16 +000011776 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011777 tp->nvram_jedecnum = JEDEC_ATMEL;
11778 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011779 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011780 }
11781}
11782
Matt Carlsona1b950d2009-09-01 13:20:17 +000011783static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11784{
11785 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11786 case FLASH_5752PAGE_SIZE_256:
11787 tp->nvram_pagesize = 256;
11788 break;
11789 case FLASH_5752PAGE_SIZE_512:
11790 tp->nvram_pagesize = 512;
11791 break;
11792 case FLASH_5752PAGE_SIZE_1K:
11793 tp->nvram_pagesize = 1024;
11794 break;
11795 case FLASH_5752PAGE_SIZE_2K:
11796 tp->nvram_pagesize = 2048;
11797 break;
11798 case FLASH_5752PAGE_SIZE_4K:
11799 tp->nvram_pagesize = 4096;
11800 break;
11801 case FLASH_5752PAGE_SIZE_264:
11802 tp->nvram_pagesize = 264;
11803 break;
11804 case FLASH_5752PAGE_SIZE_528:
11805 tp->nvram_pagesize = 528;
11806 break;
11807 }
11808}
11809
Michael Chan361b4ac2005-04-21 17:11:21 -070011810static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11811{
11812 u32 nvcfg1;
11813
11814 nvcfg1 = tr32(NVRAM_CFG1);
11815
Michael Chane6af3012005-04-21 17:12:05 -070011816 /* NVRAM protection for TPM */
11817 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000011818 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070011819
Michael Chan361b4ac2005-04-21 17:11:21 -070011820 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011821 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11822 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11823 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011824 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011825 break;
11826 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11827 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011828 tg3_flag_set(tp, NVRAM_BUFFERED);
11829 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011830 break;
11831 case FLASH_5752VENDOR_ST_M45PE10:
11832 case FLASH_5752VENDOR_ST_M45PE20:
11833 case FLASH_5752VENDOR_ST_M45PE40:
11834 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011835 tg3_flag_set(tp, NVRAM_BUFFERED);
11836 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011837 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011838 }
11839
Joe Perches63c3a662011-04-26 08:12:10 +000011840 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011841 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011842 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011843 /* For eeprom, set pagesize to maximum eeprom size */
11844 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11845
11846 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11847 tw32(NVRAM_CFG1, nvcfg1);
11848 }
11849}
11850
Michael Chand3c7b882006-03-23 01:28:25 -080011851static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11852{
Matt Carlson989a9d22007-05-05 11:51:05 -070011853 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011854
11855 nvcfg1 = tr32(NVRAM_CFG1);
11856
11857 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011858 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011859 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070011860 protect = 1;
11861 }
Michael Chand3c7b882006-03-23 01:28:25 -080011862
Matt Carlson989a9d22007-05-05 11:51:05 -070011863 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11864 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011865 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11866 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11867 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11868 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11869 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011870 tg3_flag_set(tp, NVRAM_BUFFERED);
11871 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011872 tp->nvram_pagesize = 264;
11873 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11874 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11875 tp->nvram_size = (protect ? 0x3e200 :
11876 TG3_NVRAM_SIZE_512KB);
11877 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11878 tp->nvram_size = (protect ? 0x1f200 :
11879 TG3_NVRAM_SIZE_256KB);
11880 else
11881 tp->nvram_size = (protect ? 0x1f200 :
11882 TG3_NVRAM_SIZE_128KB);
11883 break;
11884 case FLASH_5752VENDOR_ST_M45PE10:
11885 case FLASH_5752VENDOR_ST_M45PE20:
11886 case FLASH_5752VENDOR_ST_M45PE40:
11887 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011888 tg3_flag_set(tp, NVRAM_BUFFERED);
11889 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011890 tp->nvram_pagesize = 256;
11891 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11892 tp->nvram_size = (protect ?
11893 TG3_NVRAM_SIZE_64KB :
11894 TG3_NVRAM_SIZE_128KB);
11895 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11896 tp->nvram_size = (protect ?
11897 TG3_NVRAM_SIZE_64KB :
11898 TG3_NVRAM_SIZE_256KB);
11899 else
11900 tp->nvram_size = (protect ?
11901 TG3_NVRAM_SIZE_128KB :
11902 TG3_NVRAM_SIZE_512KB);
11903 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011904 }
11905}
11906
Michael Chan1b277772006-03-20 22:27:48 -080011907static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11908{
11909 u32 nvcfg1;
11910
11911 nvcfg1 = tr32(NVRAM_CFG1);
11912
11913 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011914 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11915 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11916 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11917 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11918 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011919 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011920 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011921
Matt Carlson8590a602009-08-28 12:29:16 +000011922 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11923 tw32(NVRAM_CFG1, nvcfg1);
11924 break;
11925 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11926 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11927 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11928 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11929 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011930 tg3_flag_set(tp, NVRAM_BUFFERED);
11931 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011932 tp->nvram_pagesize = 264;
11933 break;
11934 case FLASH_5752VENDOR_ST_M45PE10:
11935 case FLASH_5752VENDOR_ST_M45PE20:
11936 case FLASH_5752VENDOR_ST_M45PE40:
11937 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011938 tg3_flag_set(tp, NVRAM_BUFFERED);
11939 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011940 tp->nvram_pagesize = 256;
11941 break;
Michael Chan1b277772006-03-20 22:27:48 -080011942 }
11943}
11944
Matt Carlson6b91fa02007-10-10 18:01:09 -070011945static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11946{
11947 u32 nvcfg1, protect = 0;
11948
11949 nvcfg1 = tr32(NVRAM_CFG1);
11950
11951 /* NVRAM protection for TPM */
11952 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011953 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011954 protect = 1;
11955 }
11956
11957 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11958 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011959 case FLASH_5761VENDOR_ATMEL_ADB021D:
11960 case FLASH_5761VENDOR_ATMEL_ADB041D:
11961 case FLASH_5761VENDOR_ATMEL_ADB081D:
11962 case FLASH_5761VENDOR_ATMEL_ADB161D:
11963 case FLASH_5761VENDOR_ATMEL_MDB021D:
11964 case FLASH_5761VENDOR_ATMEL_MDB041D:
11965 case FLASH_5761VENDOR_ATMEL_MDB081D:
11966 case FLASH_5761VENDOR_ATMEL_MDB161D:
11967 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011968 tg3_flag_set(tp, NVRAM_BUFFERED);
11969 tg3_flag_set(tp, FLASH);
11970 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000011971 tp->nvram_pagesize = 256;
11972 break;
11973 case FLASH_5761VENDOR_ST_A_M45PE20:
11974 case FLASH_5761VENDOR_ST_A_M45PE40:
11975 case FLASH_5761VENDOR_ST_A_M45PE80:
11976 case FLASH_5761VENDOR_ST_A_M45PE16:
11977 case FLASH_5761VENDOR_ST_M_M45PE20:
11978 case FLASH_5761VENDOR_ST_M_M45PE40:
11979 case FLASH_5761VENDOR_ST_M_M45PE80:
11980 case FLASH_5761VENDOR_ST_M_M45PE16:
11981 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011982 tg3_flag_set(tp, NVRAM_BUFFERED);
11983 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011984 tp->nvram_pagesize = 256;
11985 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011986 }
11987
11988 if (protect) {
11989 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11990 } else {
11991 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011992 case FLASH_5761VENDOR_ATMEL_ADB161D:
11993 case FLASH_5761VENDOR_ATMEL_MDB161D:
11994 case FLASH_5761VENDOR_ST_A_M45PE16:
11995 case FLASH_5761VENDOR_ST_M_M45PE16:
11996 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11997 break;
11998 case FLASH_5761VENDOR_ATMEL_ADB081D:
11999 case FLASH_5761VENDOR_ATMEL_MDB081D:
12000 case FLASH_5761VENDOR_ST_A_M45PE80:
12001 case FLASH_5761VENDOR_ST_M_M45PE80:
12002 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12003 break;
12004 case FLASH_5761VENDOR_ATMEL_ADB041D:
12005 case FLASH_5761VENDOR_ATMEL_MDB041D:
12006 case FLASH_5761VENDOR_ST_A_M45PE40:
12007 case FLASH_5761VENDOR_ST_M_M45PE40:
12008 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12009 break;
12010 case FLASH_5761VENDOR_ATMEL_ADB021D:
12011 case FLASH_5761VENDOR_ATMEL_MDB021D:
12012 case FLASH_5761VENDOR_ST_A_M45PE20:
12013 case FLASH_5761VENDOR_ST_M_M45PE20:
12014 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12015 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012016 }
12017 }
12018}
12019
Michael Chanb5d37722006-09-27 16:06:21 -070012020static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12021{
12022 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012023 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012024 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12025}
12026
Matt Carlson321d32a2008-11-21 17:22:19 -080012027static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12028{
12029 u32 nvcfg1;
12030
12031 nvcfg1 = tr32(NVRAM_CFG1);
12032
12033 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12034 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12035 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12036 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012037 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012038 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12039
12040 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12041 tw32(NVRAM_CFG1, nvcfg1);
12042 return;
12043 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12044 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12045 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12046 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12047 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12048 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12049 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12050 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012051 tg3_flag_set(tp, NVRAM_BUFFERED);
12052 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012053
12054 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12055 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12056 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12057 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12058 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12059 break;
12060 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12061 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12062 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12063 break;
12064 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12065 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12066 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12067 break;
12068 }
12069 break;
12070 case FLASH_5752VENDOR_ST_M45PE10:
12071 case FLASH_5752VENDOR_ST_M45PE20:
12072 case FLASH_5752VENDOR_ST_M45PE40:
12073 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012074 tg3_flag_set(tp, NVRAM_BUFFERED);
12075 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012076
12077 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12078 case FLASH_5752VENDOR_ST_M45PE10:
12079 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12080 break;
12081 case FLASH_5752VENDOR_ST_M45PE20:
12082 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12083 break;
12084 case FLASH_5752VENDOR_ST_M45PE40:
12085 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12086 break;
12087 }
12088 break;
12089 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012090 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012091 return;
12092 }
12093
Matt Carlsona1b950d2009-09-01 13:20:17 +000012094 tg3_nvram_get_pagesize(tp, nvcfg1);
12095 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012096 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012097}
12098
12099
12100static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12101{
12102 u32 nvcfg1;
12103
12104 nvcfg1 = tr32(NVRAM_CFG1);
12105
12106 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12107 case FLASH_5717VENDOR_ATMEL_EEPROM:
12108 case FLASH_5717VENDOR_MICRO_EEPROM:
12109 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012110 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012111 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12112
12113 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12114 tw32(NVRAM_CFG1, nvcfg1);
12115 return;
12116 case FLASH_5717VENDOR_ATMEL_MDB011D:
12117 case FLASH_5717VENDOR_ATMEL_ADB011B:
12118 case FLASH_5717VENDOR_ATMEL_ADB011D:
12119 case FLASH_5717VENDOR_ATMEL_MDB021D:
12120 case FLASH_5717VENDOR_ATMEL_ADB021B:
12121 case FLASH_5717VENDOR_ATMEL_ADB021D:
12122 case FLASH_5717VENDOR_ATMEL_45USPT:
12123 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012124 tg3_flag_set(tp, NVRAM_BUFFERED);
12125 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012126
12127 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12128 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012129 /* Detect size with tg3_nvram_get_size() */
12130 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012131 case FLASH_5717VENDOR_ATMEL_ADB021B:
12132 case FLASH_5717VENDOR_ATMEL_ADB021D:
12133 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12134 break;
12135 default:
12136 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12137 break;
12138 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012139 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012140 case FLASH_5717VENDOR_ST_M_M25PE10:
12141 case FLASH_5717VENDOR_ST_A_M25PE10:
12142 case FLASH_5717VENDOR_ST_M_M45PE10:
12143 case FLASH_5717VENDOR_ST_A_M45PE10:
12144 case FLASH_5717VENDOR_ST_M_M25PE20:
12145 case FLASH_5717VENDOR_ST_A_M25PE20:
12146 case FLASH_5717VENDOR_ST_M_M45PE20:
12147 case FLASH_5717VENDOR_ST_A_M45PE20:
12148 case FLASH_5717VENDOR_ST_25USPT:
12149 case FLASH_5717VENDOR_ST_45USPT:
12150 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012151 tg3_flag_set(tp, NVRAM_BUFFERED);
12152 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012153
12154 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12155 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012156 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012157 /* Detect size with tg3_nvram_get_size() */
12158 break;
12159 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012160 case FLASH_5717VENDOR_ST_A_M45PE20:
12161 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12162 break;
12163 default:
12164 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12165 break;
12166 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012167 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012168 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012169 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012170 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012171 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012172
12173 tg3_nvram_get_pagesize(tp, nvcfg1);
12174 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012175 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012176}
12177
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012178static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12179{
12180 u32 nvcfg1, nvmpinstrp;
12181
12182 nvcfg1 = tr32(NVRAM_CFG1);
12183 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12184
12185 switch (nvmpinstrp) {
12186 case FLASH_5720_EEPROM_HD:
12187 case FLASH_5720_EEPROM_LD:
12188 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012189 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012190
12191 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12192 tw32(NVRAM_CFG1, nvcfg1);
12193 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12194 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12195 else
12196 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12197 return;
12198 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12199 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12200 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12201 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12202 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12203 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12204 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12205 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12206 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12207 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12208 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12209 case FLASH_5720VENDOR_ATMEL_45USPT:
12210 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012211 tg3_flag_set(tp, NVRAM_BUFFERED);
12212 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012213
12214 switch (nvmpinstrp) {
12215 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12216 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12217 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12218 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12219 break;
12220 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12221 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12222 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12223 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12224 break;
12225 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12226 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12227 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12228 break;
12229 default:
12230 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12231 break;
12232 }
12233 break;
12234 case FLASH_5720VENDOR_M_ST_M25PE10:
12235 case FLASH_5720VENDOR_M_ST_M45PE10:
12236 case FLASH_5720VENDOR_A_ST_M25PE10:
12237 case FLASH_5720VENDOR_A_ST_M45PE10:
12238 case FLASH_5720VENDOR_M_ST_M25PE20:
12239 case FLASH_5720VENDOR_M_ST_M45PE20:
12240 case FLASH_5720VENDOR_A_ST_M25PE20:
12241 case FLASH_5720VENDOR_A_ST_M45PE20:
12242 case FLASH_5720VENDOR_M_ST_M25PE40:
12243 case FLASH_5720VENDOR_M_ST_M45PE40:
12244 case FLASH_5720VENDOR_A_ST_M25PE40:
12245 case FLASH_5720VENDOR_A_ST_M45PE40:
12246 case FLASH_5720VENDOR_M_ST_M25PE80:
12247 case FLASH_5720VENDOR_M_ST_M45PE80:
12248 case FLASH_5720VENDOR_A_ST_M25PE80:
12249 case FLASH_5720VENDOR_A_ST_M45PE80:
12250 case FLASH_5720VENDOR_ST_25USPT:
12251 case FLASH_5720VENDOR_ST_45USPT:
12252 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012253 tg3_flag_set(tp, NVRAM_BUFFERED);
12254 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012255
12256 switch (nvmpinstrp) {
12257 case FLASH_5720VENDOR_M_ST_M25PE20:
12258 case FLASH_5720VENDOR_M_ST_M45PE20:
12259 case FLASH_5720VENDOR_A_ST_M25PE20:
12260 case FLASH_5720VENDOR_A_ST_M45PE20:
12261 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12262 break;
12263 case FLASH_5720VENDOR_M_ST_M25PE40:
12264 case FLASH_5720VENDOR_M_ST_M45PE40:
12265 case FLASH_5720VENDOR_A_ST_M25PE40:
12266 case FLASH_5720VENDOR_A_ST_M45PE40:
12267 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12268 break;
12269 case FLASH_5720VENDOR_M_ST_M25PE80:
12270 case FLASH_5720VENDOR_M_ST_M45PE80:
12271 case FLASH_5720VENDOR_A_ST_M25PE80:
12272 case FLASH_5720VENDOR_A_ST_M45PE80:
12273 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12274 break;
12275 default:
12276 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12277 break;
12278 }
12279 break;
12280 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012281 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012282 return;
12283 }
12284
12285 tg3_nvram_get_pagesize(tp, nvcfg1);
12286 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012287 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012288}
12289
Linus Torvalds1da177e2005-04-16 15:20:36 -070012290/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12291static void __devinit tg3_nvram_init(struct tg3 *tp)
12292{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012293 tw32_f(GRC_EEPROM_ADDR,
12294 (EEPROM_ADDR_FSM_RESET |
12295 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12296 EEPROM_ADDR_CLKPERD_SHIFT)));
12297
Michael Chan9d57f012006-12-07 00:23:25 -080012298 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012299
12300 /* Enable seeprom accesses. */
12301 tw32_f(GRC_LOCAL_CTRL,
12302 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12303 udelay(100);
12304
12305 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12306 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012307 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012308
Michael Chanec41c7d2006-01-17 02:40:55 -080012309 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012310 netdev_warn(tp->dev,
12311 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012312 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012313 return;
12314 }
Michael Chane6af3012005-04-21 17:12:05 -070012315 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012316
Matt Carlson989a9d22007-05-05 11:51:05 -070012317 tp->nvram_size = 0;
12318
Michael Chan361b4ac2005-04-21 17:11:21 -070012319 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12320 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012321 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12322 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012323 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012324 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12325 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012326 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012327 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12328 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012329 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12330 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012331 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12332 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012333 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012334 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12335 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012336 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012337 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12338 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012339 else
12340 tg3_get_nvram_info(tp);
12341
Matt Carlson989a9d22007-05-05 11:51:05 -070012342 if (tp->nvram_size == 0)
12343 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012344
Michael Chane6af3012005-04-21 17:12:05 -070012345 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012346 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012347
12348 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012349 tg3_flag_clear(tp, NVRAM);
12350 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012351
12352 tg3_get_eeprom_size(tp);
12353 }
12354}
12355
Linus Torvalds1da177e2005-04-16 15:20:36 -070012356static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12357 u32 offset, u32 len, u8 *buf)
12358{
12359 int i, j, rc = 0;
12360 u32 val;
12361
12362 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012363 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012364 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012365
12366 addr = offset + i;
12367
12368 memcpy(&data, buf + i, 4);
12369
Matt Carlson62cedd12009-04-20 14:52:29 -070012370 /*
12371 * The SEEPROM interface expects the data to always be opposite
12372 * the native endian format. We accomplish this by reversing
12373 * all the operations that would have been performed on the
12374 * data from a call to tg3_nvram_read_be32().
12375 */
12376 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012377
12378 val = tr32(GRC_EEPROM_ADDR);
12379 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12380
12381 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12382 EEPROM_ADDR_READ);
12383 tw32(GRC_EEPROM_ADDR, val |
12384 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12385 (addr & EEPROM_ADDR_ADDR_MASK) |
12386 EEPROM_ADDR_START |
12387 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012388
Michael Chan9d57f012006-12-07 00:23:25 -080012389 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012390 val = tr32(GRC_EEPROM_ADDR);
12391
12392 if (val & EEPROM_ADDR_COMPLETE)
12393 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012394 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012395 }
12396 if (!(val & EEPROM_ADDR_COMPLETE)) {
12397 rc = -EBUSY;
12398 break;
12399 }
12400 }
12401
12402 return rc;
12403}
12404
12405/* offset and length are dword aligned */
12406static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12407 u8 *buf)
12408{
12409 int ret = 0;
12410 u32 pagesize = tp->nvram_pagesize;
12411 u32 pagemask = pagesize - 1;
12412 u32 nvram_cmd;
12413 u8 *tmp;
12414
12415 tmp = kmalloc(pagesize, GFP_KERNEL);
12416 if (tmp == NULL)
12417 return -ENOMEM;
12418
12419 while (len) {
12420 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012421 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012422
12423 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012424
Linus Torvalds1da177e2005-04-16 15:20:36 -070012425 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012426 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12427 (__be32 *) (tmp + j));
12428 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012429 break;
12430 }
12431 if (ret)
12432 break;
12433
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012434 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012435 size = pagesize;
12436 if (len < size)
12437 size = len;
12438
12439 len -= size;
12440
12441 memcpy(tmp + page_off, buf, size);
12442
12443 offset = offset + (pagesize - page_off);
12444
Michael Chane6af3012005-04-21 17:12:05 -070012445 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012446
12447 /*
12448 * Before we can erase the flash page, we need
12449 * to issue a special "write enable" command.
12450 */
12451 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12452
12453 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12454 break;
12455
12456 /* Erase the target page */
12457 tw32(NVRAM_ADDR, phy_addr);
12458
12459 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12460 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12461
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012462 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012463 break;
12464
12465 /* Issue another write enable to start the write. */
12466 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12467
12468 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12469 break;
12470
12471 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012472 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012473
Al Virob9fc7dc2007-12-17 22:59:57 -080012474 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012475
Al Virob9fc7dc2007-12-17 22:59:57 -080012476 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012477
12478 tw32(NVRAM_ADDR, phy_addr + j);
12479
12480 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12481 NVRAM_CMD_WR;
12482
12483 if (j == 0)
12484 nvram_cmd |= NVRAM_CMD_FIRST;
12485 else if (j == (pagesize - 4))
12486 nvram_cmd |= NVRAM_CMD_LAST;
12487
12488 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12489 break;
12490 }
12491 if (ret)
12492 break;
12493 }
12494
12495 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12496 tg3_nvram_exec_cmd(tp, nvram_cmd);
12497
12498 kfree(tmp);
12499
12500 return ret;
12501}
12502
12503/* offset and length are dword aligned */
12504static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12505 u8 *buf)
12506{
12507 int i, ret = 0;
12508
12509 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012510 u32 page_off, phy_addr, nvram_cmd;
12511 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012512
12513 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012514 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012515
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012516 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012517
Michael Chan18201802006-03-20 22:29:15 -080012518 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012519
12520 tw32(NVRAM_ADDR, phy_addr);
12521
12522 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12523
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012524 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012525 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012526 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012527 nvram_cmd |= NVRAM_CMD_LAST;
12528
12529 if (i == (len - 4))
12530 nvram_cmd |= NVRAM_CMD_LAST;
12531
Matt Carlson321d32a2008-11-21 17:22:19 -080012532 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012533 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012534 (tp->nvram_jedecnum == JEDEC_ST) &&
12535 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012536
12537 if ((ret = tg3_nvram_exec_cmd(tp,
12538 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12539 NVRAM_CMD_DONE)))
12540
12541 break;
12542 }
Joe Perches63c3a662011-04-26 08:12:10 +000012543 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012544 /* We always do complete word writes to eeprom. */
12545 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12546 }
12547
12548 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12549 break;
12550 }
12551 return ret;
12552}
12553
12554/* offset and length are dword aligned */
12555static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12556{
12557 int ret;
12558
Joe Perches63c3a662011-04-26 08:12:10 +000012559 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012560 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12561 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012562 udelay(40);
12563 }
12564
Joe Perches63c3a662011-04-26 08:12:10 +000012565 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012566 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012567 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012568 u32 grc_mode;
12569
Michael Chanec41c7d2006-01-17 02:40:55 -080012570 ret = tg3_nvram_lock(tp);
12571 if (ret)
12572 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012573
Michael Chane6af3012005-04-21 17:12:05 -070012574 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012575 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012576 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012577
12578 grc_mode = tr32(GRC_MODE);
12579 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12580
Joe Perches63c3a662011-04-26 08:12:10 +000012581 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012582 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12583 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012584 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012585 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12586 buf);
12587 }
12588
12589 grc_mode = tr32(GRC_MODE);
12590 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12591
Michael Chane6af3012005-04-21 17:12:05 -070012592 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012593 tg3_nvram_unlock(tp);
12594 }
12595
Joe Perches63c3a662011-04-26 08:12:10 +000012596 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012597 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012598 udelay(40);
12599 }
12600
12601 return ret;
12602}
12603
12604struct subsys_tbl_ent {
12605 u16 subsys_vendor, subsys_devid;
12606 u32 phy_id;
12607};
12608
Matt Carlson24daf2b2010-02-17 15:17:02 +000012609static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012610 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012611 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012612 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012613 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012614 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012615 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012616 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012617 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12618 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12619 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012620 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012621 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012622 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012623 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12624 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12625 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012626 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012627 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012628 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012629 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012630 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012631 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012632 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012633
12634 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012635 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012636 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012637 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012638 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012639 { TG3PCI_SUBVENDOR_ID_3COM,
12640 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12641 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012642 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012643 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012644 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012645
12646 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012647 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012648 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012649 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012650 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012651 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012652 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012653 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012654 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012655
12656 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012657 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012658 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012659 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012660 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012661 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12662 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12663 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012664 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012665 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012666 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012667
12668 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012669 { TG3PCI_SUBVENDOR_ID_IBM,
12670 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012671};
12672
Matt Carlson24daf2b2010-02-17 15:17:02 +000012673static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012674{
12675 int i;
12676
12677 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12678 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12679 tp->pdev->subsystem_vendor) &&
12680 (subsys_id_to_phy_id[i].subsys_devid ==
12681 tp->pdev->subsystem_device))
12682 return &subsys_id_to_phy_id[i];
12683 }
12684 return NULL;
12685}
12686
Michael Chan7d0c41e2005-04-21 17:06:20 -070012687static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012688{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012689 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070012690
Matt Carlson79eb6902010-02-17 15:17:03 +000012691 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012692 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12693
Gary Zambranoa85feb82007-05-05 11:52:19 -070012694 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012695 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12696 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012697
Michael Chanb5d37722006-09-27 16:06:21 -070012698 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012699 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012700 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12701 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012702 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012703 val = tr32(VCPU_CFGSHDW);
12704 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012705 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012706 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012707 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012708 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012709 device_set_wakeup_enable(&tp->pdev->dev, true);
12710 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012711 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012712 }
12713
Linus Torvalds1da177e2005-04-16 15:20:36 -070012714 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12715 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12716 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012717 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012718 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012719
12720 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12721 tp->nic_sram_data_cfg = nic_cfg;
12722
12723 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12724 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000012725 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12726 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
12727 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012728 (ver > 0) && (ver < 0x100))
12729 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12730
Matt Carlsona9daf362008-05-25 23:49:44 -070012731 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12732 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12733
Linus Torvalds1da177e2005-04-16 15:20:36 -070012734 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12735 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12736 eeprom_phy_serdes = 1;
12737
12738 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12739 if (nic_phy_id != 0) {
12740 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12741 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12742
12743 eeprom_phy_id = (id1 >> 16) << 10;
12744 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12745 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12746 } else
12747 eeprom_phy_id = 0;
12748
Michael Chan7d0c41e2005-04-21 17:06:20 -070012749 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012750 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000012751 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012752 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012753 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012754 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012755 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012756
Joe Perches63c3a662011-04-26 08:12:10 +000012757 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012758 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12759 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012760 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012761 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12762
12763 switch (led_cfg) {
12764 default:
12765 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12766 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12767 break;
12768
12769 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12770 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12771 break;
12772
12773 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12774 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012775
12776 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12777 * read on some older 5700/5701 bootcode.
12778 */
12779 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12780 ASIC_REV_5700 ||
12781 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12782 ASIC_REV_5701)
12783 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12784
Linus Torvalds1da177e2005-04-16 15:20:36 -070012785 break;
12786
12787 case SHASTA_EXT_LED_SHARED:
12788 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12789 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12790 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12791 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12792 LED_CTRL_MODE_PHY_2);
12793 break;
12794
12795 case SHASTA_EXT_LED_MAC:
12796 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12797 break;
12798
12799 case SHASTA_EXT_LED_COMBO:
12800 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12801 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12802 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12803 LED_CTRL_MODE_PHY_2);
12804 break;
12805
Stephen Hemminger855e1112008-04-16 16:37:28 -070012806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012807
12808 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12809 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12810 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12811 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12812
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012813 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12814 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012815
Michael Chan9d26e212006-12-07 00:21:14 -080012816 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000012817 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012818 if ((tp->pdev->subsystem_vendor ==
12819 PCI_VENDOR_ID_ARIMA) &&
12820 (tp->pdev->subsystem_device == 0x205a ||
12821 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000012822 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012823 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012824 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12825 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012827
12828 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000012829 tg3_flag_set(tp, ENABLE_ASF);
12830 if (tg3_flag(tp, 5750_PLUS))
12831 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012832 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012833
12834 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012835 tg3_flag(tp, 5750_PLUS))
12836 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012837
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012838 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012839 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000012840 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012841
Joe Perches63c3a662011-04-26 08:12:10 +000012842 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012843 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012844 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012845 device_set_wakeup_enable(&tp->pdev->dev, true);
12846 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012847
Linus Torvalds1da177e2005-04-16 15:20:36 -070012848 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012849 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012850
12851 /* serdes signal pre-emphasis in register 0x590 set by */
12852 /* bootcode if bit 18 is set */
12853 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012854 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012855
Joe Perches63c3a662011-04-26 08:12:10 +000012856 if ((tg3_flag(tp, 57765_PLUS) ||
12857 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12858 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012859 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012860 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012861
Joe Perches63c3a662011-04-26 08:12:10 +000012862 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012863 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012864 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012865 u32 cfg3;
12866
12867 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12868 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000012869 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070012870 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012871
Matt Carlson14417062010-02-17 15:16:59 +000012872 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000012873 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070012874 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012875 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070012876 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012877 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012878 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012879done:
Joe Perches63c3a662011-04-26 08:12:10 +000012880 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012881 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000012882 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012883 else
12884 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012885}
12886
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012887static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12888{
12889 int i;
12890 u32 val;
12891
12892 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12893 tw32(OTP_CTRL, cmd);
12894
12895 /* Wait for up to 1 ms for command to execute. */
12896 for (i = 0; i < 100; i++) {
12897 val = tr32(OTP_STATUS);
12898 if (val & OTP_STATUS_CMD_DONE)
12899 break;
12900 udelay(10);
12901 }
12902
12903 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12904}
12905
12906/* Read the gphy configuration from the OTP region of the chip. The gphy
12907 * configuration is a 32-bit value that straddles the alignment boundary.
12908 * We do two 32-bit reads and then shift and merge the results.
12909 */
12910static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12911{
12912 u32 bhalf_otp, thalf_otp;
12913
12914 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12915
12916 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12917 return 0;
12918
12919 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12920
12921 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12922 return 0;
12923
12924 thalf_otp = tr32(OTP_READ_DATA);
12925
12926 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12927
12928 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12929 return 0;
12930
12931 bhalf_otp = tr32(OTP_READ_DATA);
12932
12933 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12934}
12935
Matt Carlsone256f8a2011-03-09 16:58:24 +000012936static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12937{
12938 u32 adv = ADVERTISED_Autoneg |
12939 ADVERTISED_Pause;
12940
12941 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12942 adv |= ADVERTISED_1000baseT_Half |
12943 ADVERTISED_1000baseT_Full;
12944
12945 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12946 adv |= ADVERTISED_100baseT_Half |
12947 ADVERTISED_100baseT_Full |
12948 ADVERTISED_10baseT_Half |
12949 ADVERTISED_10baseT_Full |
12950 ADVERTISED_TP;
12951 else
12952 adv |= ADVERTISED_FIBRE;
12953
12954 tp->link_config.advertising = adv;
12955 tp->link_config.speed = SPEED_INVALID;
12956 tp->link_config.duplex = DUPLEX_INVALID;
12957 tp->link_config.autoneg = AUTONEG_ENABLE;
12958 tp->link_config.active_speed = SPEED_INVALID;
12959 tp->link_config.active_duplex = DUPLEX_INVALID;
12960 tp->link_config.orig_speed = SPEED_INVALID;
12961 tp->link_config.orig_duplex = DUPLEX_INVALID;
12962 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12963}
12964
Michael Chan7d0c41e2005-04-21 17:06:20 -070012965static int __devinit tg3_phy_probe(struct tg3 *tp)
12966{
12967 u32 hw_phy_id_1, hw_phy_id_2;
12968 u32 hw_phy_id, hw_phy_id_masked;
12969 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012970
Matt Carlsone256f8a2011-03-09 16:58:24 +000012971 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000012972 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000012973 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
12974
Joe Perches63c3a662011-04-26 08:12:10 +000012975 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012976 return tg3_phy_init(tp);
12977
Linus Torvalds1da177e2005-04-16 15:20:36 -070012978 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012979 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012980 */
12981 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012982 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012983 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012984 } else {
12985 /* Now read the physical PHY_ID from the chip and verify
12986 * that it is sane. If it doesn't look good, we fall back
12987 * to either the hard-coded table based PHY_ID and failing
12988 * that the value found in the eeprom area.
12989 */
12990 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12991 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12992
12993 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12994 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12995 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12996
Matt Carlson79eb6902010-02-17 15:17:03 +000012997 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012998 }
12999
Matt Carlson79eb6902010-02-17 15:17:03 +000013000 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013001 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013002 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013003 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013004 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013005 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013006 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013007 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013008 /* Do nothing, phy ID already set up in
13009 * tg3_get_eeprom_hw_cfg().
13010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013011 } else {
13012 struct subsys_tbl_ent *p;
13013
13014 /* No eeprom signature? Try the hardcoded
13015 * subsys device table.
13016 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013017 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013018 if (!p)
13019 return -ENODEV;
13020
13021 tp->phy_id = p->phy_id;
13022 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013023 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013024 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013025 }
13026 }
13027
Matt Carlsona6b68da2010-12-06 08:28:52 +000013028 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
13029 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
13030 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13031 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13032 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013033 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13034
Matt Carlsone256f8a2011-03-09 16:58:24 +000013035 tg3_phy_init_link_config(tp);
13036
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013037 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013038 !tg3_flag(tp, ENABLE_APE) &&
13039 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013040 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013041
13042 tg3_readphy(tp, MII_BMSR, &bmsr);
13043 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13044 (bmsr & BMSR_LSTATUS))
13045 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013046
Linus Torvalds1da177e2005-04-16 15:20:36 -070013047 err = tg3_phy_reset(tp);
13048 if (err)
13049 return err;
13050
Matt Carlson42b64a42011-05-19 12:12:49 +000013051 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013052
Michael Chan3600d912006-12-07 00:21:48 -080013053 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13054 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13055 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13056 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013057 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13058 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013059
13060 tg3_writephy(tp, MII_BMCR,
13061 BMCR_ANENABLE | BMCR_ANRESTART);
13062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013063 }
13064
13065skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013066 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013067 err = tg3_init_5401phy_dsp(tp);
13068 if (err)
13069 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013070
Linus Torvalds1da177e2005-04-16 15:20:36 -070013071 err = tg3_init_5401phy_dsp(tp);
13072 }
13073
Linus Torvalds1da177e2005-04-16 15:20:36 -070013074 return err;
13075}
13076
Matt Carlson184b8902010-04-05 10:19:25 +000013077static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013078{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013079 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013080 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000013081 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013082
Matt Carlsonc3e94502011-04-13 11:05:08 +000013083 vpd_data = (u8 *)tg3_vpd_readblock(tp);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013084 if (!vpd_data)
13085 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013086
Matt Carlson4181b2c2010-02-26 14:04:45 +000013087 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
13088 PCI_VPD_LRDT_RO_DATA);
13089 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013090 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013091
13092 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13093 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13094 i += PCI_VPD_LRDT_TAG_SIZE;
13095
13096 if (block_end > TG3_NVM_VPD_LEN)
13097 goto out_not_found;
13098
Matt Carlson184b8902010-04-05 10:19:25 +000013099 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13100 PCI_VPD_RO_KEYWORD_MFR_ID);
13101 if (j > 0) {
13102 len = pci_vpd_info_field_size(&vpd_data[j]);
13103
13104 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13105 if (j + len > block_end || len != 4 ||
13106 memcmp(&vpd_data[j], "1028", 4))
13107 goto partno;
13108
13109 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13110 PCI_VPD_RO_KEYWORD_VENDOR0);
13111 if (j < 0)
13112 goto partno;
13113
13114 len = pci_vpd_info_field_size(&vpd_data[j]);
13115
13116 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13117 if (j + len > block_end)
13118 goto partno;
13119
13120 memcpy(tp->fw_ver, &vpd_data[j], len);
13121 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
13122 }
13123
13124partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013125 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13126 PCI_VPD_RO_KEYWORD_PARTNO);
13127 if (i < 0)
13128 goto out_not_found;
13129
13130 len = pci_vpd_info_field_size(&vpd_data[i]);
13131
13132 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13133 if (len > TG3_BPN_SIZE ||
13134 (len + i) > TG3_NVM_VPD_LEN)
13135 goto out_not_found;
13136
13137 memcpy(tp->board_part_number, &vpd_data[i], len);
13138
Linus Torvalds1da177e2005-04-16 15:20:36 -070013139out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013140 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013141 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013142 return;
13143
13144out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013145 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13146 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13147 strcpy(tp->board_part_number, "BCM5717");
13148 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13149 strcpy(tp->board_part_number, "BCM5718");
13150 else
13151 goto nomatch;
13152 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13153 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13154 strcpy(tp->board_part_number, "BCM57780");
13155 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13156 strcpy(tp->board_part_number, "BCM57760");
13157 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13158 strcpy(tp->board_part_number, "BCM57790");
13159 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13160 strcpy(tp->board_part_number, "BCM57788");
13161 else
13162 goto nomatch;
13163 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13164 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13165 strcpy(tp->board_part_number, "BCM57761");
13166 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13167 strcpy(tp->board_part_number, "BCM57765");
13168 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13169 strcpy(tp->board_part_number, "BCM57781");
13170 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13171 strcpy(tp->board_part_number, "BCM57785");
13172 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13173 strcpy(tp->board_part_number, "BCM57791");
13174 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13175 strcpy(tp->board_part_number, "BCM57795");
13176 else
13177 goto nomatch;
13178 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013179 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013180 } else {
13181nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013182 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013184}
13185
Matt Carlson9c8a6202007-10-21 16:16:08 -070013186static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13187{
13188 u32 val;
13189
Matt Carlsone4f34112009-02-25 14:25:00 +000013190 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013191 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013192 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013193 val != 0)
13194 return 0;
13195
13196 return 1;
13197}
13198
Matt Carlsonacd9c112009-02-25 14:26:33 +000013199static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13200{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013201 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013202 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013203 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013204
13205 if (tg3_nvram_read(tp, 0xc, &offset) ||
13206 tg3_nvram_read(tp, 0x4, &start))
13207 return;
13208
13209 offset = tg3_nvram_logical_addr(tp, offset);
13210
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013211 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013212 return;
13213
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013214 if ((val & 0xfc000000) == 0x0c000000) {
13215 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013216 return;
13217
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013218 if (val == 0)
13219 newver = true;
13220 }
13221
Matt Carlson75f99362010-04-05 10:19:24 +000013222 dst_off = strlen(tp->fw_ver);
13223
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013224 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013225 if (TG3_VER_SIZE - dst_off < 16 ||
13226 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013227 return;
13228
13229 offset = offset + ver_offset - start;
13230 for (i = 0; i < 16; i += 4) {
13231 __be32 v;
13232 if (tg3_nvram_read_be32(tp, offset + i, &v))
13233 return;
13234
Matt Carlson75f99362010-04-05 10:19:24 +000013235 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013236 }
13237 } else {
13238 u32 major, minor;
13239
13240 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13241 return;
13242
13243 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13244 TG3_NVM_BCVER_MAJSFT;
13245 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013246 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13247 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013248 }
13249}
13250
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013251static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13252{
13253 u32 val, major, minor;
13254
13255 /* Use native endian representation */
13256 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13257 return;
13258
13259 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13260 TG3_NVM_HWSB_CFG1_MAJSFT;
13261 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13262 TG3_NVM_HWSB_CFG1_MINSFT;
13263
13264 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13265}
13266
Matt Carlsondfe00d72008-11-21 17:19:41 -080013267static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13268{
13269 u32 offset, major, minor, build;
13270
Matt Carlson75f99362010-04-05 10:19:24 +000013271 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013272
13273 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13274 return;
13275
13276 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13277 case TG3_EEPROM_SB_REVISION_0:
13278 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13279 break;
13280 case TG3_EEPROM_SB_REVISION_2:
13281 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13282 break;
13283 case TG3_EEPROM_SB_REVISION_3:
13284 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13285 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013286 case TG3_EEPROM_SB_REVISION_4:
13287 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13288 break;
13289 case TG3_EEPROM_SB_REVISION_5:
13290 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13291 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013292 case TG3_EEPROM_SB_REVISION_6:
13293 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13294 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013295 default:
13296 return;
13297 }
13298
Matt Carlsone4f34112009-02-25 14:25:00 +000013299 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013300 return;
13301
13302 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13303 TG3_EEPROM_SB_EDH_BLD_SHFT;
13304 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13305 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13306 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13307
13308 if (minor > 99 || build > 26)
13309 return;
13310
Matt Carlson75f99362010-04-05 10:19:24 +000013311 offset = strlen(tp->fw_ver);
13312 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13313 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013314
13315 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013316 offset = strlen(tp->fw_ver);
13317 if (offset < TG3_VER_SIZE - 1)
13318 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013319 }
13320}
13321
Matt Carlsonacd9c112009-02-25 14:26:33 +000013322static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013323{
13324 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013325 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013326
13327 for (offset = TG3_NVM_DIR_START;
13328 offset < TG3_NVM_DIR_END;
13329 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013330 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013331 return;
13332
13333 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13334 break;
13335 }
13336
13337 if (offset == TG3_NVM_DIR_END)
13338 return;
13339
Joe Perches63c3a662011-04-26 08:12:10 +000013340 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013341 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013342 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013343 return;
13344
Matt Carlsone4f34112009-02-25 14:25:00 +000013345 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013346 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013347 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013348 return;
13349
13350 offset += val - start;
13351
Matt Carlsonacd9c112009-02-25 14:26:33 +000013352 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013353
Matt Carlsonacd9c112009-02-25 14:26:33 +000013354 tp->fw_ver[vlen++] = ',';
13355 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013356
13357 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013358 __be32 v;
13359 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013360 return;
13361
Al Virob9fc7dc2007-12-17 22:59:57 -080013362 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013363
Matt Carlsonacd9c112009-02-25 14:26:33 +000013364 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13365 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013366 break;
13367 }
13368
Matt Carlsonacd9c112009-02-25 14:26:33 +000013369 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13370 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013371 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013372}
13373
Matt Carlson7fd76442009-02-25 14:27:20 +000013374static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13375{
13376 int vlen;
13377 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013378 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013379
Joe Perches63c3a662011-04-26 08:12:10 +000013380 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013381 return;
13382
13383 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13384 if (apedata != APE_SEG_SIG_MAGIC)
13385 return;
13386
13387 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13388 if (!(apedata & APE_FW_STATUS_READY))
13389 return;
13390
13391 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13392
Matt Carlsondc6d0742010-09-15 08:59:55 +000013393 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013394 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013395 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013396 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013397 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013398 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013399
Matt Carlson7fd76442009-02-25 14:27:20 +000013400 vlen = strlen(tp->fw_ver);
13401
Matt Carlsonecc79642010-08-02 11:26:01 +000013402 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13403 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013404 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13405 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13406 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13407 (apedata & APE_FW_VERSION_BLDMSK));
13408}
13409
Matt Carlsonacd9c112009-02-25 14:26:33 +000013410static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13411{
13412 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013413 bool vpd_vers = false;
13414
13415 if (tp->fw_ver[0] != 0)
13416 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013417
Joe Perches63c3a662011-04-26 08:12:10 +000013418 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013419 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013420 return;
13421 }
13422
Matt Carlsonacd9c112009-02-25 14:26:33 +000013423 if (tg3_nvram_read(tp, 0, &val))
13424 return;
13425
13426 if (val == TG3_EEPROM_MAGIC)
13427 tg3_read_bc_ver(tp);
13428 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13429 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013430 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13431 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013432 else
13433 return;
13434
Matt Carlsonc9cab242011-07-13 09:27:27 +000013435 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013436 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013437
Matt Carlsonc9cab242011-07-13 09:27:27 +000013438 if (tg3_flag(tp, ENABLE_APE)) {
13439 if (tg3_flag(tp, ENABLE_ASF))
13440 tg3_read_dash_ver(tp);
13441 } else if (tg3_flag(tp, ENABLE_ASF)) {
13442 tg3_read_mgmtfw_ver(tp);
13443 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013444
Matt Carlson75f99362010-04-05 10:19:24 +000013445done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013446 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013447}
13448
Michael Chan7544b092007-05-05 13:08:32 -070013449static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13450
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013451static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13452{
Joe Perches63c3a662011-04-26 08:12:10 +000013453 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013454 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013455 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013456 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013457 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013458 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013459}
13460
Matt Carlson41434702011-03-09 16:58:22 +000013461static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013462 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13463 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13464 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13465 { },
13466};
13467
Linus Torvalds1da177e2005-04-16 15:20:36 -070013468static int __devinit tg3_get_invariants(struct tg3 *tp)
13469{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013470 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013471 u32 pci_state_reg, grc_misc_cfg;
13472 u32 val;
13473 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013474 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013475
Linus Torvalds1da177e2005-04-16 15:20:36 -070013476 /* Force memory write invalidate off. If we leave it on,
13477 * then on 5700_BX chips we have to enable a workaround.
13478 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13479 * to match the cacheline size. The Broadcom driver have this
13480 * workaround but turns MWI off all the times so never uses
13481 * it. This seems to suggest that the workaround is insufficient.
13482 */
13483 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13484 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13485 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13486
Matt Carlson16821282011-07-13 09:27:28 +000013487 /* Important! -- Make sure register accesses are byteswapped
13488 * correctly. Also, for those chips that require it, make
13489 * sure that indirect register accesses are enabled before
13490 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013491 */
13492 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13493 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013494 tp->misc_host_ctrl |= (misc_ctrl_reg &
13495 MISC_HOST_CTRL_CHIPREV);
13496 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13497 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013498
13499 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13500 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013501 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13502 u32 prod_id_asic_rev;
13503
Matt Carlson5001e2f2009-11-13 13:03:51 +000013504 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13505 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013506 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13507 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013508 pci_read_config_dword(tp->pdev,
13509 TG3PCI_GEN2_PRODID_ASICREV,
13510 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013511 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13512 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13513 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13514 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13515 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13516 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13517 pci_read_config_dword(tp->pdev,
13518 TG3PCI_GEN15_PRODID_ASICREV,
13519 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013520 else
13521 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13522 &prod_id_asic_rev);
13523
Matt Carlson321d32a2008-11-21 17:22:19 -080013524 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013526
Michael Chanff645be2005-04-21 17:09:53 -070013527 /* Wrong chip ID in 5752 A0. This code can be removed later
13528 * as A0 is not in production.
13529 */
13530 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13531 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13532
Michael Chan68929142005-08-09 20:17:14 -070013533 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13534 * we need to disable memory and use config. cycles
13535 * only to access all registers. The 5702/03 chips
13536 * can mistakenly decode the special cycles from the
13537 * ICH chipsets as memory write cycles, causing corruption
13538 * of register and memory space. Only certain ICH bridges
13539 * will drive special cycles with non-zero data during the
13540 * address phase which can fall within the 5703's address
13541 * range. This is not an ICH bug as the PCI spec allows
13542 * non-zero address during special cycles. However, only
13543 * these ICH bridges are known to drive non-zero addresses
13544 * during special cycles.
13545 *
13546 * Since special cycles do not cross PCI bridges, we only
13547 * enable this workaround if the 5703 is on the secondary
13548 * bus of these ICH bridges.
13549 */
13550 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13551 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13552 static struct tg3_dev_id {
13553 u32 vendor;
13554 u32 device;
13555 u32 rev;
13556 } ich_chipsets[] = {
13557 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13558 PCI_ANY_ID },
13559 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13560 PCI_ANY_ID },
13561 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13562 0xa },
13563 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13564 PCI_ANY_ID },
13565 { },
13566 };
13567 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13568 struct pci_dev *bridge = NULL;
13569
13570 while (pci_id->vendor != 0) {
13571 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13572 bridge);
13573 if (!bridge) {
13574 pci_id++;
13575 continue;
13576 }
13577 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013578 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013579 continue;
13580 }
13581 if (bridge->subordinate &&
13582 (bridge->subordinate->number ==
13583 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013584 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013585 pci_dev_put(bridge);
13586 break;
13587 }
13588 }
13589 }
13590
Matt Carlson6ff6f812011-05-19 12:12:54 +000013591 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013592 static struct tg3_dev_id {
13593 u32 vendor;
13594 u32 device;
13595 } bridge_chipsets[] = {
13596 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13597 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13598 { },
13599 };
13600 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13601 struct pci_dev *bridge = NULL;
13602
13603 while (pci_id->vendor != 0) {
13604 bridge = pci_get_device(pci_id->vendor,
13605 pci_id->device,
13606 bridge);
13607 if (!bridge) {
13608 pci_id++;
13609 continue;
13610 }
13611 if (bridge->subordinate &&
13612 (bridge->subordinate->number <=
13613 tp->pdev->bus->number) &&
13614 (bridge->subordinate->subordinate >=
13615 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013616 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013617 pci_dev_put(bridge);
13618 break;
13619 }
13620 }
13621 }
13622
Michael Chan4a29cc22006-03-19 13:21:12 -080013623 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13624 * DMA addresses > 40-bit. This bridge may have other additional
13625 * 57xx devices behind it in some 4-port NIC designs for example.
13626 * Any tg3 device found behind the bridge will also need the 40-bit
13627 * DMA workaround.
13628 */
Michael Chana4e2b342005-10-26 15:46:52 -070013629 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13630 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013631 tg3_flag_set(tp, 5780_CLASS);
13632 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013633 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000013634 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013635 struct pci_dev *bridge = NULL;
13636
13637 do {
13638 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13639 PCI_DEVICE_ID_SERVERWORKS_EPB,
13640 bridge);
13641 if (bridge && bridge->subordinate &&
13642 (bridge->subordinate->number <=
13643 tp->pdev->bus->number) &&
13644 (bridge->subordinate->subordinate >=
13645 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013646 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013647 pci_dev_put(bridge);
13648 break;
13649 }
13650 } while (bridge);
13651 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013652
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013653 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13654 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013655 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13656 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013657 tp->pdev_peer = tg3_find_peer(tp);
13658
Matt Carlsonc885e822010-08-02 11:25:57 +000013659 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013660 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13661 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013662 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013663
13664 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013665 tg3_flag(tp, 5717_PLUS))
13666 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013667
Matt Carlson321d32a2008-11-21 17:22:19 -080013668 /* Intentionally exclude ASIC_REV_5906 */
13669 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad12006-03-20 22:27:35 -080013670 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013671 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013672 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013673 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013674 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013675 tg3_flag(tp, 57765_PLUS))
13676 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013677
13678 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13679 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013680 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013681 tg3_flag(tp, 5755_PLUS) ||
13682 tg3_flag(tp, 5780_CLASS))
13683 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013684
Matt Carlson6ff6f812011-05-19 12:12:54 +000013685 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013686 tg3_flag(tp, 5750_PLUS))
13687 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013688
Matt Carlson507399f2009-11-13 13:03:37 +000013689 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013690 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013691 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013692 else if (tg3_flag(tp, 57765_PLUS))
13693 tg3_flag_set(tp, HW_TSO_3);
13694 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013695 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013696 tg3_flag_set(tp, HW_TSO_2);
13697 else if (tg3_flag(tp, 5750_PLUS)) {
13698 tg3_flag_set(tp, HW_TSO_1);
13699 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013700 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13701 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013702 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013703 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13704 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13705 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013706 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013707 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13708 tp->fw_needed = FIRMWARE_TG3TSO5;
13709 else
13710 tp->fw_needed = FIRMWARE_TG3TSO;
13711 }
13712
Matt Carlsondabc5c62011-05-19 12:12:52 +000013713 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013714 if (tg3_flag(tp, HW_TSO_1) ||
13715 tg3_flag(tp, HW_TSO_2) ||
13716 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsondabc5c62011-05-19 12:12:52 +000013717 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13718 tg3_flag_set(tp, TSO_CAPABLE);
13719 else {
13720 tg3_flag_clear(tp, TSO_CAPABLE);
13721 tg3_flag_clear(tp, TSO_BUG);
13722 tp->fw_needed = NULL;
13723 }
13724
13725 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
13726 tp->fw_needed = FIRMWARE_TG3;
13727
Matt Carlson507399f2009-11-13 13:03:37 +000013728 tp->irq_max = 1;
13729
Joe Perches63c3a662011-04-26 08:12:10 +000013730 if (tg3_flag(tp, 5750_PLUS)) {
13731 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013732 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13733 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13734 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13735 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13736 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000013737 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013738
Joe Perches63c3a662011-04-26 08:12:10 +000013739 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013740 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013741 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070013742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013743
Joe Perches63c3a662011-04-26 08:12:10 +000013744 if (tg3_flag(tp, 57765_PLUS)) {
13745 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000013746 tp->irq_max = TG3_IRQ_MAX_VECS;
13747 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013748 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013749
Matt Carlson2ffcc982011-05-19 12:12:44 +000013750 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000013751 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013752
Joe Perches63c3a662011-04-26 08:12:10 +000013753 if (tg3_flag(tp, 5717_PLUS))
13754 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000013755
Joe Perches63c3a662011-04-26 08:12:10 +000013756 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013757 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Joe Perches63c3a662011-04-26 08:12:10 +000013758 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000013759
Joe Perches63c3a662011-04-26 08:12:10 +000013760 if (!tg3_flag(tp, 5705_PLUS) ||
13761 tg3_flag(tp, 5780_CLASS) ||
13762 tg3_flag(tp, USE_JUMBO_BDFLAG))
13763 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070013764
Matt Carlson52f44902008-11-21 17:17:04 -080013765 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13766 &pci_state_reg);
13767
Jon Mason708ebb3a2011-06-27 12:56:50 +000013768 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013769 u16 lnkctl;
13770
Joe Perches63c3a662011-04-26 08:12:10 +000013771 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013772
Matt Carlsoncf790032010-11-24 08:31:48 +000013773 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013774 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13775 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013776 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013777
13778 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013779
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013780 pci_read_config_word(tp->pdev,
Jon Mason708ebb3a2011-06-27 12:56:50 +000013781 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013782 &lnkctl);
13783 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000013784 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13785 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013786 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000013787 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000013788 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013789 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013790 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013791 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13792 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000013793 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000013794 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013795 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080013796 }
Matt Carlson52f44902008-11-21 17:17:04 -080013797 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb3a2011-06-27 12:56:50 +000013798 /* BCM5785 devices are effectively PCIe devices, and should
13799 * follow PCIe codepaths, but do not have a PCIe capabilities
13800 * section.
13801 */
Joe Perches63c3a662011-04-26 08:12:10 +000013802 tg3_flag_set(tp, PCI_EXPRESS);
13803 } else if (!tg3_flag(tp, 5705_PLUS) ||
13804 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080013805 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13806 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013807 dev_err(&tp->pdev->dev,
13808 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013809 return -EIO;
13810 }
13811
13812 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000013813 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080013814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013815
Michael Chan399de502005-10-03 14:02:39 -070013816 /* If we have an AMD 762 or VIA K8T800 chipset, write
13817 * reordering to the mailbox registers done by the host
13818 * controller can cause major troubles. We read back from
13819 * every mailbox register write to force the writes to be
13820 * posted to the chip in order.
13821 */
Matt Carlson41434702011-03-09 16:58:22 +000013822 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013823 !tg3_flag(tp, PCI_EXPRESS))
13824 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070013825
Matt Carlson69fc4052008-12-21 20:19:57 -080013826 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13827 &tp->pci_cacheline_sz);
13828 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13829 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013830 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13831 tp->pci_lat_timer < 64) {
13832 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013833 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13834 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013835 }
13836
Matt Carlson16821282011-07-13 09:27:28 +000013837 /* Important! -- It is critical that the PCI-X hw workaround
13838 * situation is decided before the first MMIO register access.
13839 */
Matt Carlson52f44902008-11-21 17:17:04 -080013840 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13841 /* 5700 BX chips need to have their TX producer index
13842 * mailboxes written twice to workaround a bug.
13843 */
Joe Perches63c3a662011-04-26 08:12:10 +000013844 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070013845
Matt Carlson52f44902008-11-21 17:17:04 -080013846 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013847 *
13848 * The workaround is to use indirect register accesses
13849 * for all chip writes not to mailbox registers.
13850 */
Joe Perches63c3a662011-04-26 08:12:10 +000013851 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013852 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013853
Joe Perches63c3a662011-04-26 08:12:10 +000013854 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013855
13856 /* The chip can have it's power management PCI config
13857 * space registers clobbered due to this bug.
13858 * So explicitly force the chip into D0 here.
13859 */
Matt Carlson9974a352007-10-07 23:27:28 -070013860 pci_read_config_dword(tp->pdev,
13861 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013862 &pm_reg);
13863 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13864 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013865 pci_write_config_dword(tp->pdev,
13866 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013867 pm_reg);
13868
13869 /* Also, force SERR#/PERR# in PCI command. */
13870 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13871 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13872 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13873 }
13874 }
13875
Linus Torvalds1da177e2005-04-16 15:20:36 -070013876 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013877 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013878 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013879 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013880
13881 /* Chip-specific fixup from Broadcom driver */
13882 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13883 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13884 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13885 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13886 }
13887
Michael Chan1ee582d2005-08-09 20:16:46 -070013888 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013889 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013890 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013891 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013892 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013893 tp->write32_tx_mbox = tg3_write32;
13894 tp->write32_rx_mbox = tg3_write32;
13895
13896 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000013897 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070013898 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013899 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013900 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070013901 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13902 /*
13903 * Back to back register writes can cause problems on these
13904 * chips, the workaround is to read back all reg writes
13905 * except those to mailbox regs.
13906 *
13907 * See tg3_write_indirect_reg32().
13908 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013909 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013910 }
13911
Joe Perches63c3a662011-04-26 08:12:10 +000013912 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070013913 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000013914 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070013915 tp->write32_rx_mbox = tg3_write_flush_reg32;
13916 }
Michael Chan20094932005-08-09 20:16:32 -070013917
Joe Perches63c3a662011-04-26 08:12:10 +000013918 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070013919 tp->read32 = tg3_read_indirect_reg32;
13920 tp->write32 = tg3_write_indirect_reg32;
13921 tp->read32_mbox = tg3_read_indirect_mbox;
13922 tp->write32_mbox = tg3_write_indirect_mbox;
13923 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13924 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13925
13926 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013927 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013928
13929 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13930 pci_cmd &= ~PCI_COMMAND_MEMORY;
13931 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13932 }
Michael Chanb5d37722006-09-27 16:06:21 -070013933 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13934 tp->read32_mbox = tg3_read32_mbox_5906;
13935 tp->write32_mbox = tg3_write32_mbox_5906;
13936 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13937 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13938 }
Michael Chan68929142005-08-09 20:17:14 -070013939
Michael Chanbbadf502006-04-06 21:46:34 -070013940 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013941 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070013942 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013943 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000013944 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070013945
Matt Carlson16821282011-07-13 09:27:28 +000013946 /* The memory arbiter has to be enabled in order for SRAM accesses
13947 * to succeed. Normally on powerup the tg3 chip firmware will make
13948 * sure it is enabled, but other entities such as system netboot
13949 * code might disable it.
13950 */
13951 val = tr32(MEMARB_MODE);
13952 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
13953
Matt Carlson69f11c92011-07-13 09:27:30 +000013954 if (tg3_flag(tp, PCIX_MODE)) {
13955 pci_read_config_dword(tp->pdev,
13956 tp->pcix_cap + PCI_X_STATUS, &val);
13957 tp->pci_fn = val & 0x7;
13958 } else {
13959 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
13960 }
13961
Michael Chan7d0c41e2005-04-21 17:06:20 -070013962 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000013963 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013964 * determined before calling tg3_set_power_state() so that
13965 * we know whether or not to switch out of Vaux power.
13966 * When the flag is set, it means that GPIO1 is used for eeprom
13967 * write protect and also implies that it is a LOM where GPIOs
13968 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013969 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013970 tg3_get_eeprom_hw_cfg(tp);
13971
Joe Perches63c3a662011-04-26 08:12:10 +000013972 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070013973 /* Allow reads and writes to the
13974 * APE register and memory space.
13975 */
13976 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013977 PCISTATE_ALLOW_APE_SHMEM_WR |
13978 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013979 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13980 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000013981
13982 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070013983 }
13984
Matt Carlson9936bcf2007-10-10 18:03:07 -070013985 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013986 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013987 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013988 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013989 tg3_flag(tp, 57765_PLUS))
13990 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013991
Matt Carlson16821282011-07-13 09:27:28 +000013992 /* Set up tp->grc_local_ctrl before calling
13993 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
13994 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070013995 * It is also used as eeprom write protect on LOMs.
13996 */
13997 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013998 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013999 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014000 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14001 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014002 /* Unused GPIO3 must be driven as output on 5752 because there
14003 * are no pull-up resistors on unused GPIO pins.
14004 */
14005 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14006 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014007
Matt Carlson321d32a2008-11-21 17:22:19 -080014008 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014009 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14010 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080014011 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14012
Matt Carlson8d519ab2009-04-20 06:58:01 +000014013 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14014 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014015 /* Turn off the debug UART. */
14016 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014017 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014018 /* Keep VMain power. */
14019 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14020 GRC_LCLCTRL_GPIO_OUTPUT0;
14021 }
14022
Matt Carlson16821282011-07-13 09:27:28 +000014023 /* Switch out of Vaux if it is a NIC */
14024 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014025
Linus Torvalds1da177e2005-04-16 15:20:36 -070014026 /* Derive initial jumbo mode from MTU assigned in
14027 * ether_setup() via the alloc_etherdev() call
14028 */
Joe Perches63c3a662011-04-26 08:12:10 +000014029 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14030 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014031
14032 /* Determine WakeOnLan speed to use. */
14033 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14034 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14035 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14036 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014037 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014038 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014039 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014040 }
14041
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014042 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014043 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014044
Linus Torvalds1da177e2005-04-16 15:20:36 -070014045 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014046 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14047 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014048 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014049 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014050 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14051 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14052 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014053
14054 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14055 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014056 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014057 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014058 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014059
Joe Perches63c3a662011-04-26 08:12:10 +000014060 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014061 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014062 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014063 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014064 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014065 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014066 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014067 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14068 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014069 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14070 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014071 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014072 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014073 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014074 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014075 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014077
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014078 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14079 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14080 tp->phy_otp = tg3_read_otp_phycfg(tp);
14081 if (tp->phy_otp == 0)
14082 tp->phy_otp = TG3_OTP_DEFAULT;
14083 }
14084
Joe Perches63c3a662011-04-26 08:12:10 +000014085 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014086 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14087 else
14088 tp->mi_mode = MAC_MI_MODE_BASE;
14089
Linus Torvalds1da177e2005-04-16 15:20:36 -070014090 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014091 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14092 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14093 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14094
Matt Carlson4d958472011-04-20 07:57:35 +000014095 /* Set these bits to enable statistics workaround. */
14096 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14097 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14098 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14099 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14100 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14101 }
14102
Matt Carlson321d32a2008-11-21 17:22:19 -080014103 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14104 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014105 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014106
Matt Carlson158d7ab2008-05-29 01:37:54 -070014107 err = tg3_mdio_init(tp);
14108 if (err)
14109 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014110
14111 /* Initialize data/descriptor byte/word swapping. */
14112 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014113 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14114 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14115 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14116 GRC_MODE_B2HRX_ENABLE |
14117 GRC_MODE_HTX2B_ENABLE |
14118 GRC_MODE_HOST_STACKUP);
14119 else
14120 val &= GRC_MODE_HOST_STACKUP;
14121
Linus Torvalds1da177e2005-04-16 15:20:36 -070014122 tw32(GRC_MODE, val | tp->grc_mode);
14123
14124 tg3_switch_clocks(tp);
14125
14126 /* Clear this out for sanity. */
14127 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14128
14129 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14130 &pci_state_reg);
14131 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014132 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014133 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14134
14135 if (chiprevid == CHIPREV_ID_5701_A0 ||
14136 chiprevid == CHIPREV_ID_5701_B0 ||
14137 chiprevid == CHIPREV_ID_5701_B2 ||
14138 chiprevid == CHIPREV_ID_5701_B5) {
14139 void __iomem *sram_base;
14140
14141 /* Write some dummy words into the SRAM status block
14142 * area, see if it reads back correctly. If the return
14143 * value is bad, force enable the PCIX workaround.
14144 */
14145 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14146
14147 writel(0x00000000, sram_base);
14148 writel(0x00000000, sram_base + 4);
14149 writel(0xffffffff, sram_base + 4);
14150 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014151 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014152 }
14153 }
14154
14155 udelay(50);
14156 tg3_nvram_init(tp);
14157
14158 grc_misc_cfg = tr32(GRC_MISC_CFG);
14159 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14160
Linus Torvalds1da177e2005-04-16 15:20:36 -070014161 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14162 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14163 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014164 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014165
Joe Perches63c3a662011-04-26 08:12:10 +000014166 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014167 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014168 tg3_flag_set(tp, TAGGED_STATUS);
14169 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014170 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14171 HOSTCC_MODE_CLRTICK_TXBD);
14172
14173 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14174 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14175 tp->misc_host_ctrl);
14176 }
14177
Matt Carlson3bda1252008-08-15 14:08:22 -070014178 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014179 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014180 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014181 else
14182 tp->mac_mode = TG3_DEF_MAC_MODE;
14183
Linus Torvalds1da177e2005-04-16 15:20:36 -070014184 /* these are limited to 10/100 only */
14185 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14186 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14187 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14188 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14189 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14190 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14191 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14192 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14193 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014194 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14195 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014196 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014197 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14198 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014199 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14200 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014201
14202 err = tg3_phy_probe(tp);
14203 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014204 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014205 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014206 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014207 }
14208
Matt Carlson184b8902010-04-05 10:19:25 +000014209 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014210 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014211
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014212 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14213 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014214 } else {
14215 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014216 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014217 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014218 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014219 }
14220
14221 /* 5700 {AX,BX} chips have a broken status block link
14222 * change bit implementation, so we must use the
14223 * status register in those cases.
14224 */
14225 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014226 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014227 else
Joe Perches63c3a662011-04-26 08:12:10 +000014228 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014229
14230 /* The led_ctrl is set during tg3_phy_probe, here we might
14231 * have to force the link status polling mechanism based
14232 * upon subsystem IDs.
14233 */
14234 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014235 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014236 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14237 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014238 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014239 }
14240
14241 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014242 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014243 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014244 else
Joe Perches63c3a662011-04-26 08:12:10 +000014245 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014246
Matt Carlsonbf933c82011-01-25 15:58:49 +000014247 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014248 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014249 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014250 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014251 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014252#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014253 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014254#endif
14255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014256
Matt Carlson2c49a442010-09-30 10:34:35 +000014257 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14258 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014259 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14260
Matt Carlson2c49a442010-09-30 10:34:35 +000014261 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014262
14263 /* Increment the rx prod index on the rx std ring by at most
14264 * 8 for these chips to workaround hw errata.
14265 */
14266 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14267 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14268 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14269 tp->rx_std_max_post = 8;
14270
Joe Perches63c3a662011-04-26 08:12:10 +000014271 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014272 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14273 PCIE_PWR_MGMT_L1_THRESH_MSK;
14274
Linus Torvalds1da177e2005-04-16 15:20:36 -070014275 return err;
14276}
14277
David S. Miller49b6e95f2007-03-29 01:38:42 -070014278#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014279static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14280{
14281 struct net_device *dev = tp->dev;
14282 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014283 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014284 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014285 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014286
David S. Miller49b6e95f2007-03-29 01:38:42 -070014287 addr = of_get_property(dp, "local-mac-address", &len);
14288 if (addr && len == 6) {
14289 memcpy(dev->dev_addr, addr, 6);
14290 memcpy(dev->perm_addr, dev->dev_addr, 6);
14291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014292 }
14293 return -ENODEV;
14294}
14295
14296static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14297{
14298 struct net_device *dev = tp->dev;
14299
14300 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014301 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014302 return 0;
14303}
14304#endif
14305
14306static int __devinit tg3_get_device_address(struct tg3 *tp)
14307{
14308 struct net_device *dev = tp->dev;
14309 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014310 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014311
David S. Miller49b6e95f2007-03-29 01:38:42 -070014312#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014313 if (!tg3_get_macaddr_sparc(tp))
14314 return 0;
14315#endif
14316
14317 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014318 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014319 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014320 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14321 mac_offset = 0xcc;
14322 if (tg3_nvram_lock(tp))
14323 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14324 else
14325 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014326 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014327 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014328 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014329 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014330 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014331 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014332 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014333
14334 /* First try to get it from MAC address mailbox. */
14335 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14336 if ((hi >> 16) == 0x484b) {
14337 dev->dev_addr[0] = (hi >> 8) & 0xff;
14338 dev->dev_addr[1] = (hi >> 0) & 0xff;
14339
14340 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14341 dev->dev_addr[2] = (lo >> 24) & 0xff;
14342 dev->dev_addr[3] = (lo >> 16) & 0xff;
14343 dev->dev_addr[4] = (lo >> 8) & 0xff;
14344 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014345
Michael Chan008652b2006-03-27 23:14:53 -080014346 /* Some old bootcode may report a 0 MAC address in SRAM */
14347 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14348 }
14349 if (!addr_ok) {
14350 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014351 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014352 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014353 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014354 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14355 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014356 }
14357 /* Finally just fetch it out of the MAC control regs. */
14358 else {
14359 hi = tr32(MAC_ADDR_0_HIGH);
14360 lo = tr32(MAC_ADDR_0_LOW);
14361
14362 dev->dev_addr[5] = lo & 0xff;
14363 dev->dev_addr[4] = (lo >> 8) & 0xff;
14364 dev->dev_addr[3] = (lo >> 16) & 0xff;
14365 dev->dev_addr[2] = (lo >> 24) & 0xff;
14366 dev->dev_addr[1] = hi & 0xff;
14367 dev->dev_addr[0] = (hi >> 8) & 0xff;
14368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014369 }
14370
14371 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014372#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014373 if (!tg3_get_default_macaddr_sparc(tp))
14374 return 0;
14375#endif
14376 return -EINVAL;
14377 }
John W. Linville2ff43692005-09-12 14:44:20 -070014378 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014379 return 0;
14380}
14381
David S. Miller59e6b432005-05-18 22:50:10 -070014382#define BOUNDARY_SINGLE_CACHELINE 1
14383#define BOUNDARY_MULTI_CACHELINE 2
14384
14385static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14386{
14387 int cacheline_size;
14388 u8 byte;
14389 int goal;
14390
14391 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14392 if (byte == 0)
14393 cacheline_size = 1024;
14394 else
14395 cacheline_size = (int) byte * 4;
14396
14397 /* On 5703 and later chips, the boundary bits have no
14398 * effect.
14399 */
14400 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14401 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014402 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014403 goto out;
14404
14405#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14406 goal = BOUNDARY_MULTI_CACHELINE;
14407#else
14408#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14409 goal = BOUNDARY_SINGLE_CACHELINE;
14410#else
14411 goal = 0;
14412#endif
14413#endif
14414
Joe Perches63c3a662011-04-26 08:12:10 +000014415 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014416 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14417 goto out;
14418 }
14419
David S. Miller59e6b432005-05-18 22:50:10 -070014420 if (!goal)
14421 goto out;
14422
14423 /* PCI controllers on most RISC systems tend to disconnect
14424 * when a device tries to burst across a cache-line boundary.
14425 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14426 *
14427 * Unfortunately, for PCI-E there are only limited
14428 * write-side controls for this, and thus for reads
14429 * we will still get the disconnects. We'll also waste
14430 * these PCI cycles for both read and write for chips
14431 * other than 5700 and 5701 which do not implement the
14432 * boundary bits.
14433 */
Joe Perches63c3a662011-04-26 08:12:10 +000014434 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014435 switch (cacheline_size) {
14436 case 16:
14437 case 32:
14438 case 64:
14439 case 128:
14440 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14441 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14442 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14443 } else {
14444 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14445 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14446 }
14447 break;
14448
14449 case 256:
14450 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14451 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14452 break;
14453
14454 default:
14455 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14456 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14457 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014458 }
Joe Perches63c3a662011-04-26 08:12:10 +000014459 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014460 switch (cacheline_size) {
14461 case 16:
14462 case 32:
14463 case 64:
14464 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14465 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14466 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14467 break;
14468 }
14469 /* fallthrough */
14470 case 128:
14471 default:
14472 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14473 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14474 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014475 }
David S. Miller59e6b432005-05-18 22:50:10 -070014476 } else {
14477 switch (cacheline_size) {
14478 case 16:
14479 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14480 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14481 DMA_RWCTRL_WRITE_BNDRY_16);
14482 break;
14483 }
14484 /* fallthrough */
14485 case 32:
14486 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14487 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14488 DMA_RWCTRL_WRITE_BNDRY_32);
14489 break;
14490 }
14491 /* fallthrough */
14492 case 64:
14493 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14494 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14495 DMA_RWCTRL_WRITE_BNDRY_64);
14496 break;
14497 }
14498 /* fallthrough */
14499 case 128:
14500 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14501 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14502 DMA_RWCTRL_WRITE_BNDRY_128);
14503 break;
14504 }
14505 /* fallthrough */
14506 case 256:
14507 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14508 DMA_RWCTRL_WRITE_BNDRY_256);
14509 break;
14510 case 512:
14511 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14512 DMA_RWCTRL_WRITE_BNDRY_512);
14513 break;
14514 case 1024:
14515 default:
14516 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14517 DMA_RWCTRL_WRITE_BNDRY_1024);
14518 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014519 }
David S. Miller59e6b432005-05-18 22:50:10 -070014520 }
14521
14522out:
14523 return val;
14524}
14525
Linus Torvalds1da177e2005-04-16 15:20:36 -070014526static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14527{
14528 struct tg3_internal_buffer_desc test_desc;
14529 u32 sram_dma_descs;
14530 int i, ret;
14531
14532 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14533
14534 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14535 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14536 tw32(RDMAC_STATUS, 0);
14537 tw32(WDMAC_STATUS, 0);
14538
14539 tw32(BUFMGR_MODE, 0);
14540 tw32(FTQ_RESET, 0);
14541
14542 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14543 test_desc.addr_lo = buf_dma & 0xffffffff;
14544 test_desc.nic_mbuf = 0x00002100;
14545 test_desc.len = size;
14546
14547 /*
14548 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14549 * the *second* time the tg3 driver was getting loaded after an
14550 * initial scan.
14551 *
14552 * Broadcom tells me:
14553 * ...the DMA engine is connected to the GRC block and a DMA
14554 * reset may affect the GRC block in some unpredictable way...
14555 * The behavior of resets to individual blocks has not been tested.
14556 *
14557 * Broadcom noted the GRC reset will also reset all sub-components.
14558 */
14559 if (to_device) {
14560 test_desc.cqid_sqid = (13 << 8) | 2;
14561
14562 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14563 udelay(40);
14564 } else {
14565 test_desc.cqid_sqid = (16 << 8) | 7;
14566
14567 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14568 udelay(40);
14569 }
14570 test_desc.flags = 0x00000005;
14571
14572 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14573 u32 val;
14574
14575 val = *(((u32 *)&test_desc) + i);
14576 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14577 sram_dma_descs + (i * sizeof(u32)));
14578 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14579 }
14580 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14581
Matt Carlson859a588792010-04-05 10:19:28 +000014582 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014583 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000014584 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014585 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014586
14587 ret = -ENODEV;
14588 for (i = 0; i < 40; i++) {
14589 u32 val;
14590
14591 if (to_device)
14592 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14593 else
14594 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14595 if ((val & 0xffff) == sram_dma_descs) {
14596 ret = 0;
14597 break;
14598 }
14599
14600 udelay(100);
14601 }
14602
14603 return ret;
14604}
14605
David S. Millerded73402005-05-23 13:59:47 -070014606#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014607
Matt Carlson41434702011-03-09 16:58:22 +000014608static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014609 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14610 { },
14611};
14612
Linus Torvalds1da177e2005-04-16 15:20:36 -070014613static int __devinit tg3_test_dma(struct tg3 *tp)
14614{
14615 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014616 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014617 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014618
Matt Carlson4bae65c2010-11-24 08:31:52 +000014619 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14620 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014621 if (!buf) {
14622 ret = -ENOMEM;
14623 goto out_nofree;
14624 }
14625
14626 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14627 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14628
David S. Miller59e6b432005-05-18 22:50:10 -070014629 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014630
Joe Perches63c3a662011-04-26 08:12:10 +000014631 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014632 goto out;
14633
Joe Perches63c3a662011-04-26 08:12:10 +000014634 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014635 /* DMA read watermark not used on PCIE */
14636 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014637 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014638 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14639 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014640 tp->dma_rwctrl |= 0x003f0000;
14641 else
14642 tp->dma_rwctrl |= 0x003f000f;
14643 } else {
14644 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14645 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14646 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014647 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014648
Michael Chan4a29cc22006-03-19 13:21:12 -080014649 /* If the 5704 is behind the EPB bridge, we can
14650 * do the less restrictive ONE_DMA workaround for
14651 * better performance.
14652 */
Joe Perches63c3a662011-04-26 08:12:10 +000014653 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014654 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14655 tp->dma_rwctrl |= 0x8000;
14656 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014657 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14658
Michael Chan49afdeb2007-02-13 12:17:03 -080014659 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14660 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014661 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014662 tp->dma_rwctrl |=
14663 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14664 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14665 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014666 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14667 /* 5780 always in PCIX mode */
14668 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014669 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14670 /* 5714 always in PCIX mode */
14671 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014672 } else {
14673 tp->dma_rwctrl |= 0x001b000f;
14674 }
14675 }
14676
14677 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14678 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14679 tp->dma_rwctrl &= 0xfffffff0;
14680
14681 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14682 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14683 /* Remove this if it causes problems for some boards. */
14684 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14685
14686 /* On 5700/5701 chips, we need to set this bit.
14687 * Otherwise the chip will issue cacheline transactions
14688 * to streamable DMA memory with not all the byte
14689 * enables turned on. This is an error on several
14690 * RISC PCI controllers, in particular sparc64.
14691 *
14692 * On 5703/5704 chips, this bit has been reassigned
14693 * a different meaning. In particular, it is used
14694 * on those chips to enable a PCI-X workaround.
14695 */
14696 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14697 }
14698
14699 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14700
14701#if 0
14702 /* Unneeded, already done by tg3_get_invariants. */
14703 tg3_switch_clocks(tp);
14704#endif
14705
Linus Torvalds1da177e2005-04-16 15:20:36 -070014706 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14707 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14708 goto out;
14709
David S. Miller59e6b432005-05-18 22:50:10 -070014710 /* It is best to perform DMA test with maximum write burst size
14711 * to expose the 5700/5701 write DMA bug.
14712 */
14713 saved_dma_rwctrl = tp->dma_rwctrl;
14714 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14715 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14716
Linus Torvalds1da177e2005-04-16 15:20:36 -070014717 while (1) {
14718 u32 *p = buf, i;
14719
14720 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14721 p[i] = i;
14722
14723 /* Send the buffer to the chip. */
14724 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14725 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014726 dev_err(&tp->pdev->dev,
14727 "%s: Buffer write failed. err = %d\n",
14728 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014729 break;
14730 }
14731
14732#if 0
14733 /* validate data reached card RAM correctly. */
14734 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14735 u32 val;
14736 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14737 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014738 dev_err(&tp->pdev->dev,
14739 "%s: Buffer corrupted on device! "
14740 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014741 /* ret = -ENODEV here? */
14742 }
14743 p[i] = 0;
14744 }
14745#endif
14746 /* Now read it back. */
14747 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14748 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014749 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14750 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014751 break;
14752 }
14753
14754 /* Verify it. */
14755 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14756 if (p[i] == i)
14757 continue;
14758
David S. Miller59e6b432005-05-18 22:50:10 -070014759 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14760 DMA_RWCTRL_WRITE_BNDRY_16) {
14761 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014762 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14763 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14764 break;
14765 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014766 dev_err(&tp->pdev->dev,
14767 "%s: Buffer corrupted on read back! "
14768 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014769 ret = -ENODEV;
14770 goto out;
14771 }
14772 }
14773
14774 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14775 /* Success. */
14776 ret = 0;
14777 break;
14778 }
14779 }
David S. Miller59e6b432005-05-18 22:50:10 -070014780 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14781 DMA_RWCTRL_WRITE_BNDRY_16) {
14782 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014783 * now look for chipsets that are known to expose the
14784 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014785 */
Matt Carlson41434702011-03-09 16:58:22 +000014786 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014787 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14788 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014789 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014790 /* Safe to use the calculated DMA boundary. */
14791 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014792 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014793
David S. Miller59e6b432005-05-18 22:50:10 -070014794 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014796
14797out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014798 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014799out_nofree:
14800 return ret;
14801}
14802
Linus Torvalds1da177e2005-04-16 15:20:36 -070014803static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14804{
Joe Perches63c3a662011-04-26 08:12:10 +000014805 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000014806 tp->bufmgr_config.mbuf_read_dma_low_water =
14807 DEFAULT_MB_RDMA_LOW_WATER_5705;
14808 tp->bufmgr_config.mbuf_mac_rx_low_water =
14809 DEFAULT_MB_MACRX_LOW_WATER_57765;
14810 tp->bufmgr_config.mbuf_high_water =
14811 DEFAULT_MB_HIGH_WATER_57765;
14812
14813 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14814 DEFAULT_MB_RDMA_LOW_WATER_5705;
14815 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14816 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14817 tp->bufmgr_config.mbuf_high_water_jumbo =
14818 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000014819 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014820 tp->bufmgr_config.mbuf_read_dma_low_water =
14821 DEFAULT_MB_RDMA_LOW_WATER_5705;
14822 tp->bufmgr_config.mbuf_mac_rx_low_water =
14823 DEFAULT_MB_MACRX_LOW_WATER_5705;
14824 tp->bufmgr_config.mbuf_high_water =
14825 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014826 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14827 tp->bufmgr_config.mbuf_mac_rx_low_water =
14828 DEFAULT_MB_MACRX_LOW_WATER_5906;
14829 tp->bufmgr_config.mbuf_high_water =
14830 DEFAULT_MB_HIGH_WATER_5906;
14831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014832
Michael Chanfdfec1722005-07-25 12:31:48 -070014833 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14834 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14835 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14836 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14837 tp->bufmgr_config.mbuf_high_water_jumbo =
14838 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14839 } else {
14840 tp->bufmgr_config.mbuf_read_dma_low_water =
14841 DEFAULT_MB_RDMA_LOW_WATER;
14842 tp->bufmgr_config.mbuf_mac_rx_low_water =
14843 DEFAULT_MB_MACRX_LOW_WATER;
14844 tp->bufmgr_config.mbuf_high_water =
14845 DEFAULT_MB_HIGH_WATER;
14846
14847 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14848 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14849 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14850 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14851 tp->bufmgr_config.mbuf_high_water_jumbo =
14852 DEFAULT_MB_HIGH_WATER_JUMBO;
14853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014854
14855 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14856 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14857}
14858
14859static char * __devinit tg3_phy_string(struct tg3 *tp)
14860{
Matt Carlson79eb6902010-02-17 15:17:03 +000014861 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14862 case TG3_PHY_ID_BCM5400: return "5400";
14863 case TG3_PHY_ID_BCM5401: return "5401";
14864 case TG3_PHY_ID_BCM5411: return "5411";
14865 case TG3_PHY_ID_BCM5701: return "5701";
14866 case TG3_PHY_ID_BCM5703: return "5703";
14867 case TG3_PHY_ID_BCM5704: return "5704";
14868 case TG3_PHY_ID_BCM5705: return "5705";
14869 case TG3_PHY_ID_BCM5750: return "5750";
14870 case TG3_PHY_ID_BCM5752: return "5752";
14871 case TG3_PHY_ID_BCM5714: return "5714";
14872 case TG3_PHY_ID_BCM5780: return "5780";
14873 case TG3_PHY_ID_BCM5755: return "5755";
14874 case TG3_PHY_ID_BCM5787: return "5787";
14875 case TG3_PHY_ID_BCM5784: return "5784";
14876 case TG3_PHY_ID_BCM5756: return "5722/5756";
14877 case TG3_PHY_ID_BCM5906: return "5906";
14878 case TG3_PHY_ID_BCM5761: return "5761";
14879 case TG3_PHY_ID_BCM5718C: return "5718C";
14880 case TG3_PHY_ID_BCM5718S: return "5718S";
14881 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014882 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000014883 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014884 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014885 case 0: return "serdes";
14886 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014888}
14889
Michael Chanf9804dd2005-09-27 12:13:10 -070014890static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14891{
Joe Perches63c3a662011-04-26 08:12:10 +000014892 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014893 strcpy(str, "PCI Express");
14894 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000014895 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014896 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14897
14898 strcpy(str, "PCIX:");
14899
14900 if ((clock_ctrl == 7) ||
14901 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14902 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14903 strcat(str, "133MHz");
14904 else if (clock_ctrl == 0)
14905 strcat(str, "33MHz");
14906 else if (clock_ctrl == 2)
14907 strcat(str, "50MHz");
14908 else if (clock_ctrl == 4)
14909 strcat(str, "66MHz");
14910 else if (clock_ctrl == 6)
14911 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014912 } else {
14913 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000014914 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070014915 strcat(str, "66MHz");
14916 else
14917 strcat(str, "33MHz");
14918 }
Joe Perches63c3a662011-04-26 08:12:10 +000014919 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070014920 strcat(str, ":32-bit");
14921 else
14922 strcat(str, ":64-bit");
14923 return str;
14924}
14925
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014926static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014927{
14928 struct pci_dev *peer;
14929 unsigned int func, devnr = tp->pdev->devfn & ~7;
14930
14931 for (func = 0; func < 8; func++) {
14932 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14933 if (peer && peer != tp->pdev)
14934 break;
14935 pci_dev_put(peer);
14936 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014937 /* 5704 can be configured in single-port mode, set peer to
14938 * tp->pdev in that case.
14939 */
14940 if (!peer) {
14941 peer = tp->pdev;
14942 return peer;
14943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014944
14945 /*
14946 * We don't need to keep the refcount elevated; there's no way
14947 * to remove one half of this device without removing the other
14948 */
14949 pci_dev_put(peer);
14950
14951 return peer;
14952}
14953
David S. Miller15f98502005-05-18 22:49:26 -070014954static void __devinit tg3_init_coal(struct tg3 *tp)
14955{
14956 struct ethtool_coalesce *ec = &tp->coal;
14957
14958 memset(ec, 0, sizeof(*ec));
14959 ec->cmd = ETHTOOL_GCOALESCE;
14960 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14961 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14962 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14963 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14964 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14965 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14966 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14967 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14968 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14969
14970 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14971 HOSTCC_MODE_CLRTICK_TXBD)) {
14972 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14973 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14974 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14975 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14976 }
Michael Chand244c892005-07-05 14:42:33 -070014977
Joe Perches63c3a662011-04-26 08:12:10 +000014978 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070014979 ec->rx_coalesce_usecs_irq = 0;
14980 ec->tx_coalesce_usecs_irq = 0;
14981 ec->stats_block_coalesce_usecs = 0;
14982 }
David S. Miller15f98502005-05-18 22:49:26 -070014983}
14984
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014985static const struct net_device_ops tg3_netdev_ops = {
14986 .ndo_open = tg3_open,
14987 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014988 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014989 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014990 .ndo_validate_addr = eth_validate_addr,
14991 .ndo_set_multicast_list = tg3_set_rx_mode,
14992 .ndo_set_mac_address = tg3_set_mac_addr,
14993 .ndo_do_ioctl = tg3_ioctl,
14994 .ndo_tx_timeout = tg3_tx_timeout,
14995 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000014996 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000014997 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080014998#ifdef CONFIG_NET_POLL_CONTROLLER
14999 .ndo_poll_controller = tg3_poll_controller,
15000#endif
15001};
15002
Linus Torvalds1da177e2005-04-16 15:20:36 -070015003static int __devinit tg3_init_one(struct pci_dev *pdev,
15004 const struct pci_device_id *ent)
15005{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015006 struct net_device *dev;
15007 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015008 int i, err, pm_cap;
15009 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015010 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015011 u64 dma_mask, persist_dma_mask;
Matt Carlson0da06062011-05-19 12:12:53 +000015012 u32 features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015013
Joe Perches05dbe002010-02-17 19:44:19 +000015014 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015015
15016 err = pci_enable_device(pdev);
15017 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015018 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015019 return err;
15020 }
15021
Linus Torvalds1da177e2005-04-16 15:20:36 -070015022 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15023 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015024 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015025 goto err_out_disable_pdev;
15026 }
15027
15028 pci_set_master(pdev);
15029
15030 /* Find power-management capability. */
15031 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15032 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015033 dev_err(&pdev->dev,
15034 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015035 err = -EIO;
15036 goto err_out_free_res;
15037 }
15038
Matt Carlson16821282011-07-13 09:27:28 +000015039 err = pci_set_power_state(pdev, PCI_D0);
15040 if (err) {
15041 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15042 goto err_out_free_res;
15043 }
15044
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015045 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015046 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015047 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015048 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015049 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015050 }
15051
Linus Torvalds1da177e2005-04-16 15:20:36 -070015052 SET_NETDEV_DEV(dev, &pdev->dev);
15053
Linus Torvalds1da177e2005-04-16 15:20:36 -070015054 tp = netdev_priv(dev);
15055 tp->pdev = pdev;
15056 tp->dev = dev;
15057 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015058 tp->rx_mode = TG3_DEF_RX_MODE;
15059 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015060
Linus Torvalds1da177e2005-04-16 15:20:36 -070015061 if (tg3_debug > 0)
15062 tp->msg_enable = tg3_debug;
15063 else
15064 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15065
15066 /* The word/byte swap controls here control register access byte
15067 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15068 * setting below.
15069 */
15070 tp->misc_host_ctrl =
15071 MISC_HOST_CTRL_MASK_PCI_INT |
15072 MISC_HOST_CTRL_WORD_SWAP |
15073 MISC_HOST_CTRL_INDIR_ACCESS |
15074 MISC_HOST_CTRL_PCISTATE_RW;
15075
15076 /* The NONFRM (non-frame) byte/word swap controls take effect
15077 * on descriptor entries, anything which isn't packet data.
15078 *
15079 * The StrongARM chips on the board (one for tx, one for rx)
15080 * are running in big-endian mode.
15081 */
15082 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15083 GRC_MODE_WSWAP_NONFRM_DATA);
15084#ifdef __BIG_ENDIAN
15085 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15086#endif
15087 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015088 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015089 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015090
Matt Carlsond5fe4882008-11-21 17:20:32 -080015091 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015092 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015093 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015094 err = -ENOMEM;
15095 goto err_out_free_dev;
15096 }
15097
Matt Carlsonc9cab242011-07-13 09:27:27 +000015098 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15099 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15100 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15101 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15102 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15103 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15104 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15105 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15106 tg3_flag_set(tp, ENABLE_APE);
15107 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15108 if (!tp->aperegs) {
15109 dev_err(&pdev->dev,
15110 "Cannot map APE registers, aborting\n");
15111 err = -ENOMEM;
15112 goto err_out_iounmap;
15113 }
15114 }
15115
Linus Torvalds1da177e2005-04-16 15:20:36 -070015116 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15117 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015118
Linus Torvalds1da177e2005-04-16 15:20:36 -070015119 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015120 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015121 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015122 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015123
15124 err = tg3_get_invariants(tp);
15125 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015126 dev_err(&pdev->dev,
15127 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015128 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015129 }
15130
Michael Chan4a29cc22006-03-19 13:21:12 -080015131 /* The EPB bridge inside 5714, 5715, and 5780 and any
15132 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015133 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15134 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15135 * do DMA address check in tg3_start_xmit().
15136 */
Joe Perches63c3a662011-04-26 08:12:10 +000015137 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015138 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015139 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015140 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015141#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015142 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015143#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015144 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015145 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015146
15147 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015148 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015149 err = pci_set_dma_mask(pdev, dma_mask);
15150 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015151 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015152 err = pci_set_consistent_dma_mask(pdev,
15153 persist_dma_mask);
15154 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015155 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15156 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015157 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015158 }
15159 }
15160 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015161 if (err || dma_mask == DMA_BIT_MASK(32)) {
15162 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015163 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015164 dev_err(&pdev->dev,
15165 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015166 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015167 }
15168 }
15169
Michael Chanfdfec1722005-07-25 12:31:48 -070015170 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015171
Matt Carlson0da06062011-05-19 12:12:53 +000015172 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15173
15174 /* 5700 B0 chips do not support checksumming correctly due
15175 * to hardware bugs.
15176 */
15177 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15178 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15179
15180 if (tg3_flag(tp, 5755_PLUS))
15181 features |= NETIF_F_IPV6_CSUM;
15182 }
15183
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015184 /* TSO is on by default on chips that support hardware TSO.
15185 * Firmware TSO on older chips gives lower performance, so it
15186 * is off by default, but can be enabled using ethtool.
15187 */
Joe Perches63c3a662011-04-26 08:12:10 +000015188 if ((tg3_flag(tp, HW_TSO_1) ||
15189 tg3_flag(tp, HW_TSO_2) ||
15190 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015191 (features & NETIF_F_IP_CSUM))
15192 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015193 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015194 if (features & NETIF_F_IPV6_CSUM)
15195 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015196 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015197 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015198 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15199 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015200 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015201 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015202 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015204
Matt Carlsond542fe22011-05-19 16:02:43 +000015205 dev->features |= features;
15206 dev->vlan_features |= features;
15207
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015208 /*
15209 * Add loopback capability only for a subset of devices that support
15210 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15211 * loopback for the remaining devices.
15212 */
15213 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15214 !tg3_flag(tp, CPMU_PRESENT))
15215 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015216 features |= NETIF_F_LOOPBACK;
15217
Matt Carlson0da06062011-05-19 12:12:53 +000015218 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015219
Linus Torvalds1da177e2005-04-16 15:20:36 -070015220 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015221 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015222 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015223 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015224 tp->rx_pending = 63;
15225 }
15226
Linus Torvalds1da177e2005-04-16 15:20:36 -070015227 err = tg3_get_device_address(tp);
15228 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015229 dev_err(&pdev->dev,
15230 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015231 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015232 }
15233
Matt Carlsonc88864d2007-11-12 21:07:01 -080015234 /*
15235 * Reset chip in case UNDI or EFI driver did not shutdown
15236 * DMA self test will enable WDMAC and we'll see (spurious)
15237 * pending DMA on the PCI bus at that point.
15238 */
15239 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15240 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15241 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15242 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15243 }
15244
15245 err = tg3_test_dma(tp);
15246 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015247 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015248 goto err_out_apeunmap;
15249 }
15250
Matt Carlson78f90dc2009-11-13 13:03:42 +000015251 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15252 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15253 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015254 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015255 struct tg3_napi *tnapi = &tp->napi[i];
15256
15257 tnapi->tp = tp;
15258 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15259
15260 tnapi->int_mbox = intmbx;
15261 if (i < 4)
15262 intmbx += 0x8;
15263 else
15264 intmbx += 0x4;
15265
15266 tnapi->consmbox = rcvmbx;
15267 tnapi->prodmbox = sndmbx;
15268
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015269 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015270 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015271 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015272 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015273
Joe Perches63c3a662011-04-26 08:12:10 +000015274 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015275 break;
15276
15277 /*
15278 * If we support MSIX, we'll be using RSS. If we're using
15279 * RSS, the first vector only handles link interrupts and the
15280 * remaining vectors handle rx and tx interrupts. Reuse the
15281 * mailbox values for the next iteration. The values we setup
15282 * above are still useful for the single vectored mode.
15283 */
15284 if (!i)
15285 continue;
15286
15287 rcvmbx += 0x8;
15288
15289 if (sndmbx & 0x4)
15290 sndmbx -= 0x4;
15291 else
15292 sndmbx += 0xc;
15293 }
15294
Matt Carlsonc88864d2007-11-12 21:07:01 -080015295 tg3_init_coal(tp);
15296
Michael Chanc49a1562006-12-17 17:07:29 -080015297 pci_set_drvdata(pdev, dev);
15298
Linus Torvalds1da177e2005-04-16 15:20:36 -070015299 err = register_netdev(dev);
15300 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015301 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015302 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015303 }
15304
Joe Perches05dbe002010-02-17 19:44:19 +000015305 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15306 tp->board_part_number,
15307 tp->pci_chip_rev_id,
15308 tg3_bus_string(tp, str),
15309 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015310
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015311 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015312 struct phy_device *phydev;
15313 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015314 netdev_info(dev,
15315 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015316 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015317 } else {
15318 char *ethtype;
15319
15320 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15321 ethtype = "10/100Base-TX";
15322 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15323 ethtype = "1000Base-SX";
15324 else
15325 ethtype = "10/100/1000Base-T";
15326
Matt Carlson5129c3a2010-04-05 10:19:23 +000015327 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015328 "(WireSpeed[%d], EEE[%d])\n",
15329 tg3_phy_string(tp), ethtype,
15330 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15331 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015332 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015333
Joe Perches05dbe002010-02-17 19:44:19 +000015334 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015335 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015336 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015337 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015338 tg3_flag(tp, ENABLE_ASF) != 0,
15339 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015340 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15341 tp->dma_rwctrl,
15342 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15343 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015344
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015345 pci_save_state(pdev);
15346
Linus Torvalds1da177e2005-04-16 15:20:36 -070015347 return 0;
15348
Matt Carlson0d3031d2007-10-10 18:02:43 -070015349err_out_apeunmap:
15350 if (tp->aperegs) {
15351 iounmap(tp->aperegs);
15352 tp->aperegs = NULL;
15353 }
15354
Linus Torvalds1da177e2005-04-16 15:20:36 -070015355err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015356 if (tp->regs) {
15357 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015358 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015360
15361err_out_free_dev:
15362 free_netdev(dev);
15363
Matt Carlson16821282011-07-13 09:27:28 +000015364err_out_power_down:
15365 pci_set_power_state(pdev, PCI_D3hot);
15366
Linus Torvalds1da177e2005-04-16 15:20:36 -070015367err_out_free_res:
15368 pci_release_regions(pdev);
15369
15370err_out_disable_pdev:
15371 pci_disable_device(pdev);
15372 pci_set_drvdata(pdev, NULL);
15373 return err;
15374}
15375
15376static void __devexit tg3_remove_one(struct pci_dev *pdev)
15377{
15378 struct net_device *dev = pci_get_drvdata(pdev);
15379
15380 if (dev) {
15381 struct tg3 *tp = netdev_priv(dev);
15382
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015383 if (tp->fw)
15384 release_firmware(tp->fw);
15385
Tejun Heo23f333a2010-12-12 16:45:14 +010015386 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015387
Joe Perches63c3a662011-04-26 08:12:10 +000015388 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015389 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015390 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015391 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015392
Linus Torvalds1da177e2005-04-16 15:20:36 -070015393 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015394 if (tp->aperegs) {
15395 iounmap(tp->aperegs);
15396 tp->aperegs = NULL;
15397 }
Michael Chan68929142005-08-09 20:17:14 -070015398 if (tp->regs) {
15399 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015400 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015402 free_netdev(dev);
15403 pci_release_regions(pdev);
15404 pci_disable_device(pdev);
15405 pci_set_drvdata(pdev, NULL);
15406 }
15407}
15408
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015409#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015410static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015411{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015412 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015413 struct net_device *dev = pci_get_drvdata(pdev);
15414 struct tg3 *tp = netdev_priv(dev);
15415 int err;
15416
15417 if (!netif_running(dev))
15418 return 0;
15419
Tejun Heo23f333a2010-12-12 16:45:14 +010015420 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015421 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015422 tg3_netif_stop(tp);
15423
15424 del_timer_sync(&tp->timer);
15425
David S. Millerf47c11e2005-06-24 20:18:35 -070015426 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015427 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015428 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015429
15430 netif_device_detach(dev);
15431
David S. Millerf47c11e2005-06-24 20:18:35 -070015432 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015433 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015434 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015435 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015436
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015437 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015438 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015439 int err2;
15440
David S. Millerf47c11e2005-06-24 20:18:35 -070015441 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015442
Joe Perches63c3a662011-04-26 08:12:10 +000015443 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015444 err2 = tg3_restart_hw(tp, 1);
15445 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015446 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015447
15448 tp->timer.expires = jiffies + tp->timer_offset;
15449 add_timer(&tp->timer);
15450
15451 netif_device_attach(dev);
15452 tg3_netif_start(tp);
15453
Michael Chanb9ec6c12006-07-25 16:37:27 -070015454out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015455 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015456
15457 if (!err2)
15458 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015459 }
15460
15461 return err;
15462}
15463
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015464static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015465{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015466 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015467 struct net_device *dev = pci_get_drvdata(pdev);
15468 struct tg3 *tp = netdev_priv(dev);
15469 int err;
15470
15471 if (!netif_running(dev))
15472 return 0;
15473
Linus Torvalds1da177e2005-04-16 15:20:36 -070015474 netif_device_attach(dev);
15475
David S. Millerf47c11e2005-06-24 20:18:35 -070015476 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015477
Joe Perches63c3a662011-04-26 08:12:10 +000015478 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015479 err = tg3_restart_hw(tp, 1);
15480 if (err)
15481 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015482
15483 tp->timer.expires = jiffies + tp->timer_offset;
15484 add_timer(&tp->timer);
15485
Linus Torvalds1da177e2005-04-16 15:20:36 -070015486 tg3_netif_start(tp);
15487
Michael Chanb9ec6c12006-07-25 16:37:27 -070015488out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015489 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015490
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015491 if (!err)
15492 tg3_phy_start(tp);
15493
Michael Chanb9ec6c12006-07-25 16:37:27 -070015494 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015495}
15496
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015497static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015498#define TG3_PM_OPS (&tg3_pm_ops)
15499
15500#else
15501
15502#define TG3_PM_OPS NULL
15503
15504#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015505
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015506/**
15507 * tg3_io_error_detected - called when PCI error is detected
15508 * @pdev: Pointer to PCI device
15509 * @state: The current pci connection state
15510 *
15511 * This function is called after a PCI bus error affecting
15512 * this device has been detected.
15513 */
15514static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15515 pci_channel_state_t state)
15516{
15517 struct net_device *netdev = pci_get_drvdata(pdev);
15518 struct tg3 *tp = netdev_priv(netdev);
15519 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15520
15521 netdev_info(netdev, "PCI I/O error detected\n");
15522
15523 rtnl_lock();
15524
15525 if (!netif_running(netdev))
15526 goto done;
15527
15528 tg3_phy_stop(tp);
15529
15530 tg3_netif_stop(tp);
15531
15532 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015533 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015534
15535 /* Want to make sure that the reset task doesn't run */
15536 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015537 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15538 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015539
15540 netif_device_detach(netdev);
15541
15542 /* Clean up software state, even if MMIO is blocked */
15543 tg3_full_lock(tp, 0);
15544 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15545 tg3_full_unlock(tp);
15546
15547done:
15548 if (state == pci_channel_io_perm_failure)
15549 err = PCI_ERS_RESULT_DISCONNECT;
15550 else
15551 pci_disable_device(pdev);
15552
15553 rtnl_unlock();
15554
15555 return err;
15556}
15557
15558/**
15559 * tg3_io_slot_reset - called after the pci bus has been reset.
15560 * @pdev: Pointer to PCI device
15561 *
15562 * Restart the card from scratch, as if from a cold-boot.
15563 * At this point, the card has exprienced a hard reset,
15564 * followed by fixups by BIOS, and has its config space
15565 * set up identically to what it was at cold boot.
15566 */
15567static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15568{
15569 struct net_device *netdev = pci_get_drvdata(pdev);
15570 struct tg3 *tp = netdev_priv(netdev);
15571 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15572 int err;
15573
15574 rtnl_lock();
15575
15576 if (pci_enable_device(pdev)) {
15577 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15578 goto done;
15579 }
15580
15581 pci_set_master(pdev);
15582 pci_restore_state(pdev);
15583 pci_save_state(pdev);
15584
15585 if (!netif_running(netdev)) {
15586 rc = PCI_ERS_RESULT_RECOVERED;
15587 goto done;
15588 }
15589
15590 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000015591 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015592 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015593
15594 rc = PCI_ERS_RESULT_RECOVERED;
15595
15596done:
15597 rtnl_unlock();
15598
15599 return rc;
15600}
15601
15602/**
15603 * tg3_io_resume - called when traffic can start flowing again.
15604 * @pdev: Pointer to PCI device
15605 *
15606 * This callback is called when the error recovery driver tells
15607 * us that its OK to resume normal operation.
15608 */
15609static void tg3_io_resume(struct pci_dev *pdev)
15610{
15611 struct net_device *netdev = pci_get_drvdata(pdev);
15612 struct tg3 *tp = netdev_priv(netdev);
15613 int err;
15614
15615 rtnl_lock();
15616
15617 if (!netif_running(netdev))
15618 goto done;
15619
15620 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015621 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015622 err = tg3_restart_hw(tp, 1);
15623 tg3_full_unlock(tp);
15624 if (err) {
15625 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15626 goto done;
15627 }
15628
15629 netif_device_attach(netdev);
15630
15631 tp->timer.expires = jiffies + tp->timer_offset;
15632 add_timer(&tp->timer);
15633
15634 tg3_netif_start(tp);
15635
15636 tg3_phy_start(tp);
15637
15638done:
15639 rtnl_unlock();
15640}
15641
15642static struct pci_error_handlers tg3_err_handler = {
15643 .error_detected = tg3_io_error_detected,
15644 .slot_reset = tg3_io_slot_reset,
15645 .resume = tg3_io_resume
15646};
15647
Linus Torvalds1da177e2005-04-16 15:20:36 -070015648static struct pci_driver tg3_driver = {
15649 .name = DRV_MODULE_NAME,
15650 .id_table = tg3_pci_tbl,
15651 .probe = tg3_init_one,
15652 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015653 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015654 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015655};
15656
15657static int __init tg3_init(void)
15658{
Jeff Garzik29917622006-08-19 17:48:59 -040015659 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015660}
15661
15662static void __exit tg3_cleanup(void)
15663{
15664 pci_unregister_driver(&tg3_driver);
15665}
15666
15667module_init(tg3_init);
15668module_exit(tg3_cleanup);