blob: a7e28a2c5348596fa7ff9e16f50e7487559d9005 [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 Carlsoneaa36662011-08-19 13:58:24 +000092#define TG3_MIN_NUM 120
Matt Carlson6867c842010-07-11 09:31:44 +000093#define DRV_MODULE_VERSION \
94 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlsoneaa36662011-08-19 13:58:24 +000095#define DRV_MODULE_RELDATE "August 18, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#define TG3_DEF_RX_MODE 0
98#define TG3_DEF_TX_MODE 0
99#define TG3_DEF_MSG_ENABLE \
100 (NETIF_MSG_DRV | \
101 NETIF_MSG_PROBE | \
102 NETIF_MSG_LINK | \
103 NETIF_MSG_TIMER | \
104 NETIF_MSG_IFDOWN | \
105 NETIF_MSG_IFUP | \
106 NETIF_MSG_RX_ERR | \
107 NETIF_MSG_TX_ERR)
108
Matt Carlson520b2752011-06-13 13:39:02 +0000109#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/* length of time before we decide the hardware is borked,
112 * and dev->tx_timeout() should be called to fix the problem
113 */
Joe Perches63c3a662011-04-26 08:12:10 +0000114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define TG3_TX_TIMEOUT (5 * HZ)
116
117/* hardware minimum and maximum for a single frame's data payload */
118#define TG3_MIN_MTU 60
119#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000120 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/* These numbers seem to be hard coded in the NIC firmware somehow.
123 * You can't change the ring sizes, but you can change where you place
124 * them in the NIC onboard memory.
125 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000126#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000127 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000128 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000130#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000131 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000132 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000134#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136/* Do not place this n-ring entries value into the tp struct itself,
137 * we really want to expose these constants to GCC so that modulo et
138 * al. operations are done with shifts and masks instead of with
139 * hw multiply/modulo instructions. Another solution would be to
140 * replace things like '% foo' with '& (foo - 1)'.
141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143#define TG3_TX_RING_SIZE 512
144#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
145
Matt Carlson2c49a442010-09-30 10:34:35 +0000146#define TG3_RX_STD_RING_BYTES(tp) \
147 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
148#define TG3_RX_JMB_RING_BYTES(tp) \
149 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
150#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000151 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
153 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
155
Matt Carlson287be122009-08-28 13:58:46 +0000156#define TG3_DMA_BYTE_ENAB 64
157
158#define TG3_RX_STD_DMA_SZ 1536
159#define TG3_RX_JMB_DMA_SZ 9046
160
161#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
162
163#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
164#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Matt Carlson2c49a442010-09-30 10:34:35 +0000166#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
167 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000168
Matt Carlson2c49a442010-09-30 10:34:35 +0000169#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
170 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000171
Matt Carlsond2757fc2010-04-12 06:58:27 +0000172/* Due to a hardware bug, the 5701 can only DMA to memory addresses
173 * that are at least dword aligned when used in PCIX mode. The driver
174 * works around this bug by double copying the packet. This workaround
175 * is built into the normal double copy length check for efficiency.
176 *
177 * However, the double copy is only necessary on those architectures
178 * where unaligned memory accesses are inefficient. For those architectures
179 * where unaligned memory accesses incur little penalty, we can reintegrate
180 * the 5701 in the normal rx path. Doing so saves a device structure
181 * dereference by hardcoding the double copy threshold in place.
182 */
183#define TG3_RX_COPY_THRESHOLD 256
184#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
185 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
186#else
187 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
188#endif
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000191#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlsone31aa982011-07-27 14:20:53 +0000192#define TG3_TX_BD_DMA_MAX 4096
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[] = {
Matt Carlson28a45952011-08-19 13:58:22 +0000393 { "nvram test (online) " },
394 { "link test (online) " },
395 { "register test (offline)" },
396 { "memory test (offline)" },
397 { "mac loopback test (offline)" },
398 { "phy loopback test (offline)" },
Matt Carlson941ec902011-08-19 13:58:23 +0000399 { "ext loopback test (offline)" },
Matt Carlson28a45952011-08-19 13:58:22 +0000400 { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700401};
402
Matt Carlson48fa55a2011-04-13 11:05:06 +0000403#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
404
405
Michael Chanb401e9e2005-12-19 16:27:04 -0800406static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
407{
408 writel(val, tp->regs + off);
409}
410
411static u32 tg3_read32(struct tg3 *tp, u32 off)
412{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000413 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800414}
415
Matt Carlson0d3031d2007-10-10 18:02:43 -0700416static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
417{
418 writel(val, tp->aperegs + off);
419}
420
421static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
422{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000423 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
427{
Michael Chan68929142005-08-09 20:17:14 -0700428 unsigned long flags;
429
430 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700431 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
432 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700433 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700434}
435
436static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
437{
438 writel(val, tp->regs + off);
439 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Michael Chan68929142005-08-09 20:17:14 -0700442static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
443{
444 unsigned long flags;
445 u32 val;
446
447 spin_lock_irqsave(&tp->indirect_lock, flags);
448 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
449 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
450 spin_unlock_irqrestore(&tp->indirect_lock, flags);
451 return val;
452}
453
454static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
455{
456 unsigned long flags;
457
458 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
459 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
460 TG3_64BIT_REG_LOW, val);
461 return;
462 }
Matt Carlson66711e62009-11-13 13:03:49 +0000463 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700464 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
465 TG3_64BIT_REG_LOW, val);
466 return;
467 }
468
469 spin_lock_irqsave(&tp->indirect_lock, flags);
470 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
471 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
472 spin_unlock_irqrestore(&tp->indirect_lock, flags);
473
474 /* In indirect mode when disabling interrupts, we also need
475 * to clear the interrupt bit in the GRC local ctrl register.
476 */
477 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
478 (val == 0x1)) {
479 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
480 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
481 }
482}
483
484static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
485{
486 unsigned long flags;
487 u32 val;
488
489 spin_lock_irqsave(&tp->indirect_lock, flags);
490 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
491 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
492 spin_unlock_irqrestore(&tp->indirect_lock, flags);
493 return val;
494}
495
Michael Chanb401e9e2005-12-19 16:27:04 -0800496/* usec_wait specifies the wait time in usec when writing to certain registers
497 * where it is unsafe to read back the register without some delay.
498 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
499 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
500 */
501static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Joe Perches63c3a662011-04-26 08:12:10 +0000503 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800504 /* Non-posted methods */
505 tp->write32(tp, off, val);
506 else {
507 /* Posted method */
508 tg3_write32(tp, off, val);
509 if (usec_wait)
510 udelay(usec_wait);
511 tp->read32(tp, off);
512 }
513 /* Wait again after the read for the posted method to guarantee that
514 * the wait time is met.
515 */
516 if (usec_wait)
517 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Michael Chan09ee9292005-08-09 20:17:00 -0700520static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
521{
522 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000523 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700524 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700525}
526
Michael Chan20094932005-08-09 20:16:32 -0700527static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 void __iomem *mbox = tp->regs + off;
530 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000531 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000533 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 readl(mbox);
535}
536
Michael Chanb5d37722006-09-27 16:06:21 -0700537static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
538{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000539 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700540}
541
542static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
543{
544 writel(val, tp->regs + off + GRCMBOX_BASE);
545}
546
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000547#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700548#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000549#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
550#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
551#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700552
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000553#define tw32(reg, val) tp->write32(tp, reg, val)
554#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
555#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
556#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
559{
Michael Chan68929142005-08-09 20:17:14 -0700560 unsigned long flags;
561
Matt Carlson6ff6f812011-05-19 12:12:54 +0000562 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700563 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
564 return;
565
Michael Chan68929142005-08-09 20:17:14 -0700566 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000567 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700568 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
569 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Michael Chanbbadf502006-04-06 21:46:34 -0700571 /* Always leave this as zero. */
572 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
573 } else {
574 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
575 tw32_f(TG3PCI_MEM_WIN_DATA, val);
576
577 /* Always leave this as zero. */
578 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
579 }
Michael Chan68929142005-08-09 20:17:14 -0700580 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
584{
Michael Chan68929142005-08-09 20:17:14 -0700585 unsigned long flags;
586
Matt Carlson6ff6f812011-05-19 12:12:54 +0000587 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700588 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
589 *val = 0;
590 return;
591 }
592
Michael Chan68929142005-08-09 20:17:14 -0700593 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000594 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700595 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
596 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Michael Chanbbadf502006-04-06 21:46:34 -0700598 /* Always leave this as zero. */
599 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
600 } else {
601 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
602 *val = tr32(TG3PCI_MEM_WIN_DATA);
603
604 /* Always leave this as zero. */
605 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
606 }
Michael Chan68929142005-08-09 20:17:14 -0700607 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
Matt Carlson0d3031d2007-10-10 18:02:43 -0700610static void tg3_ape_lock_init(struct tg3 *tp)
611{
612 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000613 u32 regbase, bit;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000614
615 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
616 regbase = TG3_APE_LOCK_GRANT;
617 else
618 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700619
620 /* Make sure the driver hasn't any stale locks. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000621 for (i = 0; i < 8; i++) {
622 if (i == TG3_APE_LOCK_GPIO)
623 continue;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000624 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000625 }
626
627 /* Clear the correct bit of the GPIO lock too. */
628 if (!tp->pci_fn)
629 bit = APE_LOCK_GRANT_DRIVER;
630 else
631 bit = 1 << tp->pci_fn;
632
633 tg3_ape_write32(tp, regbase + 4 * TG3_APE_LOCK_GPIO, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700634}
635
636static int tg3_ape_lock(struct tg3 *tp, int locknum)
637{
638 int i, off;
639 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000640 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700641
Joe Perches63c3a662011-04-26 08:12:10 +0000642 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700643 return 0;
644
645 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000646 case TG3_APE_LOCK_GPIO:
647 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
648 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000649 case TG3_APE_LOCK_GRC:
650 case TG3_APE_LOCK_MEM:
651 break;
652 default:
653 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700654 }
655
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000656 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
657 req = TG3_APE_LOCK_REQ;
658 gnt = TG3_APE_LOCK_GRANT;
659 } else {
660 req = TG3_APE_PER_LOCK_REQ;
661 gnt = TG3_APE_PER_LOCK_GRANT;
662 }
663
Matt Carlson0d3031d2007-10-10 18:02:43 -0700664 off = 4 * locknum;
665
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000666 if (locknum != TG3_APE_LOCK_GPIO || !tp->pci_fn)
667 bit = APE_LOCK_REQ_DRIVER;
668 else
669 bit = 1 << tp->pci_fn;
670
671 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700672
673 /* Wait for up to 1 millisecond to acquire lock. */
674 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000675 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000676 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700677 break;
678 udelay(10);
679 }
680
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000681 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700682 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000683 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700684 ret = -EBUSY;
685 }
686
687 return ret;
688}
689
690static void tg3_ape_unlock(struct tg3 *tp, int locknum)
691{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000692 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700693
Joe Perches63c3a662011-04-26 08:12:10 +0000694 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700695 return;
696
697 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000698 case TG3_APE_LOCK_GPIO:
699 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
700 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000701 case TG3_APE_LOCK_GRC:
702 case TG3_APE_LOCK_MEM:
703 break;
704 default:
705 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700706 }
707
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000708 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
709 gnt = TG3_APE_LOCK_GRANT;
710 else
711 gnt = TG3_APE_PER_LOCK_GRANT;
712
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000713 if (locknum != TG3_APE_LOCK_GPIO || !tp->pci_fn)
714 bit = APE_LOCK_GRANT_DRIVER;
715 else
716 bit = 1 << tp->pci_fn;
717
718 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700719}
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721static void tg3_disable_ints(struct tg3 *tp)
722{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000723 int i;
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 tw32(TG3PCI_MISC_HOST_CTRL,
726 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000727 for (i = 0; i < tp->irq_max; i++)
728 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731static void tg3_enable_ints(struct tg3 *tp)
732{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000733 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000734
Michael Chanbbe832c2005-06-24 20:20:04 -0700735 tp->irq_sync = 0;
736 wmb();
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 tw32(TG3PCI_MISC_HOST_CTRL,
739 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000740
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000741 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000742 for (i = 0; i < tp->irq_cnt; i++) {
743 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000744
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000745 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000746 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000747 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
748
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000749 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000750 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000751
752 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000753 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000754 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
755 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
756 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000757 tw32(HOSTCC_MODE, tp->coal_now);
758
759 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Matt Carlson17375d22009-08-28 14:02:18 +0000762static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700763{
Matt Carlson17375d22009-08-28 14:02:18 +0000764 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000765 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700766 unsigned int work_exists = 0;
767
768 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000769 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700770 if (sblk->status & SD_STATUS_LINK_CHG)
771 work_exists = 1;
772 }
773 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000774 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000775 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700776 work_exists = 1;
777
778 return work_exists;
779}
780
Matt Carlson17375d22009-08-28 14:02:18 +0000781/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700782 * similar to tg3_enable_ints, but it accurately determines whether there
783 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400784 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
Matt Carlson17375d22009-08-28 14:02:18 +0000786static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Matt Carlson17375d22009-08-28 14:02:18 +0000788 struct tg3 *tp = tnapi->tp;
789
Matt Carlson898a56f2009-08-28 14:02:40 +0000790 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 mmiowb();
792
David S. Millerfac9b832005-05-18 22:46:34 -0700793 /* When doing tagged status, this work check is unnecessary.
794 * The last_tag we write above tells the chip which piece of
795 * work we've completed.
796 */
Joe Perches63c3a662011-04-26 08:12:10 +0000797 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700798 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000799 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802static void tg3_switch_clocks(struct tg3 *tp)
803{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000804 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 u32 orig_clock_ctrl;
806
Joe Perches63c3a662011-04-26 08:12:10 +0000807 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700808 return;
809
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000810 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 orig_clock_ctrl = clock_ctrl;
813 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
814 CLOCK_CTRL_CLKRUN_OENABLE |
815 0x1f);
816 tp->pci_clock_ctrl = clock_ctrl;
817
Joe Perches63c3a662011-04-26 08:12:10 +0000818 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800820 tw32_wait_f(TG3PCI_CLOCK_CTRL,
821 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800824 tw32_wait_f(TG3PCI_CLOCK_CTRL,
825 clock_ctrl |
826 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
827 40);
828 tw32_wait_f(TG3PCI_CLOCK_CTRL,
829 clock_ctrl | (CLOCK_CTRL_ALTCLK),
830 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800832 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835#define PHY_BUSY_LOOPS 5000
836
837static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
838{
839 u32 frame_val;
840 unsigned int loops;
841 int ret;
842
843 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
844 tw32_f(MAC_MI_MODE,
845 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
846 udelay(80);
847 }
848
849 *val = 0x0;
850
Matt Carlson882e9792009-09-01 13:21:36 +0000851 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 MI_COM_PHY_ADDR_MASK);
853 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
854 MI_COM_REG_ADDR_MASK);
855 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 tw32_f(MAC_MI_COM, frame_val);
858
859 loops = PHY_BUSY_LOOPS;
860 while (loops != 0) {
861 udelay(10);
862 frame_val = tr32(MAC_MI_COM);
863
864 if ((frame_val & MI_COM_BUSY) == 0) {
865 udelay(5);
866 frame_val = tr32(MAC_MI_COM);
867 break;
868 }
869 loops -= 1;
870 }
871
872 ret = -EBUSY;
873 if (loops != 0) {
874 *val = frame_val & MI_COM_DATA_MASK;
875 ret = 0;
876 }
877
878 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
879 tw32_f(MAC_MI_MODE, tp->mi_mode);
880 udelay(80);
881 }
882
883 return ret;
884}
885
886static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
887{
888 u32 frame_val;
889 unsigned int loops;
890 int ret;
891
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000892 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +0000893 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -0700894 return 0;
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
897 tw32_f(MAC_MI_MODE,
898 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
899 udelay(80);
900 }
901
Matt Carlson882e9792009-09-01 13:21:36 +0000902 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 MI_COM_PHY_ADDR_MASK);
904 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
905 MI_COM_REG_ADDR_MASK);
906 frame_val |= (val & MI_COM_DATA_MASK);
907 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 tw32_f(MAC_MI_COM, frame_val);
910
911 loops = PHY_BUSY_LOOPS;
912 while (loops != 0) {
913 udelay(10);
914 frame_val = tr32(MAC_MI_COM);
915 if ((frame_val & MI_COM_BUSY) == 0) {
916 udelay(5);
917 frame_val = tr32(MAC_MI_COM);
918 break;
919 }
920 loops -= 1;
921 }
922
923 ret = -EBUSY;
924 if (loops != 0)
925 ret = 0;
926
927 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
928 tw32_f(MAC_MI_MODE, tp->mi_mode);
929 udelay(80);
930 }
931
932 return ret;
933}
934
Matt Carlsonb0988c12011-04-20 07:57:39 +0000935static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
936{
937 int err;
938
939 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
940 if (err)
941 goto done;
942
943 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
944 if (err)
945 goto done;
946
947 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
948 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
949 if (err)
950 goto done;
951
952 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
953
954done:
955 return err;
956}
957
958static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
959{
960 int err;
961
962 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
963 if (err)
964 goto done;
965
966 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
967 if (err)
968 goto done;
969
970 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
971 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
972 if (err)
973 goto done;
974
975 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
976
977done:
978 return err;
979}
980
981static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
982{
983 int err;
984
985 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
986 if (!err)
987 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
988
989 return err;
990}
991
992static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
993{
994 int err;
995
996 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
997 if (!err)
998 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
999
1000 return err;
1001}
1002
Matt Carlson15ee95c2011-04-20 07:57:40 +00001003static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1004{
1005 int err;
1006
1007 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1008 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1009 MII_TG3_AUXCTL_SHDWSEL_MISC);
1010 if (!err)
1011 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1012
1013 return err;
1014}
1015
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001016static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1017{
1018 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1019 set |= MII_TG3_AUXCTL_MISC_WREN;
1020
1021 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1022}
1023
Matt Carlson1d36ba42011-04-20 07:57:42 +00001024#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1025 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1026 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1027 MII_TG3_AUXCTL_ACTL_TX_6DB)
1028
1029#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1030 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1031 MII_TG3_AUXCTL_ACTL_TX_6DB);
1032
Matt Carlson95e28692008-05-25 23:44:14 -07001033static int tg3_bmcr_reset(struct tg3 *tp)
1034{
1035 u32 phy_control;
1036 int limit, err;
1037
1038 /* OK, reset it, and poll the BMCR_RESET bit until it
1039 * clears or we time out.
1040 */
1041 phy_control = BMCR_RESET;
1042 err = tg3_writephy(tp, MII_BMCR, phy_control);
1043 if (err != 0)
1044 return -EBUSY;
1045
1046 limit = 5000;
1047 while (limit--) {
1048 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1049 if (err != 0)
1050 return -EBUSY;
1051
1052 if ((phy_control & BMCR_RESET) == 0) {
1053 udelay(40);
1054 break;
1055 }
1056 udelay(10);
1057 }
Roel Kluind4675b52009-02-12 16:33:27 -08001058 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001059 return -EBUSY;
1060
1061 return 0;
1062}
1063
Matt Carlson158d7ab2008-05-29 01:37:54 -07001064static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1065{
Francois Romieu3d165432009-01-19 16:56:50 -08001066 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001067 u32 val;
1068
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001069 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001070
1071 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001072 val = -EIO;
1073
1074 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001075
1076 return val;
1077}
1078
1079static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1080{
Francois Romieu3d165432009-01-19 16:56:50 -08001081 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001082 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001083
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001084 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001085
1086 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001087 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001088
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001089 spin_unlock_bh(&tp->lock);
1090
1091 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001092}
1093
1094static int tg3_mdio_reset(struct mii_bus *bp)
1095{
1096 return 0;
1097}
1098
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001099static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001100{
1101 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001102 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001103
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001104 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001105 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001106 case PHY_ID_BCM50610:
1107 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001108 val = MAC_PHYCFG2_50610_LED_MODES;
1109 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001110 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001111 val = MAC_PHYCFG2_AC131_LED_MODES;
1112 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001113 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001114 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1115 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001116 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001117 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1118 break;
1119 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001120 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001121 }
1122
1123 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1124 tw32(MAC_PHYCFG2, val);
1125
1126 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001127 val &= ~(MAC_PHYCFG1_RGMII_INT |
1128 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1129 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001130 tw32(MAC_PHYCFG1, val);
1131
1132 return;
1133 }
1134
Joe Perches63c3a662011-04-26 08:12:10 +00001135 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001136 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1137 MAC_PHYCFG2_FMODE_MASK_MASK |
1138 MAC_PHYCFG2_GMODE_MASK_MASK |
1139 MAC_PHYCFG2_ACT_MASK_MASK |
1140 MAC_PHYCFG2_QUAL_MASK_MASK |
1141 MAC_PHYCFG2_INBAND_ENABLE;
1142
1143 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001144
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001145 val = tr32(MAC_PHYCFG1);
1146 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1147 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001148 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1149 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001150 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001151 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001152 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1153 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001154 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1155 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1156 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001157
Matt Carlsona9daf362008-05-25 23:49:44 -07001158 val = tr32(MAC_EXT_RGMII_MODE);
1159 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1160 MAC_RGMII_MODE_RX_QUALITY |
1161 MAC_RGMII_MODE_RX_ACTIVITY |
1162 MAC_RGMII_MODE_RX_ENG_DET |
1163 MAC_RGMII_MODE_TX_ENABLE |
1164 MAC_RGMII_MODE_TX_LOWPWR |
1165 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001166 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1167 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001168 val |= MAC_RGMII_MODE_RX_INT_B |
1169 MAC_RGMII_MODE_RX_QUALITY |
1170 MAC_RGMII_MODE_RX_ACTIVITY |
1171 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001172 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001173 val |= MAC_RGMII_MODE_TX_ENABLE |
1174 MAC_RGMII_MODE_TX_LOWPWR |
1175 MAC_RGMII_MODE_TX_RESET;
1176 }
1177 tw32(MAC_EXT_RGMII_MODE, val);
1178}
1179
Matt Carlson158d7ab2008-05-29 01:37:54 -07001180static void tg3_mdio_start(struct tg3 *tp)
1181{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001182 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1183 tw32_f(MAC_MI_MODE, tp->mi_mode);
1184 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001185
Joe Perches63c3a662011-04-26 08:12:10 +00001186 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001187 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1188 tg3_mdio_config_5785(tp);
1189}
1190
1191static int tg3_mdio_init(struct tg3 *tp)
1192{
1193 int i;
1194 u32 reg;
1195 struct phy_device *phydev;
1196
Joe Perches63c3a662011-04-26 08:12:10 +00001197 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001198 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001199
Matt Carlson69f11c92011-07-13 09:27:30 +00001200 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001201
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001202 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1203 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1204 else
1205 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1206 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001207 if (is_serdes)
1208 tp->phy_addr += 7;
1209 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001210 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001211
Matt Carlson158d7ab2008-05-29 01:37:54 -07001212 tg3_mdio_start(tp);
1213
Joe Perches63c3a662011-04-26 08:12:10 +00001214 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001215 return 0;
1216
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001217 tp->mdio_bus = mdiobus_alloc();
1218 if (tp->mdio_bus == NULL)
1219 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001220
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001221 tp->mdio_bus->name = "tg3 mdio bus";
1222 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001223 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001224 tp->mdio_bus->priv = tp;
1225 tp->mdio_bus->parent = &tp->pdev->dev;
1226 tp->mdio_bus->read = &tg3_mdio_read;
1227 tp->mdio_bus->write = &tg3_mdio_write;
1228 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001229 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001230 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001231
1232 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001233 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001234
1235 /* The bus registration will look for all the PHYs on the mdio bus.
1236 * Unfortunately, it does not ensure the PHY is powered up before
1237 * accessing the PHY ID registers. A chip reset is the
1238 * quickest way to bring the device back to an operational state..
1239 */
1240 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1241 tg3_bmcr_reset(tp);
1242
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001243 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001244 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001245 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001246 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001247 return i;
1248 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001249
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001250 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001251
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001252 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001253 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001254 mdiobus_unregister(tp->mdio_bus);
1255 mdiobus_free(tp->mdio_bus);
1256 return -ENODEV;
1257 }
1258
1259 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001260 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001261 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001262 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001263 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001264 case PHY_ID_BCM50610:
1265 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001266 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001267 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001268 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001269 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001270 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001271 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001272 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001273 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001274 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001275 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001276 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001277 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001278 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001279 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001280 case PHY_ID_RTL8201E:
1281 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001282 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e092009-11-02 14:31:11 +00001283 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001284 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001285 break;
1286 }
1287
Joe Perches63c3a662011-04-26 08:12:10 +00001288 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001289
1290 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1291 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001292
1293 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001294}
1295
1296static void tg3_mdio_fini(struct tg3 *tp)
1297{
Joe Perches63c3a662011-04-26 08:12:10 +00001298 if (tg3_flag(tp, MDIOBUS_INITED)) {
1299 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001300 mdiobus_unregister(tp->mdio_bus);
1301 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001302 }
1303}
1304
Matt Carlson95e28692008-05-25 23:44:14 -07001305/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001306static inline void tg3_generate_fw_event(struct tg3 *tp)
1307{
1308 u32 val;
1309
1310 val = tr32(GRC_RX_CPU_EVENT);
1311 val |= GRC_RX_CPU_DRIVER_EVENT;
1312 tw32_f(GRC_RX_CPU_EVENT, val);
1313
1314 tp->last_event_jiffies = jiffies;
1315}
1316
1317#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1318
1319/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001320static void tg3_wait_for_event_ack(struct tg3 *tp)
1321{
1322 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001323 unsigned int delay_cnt;
1324 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001325
Matt Carlson4ba526c2008-08-15 14:10:04 -07001326 /* If enough time has passed, no wait is necessary. */
1327 time_remain = (long)(tp->last_event_jiffies + 1 +
1328 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1329 (long)jiffies;
1330 if (time_remain < 0)
1331 return;
1332
1333 /* Check if we can shorten the wait time. */
1334 delay_cnt = jiffies_to_usecs(time_remain);
1335 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1336 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1337 delay_cnt = (delay_cnt >> 3) + 1;
1338
1339 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001340 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1341 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001342 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001343 }
1344}
1345
1346/* tp->lock is held. */
1347static void tg3_ump_link_report(struct tg3 *tp)
1348{
1349 u32 reg;
1350 u32 val;
1351
Joe Perches63c3a662011-04-26 08:12:10 +00001352 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001353 return;
1354
1355 tg3_wait_for_event_ack(tp);
1356
1357 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1358
1359 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1360
1361 val = 0;
1362 if (!tg3_readphy(tp, MII_BMCR, &reg))
1363 val = reg << 16;
1364 if (!tg3_readphy(tp, MII_BMSR, &reg))
1365 val |= (reg & 0xffff);
1366 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1367
1368 val = 0;
1369 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1370 val = reg << 16;
1371 if (!tg3_readphy(tp, MII_LPA, &reg))
1372 val |= (reg & 0xffff);
1373 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1374
1375 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001376 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001377 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1378 val = reg << 16;
1379 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1380 val |= (reg & 0xffff);
1381 }
1382 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1383
1384 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1385 val = reg << 16;
1386 else
1387 val = 0;
1388 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1389
Matt Carlson4ba526c2008-08-15 14:10:04 -07001390 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001391}
1392
1393static void tg3_link_report(struct tg3 *tp)
1394{
1395 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001396 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001397 tg3_ump_link_report(tp);
1398 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001399 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1400 (tp->link_config.active_speed == SPEED_1000 ?
1401 1000 :
1402 (tp->link_config.active_speed == SPEED_100 ?
1403 100 : 10)),
1404 (tp->link_config.active_duplex == DUPLEX_FULL ?
1405 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001406
Joe Perches05dbe002010-02-17 19:44:19 +00001407 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1408 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1409 "on" : "off",
1410 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1411 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001412
1413 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1414 netdev_info(tp->dev, "EEE is %s\n",
1415 tp->setlpicnt ? "enabled" : "disabled");
1416
Matt Carlson95e28692008-05-25 23:44:14 -07001417 tg3_ump_link_report(tp);
1418 }
1419}
1420
1421static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1422{
1423 u16 miireg;
1424
Steve Glendinninge18ce342008-12-16 02:00:00 -08001425 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001426 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001427 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001428 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001429 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001430 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1431 else
1432 miireg = 0;
1433
1434 return miireg;
1435}
1436
1437static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1438{
1439 u16 miireg;
1440
Steve Glendinninge18ce342008-12-16 02:00:00 -08001441 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001442 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001443 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001444 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001445 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001446 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1447 else
1448 miireg = 0;
1449
1450 return miireg;
1451}
1452
Matt Carlson95e28692008-05-25 23:44:14 -07001453static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1454{
1455 u8 cap = 0;
1456
1457 if (lcladv & ADVERTISE_1000XPAUSE) {
1458 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1459 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001460 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001461 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001462 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001463 } else {
1464 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001465 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001466 }
1467 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1468 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001469 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001470 }
1471
1472 return cap;
1473}
1474
Matt Carlsonf51f3562008-05-25 23:45:08 -07001475static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001476{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001477 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001478 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001479 u32 old_rx_mode = tp->rx_mode;
1480 u32 old_tx_mode = tp->tx_mode;
1481
Joe Perches63c3a662011-04-26 08:12:10 +00001482 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001483 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001484 else
1485 autoneg = tp->link_config.autoneg;
1486
Joe Perches63c3a662011-04-26 08:12:10 +00001487 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001488 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001489 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001490 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001491 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001492 } else
1493 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001494
Matt Carlsonf51f3562008-05-25 23:45:08 -07001495 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001496
Steve Glendinninge18ce342008-12-16 02:00:00 -08001497 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001498 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1499 else
1500 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1501
Matt Carlsonf51f3562008-05-25 23:45:08 -07001502 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001503 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001504
Steve Glendinninge18ce342008-12-16 02:00:00 -08001505 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001506 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1507 else
1508 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1509
Matt Carlsonf51f3562008-05-25 23:45:08 -07001510 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001511 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001512}
1513
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001514static void tg3_adjust_link(struct net_device *dev)
1515{
1516 u8 oldflowctrl, linkmesg = 0;
1517 u32 mac_mode, lcl_adv, rmt_adv;
1518 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001519 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001520
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001521 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001522
1523 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1524 MAC_MODE_HALF_DUPLEX);
1525
1526 oldflowctrl = tp->link_config.active_flowctrl;
1527
1528 if (phydev->link) {
1529 lcl_adv = 0;
1530 rmt_adv = 0;
1531
1532 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1533 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001534 else if (phydev->speed == SPEED_1000 ||
1535 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001536 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001537 else
1538 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001539
1540 if (phydev->duplex == DUPLEX_HALF)
1541 mac_mode |= MAC_MODE_HALF_DUPLEX;
1542 else {
1543 lcl_adv = tg3_advert_flowctrl_1000T(
1544 tp->link_config.flowctrl);
1545
1546 if (phydev->pause)
1547 rmt_adv = LPA_PAUSE_CAP;
1548 if (phydev->asym_pause)
1549 rmt_adv |= LPA_PAUSE_ASYM;
1550 }
1551
1552 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1553 } else
1554 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1555
1556 if (mac_mode != tp->mac_mode) {
1557 tp->mac_mode = mac_mode;
1558 tw32_f(MAC_MODE, tp->mac_mode);
1559 udelay(40);
1560 }
1561
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001562 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1563 if (phydev->speed == SPEED_10)
1564 tw32(MAC_MI_STAT,
1565 MAC_MI_STAT_10MBPS_MODE |
1566 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1567 else
1568 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1569 }
1570
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001571 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1572 tw32(MAC_TX_LENGTHS,
1573 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1574 (6 << TX_LENGTHS_IPG_SHIFT) |
1575 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1576 else
1577 tw32(MAC_TX_LENGTHS,
1578 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1579 (6 << TX_LENGTHS_IPG_SHIFT) |
1580 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1581
1582 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1583 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1584 phydev->speed != tp->link_config.active_speed ||
1585 phydev->duplex != tp->link_config.active_duplex ||
1586 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001587 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001588
1589 tp->link_config.active_speed = phydev->speed;
1590 tp->link_config.active_duplex = phydev->duplex;
1591
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001592 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001593
1594 if (linkmesg)
1595 tg3_link_report(tp);
1596}
1597
1598static int tg3_phy_init(struct tg3 *tp)
1599{
1600 struct phy_device *phydev;
1601
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001602 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001603 return 0;
1604
1605 /* Bring the PHY back to a known state. */
1606 tg3_bmcr_reset(tp);
1607
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001608 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001609
1610 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001611 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001612 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001613 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001614 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001615 return PTR_ERR(phydev);
1616 }
1617
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001618 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001619 switch (phydev->interface) {
1620 case PHY_INTERFACE_MODE_GMII:
1621 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001622 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001623 phydev->supported &= (PHY_GBIT_FEATURES |
1624 SUPPORTED_Pause |
1625 SUPPORTED_Asym_Pause);
1626 break;
1627 }
1628 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001629 case PHY_INTERFACE_MODE_MII:
1630 phydev->supported &= (PHY_BASIC_FEATURES |
1631 SUPPORTED_Pause |
1632 SUPPORTED_Asym_Pause);
1633 break;
1634 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001635 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001636 return -EINVAL;
1637 }
1638
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001639 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001640
1641 phydev->advertising = phydev->supported;
1642
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001643 return 0;
1644}
1645
1646static void tg3_phy_start(struct tg3 *tp)
1647{
1648 struct phy_device *phydev;
1649
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001650 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001651 return;
1652
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001653 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001654
Matt Carlson80096062010-08-02 11:26:06 +00001655 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1656 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001657 phydev->speed = tp->link_config.orig_speed;
1658 phydev->duplex = tp->link_config.orig_duplex;
1659 phydev->autoneg = tp->link_config.orig_autoneg;
1660 phydev->advertising = tp->link_config.orig_advertising;
1661 }
1662
1663 phy_start(phydev);
1664
1665 phy_start_aneg(phydev);
1666}
1667
1668static void tg3_phy_stop(struct tg3 *tp)
1669{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001670 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001671 return;
1672
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001673 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001674}
1675
1676static void tg3_phy_fini(struct tg3 *tp)
1677{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001678 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001679 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001680 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001681 }
1682}
1683
Matt Carlson941ec902011-08-19 13:58:23 +00001684static int tg3_phy_set_extloopbk(struct tg3 *tp)
1685{
1686 int err;
1687 u32 val;
1688
1689 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
1690 return 0;
1691
1692 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
1693 /* Cannot do read-modify-write on 5401 */
1694 err = tg3_phy_auxctl_write(tp,
1695 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1696 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
1697 0x4c20);
1698 goto done;
1699 }
1700
1701 err = tg3_phy_auxctl_read(tp,
1702 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1703 if (err)
1704 return err;
1705
1706 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
1707 err = tg3_phy_auxctl_write(tp,
1708 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
1709
1710done:
1711 return err;
1712}
1713
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001714static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1715{
1716 u32 phytest;
1717
1718 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1719 u32 phy;
1720
1721 tg3_writephy(tp, MII_TG3_FET_TEST,
1722 phytest | MII_TG3_FET_SHADOW_EN);
1723 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1724 if (enable)
1725 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1726 else
1727 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1728 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1729 }
1730 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1731 }
1732}
1733
Matt Carlson6833c042008-11-21 17:18:59 -08001734static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1735{
1736 u32 reg;
1737
Joe Perches63c3a662011-04-26 08:12:10 +00001738 if (!tg3_flag(tp, 5705_PLUS) ||
1739 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001740 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001741 return;
1742
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001743 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001744 tg3_phy_fet_toggle_apd(tp, enable);
1745 return;
1746 }
1747
Matt Carlson6833c042008-11-21 17:18:59 -08001748 reg = MII_TG3_MISC_SHDW_WREN |
1749 MII_TG3_MISC_SHDW_SCR5_SEL |
1750 MII_TG3_MISC_SHDW_SCR5_LPED |
1751 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1752 MII_TG3_MISC_SHDW_SCR5_SDTL |
1753 MII_TG3_MISC_SHDW_SCR5_C125OE;
1754 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1755 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1756
1757 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1758
1759
1760 reg = MII_TG3_MISC_SHDW_WREN |
1761 MII_TG3_MISC_SHDW_APD_SEL |
1762 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1763 if (enable)
1764 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1765
1766 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1767}
1768
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001769static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1770{
1771 u32 phy;
1772
Joe Perches63c3a662011-04-26 08:12:10 +00001773 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001774 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001775 return;
1776
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001777 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001778 u32 ephy;
1779
Matt Carlson535ef6e2009-08-25 10:09:36 +00001780 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1781 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1782
1783 tg3_writephy(tp, MII_TG3_FET_TEST,
1784 ephy | MII_TG3_FET_SHADOW_EN);
1785 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001786 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001787 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001788 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001789 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1790 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001791 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001792 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001793 }
1794 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00001795 int ret;
1796
1797 ret = tg3_phy_auxctl_read(tp,
1798 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
1799 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001800 if (enable)
1801 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1802 else
1803 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001804 tg3_phy_auxctl_write(tp,
1805 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001806 }
1807 }
1808}
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810static void tg3_phy_set_wirespeed(struct tg3 *tp)
1811{
Matt Carlson15ee95c2011-04-20 07:57:40 +00001812 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 u32 val;
1814
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001815 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return;
1817
Matt Carlson15ee95c2011-04-20 07:57:40 +00001818 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
1819 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001820 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
1821 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001824static void tg3_phy_apply_otp(struct tg3 *tp)
1825{
1826 u32 otp, phy;
1827
1828 if (!tp->phy_otp)
1829 return;
1830
1831 otp = tp->phy_otp;
1832
Matt Carlson1d36ba42011-04-20 07:57:42 +00001833 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
1834 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001835
1836 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1837 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1838 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1839
1840 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1841 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1842 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1843
1844 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1845 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1846 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1847
1848 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1849 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1850
1851 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1852 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1853
1854 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1855 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1856 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1857
Matt Carlson1d36ba42011-04-20 07:57:42 +00001858 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001859}
1860
Matt Carlson52b02d02010-10-14 10:37:41 +00001861static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1862{
1863 u32 val;
1864
1865 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1866 return;
1867
1868 tp->setlpicnt = 0;
1869
1870 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1871 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001872 tp->link_config.active_duplex == DUPLEX_FULL &&
1873 (tp->link_config.active_speed == SPEED_100 ||
1874 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001875 u32 eeectl;
1876
1877 if (tp->link_config.active_speed == SPEED_1000)
1878 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1879 else
1880 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1881
1882 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1883
Matt Carlson3110f5f52010-12-06 08:28:50 +00001884 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1885 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001886
Matt Carlsonb0c59432011-05-19 12:12:48 +00001887 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
1888 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00001889 tp->setlpicnt = 2;
1890 }
1891
1892 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00001893 if (current_link_up == 1 &&
1894 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
1895 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
1896 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1897 }
1898
Matt Carlson52b02d02010-10-14 10:37:41 +00001899 val = tr32(TG3_CPMU_EEE_MODE);
1900 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1901 }
1902}
1903
Matt Carlsonb0c59432011-05-19 12:12:48 +00001904static void tg3_phy_eee_enable(struct tg3 *tp)
1905{
1906 u32 val;
1907
1908 if (tp->link_config.active_speed == SPEED_1000 &&
1909 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1910 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
1911 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
1912 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00001913 val = MII_TG3_DSP_TAP26_ALNOKO |
1914 MII_TG3_DSP_TAP26_RMRXSTO;
1915 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00001916 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1917 }
1918
1919 val = tr32(TG3_CPMU_EEE_MODE);
1920 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
1921}
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923static int tg3_wait_macro_done(struct tg3 *tp)
1924{
1925 int limit = 100;
1926
1927 while (limit--) {
1928 u32 tmp32;
1929
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001930 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if ((tmp32 & 0x1000) == 0)
1932 break;
1933 }
1934 }
Roel Kluind4675b52009-02-12 16:33:27 -08001935 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return -EBUSY;
1937
1938 return 0;
1939}
1940
1941static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1942{
1943 static const u32 test_pat[4][6] = {
1944 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1945 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1946 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1947 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1948 };
1949 int chan;
1950
1951 for (chan = 0; chan < 4; chan++) {
1952 int i;
1953
1954 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1955 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001956 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 for (i = 0; i < 6; i++)
1959 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1960 test_pat[chan][i]);
1961
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001962 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (tg3_wait_macro_done(tp)) {
1964 *resetp = 1;
1965 return -EBUSY;
1966 }
1967
1968 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1969 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001970 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (tg3_wait_macro_done(tp)) {
1972 *resetp = 1;
1973 return -EBUSY;
1974 }
1975
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001976 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (tg3_wait_macro_done(tp)) {
1978 *resetp = 1;
1979 return -EBUSY;
1980 }
1981
1982 for (i = 0; i < 6; i += 2) {
1983 u32 low, high;
1984
1985 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1986 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1987 tg3_wait_macro_done(tp)) {
1988 *resetp = 1;
1989 return -EBUSY;
1990 }
1991 low &= 0x7fff;
1992 high &= 0x000f;
1993 if (low != test_pat[chan][i] ||
1994 high != test_pat[chan][i+1]) {
1995 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1996 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1997 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1998
1999 return -EBUSY;
2000 }
2001 }
2002 }
2003
2004 return 0;
2005}
2006
2007static int tg3_phy_reset_chanpat(struct tg3 *tp)
2008{
2009 int chan;
2010
2011 for (chan = 0; chan < 4; chan++) {
2012 int i;
2013
2014 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2015 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002016 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 for (i = 0; i < 6; i++)
2018 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002019 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (tg3_wait_macro_done(tp))
2021 return -EBUSY;
2022 }
2023
2024 return 0;
2025}
2026
2027static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2028{
2029 u32 reg32, phy9_orig;
2030 int retries, do_phy_reset, err;
2031
2032 retries = 10;
2033 do_phy_reset = 1;
2034 do {
2035 if (do_phy_reset) {
2036 err = tg3_bmcr_reset(tp);
2037 if (err)
2038 return err;
2039 do_phy_reset = 0;
2040 }
2041
2042 /* Disable transmitter and interrupt. */
2043 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2044 continue;
2045
2046 reg32 |= 0x3000;
2047 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2048
2049 /* Set full-duplex, 1000 mbps. */
2050 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002051 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002054 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 continue;
2056
Matt Carlson221c5632011-06-13 13:39:01 +00002057 tg3_writephy(tp, MII_CTRL1000,
2058 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Matt Carlson1d36ba42011-04-20 07:57:42 +00002060 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2061 if (err)
2062 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002065 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2068 if (!err)
2069 break;
2070 } while (--retries);
2071
2072 err = tg3_phy_reset_chanpat(tp);
2073 if (err)
2074 return err;
2075
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002076 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002079 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Matt Carlson1d36ba42011-04-20 07:57:42 +00002081 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Matt Carlson221c5632011-06-13 13:39:01 +00002083 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2086 reg32 &= ~0x3000;
2087 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2088 } else if (!err)
2089 err = -EBUSY;
2090
2091 return err;
2092}
2093
2094/* This will reset the tigon3 PHY if there is no valid
2095 * link unless the FORCE argument is non-zero.
2096 */
2097static int tg3_phy_reset(struct tg3 *tp)
2098{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002099 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 int err;
2101
Michael Chan60189dd2006-12-17 17:08:07 -08002102 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002103 val = tr32(GRC_MISC_CFG);
2104 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2105 udelay(40);
2106 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002107 err = tg3_readphy(tp, MII_BMSR, &val);
2108 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (err != 0)
2110 return -EBUSY;
2111
Michael Chanc8e1e822006-04-29 18:55:17 -07002112 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2113 netif_carrier_off(tp->dev);
2114 tg3_link_report(tp);
2115 }
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2118 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2119 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2120 err = tg3_phy_reset_5703_4_5(tp);
2121 if (err)
2122 return err;
2123 goto out;
2124 }
2125
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002126 cpmuctrl = 0;
2127 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2128 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2129 cpmuctrl = tr32(TG3_CPMU_CTRL);
2130 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2131 tw32(TG3_CPMU_CTRL,
2132 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2133 }
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 err = tg3_bmcr_reset(tp);
2136 if (err)
2137 return err;
2138
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002139 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002140 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2141 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002142
2143 tw32(TG3_CPMU_CTRL, cpmuctrl);
2144 }
2145
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002146 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2147 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002148 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2149 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2150 CPMU_LSPD_1000MB_MACCLK_12_5) {
2151 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2152 udelay(40);
2153 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2154 }
2155 }
2156
Joe Perches63c3a662011-04-26 08:12:10 +00002157 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002158 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002159 return 0;
2160
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002161 tg3_phy_apply_otp(tp);
2162
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002163 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002164 tg3_phy_toggle_apd(tp, true);
2165 else
2166 tg3_phy_toggle_apd(tp, false);
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002169 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2170 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002171 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2172 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002173 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002175
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002176 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002177 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2178 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002180
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002181 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002182 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2183 tg3_phydsp_write(tp, 0x000a, 0x310b);
2184 tg3_phydsp_write(tp, 0x201f, 0x9506);
2185 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2186 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2187 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002188 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002189 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2190 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2191 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2192 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2193 tg3_writephy(tp, MII_TG3_TEST1,
2194 MII_TG3_TEST1_TRIM_EN | 0x4);
2195 } else
2196 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2197
2198 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2199 }
Michael Chanc424cb22006-04-29 18:56:34 -07002200 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 /* Set Extended packet length bit (bit 14) on all chips that */
2203 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002204 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002206 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002207 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002209 err = tg3_phy_auxctl_read(tp,
2210 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2211 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002212 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2213 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215
2216 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2217 * jumbo frames transmission.
2218 */
Joe Perches63c3a662011-04-26 08:12:10 +00002219 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002220 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002221 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002222 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
2224
Michael Chan715116a2006-09-27 16:09:25 -07002225 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002226 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002227 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002228 }
2229
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002230 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 tg3_phy_set_wirespeed(tp);
2232 return 0;
2233}
2234
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002235#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2236#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2237#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2238 TG3_GPIO_MSG_NEED_VAUX)
2239#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2240 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2241 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2242 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2243 (TG3_GPIO_MSG_DRVR_PRES << 12))
2244
2245#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2246 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2247 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2248 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2249 (TG3_GPIO_MSG_NEED_VAUX << 12))
2250
2251static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2252{
2253 u32 status, shift;
2254
2255 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2256 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2257 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2258 else
2259 status = tr32(TG3_CPMU_DRV_STATUS);
2260
2261 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2262 status &= ~(TG3_GPIO_MSG_MASK << shift);
2263 status |= (newstat << shift);
2264
2265 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2266 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2267 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2268 else
2269 tw32(TG3_CPMU_DRV_STATUS, status);
2270
2271 return status >> TG3_APE_GPIO_MSG_SHIFT;
2272}
2273
Matt Carlson520b2752011-06-13 13:39:02 +00002274static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2275{
2276 if (!tg3_flag(tp, IS_NIC))
2277 return 0;
2278
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002279 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2280 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2281 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2282 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2283 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002284
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002285 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2286
2287 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2288 TG3_GRC_LCLCTL_PWRSW_DELAY);
2289
2290 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2291 } else {
2292 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2293 TG3_GRC_LCLCTL_PWRSW_DELAY);
2294 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002295
Matt Carlson520b2752011-06-13 13:39:02 +00002296 return 0;
2297}
2298
2299static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2300{
2301 u32 grc_local_ctrl;
2302
2303 if (!tg3_flag(tp, IS_NIC) ||
2304 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2305 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2306 return;
2307
2308 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2309
2310 tw32_wait_f(GRC_LOCAL_CTRL,
2311 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2312 TG3_GRC_LCLCTL_PWRSW_DELAY);
2313
2314 tw32_wait_f(GRC_LOCAL_CTRL,
2315 grc_local_ctrl,
2316 TG3_GRC_LCLCTL_PWRSW_DELAY);
2317
2318 tw32_wait_f(GRC_LOCAL_CTRL,
2319 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2320 TG3_GRC_LCLCTL_PWRSW_DELAY);
2321}
2322
2323static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2324{
2325 if (!tg3_flag(tp, IS_NIC))
2326 return;
2327
2328 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2329 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2330 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2331 (GRC_LCLCTRL_GPIO_OE0 |
2332 GRC_LCLCTRL_GPIO_OE1 |
2333 GRC_LCLCTRL_GPIO_OE2 |
2334 GRC_LCLCTRL_GPIO_OUTPUT0 |
2335 GRC_LCLCTRL_GPIO_OUTPUT1),
2336 TG3_GRC_LCLCTL_PWRSW_DELAY);
2337 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2338 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2339 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2340 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2341 GRC_LCLCTRL_GPIO_OE1 |
2342 GRC_LCLCTRL_GPIO_OE2 |
2343 GRC_LCLCTRL_GPIO_OUTPUT0 |
2344 GRC_LCLCTRL_GPIO_OUTPUT1 |
2345 tp->grc_local_ctrl;
2346 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2347 TG3_GRC_LCLCTL_PWRSW_DELAY);
2348
2349 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2350 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2351 TG3_GRC_LCLCTL_PWRSW_DELAY);
2352
2353 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2354 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2355 TG3_GRC_LCLCTL_PWRSW_DELAY);
2356 } else {
2357 u32 no_gpio2;
2358 u32 grc_local_ctrl = 0;
2359
2360 /* Workaround to prevent overdrawing Amps. */
2361 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2362 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2363 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2364 grc_local_ctrl,
2365 TG3_GRC_LCLCTL_PWRSW_DELAY);
2366 }
2367
2368 /* On 5753 and variants, GPIO2 cannot be used. */
2369 no_gpio2 = tp->nic_sram_data_cfg &
2370 NIC_SRAM_DATA_CFG_NO_GPIO2;
2371
2372 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2373 GRC_LCLCTRL_GPIO_OE1 |
2374 GRC_LCLCTRL_GPIO_OE2 |
2375 GRC_LCLCTRL_GPIO_OUTPUT1 |
2376 GRC_LCLCTRL_GPIO_OUTPUT2;
2377 if (no_gpio2) {
2378 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2379 GRC_LCLCTRL_GPIO_OUTPUT2);
2380 }
2381 tw32_wait_f(GRC_LOCAL_CTRL,
2382 tp->grc_local_ctrl | grc_local_ctrl,
2383 TG3_GRC_LCLCTL_PWRSW_DELAY);
2384
2385 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2386
2387 tw32_wait_f(GRC_LOCAL_CTRL,
2388 tp->grc_local_ctrl | grc_local_ctrl,
2389 TG3_GRC_LCLCTL_PWRSW_DELAY);
2390
2391 if (!no_gpio2) {
2392 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2393 tw32_wait_f(GRC_LOCAL_CTRL,
2394 tp->grc_local_ctrl | grc_local_ctrl,
2395 TG3_GRC_LCLCTL_PWRSW_DELAY);
2396 }
2397 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002398}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002399
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002400static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002401{
2402 u32 msg = 0;
2403
2404 /* Serialize power state transitions */
2405 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2406 return;
2407
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002408 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002409 msg = TG3_GPIO_MSG_NEED_VAUX;
2410
2411 msg = tg3_set_function_status(tp, msg);
2412
2413 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2414 goto done;
2415
2416 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2417 tg3_pwrsrc_switch_to_vaux(tp);
2418 else
2419 tg3_pwrsrc_die_with_vmain(tp);
2420
2421done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002422 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002423}
2424
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002425static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Matt Carlson683644b2011-03-09 16:58:23 +00002427 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Matt Carlson334355a2010-01-20 16:58:10 +00002429 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002430 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlson334355a2010-01-20 16:58:10 +00002431 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return;
2433
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002434 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2435 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2436 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002437 tg3_frob_aux_power_5717(tp, include_wol ?
2438 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002439 return;
2440 }
2441
2442 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002443 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002445 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002446
Michael Chanbc1c7562006-03-20 17:48:03 -08002447 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002448 if (dev_peer) {
2449 struct tg3 *tp_peer = netdev_priv(dev_peer);
2450
Joe Perches63c3a662011-04-26 08:12:10 +00002451 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002452 return;
2453
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002454 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002455 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002456 need_vaux = true;
2457 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002460 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2461 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002462 need_vaux = true;
2463
Matt Carlson520b2752011-06-13 13:39:02 +00002464 if (need_vaux)
2465 tg3_pwrsrc_switch_to_vaux(tp);
2466 else
2467 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002470static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2471{
2472 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2473 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002474 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002475 if (speed != SPEED_10)
2476 return 1;
2477 } else if (speed == SPEED_10)
2478 return 1;
2479
2480 return 0;
2481}
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483static int tg3_setup_phy(struct tg3 *, int);
2484
2485#define RESET_KIND_SHUTDOWN 0
2486#define RESET_KIND_INIT 1
2487#define RESET_KIND_SUSPEND 2
2488
2489static void tg3_write_sig_post_reset(struct tg3 *, int);
2490static int tg3_halt_cpu(struct tg3 *, u32);
2491
Matt Carlson0a459aa2008-11-03 16:54:15 -08002492static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002493{
Matt Carlsonce057f02007-11-12 21:08:03 -08002494 u32 val;
2495
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002496 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002497 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2498 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2499 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2500
2501 sg_dig_ctrl |=
2502 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2503 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2504 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2505 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002506 return;
Michael Chan51297242007-02-13 12:17:57 -08002507 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002508
Michael Chan60189dd2006-12-17 17:08:07 -08002509 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002510 tg3_bmcr_reset(tp);
2511 val = tr32(GRC_MISC_CFG);
2512 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2513 udelay(40);
2514 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002515 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002516 u32 phytest;
2517 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2518 u32 phy;
2519
2520 tg3_writephy(tp, MII_ADVERTISE, 0);
2521 tg3_writephy(tp, MII_BMCR,
2522 BMCR_ANENABLE | BMCR_ANRESTART);
2523
2524 tg3_writephy(tp, MII_TG3_FET_TEST,
2525 phytest | MII_TG3_FET_SHADOW_EN);
2526 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2527 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2528 tg3_writephy(tp,
2529 MII_TG3_FET_SHDW_AUXMODE4,
2530 phy);
2531 }
2532 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2533 }
2534 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002535 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002536 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2537 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002538
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002539 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2540 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2541 MII_TG3_AUXCTL_PCTL_VREG_11V;
2542 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002543 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002544
Michael Chan15c3b692006-03-22 01:06:52 -08002545 /* The PHY should not be powered down on some chips because
2546 * of bugs.
2547 */
2548 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2549 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2550 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002551 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002552 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002553
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002554 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2555 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002556 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2557 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2558 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2559 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2560 }
2561
Michael Chan15c3b692006-03-22 01:06:52 -08002562 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2563}
2564
Matt Carlson3f007892008-11-03 16:51:36 -08002565/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002566static int tg3_nvram_lock(struct tg3 *tp)
2567{
Joe Perches63c3a662011-04-26 08:12:10 +00002568 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002569 int i;
2570
2571 if (tp->nvram_lock_cnt == 0) {
2572 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2573 for (i = 0; i < 8000; i++) {
2574 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2575 break;
2576 udelay(20);
2577 }
2578 if (i == 8000) {
2579 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2580 return -ENODEV;
2581 }
2582 }
2583 tp->nvram_lock_cnt++;
2584 }
2585 return 0;
2586}
2587
2588/* tp->lock is held. */
2589static void tg3_nvram_unlock(struct tg3 *tp)
2590{
Joe Perches63c3a662011-04-26 08:12:10 +00002591 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002592 if (tp->nvram_lock_cnt > 0)
2593 tp->nvram_lock_cnt--;
2594 if (tp->nvram_lock_cnt == 0)
2595 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2596 }
2597}
2598
2599/* tp->lock is held. */
2600static void tg3_enable_nvram_access(struct tg3 *tp)
2601{
Joe Perches63c3a662011-04-26 08:12:10 +00002602 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002603 u32 nvaccess = tr32(NVRAM_ACCESS);
2604
2605 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2606 }
2607}
2608
2609/* tp->lock is held. */
2610static void tg3_disable_nvram_access(struct tg3 *tp)
2611{
Joe Perches63c3a662011-04-26 08:12:10 +00002612 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002613 u32 nvaccess = tr32(NVRAM_ACCESS);
2614
2615 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2616 }
2617}
2618
2619static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2620 u32 offset, u32 *val)
2621{
2622 u32 tmp;
2623 int i;
2624
2625 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2626 return -EINVAL;
2627
2628 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2629 EEPROM_ADDR_DEVID_MASK |
2630 EEPROM_ADDR_READ);
2631 tw32(GRC_EEPROM_ADDR,
2632 tmp |
2633 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2634 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2635 EEPROM_ADDR_ADDR_MASK) |
2636 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2637
2638 for (i = 0; i < 1000; i++) {
2639 tmp = tr32(GRC_EEPROM_ADDR);
2640
2641 if (tmp & EEPROM_ADDR_COMPLETE)
2642 break;
2643 msleep(1);
2644 }
2645 if (!(tmp & EEPROM_ADDR_COMPLETE))
2646 return -EBUSY;
2647
Matt Carlson62cedd12009-04-20 14:52:29 -07002648 tmp = tr32(GRC_EEPROM_DATA);
2649
2650 /*
2651 * The data will always be opposite the native endian
2652 * format. Perform a blind byteswap to compensate.
2653 */
2654 *val = swab32(tmp);
2655
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002656 return 0;
2657}
2658
2659#define NVRAM_CMD_TIMEOUT 10000
2660
2661static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2662{
2663 int i;
2664
2665 tw32(NVRAM_CMD, nvram_cmd);
2666 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2667 udelay(10);
2668 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2669 udelay(10);
2670 break;
2671 }
2672 }
2673
2674 if (i == NVRAM_CMD_TIMEOUT)
2675 return -EBUSY;
2676
2677 return 0;
2678}
2679
2680static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2681{
Joe Perches63c3a662011-04-26 08:12:10 +00002682 if (tg3_flag(tp, NVRAM) &&
2683 tg3_flag(tp, NVRAM_BUFFERED) &&
2684 tg3_flag(tp, FLASH) &&
2685 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002686 (tp->nvram_jedecnum == JEDEC_ATMEL))
2687
2688 addr = ((addr / tp->nvram_pagesize) <<
2689 ATMEL_AT45DB0X1B_PAGE_POS) +
2690 (addr % tp->nvram_pagesize);
2691
2692 return addr;
2693}
2694
2695static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2696{
Joe Perches63c3a662011-04-26 08:12:10 +00002697 if (tg3_flag(tp, NVRAM) &&
2698 tg3_flag(tp, NVRAM_BUFFERED) &&
2699 tg3_flag(tp, FLASH) &&
2700 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002701 (tp->nvram_jedecnum == JEDEC_ATMEL))
2702
2703 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2704 tp->nvram_pagesize) +
2705 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2706
2707 return addr;
2708}
2709
Matt Carlsone4f34112009-02-25 14:25:00 +00002710/* NOTE: Data read in from NVRAM is byteswapped according to
2711 * the byteswapping settings for all other register accesses.
2712 * tg3 devices are BE devices, so on a BE machine, the data
2713 * returned will be exactly as it is seen in NVRAM. On a LE
2714 * machine, the 32-bit value will be byteswapped.
2715 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002716static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2717{
2718 int ret;
2719
Joe Perches63c3a662011-04-26 08:12:10 +00002720 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002721 return tg3_nvram_read_using_eeprom(tp, offset, val);
2722
2723 offset = tg3_nvram_phys_addr(tp, offset);
2724
2725 if (offset > NVRAM_ADDR_MSK)
2726 return -EINVAL;
2727
2728 ret = tg3_nvram_lock(tp);
2729 if (ret)
2730 return ret;
2731
2732 tg3_enable_nvram_access(tp);
2733
2734 tw32(NVRAM_ADDR, offset);
2735 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2736 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2737
2738 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002739 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002740
2741 tg3_disable_nvram_access(tp);
2742
2743 tg3_nvram_unlock(tp);
2744
2745 return ret;
2746}
2747
Matt Carlsona9dc5292009-02-25 14:25:30 +00002748/* Ensures NVRAM data is in bytestream format. */
2749static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002750{
2751 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002752 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002753 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002754 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002755 return res;
2756}
2757
2758/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002759static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2760{
2761 u32 addr_high, addr_low;
2762 int i;
2763
2764 addr_high = ((tp->dev->dev_addr[0] << 8) |
2765 tp->dev->dev_addr[1]);
2766 addr_low = ((tp->dev->dev_addr[2] << 24) |
2767 (tp->dev->dev_addr[3] << 16) |
2768 (tp->dev->dev_addr[4] << 8) |
2769 (tp->dev->dev_addr[5] << 0));
2770 for (i = 0; i < 4; i++) {
2771 if (i == 1 && skip_mac_1)
2772 continue;
2773 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2774 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2775 }
2776
2777 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2778 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2779 for (i = 0; i < 12; i++) {
2780 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2781 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2782 }
2783 }
2784
2785 addr_high = (tp->dev->dev_addr[0] +
2786 tp->dev->dev_addr[1] +
2787 tp->dev->dev_addr[2] +
2788 tp->dev->dev_addr[3] +
2789 tp->dev->dev_addr[4] +
2790 tp->dev->dev_addr[5]) &
2791 TX_BACKOFF_SEED_MASK;
2792 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2793}
2794
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002795static void tg3_enable_register_access(struct tg3 *tp)
2796{
2797 /*
2798 * Make sure register accesses (indirect or otherwise) will function
2799 * correctly.
2800 */
2801 pci_write_config_dword(tp->pdev,
2802 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2803}
2804
2805static int tg3_power_up(struct tg3 *tp)
2806{
Matt Carlsonbed98292011-07-13 09:27:29 +00002807 int err;
2808
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002809 tg3_enable_register_access(tp);
2810
Matt Carlsonbed98292011-07-13 09:27:29 +00002811 err = pci_set_power_state(tp->pdev, PCI_D0);
2812 if (!err) {
2813 /* Switch out of Vaux if it is a NIC */
2814 tg3_pwrsrc_switch_to_vmain(tp);
2815 } else {
2816 netdev_err(tp->dev, "Transition to D0 failed\n");
2817 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002818
Matt Carlsonbed98292011-07-13 09:27:29 +00002819 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002820}
2821
2822static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
2824 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002825 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002827 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002828
2829 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00002830 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002831 u16 lnkctl;
2832
2833 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00002834 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002835 &lnkctl);
2836 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2837 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00002838 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002839 lnkctl);
2840 }
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2843 tw32(TG3PCI_MISC_HOST_CTRL,
2844 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2845
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002846 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00002847 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002848
Joe Perches63c3a662011-04-26 08:12:10 +00002849 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002850 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002851 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002852 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002853 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002854 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002855
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002856 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002857
Matt Carlson80096062010-08-02 11:26:06 +00002858 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002859
2860 tp->link_config.orig_speed = phydev->speed;
2861 tp->link_config.orig_duplex = phydev->duplex;
2862 tp->link_config.orig_autoneg = phydev->autoneg;
2863 tp->link_config.orig_advertising = phydev->advertising;
2864
2865 advertising = ADVERTISED_TP |
2866 ADVERTISED_Pause |
2867 ADVERTISED_Autoneg |
2868 ADVERTISED_10baseT_Half;
2869
Joe Perches63c3a662011-04-26 08:12:10 +00002870 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
2871 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002872 advertising |=
2873 ADVERTISED_100baseT_Half |
2874 ADVERTISED_100baseT_Full |
2875 ADVERTISED_10baseT_Full;
2876 else
2877 advertising |= ADVERTISED_10baseT_Full;
2878 }
2879
2880 phydev->advertising = advertising;
2881
2882 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002883
2884 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002885 if (phyid != PHY_ID_BCMAC131) {
2886 phyid &= PHY_BCM_OUI_MASK;
2887 if (phyid == PHY_BCM_OUI_1 ||
2888 phyid == PHY_BCM_OUI_2 ||
2889 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002890 do_low_power = true;
2891 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002892 }
Matt Carlsondd477002008-05-25 23:45:58 -07002893 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002894 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002895
Matt Carlson80096062010-08-02 11:26:06 +00002896 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2897 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002898 tp->link_config.orig_speed = tp->link_config.speed;
2899 tp->link_config.orig_duplex = tp->link_config.duplex;
2900 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002903 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002904 tp->link_config.speed = SPEED_10;
2905 tp->link_config.duplex = DUPLEX_HALF;
2906 tp->link_config.autoneg = AUTONEG_ENABLE;
2907 tg3_setup_phy(tp, 0);
2908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 }
2910
Michael Chanb5d37722006-09-27 16:06:21 -07002911 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2912 u32 val;
2913
2914 val = tr32(GRC_VCPU_EXT_CTRL);
2915 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00002916 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002917 int i;
2918 u32 val;
2919
2920 for (i = 0; i < 200; i++) {
2921 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2922 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2923 break;
2924 msleep(1);
2925 }
2926 }
Joe Perches63c3a662011-04-26 08:12:10 +00002927 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07002928 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2929 WOL_DRV_STATE_SHUTDOWN |
2930 WOL_DRV_WOL |
2931 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002932
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002933 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 u32 mac_mode;
2935
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002936 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002937 if (do_low_power &&
2938 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
2939 tg3_phy_auxctl_write(tp,
2940 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
2941 MII_TG3_AUXCTL_PCTL_WOL_EN |
2942 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2943 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07002944 udelay(40);
2945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002947 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002948 mac_mode = MAC_MODE_PORT_MODE_GMII;
2949 else
2950 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002952 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2953 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2954 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00002955 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002956 SPEED_100 : SPEED_10;
2957 if (tg3_5700_link_polarity(tp, speed))
2958 mac_mode |= MAC_MODE_LINK_POLARITY;
2959 else
2960 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 } else {
2963 mac_mode = MAC_MODE_PORT_MODE_TBI;
2964 }
2965
Joe Perches63c3a662011-04-26 08:12:10 +00002966 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 tw32(MAC_LED_CTRL, tp->led_ctrl);
2968
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002969 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00002970 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
2971 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002972 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Joe Perches63c3a662011-04-26 08:12:10 +00002974 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002975 mac_mode |= MAC_MODE_APE_TX_EN |
2976 MAC_MODE_APE_RX_EN |
2977 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 tw32_f(MAC_MODE, mac_mode);
2980 udelay(100);
2981
2982 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2983 udelay(10);
2984 }
2985
Joe Perches63c3a662011-04-26 08:12:10 +00002986 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2988 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2989 u32 base_val;
2990
2991 base_val = tp->pci_clock_ctrl;
2992 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2993 CLOCK_CTRL_TXCLK_DISABLE);
2994
Michael Chanb401e9e2005-12-19 16:27:04 -08002995 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2996 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00002997 } else if (tg3_flag(tp, 5780_CLASS) ||
2998 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00002999 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003000 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003001 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 u32 newbits1, newbits2;
3003
3004 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3005 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3006 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3007 CLOCK_CTRL_TXCLK_DISABLE |
3008 CLOCK_CTRL_ALTCLK);
3009 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003010 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 newbits1 = CLOCK_CTRL_625_CORE;
3012 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3013 } else {
3014 newbits1 = CLOCK_CTRL_ALTCLK;
3015 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3016 }
3017
Michael Chanb401e9e2005-12-19 16:27:04 -08003018 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3019 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Michael Chanb401e9e2005-12-19 16:27:04 -08003021 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3022 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Joe Perches63c3a662011-04-26 08:12:10 +00003024 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 u32 newbits3;
3026
3027 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3028 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3029 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3030 CLOCK_CTRL_TXCLK_DISABLE |
3031 CLOCK_CTRL_44MHZ_CORE);
3032 } else {
3033 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3034 }
3035
Michael Chanb401e9e2005-12-19 16:27:04 -08003036 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3037 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
3039 }
3040
Joe Perches63c3a662011-04-26 08:12:10 +00003041 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003042 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003043
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003044 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 /* Workaround for unstable PLL clock */
3047 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3048 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3049 u32 val = tr32(0x7d00);
3050
3051 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3052 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003053 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003054 int err;
3055
3056 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003058 if (!err)
3059 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
3062
Michael Chanbbadf502006-04-06 21:46:34 -07003063 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3064
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return 0;
3066}
3067
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003068static void tg3_power_down(struct tg3 *tp)
3069{
3070 tg3_power_down_prepare(tp);
3071
Joe Perches63c3a662011-04-26 08:12:10 +00003072 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003073 pci_set_power_state(tp->pdev, PCI_D3hot);
3074}
3075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3077{
3078 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3079 case MII_TG3_AUX_STAT_10HALF:
3080 *speed = SPEED_10;
3081 *duplex = DUPLEX_HALF;
3082 break;
3083
3084 case MII_TG3_AUX_STAT_10FULL:
3085 *speed = SPEED_10;
3086 *duplex = DUPLEX_FULL;
3087 break;
3088
3089 case MII_TG3_AUX_STAT_100HALF:
3090 *speed = SPEED_100;
3091 *duplex = DUPLEX_HALF;
3092 break;
3093
3094 case MII_TG3_AUX_STAT_100FULL:
3095 *speed = SPEED_100;
3096 *duplex = DUPLEX_FULL;
3097 break;
3098
3099 case MII_TG3_AUX_STAT_1000HALF:
3100 *speed = SPEED_1000;
3101 *duplex = DUPLEX_HALF;
3102 break;
3103
3104 case MII_TG3_AUX_STAT_1000FULL:
3105 *speed = SPEED_1000;
3106 *duplex = DUPLEX_FULL;
3107 break;
3108
3109 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003110 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003111 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3112 SPEED_10;
3113 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3114 DUPLEX_HALF;
3115 break;
3116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 *speed = SPEED_INVALID;
3118 *duplex = DUPLEX_INVALID;
3119 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
Matt Carlson42b64a42011-05-19 12:12:49 +00003123static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
Matt Carlson42b64a42011-05-19 12:12:49 +00003125 int err = 0;
3126 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Matt Carlson42b64a42011-05-19 12:12:49 +00003128 new_adv = ADVERTISE_CSMA;
3129 if (advertise & ADVERTISED_10baseT_Half)
3130 new_adv |= ADVERTISE_10HALF;
3131 if (advertise & ADVERTISED_10baseT_Full)
3132 new_adv |= ADVERTISE_10FULL;
3133 if (advertise & ADVERTISED_100baseT_Half)
3134 new_adv |= ADVERTISE_100HALF;
3135 if (advertise & ADVERTISED_100baseT_Full)
3136 new_adv |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Matt Carlson42b64a42011-05-19 12:12:49 +00003138 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Matt Carlson42b64a42011-05-19 12:12:49 +00003140 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3141 if (err)
3142 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Matt Carlson42b64a42011-05-19 12:12:49 +00003144 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3145 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003146
Matt Carlson42b64a42011-05-19 12:12:49 +00003147 new_adv = 0;
3148 if (advertise & ADVERTISED_1000baseT_Half)
Matt Carlson221c5632011-06-13 13:39:01 +00003149 new_adv |= ADVERTISE_1000HALF;
Matt Carlson42b64a42011-05-19 12:12:49 +00003150 if (advertise & ADVERTISED_1000baseT_Full)
Matt Carlson221c5632011-06-13 13:39:01 +00003151 new_adv |= ADVERTISE_1000FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003152
Matt Carlson42b64a42011-05-19 12:12:49 +00003153 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3154 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
Matt Carlson221c5632011-06-13 13:39:01 +00003155 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Matt Carlson221c5632011-06-13 13:39:01 +00003157 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
Matt Carlson42b64a42011-05-19 12:12:49 +00003158 if (err)
3159 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003160
Matt Carlson42b64a42011-05-19 12:12:49 +00003161 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3162 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Matt Carlson42b64a42011-05-19 12:12:49 +00003164 tw32(TG3_CPMU_EEE_MODE,
3165 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003166
Matt Carlson42b64a42011-05-19 12:12:49 +00003167 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3168 if (!err) {
3169 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003170
Matt Carlsona6b68da2010-12-06 08:28:52 +00003171 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003172 /* Advertise 100-BaseTX EEE ability */
3173 if (advertise & ADVERTISED_100baseT_Full)
3174 val |= MDIO_AN_EEE_ADV_100TX;
3175 /* Advertise 1000-BaseT EEE ability */
3176 if (advertise & ADVERTISED_1000baseT_Full)
3177 val |= MDIO_AN_EEE_ADV_1000T;
3178 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003179 if (err)
3180 val = 0;
3181
3182 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3183 case ASIC_REV_5717:
3184 case ASIC_REV_57765:
3185 case ASIC_REV_5719:
3186 /* If we advertised any eee advertisements above... */
3187 if (val)
3188 val = MII_TG3_DSP_TAP26_ALNOKO |
3189 MII_TG3_DSP_TAP26_RMRXSTO |
3190 MII_TG3_DSP_TAP26_OPCSINPT;
3191 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3192 /* Fall through */
3193 case ASIC_REV_5720:
3194 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3195 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3196 MII_TG3_DSP_CH34TP2_HIBW01);
3197 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003198
Matt Carlson42b64a42011-05-19 12:12:49 +00003199 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3200 if (!err)
3201 err = err2;
3202 }
3203
3204done:
3205 return err;
3206}
3207
3208static void tg3_phy_copper_begin(struct tg3 *tp)
3209{
3210 u32 new_adv;
3211 int i;
3212
3213 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3214 new_adv = ADVERTISED_10baseT_Half |
3215 ADVERTISED_10baseT_Full;
3216 if (tg3_flag(tp, WOL_SPEED_100MB))
3217 new_adv |= ADVERTISED_100baseT_Half |
3218 ADVERTISED_100baseT_Full;
3219
3220 tg3_phy_autoneg_cfg(tp, new_adv,
3221 FLOW_CTRL_TX | FLOW_CTRL_RX);
3222 } else if (tp->link_config.speed == SPEED_INVALID) {
3223 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3224 tp->link_config.advertising &=
3225 ~(ADVERTISED_1000baseT_Half |
3226 ADVERTISED_1000baseT_Full);
3227
3228 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3229 tp->link_config.flowctrl);
3230 } else {
3231 /* Asking for a specific link mode. */
3232 if (tp->link_config.speed == SPEED_1000) {
3233 if (tp->link_config.duplex == DUPLEX_FULL)
3234 new_adv = ADVERTISED_1000baseT_Full;
3235 else
3236 new_adv = ADVERTISED_1000baseT_Half;
3237 } else if (tp->link_config.speed == SPEED_100) {
3238 if (tp->link_config.duplex == DUPLEX_FULL)
3239 new_adv = ADVERTISED_100baseT_Full;
3240 else
3241 new_adv = ADVERTISED_100baseT_Half;
3242 } else {
3243 if (tp->link_config.duplex == DUPLEX_FULL)
3244 new_adv = ADVERTISED_10baseT_Full;
3245 else
3246 new_adv = ADVERTISED_10baseT_Half;
3247 }
3248
3249 tg3_phy_autoneg_cfg(tp, new_adv,
3250 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003251 }
3252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3254 tp->link_config.speed != SPEED_INVALID) {
3255 u32 bmcr, orig_bmcr;
3256
3257 tp->link_config.active_speed = tp->link_config.speed;
3258 tp->link_config.active_duplex = tp->link_config.duplex;
3259
3260 bmcr = 0;
3261 switch (tp->link_config.speed) {
3262 default:
3263 case SPEED_10:
3264 break;
3265
3266 case SPEED_100:
3267 bmcr |= BMCR_SPEED100;
3268 break;
3269
3270 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003271 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
3275 if (tp->link_config.duplex == DUPLEX_FULL)
3276 bmcr |= BMCR_FULLDPLX;
3277
3278 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3279 (bmcr != orig_bmcr)) {
3280 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3281 for (i = 0; i < 1500; i++) {
3282 u32 tmp;
3283
3284 udelay(10);
3285 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3286 tg3_readphy(tp, MII_BMSR, &tmp))
3287 continue;
3288 if (!(tmp & BMSR_LSTATUS)) {
3289 udelay(40);
3290 break;
3291 }
3292 }
3293 tg3_writephy(tp, MII_BMCR, bmcr);
3294 udelay(40);
3295 }
3296 } else {
3297 tg3_writephy(tp, MII_BMCR,
3298 BMCR_ANENABLE | BMCR_ANRESTART);
3299 }
3300}
3301
3302static int tg3_init_5401phy_dsp(struct tg3 *tp)
3303{
3304 int err;
3305
3306 /* Turn off tap power management. */
3307 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003308 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003310 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3311 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3312 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3313 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3314 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
3316 udelay(40);
3317
3318 return err;
3319}
3320
Michael Chan3600d912006-12-07 00:21:48 -08003321static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
Michael Chan3600d912006-12-07 00:21:48 -08003323 u32 adv_reg, all_mask = 0;
3324
3325 if (mask & ADVERTISED_10baseT_Half)
3326 all_mask |= ADVERTISE_10HALF;
3327 if (mask & ADVERTISED_10baseT_Full)
3328 all_mask |= ADVERTISE_10FULL;
3329 if (mask & ADVERTISED_100baseT_Half)
3330 all_mask |= ADVERTISE_100HALF;
3331 if (mask & ADVERTISED_100baseT_Full)
3332 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
3334 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3335 return 0;
3336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 if ((adv_reg & all_mask) != all_mask)
3338 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003339 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 u32 tg3_ctrl;
3341
Michael Chan3600d912006-12-07 00:21:48 -08003342 all_mask = 0;
3343 if (mask & ADVERTISED_1000baseT_Half)
3344 all_mask |= ADVERTISE_1000HALF;
3345 if (mask & ADVERTISED_1000baseT_Full)
3346 all_mask |= ADVERTISE_1000FULL;
3347
Matt Carlson221c5632011-06-13 13:39:01 +00003348 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 return 0;
3350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 if ((tg3_ctrl & all_mask) != all_mask)
3352 return 0;
3353 }
3354 return 1;
3355}
3356
Matt Carlsonef167e22007-12-20 20:10:01 -08003357static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3358{
3359 u32 curadv, reqadv;
3360
3361 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3362 return 1;
3363
3364 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3365 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3366
3367 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3368 if (curadv != reqadv)
3369 return 0;
3370
Joe Perches63c3a662011-04-26 08:12:10 +00003371 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003372 tg3_readphy(tp, MII_LPA, rmtadv);
3373 } else {
3374 /* Reprogram the advertisement register, even if it
3375 * does not affect the current link. If the link
3376 * gets renegotiated in the future, we can save an
3377 * additional renegotiation cycle by advertising
3378 * it correctly in the first place.
3379 */
3380 if (curadv != reqadv) {
3381 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3382 ADVERTISE_PAUSE_ASYM);
3383 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3384 }
3385 }
3386
3387 return 1;
3388}
3389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3391{
3392 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003393 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003394 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 u16 current_speed;
3396 u8 current_duplex;
3397 int i, err;
3398
3399 tw32(MAC_EVENT, 0);
3400
3401 tw32_f(MAC_STATUS,
3402 (MAC_STATUS_SYNC_CHANGED |
3403 MAC_STATUS_CFG_CHANGED |
3404 MAC_STATUS_MI_COMPLETION |
3405 MAC_STATUS_LNKSTATE_CHANGED));
3406 udelay(40);
3407
Matt Carlson8ef21422008-05-02 16:47:53 -07003408 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3409 tw32_f(MAC_MI_MODE,
3410 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3411 udelay(80);
3412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003414 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
3416 /* Some third-party PHYs need to be reset on link going
3417 * down.
3418 */
3419 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3420 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3421 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3422 netif_carrier_ok(tp->dev)) {
3423 tg3_readphy(tp, MII_BMSR, &bmsr);
3424 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3425 !(bmsr & BMSR_LSTATUS))
3426 force_reset = 1;
3427 }
3428 if (force_reset)
3429 tg3_phy_reset(tp);
3430
Matt Carlson79eb6902010-02-17 15:17:03 +00003431 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 tg3_readphy(tp, MII_BMSR, &bmsr);
3433 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003434 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 bmsr = 0;
3436
3437 if (!(bmsr & BMSR_LSTATUS)) {
3438 err = tg3_init_5401phy_dsp(tp);
3439 if (err)
3440 return err;
3441
3442 tg3_readphy(tp, MII_BMSR, &bmsr);
3443 for (i = 0; i < 1000; i++) {
3444 udelay(10);
3445 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3446 (bmsr & BMSR_LSTATUS)) {
3447 udelay(40);
3448 break;
3449 }
3450 }
3451
Matt Carlson79eb6902010-02-17 15:17:03 +00003452 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3453 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 !(bmsr & BMSR_LSTATUS) &&
3455 tp->link_config.active_speed == SPEED_1000) {
3456 err = tg3_phy_reset(tp);
3457 if (!err)
3458 err = tg3_init_5401phy_dsp(tp);
3459 if (err)
3460 return err;
3461 }
3462 }
3463 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3464 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3465 /* 5701 {A0,B0} CRC bug workaround */
3466 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003467 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3468 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3469 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 }
3471
3472 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003473 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3474 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003476 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003478 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3480
3481 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3482 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3483 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3484 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3485 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3486 else
3487 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3488 }
3489
3490 current_link_up = 0;
3491 current_speed = SPEED_INVALID;
3492 current_duplex = DUPLEX_INVALID;
3493
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003494 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003495 err = tg3_phy_auxctl_read(tp,
3496 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3497 &val);
3498 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003499 tg3_phy_auxctl_write(tp,
3500 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3501 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 goto relink;
3503 }
3504 }
3505
3506 bmsr = 0;
3507 for (i = 0; i < 100; i++) {
3508 tg3_readphy(tp, MII_BMSR, &bmsr);
3509 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3510 (bmsr & BMSR_LSTATUS))
3511 break;
3512 udelay(40);
3513 }
3514
3515 if (bmsr & BMSR_LSTATUS) {
3516 u32 aux_stat, bmcr;
3517
3518 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3519 for (i = 0; i < 2000; i++) {
3520 udelay(10);
3521 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3522 aux_stat)
3523 break;
3524 }
3525
3526 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3527 &current_speed,
3528 &current_duplex);
3529
3530 bmcr = 0;
3531 for (i = 0; i < 200; i++) {
3532 tg3_readphy(tp, MII_BMCR, &bmcr);
3533 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3534 continue;
3535 if (bmcr && bmcr != 0x7fff)
3536 break;
3537 udelay(10);
3538 }
3539
Matt Carlsonef167e22007-12-20 20:10:01 -08003540 lcl_adv = 0;
3541 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Matt Carlsonef167e22007-12-20 20:10:01 -08003543 tp->link_config.active_speed = current_speed;
3544 tp->link_config.active_duplex = current_duplex;
3545
3546 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3547 if ((bmcr & BMCR_ANENABLE) &&
3548 tg3_copper_is_advertising_all(tp,
3549 tp->link_config.advertising)) {
3550 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3551 &rmt_adv))
3552 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 }
3554 } else {
3555 if (!(bmcr & BMCR_ANENABLE) &&
3556 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003557 tp->link_config.duplex == current_duplex &&
3558 tp->link_config.flowctrl ==
3559 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 }
3562 }
3563
Matt Carlsonef167e22007-12-20 20:10:01 -08003564 if (current_link_up == 1 &&
3565 tp->link_config.active_duplex == DUPLEX_FULL)
3566 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 }
3568
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569relink:
Matt Carlson80096062010-08-02 11:26:06 +00003570 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 tg3_phy_copper_begin(tp);
3572
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003573 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00003574 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
3575 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 current_link_up = 1;
3577 }
3578
3579 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3580 if (current_link_up == 1) {
3581 if (tp->link_config.active_speed == SPEED_100 ||
3582 tp->link_config.active_speed == SPEED_10)
3583 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3584 else
3585 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003586 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003587 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3588 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3590
3591 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3592 if (tp->link_config.active_duplex == DUPLEX_HALF)
3593 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003596 if (current_link_up == 1 &&
3597 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003599 else
3600 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 }
3602
3603 /* ??? Without this setting Netgear GA302T PHY does not
3604 * ??? send/receive packets...
3605 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003606 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3608 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3609 tw32_f(MAC_MI_MODE, tp->mi_mode);
3610 udelay(80);
3611 }
3612
3613 tw32_f(MAC_MODE, tp->mac_mode);
3614 udelay(40);
3615
Matt Carlson52b02d02010-10-14 10:37:41 +00003616 tg3_phy_eee_adjust(tp, current_link_up);
3617
Joe Perches63c3a662011-04-26 08:12:10 +00003618 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 /* Polled via timer. */
3620 tw32_f(MAC_EVENT, 0);
3621 } else {
3622 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3623 }
3624 udelay(40);
3625
3626 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3627 current_link_up == 1 &&
3628 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00003629 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 udelay(120);
3631 tw32_f(MAC_STATUS,
3632 (MAC_STATUS_SYNC_CHANGED |
3633 MAC_STATUS_CFG_CHANGED));
3634 udelay(40);
3635 tg3_write_mem(tp,
3636 NIC_SRAM_FIRMWARE_MBOX,
3637 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3638 }
3639
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003640 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00003641 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003642 u16 oldlnkctl, newlnkctl;
3643
3644 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003645 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003646 &oldlnkctl);
3647 if (tp->link_config.active_speed == SPEED_100 ||
3648 tp->link_config.active_speed == SPEED_10)
3649 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3650 else
3651 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3652 if (newlnkctl != oldlnkctl)
3653 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003654 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003655 newlnkctl);
3656 }
3657
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 if (current_link_up != netif_carrier_ok(tp->dev)) {
3659 if (current_link_up)
3660 netif_carrier_on(tp->dev);
3661 else
3662 netif_carrier_off(tp->dev);
3663 tg3_link_report(tp);
3664 }
3665
3666 return 0;
3667}
3668
3669struct tg3_fiber_aneginfo {
3670 int state;
3671#define ANEG_STATE_UNKNOWN 0
3672#define ANEG_STATE_AN_ENABLE 1
3673#define ANEG_STATE_RESTART_INIT 2
3674#define ANEG_STATE_RESTART 3
3675#define ANEG_STATE_DISABLE_LINK_OK 4
3676#define ANEG_STATE_ABILITY_DETECT_INIT 5
3677#define ANEG_STATE_ABILITY_DETECT 6
3678#define ANEG_STATE_ACK_DETECT_INIT 7
3679#define ANEG_STATE_ACK_DETECT 8
3680#define ANEG_STATE_COMPLETE_ACK_INIT 9
3681#define ANEG_STATE_COMPLETE_ACK 10
3682#define ANEG_STATE_IDLE_DETECT_INIT 11
3683#define ANEG_STATE_IDLE_DETECT 12
3684#define ANEG_STATE_LINK_OK 13
3685#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3686#define ANEG_STATE_NEXT_PAGE_WAIT 15
3687
3688 u32 flags;
3689#define MR_AN_ENABLE 0x00000001
3690#define MR_RESTART_AN 0x00000002
3691#define MR_AN_COMPLETE 0x00000004
3692#define MR_PAGE_RX 0x00000008
3693#define MR_NP_LOADED 0x00000010
3694#define MR_TOGGLE_TX 0x00000020
3695#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3696#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3697#define MR_LP_ADV_SYM_PAUSE 0x00000100
3698#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3699#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3700#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3701#define MR_LP_ADV_NEXT_PAGE 0x00001000
3702#define MR_TOGGLE_RX 0x00002000
3703#define MR_NP_RX 0x00004000
3704
3705#define MR_LINK_OK 0x80000000
3706
3707 unsigned long link_time, cur_time;
3708
3709 u32 ability_match_cfg;
3710 int ability_match_count;
3711
3712 char ability_match, idle_match, ack_match;
3713
3714 u32 txconfig, rxconfig;
3715#define ANEG_CFG_NP 0x00000080
3716#define ANEG_CFG_ACK 0x00000040
3717#define ANEG_CFG_RF2 0x00000020
3718#define ANEG_CFG_RF1 0x00000010
3719#define ANEG_CFG_PS2 0x00000001
3720#define ANEG_CFG_PS1 0x00008000
3721#define ANEG_CFG_HD 0x00004000
3722#define ANEG_CFG_FD 0x00002000
3723#define ANEG_CFG_INVAL 0x00001f06
3724
3725};
3726#define ANEG_OK 0
3727#define ANEG_DONE 1
3728#define ANEG_TIMER_ENAB 2
3729#define ANEG_FAILED -1
3730
3731#define ANEG_STATE_SETTLE_TIME 10000
3732
3733static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3734 struct tg3_fiber_aneginfo *ap)
3735{
Matt Carlson5be73b42007-12-20 20:09:29 -08003736 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 unsigned long delta;
3738 u32 rx_cfg_reg;
3739 int ret;
3740
3741 if (ap->state == ANEG_STATE_UNKNOWN) {
3742 ap->rxconfig = 0;
3743 ap->link_time = 0;
3744 ap->cur_time = 0;
3745 ap->ability_match_cfg = 0;
3746 ap->ability_match_count = 0;
3747 ap->ability_match = 0;
3748 ap->idle_match = 0;
3749 ap->ack_match = 0;
3750 }
3751 ap->cur_time++;
3752
3753 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3754 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3755
3756 if (rx_cfg_reg != ap->ability_match_cfg) {
3757 ap->ability_match_cfg = rx_cfg_reg;
3758 ap->ability_match = 0;
3759 ap->ability_match_count = 0;
3760 } else {
3761 if (++ap->ability_match_count > 1) {
3762 ap->ability_match = 1;
3763 ap->ability_match_cfg = rx_cfg_reg;
3764 }
3765 }
3766 if (rx_cfg_reg & ANEG_CFG_ACK)
3767 ap->ack_match = 1;
3768 else
3769 ap->ack_match = 0;
3770
3771 ap->idle_match = 0;
3772 } else {
3773 ap->idle_match = 1;
3774 ap->ability_match_cfg = 0;
3775 ap->ability_match_count = 0;
3776 ap->ability_match = 0;
3777 ap->ack_match = 0;
3778
3779 rx_cfg_reg = 0;
3780 }
3781
3782 ap->rxconfig = rx_cfg_reg;
3783 ret = ANEG_OK;
3784
Matt Carlson33f401a2010-04-05 10:19:27 +00003785 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 case ANEG_STATE_UNKNOWN:
3787 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3788 ap->state = ANEG_STATE_AN_ENABLE;
3789
3790 /* fallthru */
3791 case ANEG_STATE_AN_ENABLE:
3792 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3793 if (ap->flags & MR_AN_ENABLE) {
3794 ap->link_time = 0;
3795 ap->cur_time = 0;
3796 ap->ability_match_cfg = 0;
3797 ap->ability_match_count = 0;
3798 ap->ability_match = 0;
3799 ap->idle_match = 0;
3800 ap->ack_match = 0;
3801
3802 ap->state = ANEG_STATE_RESTART_INIT;
3803 } else {
3804 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3805 }
3806 break;
3807
3808 case ANEG_STATE_RESTART_INIT:
3809 ap->link_time = ap->cur_time;
3810 ap->flags &= ~(MR_NP_LOADED);
3811 ap->txconfig = 0;
3812 tw32(MAC_TX_AUTO_NEG, 0);
3813 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3814 tw32_f(MAC_MODE, tp->mac_mode);
3815 udelay(40);
3816
3817 ret = ANEG_TIMER_ENAB;
3818 ap->state = ANEG_STATE_RESTART;
3819
3820 /* fallthru */
3821 case ANEG_STATE_RESTART:
3822 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00003823 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00003825 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 break;
3828
3829 case ANEG_STATE_DISABLE_LINK_OK:
3830 ret = ANEG_DONE;
3831 break;
3832
3833 case ANEG_STATE_ABILITY_DETECT_INIT:
3834 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003835 ap->txconfig = ANEG_CFG_FD;
3836 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3837 if (flowctrl & ADVERTISE_1000XPAUSE)
3838 ap->txconfig |= ANEG_CFG_PS1;
3839 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3840 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3842 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3843 tw32_f(MAC_MODE, tp->mac_mode);
3844 udelay(40);
3845
3846 ap->state = ANEG_STATE_ABILITY_DETECT;
3847 break;
3848
3849 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00003850 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 break;
3853
3854 case ANEG_STATE_ACK_DETECT_INIT:
3855 ap->txconfig |= ANEG_CFG_ACK;
3856 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3857 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3858 tw32_f(MAC_MODE, tp->mac_mode);
3859 udelay(40);
3860
3861 ap->state = ANEG_STATE_ACK_DETECT;
3862
3863 /* fallthru */
3864 case ANEG_STATE_ACK_DETECT:
3865 if (ap->ack_match != 0) {
3866 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3867 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3868 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3869 } else {
3870 ap->state = ANEG_STATE_AN_ENABLE;
3871 }
3872 } else if (ap->ability_match != 0 &&
3873 ap->rxconfig == 0) {
3874 ap->state = ANEG_STATE_AN_ENABLE;
3875 }
3876 break;
3877
3878 case ANEG_STATE_COMPLETE_ACK_INIT:
3879 if (ap->rxconfig & ANEG_CFG_INVAL) {
3880 ret = ANEG_FAILED;
3881 break;
3882 }
3883 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3884 MR_LP_ADV_HALF_DUPLEX |
3885 MR_LP_ADV_SYM_PAUSE |
3886 MR_LP_ADV_ASYM_PAUSE |
3887 MR_LP_ADV_REMOTE_FAULT1 |
3888 MR_LP_ADV_REMOTE_FAULT2 |
3889 MR_LP_ADV_NEXT_PAGE |
3890 MR_TOGGLE_RX |
3891 MR_NP_RX);
3892 if (ap->rxconfig & ANEG_CFG_FD)
3893 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3894 if (ap->rxconfig & ANEG_CFG_HD)
3895 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3896 if (ap->rxconfig & ANEG_CFG_PS1)
3897 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3898 if (ap->rxconfig & ANEG_CFG_PS2)
3899 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3900 if (ap->rxconfig & ANEG_CFG_RF1)
3901 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3902 if (ap->rxconfig & ANEG_CFG_RF2)
3903 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3904 if (ap->rxconfig & ANEG_CFG_NP)
3905 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3906
3907 ap->link_time = ap->cur_time;
3908
3909 ap->flags ^= (MR_TOGGLE_TX);
3910 if (ap->rxconfig & 0x0008)
3911 ap->flags |= MR_TOGGLE_RX;
3912 if (ap->rxconfig & ANEG_CFG_NP)
3913 ap->flags |= MR_NP_RX;
3914 ap->flags |= MR_PAGE_RX;
3915
3916 ap->state = ANEG_STATE_COMPLETE_ACK;
3917 ret = ANEG_TIMER_ENAB;
3918 break;
3919
3920 case ANEG_STATE_COMPLETE_ACK:
3921 if (ap->ability_match != 0 &&
3922 ap->rxconfig == 0) {
3923 ap->state = ANEG_STATE_AN_ENABLE;
3924 break;
3925 }
3926 delta = ap->cur_time - ap->link_time;
3927 if (delta > ANEG_STATE_SETTLE_TIME) {
3928 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3929 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3930 } else {
3931 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3932 !(ap->flags & MR_NP_RX)) {
3933 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3934 } else {
3935 ret = ANEG_FAILED;
3936 }
3937 }
3938 }
3939 break;
3940
3941 case ANEG_STATE_IDLE_DETECT_INIT:
3942 ap->link_time = ap->cur_time;
3943 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3944 tw32_f(MAC_MODE, tp->mac_mode);
3945 udelay(40);
3946
3947 ap->state = ANEG_STATE_IDLE_DETECT;
3948 ret = ANEG_TIMER_ENAB;
3949 break;
3950
3951 case ANEG_STATE_IDLE_DETECT:
3952 if (ap->ability_match != 0 &&
3953 ap->rxconfig == 0) {
3954 ap->state = ANEG_STATE_AN_ENABLE;
3955 break;
3956 }
3957 delta = ap->cur_time - ap->link_time;
3958 if (delta > ANEG_STATE_SETTLE_TIME) {
3959 /* XXX another gem from the Broadcom driver :( */
3960 ap->state = ANEG_STATE_LINK_OK;
3961 }
3962 break;
3963
3964 case ANEG_STATE_LINK_OK:
3965 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3966 ret = ANEG_DONE;
3967 break;
3968
3969 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3970 /* ??? unimplemented */
3971 break;
3972
3973 case ANEG_STATE_NEXT_PAGE_WAIT:
3974 /* ??? unimplemented */
3975 break;
3976
3977 default:
3978 ret = ANEG_FAILED;
3979 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
3982 return ret;
3983}
3984
Matt Carlson5be73b42007-12-20 20:09:29 -08003985static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986{
3987 int res = 0;
3988 struct tg3_fiber_aneginfo aninfo;
3989 int status = ANEG_FAILED;
3990 unsigned int tick;
3991 u32 tmp;
3992
3993 tw32_f(MAC_TX_AUTO_NEG, 0);
3994
3995 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3996 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3997 udelay(40);
3998
3999 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4000 udelay(40);
4001
4002 memset(&aninfo, 0, sizeof(aninfo));
4003 aninfo.flags |= MR_AN_ENABLE;
4004 aninfo.state = ANEG_STATE_UNKNOWN;
4005 aninfo.cur_time = 0;
4006 tick = 0;
4007 while (++tick < 195000) {
4008 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4009 if (status == ANEG_DONE || status == ANEG_FAILED)
4010 break;
4011
4012 udelay(1);
4013 }
4014
4015 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4016 tw32_f(MAC_MODE, tp->mac_mode);
4017 udelay(40);
4018
Matt Carlson5be73b42007-12-20 20:09:29 -08004019 *txflags = aninfo.txconfig;
4020 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
4022 if (status == ANEG_DONE &&
4023 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4024 MR_LP_ADV_FULL_DUPLEX)))
4025 res = 1;
4026
4027 return res;
4028}
4029
4030static void tg3_init_bcm8002(struct tg3 *tp)
4031{
4032 u32 mac_status = tr32(MAC_STATUS);
4033 int i;
4034
4035 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004036 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 !(mac_status & MAC_STATUS_PCS_SYNCED))
4038 return;
4039
4040 /* Set PLL lock range. */
4041 tg3_writephy(tp, 0x16, 0x8007);
4042
4043 /* SW reset */
4044 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4045
4046 /* Wait for reset to complete. */
4047 /* XXX schedule_timeout() ... */
4048 for (i = 0; i < 500; i++)
4049 udelay(10);
4050
4051 /* Config mode; select PMA/Ch 1 regs. */
4052 tg3_writephy(tp, 0x10, 0x8411);
4053
4054 /* Enable auto-lock and comdet, select txclk for tx. */
4055 tg3_writephy(tp, 0x11, 0x0a10);
4056
4057 tg3_writephy(tp, 0x18, 0x00a0);
4058 tg3_writephy(tp, 0x16, 0x41ff);
4059
4060 /* Assert and deassert POR. */
4061 tg3_writephy(tp, 0x13, 0x0400);
4062 udelay(40);
4063 tg3_writephy(tp, 0x13, 0x0000);
4064
4065 tg3_writephy(tp, 0x11, 0x0a50);
4066 udelay(40);
4067 tg3_writephy(tp, 0x11, 0x0a10);
4068
4069 /* Wait for signal to stabilize */
4070 /* XXX schedule_timeout() ... */
4071 for (i = 0; i < 15000; i++)
4072 udelay(10);
4073
4074 /* Deselect the channel register so we can read the PHYID
4075 * later.
4076 */
4077 tg3_writephy(tp, 0x10, 0x8011);
4078}
4079
4080static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4081{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004082 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 u32 sg_dig_ctrl, sg_dig_status;
4084 u32 serdes_cfg, expected_sg_dig_ctrl;
4085 int workaround, port_a;
4086 int current_link_up;
4087
4088 serdes_cfg = 0;
4089 expected_sg_dig_ctrl = 0;
4090 workaround = 0;
4091 port_a = 1;
4092 current_link_up = 0;
4093
4094 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4095 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4096 workaround = 1;
4097 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4098 port_a = 0;
4099
4100 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4101 /* preserve bits 20-23 for voltage regulator */
4102 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4103 }
4104
4105 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4106
4107 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004108 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 if (workaround) {
4110 u32 val = serdes_cfg;
4111
4112 if (port_a)
4113 val |= 0xc010000;
4114 else
4115 val |= 0x4010000;
4116 tw32_f(MAC_SERDES_CFG, val);
4117 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004118
4119 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 }
4121 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4122 tg3_setup_flow_control(tp, 0, 0);
4123 current_link_up = 1;
4124 }
4125 goto out;
4126 }
4127
4128 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004129 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
Matt Carlson82cd3d12007-12-20 20:09:00 -08004131 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4132 if (flowctrl & ADVERTISE_1000XPAUSE)
4133 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4134 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4135 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
4137 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004138 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004139 tp->serdes_counter &&
4140 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4141 MAC_STATUS_RCVD_CFG)) ==
4142 MAC_STATUS_PCS_SYNCED)) {
4143 tp->serdes_counter--;
4144 current_link_up = 1;
4145 goto out;
4146 }
4147restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 if (workaround)
4149 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004150 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 udelay(5);
4152 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4153
Michael Chan3d3ebe72006-09-27 15:59:15 -07004154 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004155 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4157 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004158 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 mac_status = tr32(MAC_STATUS);
4160
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004161 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004163 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
Matt Carlson82cd3d12007-12-20 20:09:00 -08004165 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4166 local_adv |= ADVERTISE_1000XPAUSE;
4167 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4168 local_adv |= ADVERTISE_1000XPSE_ASYM;
4169
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004170 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004171 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004172 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004173 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
4175 tg3_setup_flow_control(tp, local_adv, remote_adv);
4176 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004177 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004178 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004179 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004180 if (tp->serdes_counter)
4181 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 else {
4183 if (workaround) {
4184 u32 val = serdes_cfg;
4185
4186 if (port_a)
4187 val |= 0xc010000;
4188 else
4189 val |= 0x4010000;
4190
4191 tw32_f(MAC_SERDES_CFG, val);
4192 }
4193
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004194 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 udelay(40);
4196
4197 /* Link parallel detection - link is up */
4198 /* only if we have PCS_SYNC and not */
4199 /* receiving config code words */
4200 mac_status = tr32(MAC_STATUS);
4201 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4202 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4203 tg3_setup_flow_control(tp, 0, 0);
4204 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004205 tp->phy_flags |=
4206 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004207 tp->serdes_counter =
4208 SERDES_PARALLEL_DET_TIMEOUT;
4209 } else
4210 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 }
4212 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004213 } else {
4214 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004215 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 }
4217
4218out:
4219 return current_link_up;
4220}
4221
4222static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4223{
4224 int current_link_up = 0;
4225
Michael Chan5cf64b82007-05-05 12:11:21 -07004226 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
4229 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004230 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004232
Matt Carlson5be73b42007-12-20 20:09:29 -08004233 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4234 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
Matt Carlson5be73b42007-12-20 20:09:29 -08004236 if (txflags & ANEG_CFG_PS1)
4237 local_adv |= ADVERTISE_1000XPAUSE;
4238 if (txflags & ANEG_CFG_PS2)
4239 local_adv |= ADVERTISE_1000XPSE_ASYM;
4240
4241 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4242 remote_adv |= LPA_1000XPAUSE;
4243 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4244 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
4246 tg3_setup_flow_control(tp, local_adv, remote_adv);
4247
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 current_link_up = 1;
4249 }
4250 for (i = 0; i < 30; i++) {
4251 udelay(20);
4252 tw32_f(MAC_STATUS,
4253 (MAC_STATUS_SYNC_CHANGED |
4254 MAC_STATUS_CFG_CHANGED));
4255 udelay(40);
4256 if ((tr32(MAC_STATUS) &
4257 (MAC_STATUS_SYNC_CHANGED |
4258 MAC_STATUS_CFG_CHANGED)) == 0)
4259 break;
4260 }
4261
4262 mac_status = tr32(MAC_STATUS);
4263 if (current_link_up == 0 &&
4264 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4265 !(mac_status & MAC_STATUS_RCVD_CFG))
4266 current_link_up = 1;
4267 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004268 tg3_setup_flow_control(tp, 0, 0);
4269
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 /* Forcing 1000FD link up. */
4271 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
4273 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4274 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004275
4276 tw32_f(MAC_MODE, tp->mac_mode);
4277 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 }
4279
4280out:
4281 return current_link_up;
4282}
4283
4284static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4285{
4286 u32 orig_pause_cfg;
4287 u16 orig_active_speed;
4288 u8 orig_active_duplex;
4289 u32 mac_status;
4290 int current_link_up;
4291 int i;
4292
Matt Carlson8d018622007-12-20 20:05:44 -08004293 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 orig_active_speed = tp->link_config.active_speed;
4295 orig_active_duplex = tp->link_config.active_duplex;
4296
Joe Perches63c3a662011-04-26 08:12:10 +00004297 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004299 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 mac_status = tr32(MAC_STATUS);
4301 mac_status &= (MAC_STATUS_PCS_SYNCED |
4302 MAC_STATUS_SIGNAL_DET |
4303 MAC_STATUS_CFG_CHANGED |
4304 MAC_STATUS_RCVD_CFG);
4305 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4306 MAC_STATUS_SIGNAL_DET)) {
4307 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4308 MAC_STATUS_CFG_CHANGED));
4309 return 0;
4310 }
4311 }
4312
4313 tw32_f(MAC_TX_AUTO_NEG, 0);
4314
4315 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4316 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4317 tw32_f(MAC_MODE, tp->mac_mode);
4318 udelay(40);
4319
Matt Carlson79eb6902010-02-17 15:17:03 +00004320 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 tg3_init_bcm8002(tp);
4322
4323 /* Enable link change event even when serdes polling. */
4324 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4325 udelay(40);
4326
4327 current_link_up = 0;
4328 mac_status = tr32(MAC_STATUS);
4329
Joe Perches63c3a662011-04-26 08:12:10 +00004330 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4332 else
4333 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4334
Matt Carlson898a56f2009-08-28 14:02:40 +00004335 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004337 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338
4339 for (i = 0; i < 100; i++) {
4340 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4341 MAC_STATUS_CFG_CHANGED));
4342 udelay(5);
4343 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004344 MAC_STATUS_CFG_CHANGED |
4345 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 break;
4347 }
4348
4349 mac_status = tr32(MAC_STATUS);
4350 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4351 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004352 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4353 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 tw32_f(MAC_MODE, (tp->mac_mode |
4355 MAC_MODE_SEND_CONFIGS));
4356 udelay(1);
4357 tw32_f(MAC_MODE, tp->mac_mode);
4358 }
4359 }
4360
4361 if (current_link_up == 1) {
4362 tp->link_config.active_speed = SPEED_1000;
4363 tp->link_config.active_duplex = DUPLEX_FULL;
4364 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4365 LED_CTRL_LNKLED_OVERRIDE |
4366 LED_CTRL_1000MBPS_ON));
4367 } else {
4368 tp->link_config.active_speed = SPEED_INVALID;
4369 tp->link_config.active_duplex = DUPLEX_INVALID;
4370 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4371 LED_CTRL_LNKLED_OVERRIDE |
4372 LED_CTRL_TRAFFIC_OVERRIDE));
4373 }
4374
4375 if (current_link_up != netif_carrier_ok(tp->dev)) {
4376 if (current_link_up)
4377 netif_carrier_on(tp->dev);
4378 else
4379 netif_carrier_off(tp->dev);
4380 tg3_link_report(tp);
4381 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004382 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 if (orig_pause_cfg != now_pause_cfg ||
4384 orig_active_speed != tp->link_config.active_speed ||
4385 orig_active_duplex != tp->link_config.active_duplex)
4386 tg3_link_report(tp);
4387 }
4388
4389 return 0;
4390}
4391
Michael Chan747e8f82005-07-25 12:33:22 -07004392static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4393{
4394 int current_link_up, err = 0;
4395 u32 bmsr, bmcr;
4396 u16 current_speed;
4397 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004398 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004399
4400 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4401 tw32_f(MAC_MODE, tp->mac_mode);
4402 udelay(40);
4403
4404 tw32(MAC_EVENT, 0);
4405
4406 tw32_f(MAC_STATUS,
4407 (MAC_STATUS_SYNC_CHANGED |
4408 MAC_STATUS_CFG_CHANGED |
4409 MAC_STATUS_MI_COMPLETION |
4410 MAC_STATUS_LNKSTATE_CHANGED));
4411 udelay(40);
4412
4413 if (force_reset)
4414 tg3_phy_reset(tp);
4415
4416 current_link_up = 0;
4417 current_speed = SPEED_INVALID;
4418 current_duplex = DUPLEX_INVALID;
4419
4420 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4421 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004422 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4423 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4424 bmsr |= BMSR_LSTATUS;
4425 else
4426 bmsr &= ~BMSR_LSTATUS;
4427 }
Michael Chan747e8f82005-07-25 12:33:22 -07004428
4429 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4430
4431 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004432 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004433 /* do nothing, just check for link up at the end */
4434 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4435 u32 adv, new_adv;
4436
4437 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4438 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4439 ADVERTISE_1000XPAUSE |
4440 ADVERTISE_1000XPSE_ASYM |
4441 ADVERTISE_SLCT);
4442
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004443 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004444
4445 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4446 new_adv |= ADVERTISE_1000XHALF;
4447 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4448 new_adv |= ADVERTISE_1000XFULL;
4449
4450 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4451 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4452 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4453 tg3_writephy(tp, MII_BMCR, bmcr);
4454
4455 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004456 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004457 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004458
4459 return err;
4460 }
4461 } else {
4462 u32 new_bmcr;
4463
4464 bmcr &= ~BMCR_SPEED1000;
4465 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4466
4467 if (tp->link_config.duplex == DUPLEX_FULL)
4468 new_bmcr |= BMCR_FULLDPLX;
4469
4470 if (new_bmcr != bmcr) {
4471 /* BMCR_SPEED1000 is a reserved bit that needs
4472 * to be set on write.
4473 */
4474 new_bmcr |= BMCR_SPEED1000;
4475
4476 /* Force a linkdown */
4477 if (netif_carrier_ok(tp->dev)) {
4478 u32 adv;
4479
4480 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4481 adv &= ~(ADVERTISE_1000XFULL |
4482 ADVERTISE_1000XHALF |
4483 ADVERTISE_SLCT);
4484 tg3_writephy(tp, MII_ADVERTISE, adv);
4485 tg3_writephy(tp, MII_BMCR, bmcr |
4486 BMCR_ANRESTART |
4487 BMCR_ANENABLE);
4488 udelay(10);
4489 netif_carrier_off(tp->dev);
4490 }
4491 tg3_writephy(tp, MII_BMCR, new_bmcr);
4492 bmcr = new_bmcr;
4493 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4494 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004495 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4496 ASIC_REV_5714) {
4497 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4498 bmsr |= BMSR_LSTATUS;
4499 else
4500 bmsr &= ~BMSR_LSTATUS;
4501 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004502 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004503 }
4504 }
4505
4506 if (bmsr & BMSR_LSTATUS) {
4507 current_speed = SPEED_1000;
4508 current_link_up = 1;
4509 if (bmcr & BMCR_FULLDPLX)
4510 current_duplex = DUPLEX_FULL;
4511 else
4512 current_duplex = DUPLEX_HALF;
4513
Matt Carlsonef167e22007-12-20 20:10:01 -08004514 local_adv = 0;
4515 remote_adv = 0;
4516
Michael Chan747e8f82005-07-25 12:33:22 -07004517 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004518 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004519
4520 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4521 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4522 common = local_adv & remote_adv;
4523 if (common & (ADVERTISE_1000XHALF |
4524 ADVERTISE_1000XFULL)) {
4525 if (common & ADVERTISE_1000XFULL)
4526 current_duplex = DUPLEX_FULL;
4527 else
4528 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004529 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004530 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004531 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004532 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004533 }
Michael Chan747e8f82005-07-25 12:33:22 -07004534 }
4535 }
4536
Matt Carlsonef167e22007-12-20 20:10:01 -08004537 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4538 tg3_setup_flow_control(tp, local_adv, remote_adv);
4539
Michael Chan747e8f82005-07-25 12:33:22 -07004540 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4541 if (tp->link_config.active_duplex == DUPLEX_HALF)
4542 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4543
4544 tw32_f(MAC_MODE, tp->mac_mode);
4545 udelay(40);
4546
4547 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4548
4549 tp->link_config.active_speed = current_speed;
4550 tp->link_config.active_duplex = current_duplex;
4551
4552 if (current_link_up != netif_carrier_ok(tp->dev)) {
4553 if (current_link_up)
4554 netif_carrier_on(tp->dev);
4555 else {
4556 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004557 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004558 }
4559 tg3_link_report(tp);
4560 }
4561 return err;
4562}
4563
4564static void tg3_serdes_parallel_detect(struct tg3 *tp)
4565{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004566 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004567 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004568 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004569 return;
4570 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004571
Michael Chan747e8f82005-07-25 12:33:22 -07004572 if (!netif_carrier_ok(tp->dev) &&
4573 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4574 u32 bmcr;
4575
4576 tg3_readphy(tp, MII_BMCR, &bmcr);
4577 if (bmcr & BMCR_ANENABLE) {
4578 u32 phy1, phy2;
4579
4580 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004581 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4582 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004583
4584 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004585 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4586 MII_TG3_DSP_EXP1_INT_STAT);
4587 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4588 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004589
4590 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4591 /* We have signal detect and not receiving
4592 * config code words, link is up by parallel
4593 * detection.
4594 */
4595
4596 bmcr &= ~BMCR_ANENABLE;
4597 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4598 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004599 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004600 }
4601 }
Matt Carlson859a5882010-04-05 10:19:28 +00004602 } else if (netif_carrier_ok(tp->dev) &&
4603 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004604 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004605 u32 phy2;
4606
4607 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004608 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4609 MII_TG3_DSP_EXP1_INT_STAT);
4610 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004611 if (phy2 & 0x20) {
4612 u32 bmcr;
4613
4614 /* Config code words received, turn on autoneg. */
4615 tg3_readphy(tp, MII_BMCR, &bmcr);
4616 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4617
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004618 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004619
4620 }
4621 }
4622}
4623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4625{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004626 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 int err;
4628
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004629 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004631 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004632 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00004633 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004636 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004637 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004638
4639 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4640 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4641 scale = 65;
4642 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4643 scale = 6;
4644 else
4645 scale = 12;
4646
4647 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4648 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4649 tw32(GRC_MISC_CFG, val);
4650 }
4651
Matt Carlsonf2096f92011-04-05 14:22:48 +00004652 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4653 (6 << TX_LENGTHS_IPG_SHIFT);
4654 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4655 val |= tr32(MAC_TX_LENGTHS) &
4656 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4657 TX_LENGTHS_CNT_DWN_VAL_MSK);
4658
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 if (tp->link_config.active_speed == SPEED_1000 &&
4660 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004661 tw32(MAC_TX_LENGTHS, val |
4662 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004664 tw32(MAC_TX_LENGTHS, val |
4665 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
Joe Perches63c3a662011-04-26 08:12:10 +00004667 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 if (netif_carrier_ok(tp->dev)) {
4669 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004670 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 } else {
4672 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4673 }
4674 }
4675
Joe Perches63c3a662011-04-26 08:12:10 +00004676 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004677 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004678 if (!netif_carrier_ok(tp->dev))
4679 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4680 tp->pwrmgmt_thresh;
4681 else
4682 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4683 tw32(PCIE_PWR_MGMT_THRESH, val);
4684 }
4685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 return err;
4687}
4688
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004689static inline int tg3_irq_sync(struct tg3 *tp)
4690{
4691 return tp->irq_sync;
4692}
4693
Matt Carlson97bd8e42011-04-13 11:05:04 +00004694static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4695{
4696 int i;
4697
4698 dst = (u32 *)((u8 *)dst + off);
4699 for (i = 0; i < len; i += sizeof(u32))
4700 *dst++ = tr32(off + i);
4701}
4702
4703static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4704{
4705 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4706 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4707 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4708 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4709 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4710 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4711 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4712 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4713 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4714 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4715 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4716 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4717 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4718 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4719 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4720 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4721 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4722 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4723 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4724
Joe Perches63c3a662011-04-26 08:12:10 +00004725 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004726 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4727
4728 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4729 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4730 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4731 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4732 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4733 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4734 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4735 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4736
Joe Perches63c3a662011-04-26 08:12:10 +00004737 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004738 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4739 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4740 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4741 }
4742
4743 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4744 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4745 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4746 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4747 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4748
Joe Perches63c3a662011-04-26 08:12:10 +00004749 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004750 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4751}
4752
4753static void tg3_dump_state(struct tg3 *tp)
4754{
4755 int i;
4756 u32 *regs;
4757
4758 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4759 if (!regs) {
4760 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4761 return;
4762 }
4763
Joe Perches63c3a662011-04-26 08:12:10 +00004764 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004765 /* Read up to but not including private PCI registers */
4766 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4767 regs[i / sizeof(u32)] = tr32(i);
4768 } else
4769 tg3_dump_legacy_regs(tp, regs);
4770
4771 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4772 if (!regs[i + 0] && !regs[i + 1] &&
4773 !regs[i + 2] && !regs[i + 3])
4774 continue;
4775
4776 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4777 i * 4,
4778 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4779 }
4780
4781 kfree(regs);
4782
4783 for (i = 0; i < tp->irq_cnt; i++) {
4784 struct tg3_napi *tnapi = &tp->napi[i];
4785
4786 /* SW status block */
4787 netdev_err(tp->dev,
4788 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4789 i,
4790 tnapi->hw_status->status,
4791 tnapi->hw_status->status_tag,
4792 tnapi->hw_status->rx_jumbo_consumer,
4793 tnapi->hw_status->rx_consumer,
4794 tnapi->hw_status->rx_mini_consumer,
4795 tnapi->hw_status->idx[0].rx_producer,
4796 tnapi->hw_status->idx[0].tx_consumer);
4797
4798 netdev_err(tp->dev,
4799 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4800 i,
4801 tnapi->last_tag, tnapi->last_irq_tag,
4802 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4803 tnapi->rx_rcb_ptr,
4804 tnapi->prodring.rx_std_prod_idx,
4805 tnapi->prodring.rx_std_cons_idx,
4806 tnapi->prodring.rx_jmb_prod_idx,
4807 tnapi->prodring.rx_jmb_cons_idx);
4808 }
4809}
4810
Michael Chandf3e6542006-05-26 17:48:07 -07004811/* This is called whenever we suspect that the system chipset is re-
4812 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4813 * is bogus tx completions. We try to recover by setting the
4814 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4815 * in the workqueue.
4816 */
4817static void tg3_tx_recover(struct tg3 *tp)
4818{
Joe Perches63c3a662011-04-26 08:12:10 +00004819 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07004820 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4821
Matt Carlson5129c3a2010-04-05 10:19:23 +00004822 netdev_warn(tp->dev,
4823 "The system may be re-ordering memory-mapped I/O "
4824 "cycles to the network device, attempting to recover. "
4825 "Please report the problem to the driver maintainer "
4826 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004827
4828 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00004829 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07004830 spin_unlock(&tp->lock);
4831}
4832
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004833static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004834{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004835 /* Tell compiler to fetch tx indices from memory. */
4836 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004837 return tnapi->tx_pending -
4838 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004839}
4840
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841/* Tigon3 never reports partial packet sends. So we do not
4842 * need special logic to handle SKBs that have not had all
4843 * of their frags sent yet, like SunGEM does.
4844 */
Matt Carlson17375d22009-08-28 14:02:18 +00004845static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846{
Matt Carlson17375d22009-08-28 14:02:18 +00004847 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004848 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004849 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004850 struct netdev_queue *txq;
4851 int index = tnapi - tp->napi;
4852
Joe Perches63c3a662011-04-26 08:12:10 +00004853 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004854 index--;
4855
4856 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
4858 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00004859 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004861 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
Michael Chandf3e6542006-05-26 17:48:07 -07004863 if (unlikely(skb == NULL)) {
4864 tg3_tx_recover(tp);
4865 return;
4866 }
4867
Alexander Duyckf4188d82009-12-02 16:48:38 +00004868 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004869 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004870 skb_headlen(skb),
4871 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
4873 ri->skb = NULL;
4874
Matt Carlsone01ee142011-07-27 14:20:50 +00004875 while (ri->fragmented) {
4876 ri->fragmented = false;
4877 sw_idx = NEXT_TX(sw_idx);
4878 ri = &tnapi->tx_buffers[sw_idx];
4879 }
4880
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 sw_idx = NEXT_TX(sw_idx);
4882
4883 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004884 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004885 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4886 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004887
4888 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004889 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004890 skb_shinfo(skb)->frags[i].size,
4891 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00004892
4893 while (ri->fragmented) {
4894 ri->fragmented = false;
4895 sw_idx = NEXT_TX(sw_idx);
4896 ri = &tnapi->tx_buffers[sw_idx];
4897 }
4898
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 sw_idx = NEXT_TX(sw_idx);
4900 }
4901
David S. Millerf47c11e2005-06-24 20:18:35 -07004902 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004903
4904 if (unlikely(tx_bug)) {
4905 tg3_tx_recover(tp);
4906 return;
4907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 }
4909
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004910 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
Michael Chan1b2a7202006-08-07 21:46:02 -07004912 /* Need to make the tx_cons update visible to tg3_start_xmit()
4913 * before checking for netif_queue_stopped(). Without the
4914 * memory barrier, there is a small possibility that tg3_start_xmit()
4915 * will miss it and cause the queue to be stopped forever.
4916 */
4917 smp_mb();
4918
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004919 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004920 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004921 __netif_tx_lock(txq, smp_processor_id());
4922 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004923 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004924 netif_tx_wake_queue(txq);
4925 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927}
4928
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004929static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4930{
4931 if (!ri->skb)
4932 return;
4933
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004934 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004935 map_sz, PCI_DMA_FROMDEVICE);
4936 dev_kfree_skb_any(ri->skb);
4937 ri->skb = NULL;
4938}
4939
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940/* Returns size of skb allocated or < 0 on error.
4941 *
4942 * We only need to fill in the address because the other members
4943 * of the RX descriptor are invariant, see tg3_init_rings.
4944 *
4945 * Note the purposeful assymetry of cpu vs. chip accesses. For
4946 * posting buffers we only dirty the first cache line of the RX
4947 * descriptor (containing the address). Whereas for the RX status
4948 * buffers the cpu only reads the last cacheline of the RX descriptor
4949 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4950 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004951static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004952 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953{
4954 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004955 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 struct sk_buff *skb;
4957 dma_addr_t mapping;
4958 int skb_size, dest_idx;
4959
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 switch (opaque_key) {
4961 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004962 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004963 desc = &tpr->rx_std[dest_idx];
4964 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004965 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 break;
4967
4968 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004969 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004970 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004971 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004972 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 break;
4974
4975 default:
4976 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978
4979 /* Do not overwrite any of the map or rp information
4980 * until we are sure we can commit to a new buffer.
4981 *
4982 * Callers depend upon this behavior and assume that
4983 * we leave everything unchanged if we fail.
4984 */
Matt Carlson287be122009-08-28 13:58:46 +00004985 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 if (skb == NULL)
4987 return -ENOMEM;
4988
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 skb_reserve(skb, tp->rx_offset);
4990
Matt Carlson287be122009-08-28 13:58:46 +00004991 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004993 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4994 dev_kfree_skb(skb);
4995 return -EIO;
4996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997
4998 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004999 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 desc->addr_hi = ((u64)mapping >> 32);
5002 desc->addr_lo = ((u64)mapping & 0xffffffff);
5003
5004 return skb_size;
5005}
5006
5007/* We only need to move over in the address because the other
5008 * members of the RX descriptor are invariant. See notes above
5009 * tg3_alloc_rx_skb for full details.
5010 */
Matt Carlsona3896162009-11-13 13:03:44 +00005011static void tg3_recycle_rx(struct tg3_napi *tnapi,
5012 struct tg3_rx_prodring_set *dpr,
5013 u32 opaque_key, int src_idx,
5014 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015{
Matt Carlson17375d22009-08-28 14:02:18 +00005016 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5018 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005019 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005020 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
5022 switch (opaque_key) {
5023 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005024 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005025 dest_desc = &dpr->rx_std[dest_idx];
5026 dest_map = &dpr->rx_std_buffers[dest_idx];
5027 src_desc = &spr->rx_std[src_idx];
5028 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 break;
5030
5031 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005032 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005033 dest_desc = &dpr->rx_jmb[dest_idx].std;
5034 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5035 src_desc = &spr->rx_jmb[src_idx].std;
5036 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 break;
5038
5039 default:
5040 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042
5043 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005044 dma_unmap_addr_set(dest_map, mapping,
5045 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 dest_desc->addr_hi = src_desc->addr_hi;
5047 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005048
5049 /* Ensure that the update to the skb happens after the physical
5050 * addresses have been transferred to the new BD location.
5051 */
5052 smp_wmb();
5053
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 src_map->skb = NULL;
5055}
5056
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057/* The RX ring scheme is composed of multiple rings which post fresh
5058 * buffers to the chip, and one special ring the chip uses to report
5059 * status back to the host.
5060 *
5061 * The special ring reports the status of received packets to the
5062 * host. The chip does not write into the original descriptor the
5063 * RX buffer was obtained from. The chip simply takes the original
5064 * descriptor as provided by the host, updates the status and length
5065 * field, then writes this into the next status ring entry.
5066 *
5067 * Each ring the host uses to post buffers to the chip is described
5068 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5069 * it is first placed into the on-chip ram. When the packet's length
5070 * is known, it walks down the TG3_BDINFO entries to select the ring.
5071 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5072 * which is within the range of the new packet's length is chosen.
5073 *
5074 * The "separate ring for rx status" scheme may sound queer, but it makes
5075 * sense from a cache coherency perspective. If only the host writes
5076 * to the buffer post rings, and only the chip writes to the rx status
5077 * rings, then cache lines never move beyond shared-modified state.
5078 * If both the host and chip were to write into the same ring, cache line
5079 * eviction could occur since both entities want it in an exclusive state.
5080 */
Matt Carlson17375d22009-08-28 14:02:18 +00005081static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082{
Matt Carlson17375d22009-08-28 14:02:18 +00005083 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005084 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005085 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005086 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005087 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005089 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005091 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 /*
5093 * We need to order the read of hw_idx and the read of
5094 * the opaque cookie.
5095 */
5096 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 work_mask = 0;
5098 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005099 std_prod_idx = tpr->rx_std_prod_idx;
5100 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005102 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005103 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 unsigned int len;
5105 struct sk_buff *skb;
5106 dma_addr_t dma_addr;
5107 u32 opaque_key, desc_idx, *post_ptr;
5108
5109 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5110 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5111 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005112 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005113 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00005114 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00005115 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005116 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005118 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005119 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00005120 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00005121 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005122 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124
5125 work_mask |= opaque_key;
5126
5127 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5128 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5129 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005130 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 desc_idx, *post_ptr);
5132 drop_it_no_recycle:
5133 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005134 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 goto next_pkt;
5136 }
5137
Matt Carlsonad829262008-11-21 17:16:16 -08005138 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5139 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
Matt Carlsond2757fc2010-04-12 06:58:27 +00005141 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 int skb_size;
5143
Matt Carlson86b21e52009-11-13 13:03:45 +00005144 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005145 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 if (skb_size < 0)
5147 goto drop_it;
5148
Matt Carlson287be122009-08-28 13:58:46 +00005149 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 PCI_DMA_FROMDEVICE);
5151
Matt Carlson61e800c2010-02-17 15:16:54 +00005152 /* Ensure that the update to the skb happens
5153 * after the usage of the old DMA mapping.
5154 */
5155 smp_wmb();
5156
5157 ri->skb = NULL;
5158
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 skb_put(skb, len);
5160 } else {
5161 struct sk_buff *copy_skb;
5162
Matt Carlsona3896162009-11-13 13:03:44 +00005163 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 desc_idx, *post_ptr);
5165
Matt Carlsonbf933c82011-01-25 15:58:49 +00005166 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00005167 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 if (copy_skb == NULL)
5169 goto drop_it_no_recycle;
5170
Matt Carlsonbf933c82011-01-25 15:58:49 +00005171 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 skb_put(copy_skb, len);
5173 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03005174 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
5176
5177 /* We'll reuse the original ring buffer. */
5178 skb = copy_skb;
5179 }
5180
Michał Mirosławdc668912011-04-07 03:35:07 +00005181 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5183 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5184 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5185 skb->ip_summed = CHECKSUM_UNNECESSARY;
5186 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005187 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
5189 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005190
5191 if (len > (tp->dev->mtu + ETH_HLEN) &&
5192 skb->protocol != htons(ETH_P_8021Q)) {
5193 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005194 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005195 }
5196
Matt Carlson9dc7a112010-04-12 06:58:28 +00005197 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005198 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5199 __vlan_hwaccel_put_tag(skb,
5200 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005201
Matt Carlsonbf933c82011-01-25 15:58:49 +00005202 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 received++;
5205 budget--;
5206
5207next_pkt:
5208 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005209
5210 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005211 tpr->rx_std_prod_idx = std_prod_idx &
5212 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005213 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5214 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005215 work_mask &= ~RXD_OPAQUE_RING_STD;
5216 rx_std_posted = 0;
5217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005219 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005220 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005221
5222 /* Refresh hw_idx to see if there is new work */
5223 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005224 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005225 rmb();
5226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 }
5228
5229 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005230 tnapi->rx_rcb_ptr = sw_idx;
5231 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232
5233 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005234 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005235 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005236 tpr->rx_std_prod_idx = std_prod_idx &
5237 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005238 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5239 tpr->rx_std_prod_idx);
5240 }
5241 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005242 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5243 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005244 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5245 tpr->rx_jmb_prod_idx);
5246 }
5247 mmiowb();
5248 } else if (work_mask) {
5249 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5250 * updated before the producer indices can be updated.
5251 */
5252 smp_wmb();
5253
Matt Carlson2c49a442010-09-30 10:34:35 +00005254 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5255 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005256
Matt Carlsone4af1af2010-02-12 14:47:05 +00005257 if (tnapi != &tp->napi[1])
5258 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260
5261 return received;
5262}
5263
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005264static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005267 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005268 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5269
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 if (sblk->status & SD_STATUS_LINK_CHG) {
5271 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005272 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005273 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005274 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005275 tw32_f(MAC_STATUS,
5276 (MAC_STATUS_SYNC_CHANGED |
5277 MAC_STATUS_CFG_CHANGED |
5278 MAC_STATUS_MI_COMPLETION |
5279 MAC_STATUS_LNKSTATE_CHANGED));
5280 udelay(40);
5281 } else
5282 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005283 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 }
5285 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005286}
5287
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005288static int tg3_rx_prodring_xfer(struct tg3 *tp,
5289 struct tg3_rx_prodring_set *dpr,
5290 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005291{
5292 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005293 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005294
5295 while (1) {
5296 src_prod_idx = spr->rx_std_prod_idx;
5297
5298 /* Make sure updates to the rx_std_buffers[] entries and the
5299 * standard producer index are seen in the correct order.
5300 */
5301 smp_rmb();
5302
5303 if (spr->rx_std_cons_idx == src_prod_idx)
5304 break;
5305
5306 if (spr->rx_std_cons_idx < src_prod_idx)
5307 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5308 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005309 cpycnt = tp->rx_std_ring_mask + 1 -
5310 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005311
Matt Carlson2c49a442010-09-30 10:34:35 +00005312 cpycnt = min(cpycnt,
5313 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005314
5315 si = spr->rx_std_cons_idx;
5316 di = dpr->rx_std_prod_idx;
5317
Matt Carlsone92967b2010-02-12 14:47:06 +00005318 for (i = di; i < di + cpycnt; i++) {
5319 if (dpr->rx_std_buffers[i].skb) {
5320 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005321 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005322 break;
5323 }
5324 }
5325
5326 if (!cpycnt)
5327 break;
5328
5329 /* Ensure that updates to the rx_std_buffers ring and the
5330 * shadowed hardware producer ring from tg3_recycle_skb() are
5331 * ordered correctly WRT the skb check above.
5332 */
5333 smp_rmb();
5334
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005335 memcpy(&dpr->rx_std_buffers[di],
5336 &spr->rx_std_buffers[si],
5337 cpycnt * sizeof(struct ring_info));
5338
5339 for (i = 0; i < cpycnt; i++, di++, si++) {
5340 struct tg3_rx_buffer_desc *sbd, *dbd;
5341 sbd = &spr->rx_std[si];
5342 dbd = &dpr->rx_std[di];
5343 dbd->addr_hi = sbd->addr_hi;
5344 dbd->addr_lo = sbd->addr_lo;
5345 }
5346
Matt Carlson2c49a442010-09-30 10:34:35 +00005347 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5348 tp->rx_std_ring_mask;
5349 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5350 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005351 }
5352
5353 while (1) {
5354 src_prod_idx = spr->rx_jmb_prod_idx;
5355
5356 /* Make sure updates to the rx_jmb_buffers[] entries and
5357 * the jumbo producer index are seen in the correct order.
5358 */
5359 smp_rmb();
5360
5361 if (spr->rx_jmb_cons_idx == src_prod_idx)
5362 break;
5363
5364 if (spr->rx_jmb_cons_idx < src_prod_idx)
5365 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5366 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005367 cpycnt = tp->rx_jmb_ring_mask + 1 -
5368 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005369
5370 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005371 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005372
5373 si = spr->rx_jmb_cons_idx;
5374 di = dpr->rx_jmb_prod_idx;
5375
Matt Carlsone92967b2010-02-12 14:47:06 +00005376 for (i = di; i < di + cpycnt; i++) {
5377 if (dpr->rx_jmb_buffers[i].skb) {
5378 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005379 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005380 break;
5381 }
5382 }
5383
5384 if (!cpycnt)
5385 break;
5386
5387 /* Ensure that updates to the rx_jmb_buffers ring and the
5388 * shadowed hardware producer ring from tg3_recycle_skb() are
5389 * ordered correctly WRT the skb check above.
5390 */
5391 smp_rmb();
5392
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005393 memcpy(&dpr->rx_jmb_buffers[di],
5394 &spr->rx_jmb_buffers[si],
5395 cpycnt * sizeof(struct ring_info));
5396
5397 for (i = 0; i < cpycnt; i++, di++, si++) {
5398 struct tg3_rx_buffer_desc *sbd, *dbd;
5399 sbd = &spr->rx_jmb[si].std;
5400 dbd = &dpr->rx_jmb[di].std;
5401 dbd->addr_hi = sbd->addr_hi;
5402 dbd->addr_lo = sbd->addr_lo;
5403 }
5404
Matt Carlson2c49a442010-09-30 10:34:35 +00005405 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5406 tp->rx_jmb_ring_mask;
5407 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5408 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005409 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005410
5411 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005412}
5413
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005414static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5415{
5416 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
5418 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005419 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005420 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005421 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005422 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 }
5424
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 /* run RX thread, within the bounds set by NAPI.
5426 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005427 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005429 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005430 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431
Joe Perches63c3a662011-04-26 08:12:10 +00005432 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005433 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005434 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005435 u32 std_prod_idx = dpr->rx_std_prod_idx;
5436 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005437
Matt Carlsone4af1af2010-02-12 14:47:05 +00005438 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005439 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005440 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005441
5442 wmb();
5443
Matt Carlsone4af1af2010-02-12 14:47:05 +00005444 if (std_prod_idx != dpr->rx_std_prod_idx)
5445 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5446 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005447
Matt Carlsone4af1af2010-02-12 14:47:05 +00005448 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5449 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5450 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005451
5452 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005453
5454 if (err)
5455 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005456 }
5457
David S. Miller6f535762007-10-11 18:08:29 -07005458 return work_done;
5459}
David S. Millerf7383c22005-05-18 22:50:53 -07005460
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005461static int tg3_poll_msix(struct napi_struct *napi, int budget)
5462{
5463 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5464 struct tg3 *tp = tnapi->tp;
5465 int work_done = 0;
5466 struct tg3_hw_status *sblk = tnapi->hw_status;
5467
5468 while (1) {
5469 work_done = tg3_poll_work(tnapi, work_done, budget);
5470
Joe Perches63c3a662011-04-26 08:12:10 +00005471 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005472 goto tx_recovery;
5473
5474 if (unlikely(work_done >= budget))
5475 break;
5476
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005477 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005478 * to tell the hw how much work has been processed,
5479 * so we must read it before checking for more work.
5480 */
5481 tnapi->last_tag = sblk->status_tag;
5482 tnapi->last_irq_tag = tnapi->last_tag;
5483 rmb();
5484
5485 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005486 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5487 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005488 napi_complete(napi);
5489 /* Reenable interrupts. */
5490 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5491 mmiowb();
5492 break;
5493 }
5494 }
5495
5496 return work_done;
5497
5498tx_recovery:
5499 /* work_done is guaranteed to be less than budget. */
5500 napi_complete(napi);
5501 schedule_work(&tp->reset_task);
5502 return work_done;
5503}
5504
Matt Carlsone64de4e2011-04-13 11:05:05 +00005505static void tg3_process_error(struct tg3 *tp)
5506{
5507 u32 val;
5508 bool real_error = false;
5509
Joe Perches63c3a662011-04-26 08:12:10 +00005510 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005511 return;
5512
5513 /* Check Flow Attention register */
5514 val = tr32(HOSTCC_FLOW_ATTN);
5515 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5516 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5517 real_error = true;
5518 }
5519
5520 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5521 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5522 real_error = true;
5523 }
5524
5525 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5526 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5527 real_error = true;
5528 }
5529
5530 if (!real_error)
5531 return;
5532
5533 tg3_dump_state(tp);
5534
Joe Perches63c3a662011-04-26 08:12:10 +00005535 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005536 schedule_work(&tp->reset_task);
5537}
5538
David S. Miller6f535762007-10-11 18:08:29 -07005539static int tg3_poll(struct napi_struct *napi, int budget)
5540{
Matt Carlson8ef04422009-08-28 14:01:37 +00005541 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5542 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005543 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005544 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005545
5546 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005547 if (sblk->status & SD_STATUS_ERROR)
5548 tg3_process_error(tp);
5549
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005550 tg3_poll_link(tp);
5551
Matt Carlson17375d22009-08-28 14:02:18 +00005552 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005553
Joe Perches63c3a662011-04-26 08:12:10 +00005554 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07005555 goto tx_recovery;
5556
5557 if (unlikely(work_done >= budget))
5558 break;
5559
Joe Perches63c3a662011-04-26 08:12:10 +00005560 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00005561 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005562 * to tell the hw how much work has been processed,
5563 * so we must read it before checking for more work.
5564 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005565 tnapi->last_tag = sblk->status_tag;
5566 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005567 rmb();
5568 } else
5569 sblk->status &= ~SD_STATUS_UPDATED;
5570
Matt Carlson17375d22009-08-28 14:02:18 +00005571 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005572 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005573 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005574 break;
5575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 }
5577
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005578 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005579
5580tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005581 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005582 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005583 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005584 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585}
5586
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005587static void tg3_napi_disable(struct tg3 *tp)
5588{
5589 int i;
5590
5591 for (i = tp->irq_cnt - 1; i >= 0; i--)
5592 napi_disable(&tp->napi[i].napi);
5593}
5594
5595static void tg3_napi_enable(struct tg3 *tp)
5596{
5597 int i;
5598
5599 for (i = 0; i < tp->irq_cnt; i++)
5600 napi_enable(&tp->napi[i].napi);
5601}
5602
5603static void tg3_napi_init(struct tg3 *tp)
5604{
5605 int i;
5606
5607 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5608 for (i = 1; i < tp->irq_cnt; i++)
5609 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5610}
5611
5612static void tg3_napi_fini(struct tg3 *tp)
5613{
5614 int i;
5615
5616 for (i = 0; i < tp->irq_cnt; i++)
5617 netif_napi_del(&tp->napi[i].napi);
5618}
5619
5620static inline void tg3_netif_stop(struct tg3 *tp)
5621{
5622 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5623 tg3_napi_disable(tp);
5624 netif_tx_disable(tp->dev);
5625}
5626
5627static inline void tg3_netif_start(struct tg3 *tp)
5628{
5629 /* NOTE: unconditional netif_tx_wake_all_queues is only
5630 * appropriate so long as all callers are assured to
5631 * have free tx slots (such as after tg3_init_hw)
5632 */
5633 netif_tx_wake_all_queues(tp->dev);
5634
5635 tg3_napi_enable(tp);
5636 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5637 tg3_enable_ints(tp);
5638}
5639
David S. Millerf47c11e2005-06-24 20:18:35 -07005640static void tg3_irq_quiesce(struct tg3 *tp)
5641{
Matt Carlson4f125f42009-09-01 12:55:02 +00005642 int i;
5643
David S. Millerf47c11e2005-06-24 20:18:35 -07005644 BUG_ON(tp->irq_sync);
5645
5646 tp->irq_sync = 1;
5647 smp_mb();
5648
Matt Carlson4f125f42009-09-01 12:55:02 +00005649 for (i = 0; i < tp->irq_cnt; i++)
5650 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005651}
5652
David S. Millerf47c11e2005-06-24 20:18:35 -07005653/* Fully shutdown all tg3 driver activity elsewhere in the system.
5654 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5655 * with as well. Most of the time, this is not necessary except when
5656 * shutting down the device.
5657 */
5658static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5659{
Michael Chan46966542007-07-11 19:47:19 -07005660 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005661 if (irq_sync)
5662 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005663}
5664
5665static inline void tg3_full_unlock(struct tg3 *tp)
5666{
David S. Millerf47c11e2005-06-24 20:18:35 -07005667 spin_unlock_bh(&tp->lock);
5668}
5669
Michael Chanfcfa0a32006-03-20 22:28:41 -08005670/* One-shot MSI handler - Chip automatically disables interrupt
5671 * after sending MSI so driver doesn't have to do it.
5672 */
David Howells7d12e782006-10-05 14:55:46 +01005673static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005674{
Matt Carlson09943a12009-08-28 14:01:57 +00005675 struct tg3_napi *tnapi = dev_id;
5676 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005677
Matt Carlson898a56f2009-08-28 14:02:40 +00005678 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005679 if (tnapi->rx_rcb)
5680 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005681
5682 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005683 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005684
5685 return IRQ_HANDLED;
5686}
5687
Michael Chan88b06bc2005-04-21 17:13:25 -07005688/* MSI ISR - No need to check for interrupt sharing and no need to
5689 * flush status block and interrupt mailbox. PCI ordering rules
5690 * guarantee that MSI will arrive after the status block.
5691 */
David Howells7d12e782006-10-05 14:55:46 +01005692static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07005693{
Matt Carlson09943a12009-08-28 14:01:57 +00005694 struct tg3_napi *tnapi = dev_id;
5695 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07005696
Matt Carlson898a56f2009-08-28 14:02:40 +00005697 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005698 if (tnapi->rx_rcb)
5699 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07005700 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005701 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07005702 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005703 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07005704 * NIC to stop sending us irqs, engaging "in-intr-handler"
5705 * event coalescing.
5706 */
5707 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005708 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005709 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005710
Michael Chan88b06bc2005-04-21 17:13:25 -07005711 return IRQ_RETVAL(1);
5712}
5713
David Howells7d12e782006-10-05 14:55:46 +01005714static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715{
Matt Carlson09943a12009-08-28 14:01:57 +00005716 struct tg3_napi *tnapi = dev_id;
5717 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005718 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 unsigned int handled = 1;
5720
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 /* In INTx mode, it is possible for the interrupt to arrive at
5722 * the CPU before the status block posted prior to the interrupt.
5723 * Reading the PCI State register will confirm whether the
5724 * interrupt is ours and will flush the status block.
5725 */
Michael Chand18edcb2007-03-24 20:57:11 -07005726 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00005727 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005728 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5729 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005730 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005731 }
Michael Chand18edcb2007-03-24 20:57:11 -07005732 }
5733
5734 /*
5735 * Writing any value to intr-mbox-0 clears PCI INTA# and
5736 * chip-internal interrupt pending events.
5737 * Writing non-zero to intr-mbox-0 additional tells the
5738 * NIC to stop sending us irqs, engaging "in-intr-handler"
5739 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005740 *
5741 * Flush the mailbox to de-assert the IRQ immediately to prevent
5742 * spurious interrupts. The flush impacts performance but
5743 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005744 */
Michael Chanc04cb342007-05-07 00:26:15 -07005745 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005746 if (tg3_irq_sync(tp))
5747 goto out;
5748 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005749 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005750 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005751 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005752 } else {
5753 /* No work, shared interrupt perhaps? re-enable
5754 * interrupts, and flush that PCI write
5755 */
5756 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5757 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005758 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005759out:
David S. Millerfac9b832005-05-18 22:46:34 -07005760 return IRQ_RETVAL(handled);
5761}
5762
David Howells7d12e782006-10-05 14:55:46 +01005763static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005764{
Matt Carlson09943a12009-08-28 14:01:57 +00005765 struct tg3_napi *tnapi = dev_id;
5766 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005767 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005768 unsigned int handled = 1;
5769
David S. Millerfac9b832005-05-18 22:46:34 -07005770 /* In INTx mode, it is possible for the interrupt to arrive at
5771 * the CPU before the status block posted prior to the interrupt.
5772 * Reading the PCI State register will confirm whether the
5773 * interrupt is ours and will flush the status block.
5774 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005775 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00005776 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005777 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5778 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005779 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 }
Michael Chand18edcb2007-03-24 20:57:11 -07005781 }
5782
5783 /*
5784 * writing any value to intr-mbox-0 clears PCI INTA# and
5785 * chip-internal interrupt pending events.
5786 * writing non-zero to intr-mbox-0 additional tells the
5787 * NIC to stop sending us irqs, engaging "in-intr-handler"
5788 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005789 *
5790 * Flush the mailbox to de-assert the IRQ immediately to prevent
5791 * spurious interrupts. The flush impacts performance but
5792 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005793 */
Michael Chanc04cb342007-05-07 00:26:15 -07005794 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005795
5796 /*
5797 * In a shared interrupt configuration, sometimes other devices'
5798 * interrupts will scream. We record the current status tag here
5799 * so that the above check can report that the screaming interrupts
5800 * are unhandled. Eventually they will be silenced.
5801 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005802 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005803
Michael Chand18edcb2007-03-24 20:57:11 -07005804 if (tg3_irq_sync(tp))
5805 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005806
Matt Carlson72334482009-08-28 14:03:01 +00005807 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005808
Matt Carlson09943a12009-08-28 14:01:57 +00005809 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005810
David S. Millerf47c11e2005-06-24 20:18:35 -07005811out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 return IRQ_RETVAL(handled);
5813}
5814
Michael Chan79381092005-04-21 17:13:59 -07005815/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005816static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005817{
Matt Carlson09943a12009-08-28 14:01:57 +00005818 struct tg3_napi *tnapi = dev_id;
5819 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005820 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005821
Michael Chanf9804dd2005-09-27 12:13:10 -07005822 if ((sblk->status & SD_STATUS_UPDATED) ||
5823 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005824 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005825 return IRQ_RETVAL(1);
5826 }
5827 return IRQ_RETVAL(0);
5828}
5829
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005830static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005831static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
Michael Chanb9ec6c12006-07-25 16:37:27 -07005833/* Restart hardware after configuration changes, self-test, etc.
5834 * Invoked with tp->lock held.
5835 */
5836static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005837 __releases(tp->lock)
5838 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005839{
5840 int err;
5841
5842 err = tg3_init_hw(tp, reset_phy);
5843 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005844 netdev_err(tp->dev,
5845 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005846 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5847 tg3_full_unlock(tp);
5848 del_timer_sync(&tp->timer);
5849 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005850 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005851 dev_close(tp->dev);
5852 tg3_full_lock(tp, 0);
5853 }
5854 return err;
5855}
5856
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857#ifdef CONFIG_NET_POLL_CONTROLLER
5858static void tg3_poll_controller(struct net_device *dev)
5859{
Matt Carlson4f125f42009-09-01 12:55:02 +00005860 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07005861 struct tg3 *tp = netdev_priv(dev);
5862
Matt Carlson4f125f42009-09-01 12:55:02 +00005863 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005864 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865}
5866#endif
5867
David Howellsc4028952006-11-22 14:57:56 +00005868static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869{
David Howellsc4028952006-11-22 14:57:56 +00005870 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005871 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 unsigned int restart_timer;
5873
Michael Chan7faa0062006-02-02 17:29:28 -08005874 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005875
5876 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005877 tg3_full_unlock(tp);
5878 return;
5879 }
5880
5881 tg3_full_unlock(tp);
5882
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005883 tg3_phy_stop(tp);
5884
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 tg3_netif_stop(tp);
5886
David S. Millerf47c11e2005-06-24 20:18:35 -07005887 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888
Joe Perches63c3a662011-04-26 08:12:10 +00005889 restart_timer = tg3_flag(tp, RESTART_TIMER);
5890 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
Joe Perches63c3a662011-04-26 08:12:10 +00005892 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07005893 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5894 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00005895 tg3_flag_set(tp, MBOX_WRITE_REORDER);
5896 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005897 }
5898
Michael Chan944d9802005-05-29 14:57:48 -07005899 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005900 err = tg3_init_hw(tp, 1);
5901 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005902 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
5904 tg3_netif_start(tp);
5905
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 if (restart_timer)
5907 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005908
Michael Chanb9ec6c12006-07-25 16:37:27 -07005909out:
Michael Chan7faa0062006-02-02 17:29:28 -08005910 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005911
5912 if (!err)
5913 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914}
5915
5916static void tg3_tx_timeout(struct net_device *dev)
5917{
5918 struct tg3 *tp = netdev_priv(dev);
5919
Michael Chanb0408752007-02-13 12:18:30 -08005920 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005921 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005922 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924
5925 schedule_work(&tp->reset_task);
5926}
5927
Michael Chanc58ec932005-09-17 00:46:27 -07005928/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5929static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5930{
5931 u32 base = (u32) mapping & 0xffffffff;
5932
Eric Dumazet807540b2010-09-23 05:40:09 +00005933 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005934}
5935
Michael Chan72f2afb2006-03-06 19:28:35 -08005936/* Test for DMA addresses > 40-bit */
5937static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5938 int len)
5939{
5940#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00005941 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00005942 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005943 return 0;
5944#else
5945 return 0;
5946#endif
5947}
5948
Matt Carlsond1a3b732011-07-27 14:20:51 +00005949static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00005950 dma_addr_t mapping, u32 len, u32 flags,
5951 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00005952{
Matt Carlson92cd3a12011-07-27 14:20:47 +00005953 txbd->addr_hi = ((u64) mapping >> 32);
5954 txbd->addr_lo = ((u64) mapping & 0xffffffff);
5955 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
5956 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00005957}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958
Matt Carlson84b67b22011-07-27 14:20:52 +00005959static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00005960 dma_addr_t map, u32 len, u32 flags,
5961 u32 mss, u32 vlan)
5962{
5963 struct tg3 *tp = tnapi->tp;
5964 bool hwbug = false;
5965
5966 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
5967 hwbug = 1;
5968
5969 if (tg3_4g_overflow_test(map, len))
5970 hwbug = 1;
5971
5972 if (tg3_40bit_overflow_test(tp, map, len))
5973 hwbug = 1;
5974
Matt Carlsone31aa982011-07-27 14:20:53 +00005975 if (tg3_flag(tp, 4K_FIFO_LIMIT)) {
5976 u32 tmp_flag = flags & ~TXD_FLAG_END;
5977 while (len > TG3_TX_BD_DMA_MAX) {
5978 u32 frag_len = TG3_TX_BD_DMA_MAX;
5979 len -= TG3_TX_BD_DMA_MAX;
5980
5981 if (len) {
5982 tnapi->tx_buffers[*entry].fragmented = true;
5983 /* Avoid the 8byte DMA problem */
5984 if (len <= 8) {
5985 len += TG3_TX_BD_DMA_MAX / 2;
5986 frag_len = TG3_TX_BD_DMA_MAX / 2;
5987 }
5988 } else
5989 tmp_flag = flags;
5990
5991 if (*budget) {
5992 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
5993 frag_len, tmp_flag, mss, vlan);
5994 (*budget)--;
5995 *entry = NEXT_TX(*entry);
5996 } else {
5997 hwbug = 1;
5998 break;
5999 }
6000
6001 map += frag_len;
6002 }
6003
6004 if (len) {
6005 if (*budget) {
6006 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6007 len, flags, mss, vlan);
6008 (*budget)--;
6009 *entry = NEXT_TX(*entry);
6010 } else {
6011 hwbug = 1;
6012 }
6013 }
6014 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006015 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6016 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006017 *entry = NEXT_TX(*entry);
6018 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006019
6020 return hwbug;
6021}
6022
Matt Carlson0d681b22011-07-27 14:20:49 +00006023static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006024{
6025 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006026 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006027 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006028
Matt Carlson0d681b22011-07-27 14:20:49 +00006029 skb = txb->skb;
6030 txb->skb = NULL;
6031
Matt Carlson432aa7e2011-05-19 12:12:45 +00006032 pci_unmap_single(tnapi->tp->pdev,
6033 dma_unmap_addr(txb, mapping),
6034 skb_headlen(skb),
6035 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006036
6037 while (txb->fragmented) {
6038 txb->fragmented = false;
6039 entry = NEXT_TX(entry);
6040 txb = &tnapi->tx_buffers[entry];
6041 }
6042
Matt Carlson9a2e0fb2011-06-02 13:01:39 +00006043 for (i = 0; i < last; i++) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00006044 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6045
6046 entry = NEXT_TX(entry);
6047 txb = &tnapi->tx_buffers[entry];
6048
6049 pci_unmap_page(tnapi->tp->pdev,
6050 dma_unmap_addr(txb, mapping),
6051 frag->size, PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006052
6053 while (txb->fragmented) {
6054 txb->fragmented = false;
6055 entry = NEXT_TX(entry);
6056 txb = &tnapi->tx_buffers[entry];
6057 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006058 }
6059}
6060
Michael Chan72f2afb2006-03-06 19:28:35 -08006061/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006062static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
Matt Carlson432aa7e2011-05-19 12:12:45 +00006063 struct sk_buff *skb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006064 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006065 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006067 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07006068 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006069 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006070 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
Matt Carlson41588ba2008-04-19 18:12:33 -07006072 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6073 new_skb = skb_copy(skb, GFP_ATOMIC);
6074 else {
6075 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6076
6077 new_skb = skb_copy_expand(skb,
6078 skb_headroom(skb) + more_headroom,
6079 skb_tailroom(skb), GFP_ATOMIC);
6080 }
6081
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006083 ret = -1;
6084 } else {
6085 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006086 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6087 PCI_DMA_TODEVICE);
6088 /* Make sure the mapping succeeded */
6089 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006090 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006091 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006092 } else {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006093 base_flags |= TXD_FLAG_END;
6094
Matt Carlson84b67b22011-07-27 14:20:52 +00006095 tnapi->tx_buffers[*entry].skb = new_skb;
6096 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006097 mapping, new_addr);
6098
Matt Carlson84b67b22011-07-27 14:20:52 +00006099 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006100 new_skb->len, base_flags,
6101 mss, vlan)) {
Matt Carlson84b67b22011-07-27 14:20:52 +00006102 tg3_tx_skb_unmap(tnapi, *entry, 0);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006103 dev_kfree_skb(new_skb);
6104 ret = -1;
6105 }
Michael Chanc58ec932005-09-17 00:46:27 -07006106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 }
6108
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 dev_kfree_skb(skb);
6110
Michael Chanc58ec932005-09-17 00:46:27 -07006111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112}
6113
Matt Carlson2ffcc982011-05-19 12:12:44 +00006114static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006115
6116/* Use GSO to workaround a rare TSO bug that may be triggered when the
6117 * TSO header is greater than 80 bytes.
6118 */
6119static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6120{
6121 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006122 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006123
6124 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006125 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006126 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006127
6128 /* netif_tx_stop_queue() must be done before checking
6129 * checking tx index in tg3_tx_avail() below, because in
6130 * tg3_tx(), we update tx index before checking for
6131 * netif_tx_queue_stopped().
6132 */
6133 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006134 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006135 return NETDEV_TX_BUSY;
6136
6137 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006138 }
6139
6140 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006141 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006142 goto tg3_tso_bug_end;
6143
6144 do {
6145 nskb = segs;
6146 segs = segs->next;
6147 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006148 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006149 } while (segs);
6150
6151tg3_tso_bug_end:
6152 dev_kfree_skb(skb);
6153
6154 return NETDEV_TX_OK;
6155}
Michael Chan52c0fd82006-06-29 20:15:54 -07006156
Michael Chan5a6f3072006-03-20 22:28:05 -08006157/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006158 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006159 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006160static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006161{
6162 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006163 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006164 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006165 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006166 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006167 struct tg3_napi *tnapi;
6168 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006169 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006170
Matt Carlson24f4efd2009-11-13 13:03:35 +00006171 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6172 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006173 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006174 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175
Matt Carlson84b67b22011-07-27 14:20:52 +00006176 budget = tg3_tx_avail(tnapi);
6177
Michael Chan00b70502006-06-17 21:58:45 -07006178 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006179 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006180 * interrupt. Furthermore, IRQ processing runs lockless so we have
6181 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006183 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006184 if (!netif_tx_queue_stopped(txq)) {
6185 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006186
6187 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006188 netdev_err(dev,
6189 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 return NETDEV_TX_BUSY;
6192 }
6193
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006194 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006196 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006198
Matt Carlsonbe98da62010-07-11 09:31:46 +00006199 mss = skb_shinfo(skb)->gso_size;
6200 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006201 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006202 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203
6204 if (skb_header_cloned(skb) &&
6205 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
6206 dev_kfree_skb(skb);
6207 goto out_unlock;
6208 }
6209
Matt Carlson34195c32010-07-11 09:31:42 +00006210 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006211 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212
Matt Carlson02e96082010-09-15 08:59:59 +00006213 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006214 hdr_len = skb_headlen(skb) - ETH_HLEN;
6215 } else {
6216 u32 ip_tcp_len;
6217
6218 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6219 hdr_len = ip_tcp_len + tcp_opt_len;
6220
6221 iph->check = 0;
6222 iph->tot_len = htons(mss + hdr_len);
6223 }
6224
Michael Chan52c0fd82006-06-29 20:15:54 -07006225 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006226 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006227 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006228
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6230 TXD_FLAG_CPU_POST_DMA);
6231
Joe Perches63c3a662011-04-26 08:12:10 +00006232 if (tg3_flag(tp, HW_TSO_1) ||
6233 tg3_flag(tp, HW_TSO_2) ||
6234 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006235 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006237 } else
6238 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6239 iph->daddr, 0,
6240 IPPROTO_TCP,
6241 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242
Joe Perches63c3a662011-04-26 08:12:10 +00006243 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006244 mss |= (hdr_len & 0xc) << 12;
6245 if (hdr_len & 0x10)
6246 base_flags |= 0x00000010;
6247 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006248 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006249 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006250 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006251 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006252 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 int tsflags;
6254
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006255 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 mss |= (tsflags << 11);
6257 }
6258 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006259 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 int tsflags;
6261
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006262 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 base_flags |= tsflags << 12;
6264 }
6265 }
6266 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006267
Matt Carlson92cd3a12011-07-27 14:20:47 +00006268#ifdef BCM_KERNEL_SUPPORTS_8021Q
6269 if (vlan_tx_tag_present(skb)) {
6270 base_flags |= TXD_FLAG_VLAN;
6271 vlan = vlan_tx_tag_get(skb);
6272 }
6273#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274
Joe Perches63c3a662011-04-26 08:12:10 +00006275 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006276 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006277 base_flags |= TXD_FLAG_JMB_PKT;
6278
Alexander Duyckf4188d82009-12-02 16:48:38 +00006279 len = skb_headlen(skb);
6280
6281 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6282 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006283 dev_kfree_skb(skb);
6284 goto out_unlock;
6285 }
6286
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006287 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006288 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289
6290 would_hit_hwbug = 0;
6291
Joe Perches63c3a662011-04-26 08:12:10 +00006292 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006293 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
Matt Carlson84b67b22011-07-27 14:20:52 +00006295 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006296 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
6297 mss, vlan))
6298 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 /* Now loop through additional data fragments, and queue them. */
6301 if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006302 u32 tmp_mss = mss;
6303
6304 if (!tg3_flag(tp, HW_TSO_1) &&
6305 !tg3_flag(tp, HW_TSO_2) &&
6306 !tg3_flag(tp, HW_TSO_3))
6307 tmp_mss = 0;
6308
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 last = skb_shinfo(skb)->nr_frags - 1;
6310 for (i = 0; i <= last; i++) {
6311 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6312
6313 len = frag->size;
Ian Campbelldc234d02011-08-24 22:28:11 +00006314 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
6315 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006317 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006318 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006319 mapping);
6320 if (pci_dma_mapping_error(tp->pdev, mapping))
6321 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322
Matt Carlson84b67b22011-07-27 14:20:52 +00006323 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
6324 len, base_flags |
6325 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsond1a3b732011-07-27 14:20:51 +00006326 tmp_mss, vlan))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006327 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 }
6329 }
6330
6331 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006332 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333
6334 /* If the workaround fails due to memory/mapping
6335 * failure, silently drop this packet.
6336 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006337 entry = tnapi->tx_prod;
6338 budget = tg3_tx_avail(tnapi);
6339 if (tigon3_dma_hwbug_workaround(tnapi, skb, &entry, &budget,
6340 base_flags, mss, vlan))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 }
6343
Richard Cochrand515b452011-06-19 03:31:41 +00006344 skb_tx_timestamp(skb);
6345
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006347 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006349 tnapi->tx_prod = entry;
6350 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006351 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006352
6353 /* netif_tx_stop_queue() must be done before checking
6354 * checking tx index in tg3_tx_avail() below, because in
6355 * tg3_tx(), we update tx index before checking for
6356 * netif_tx_queue_stopped().
6357 */
6358 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006359 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006360 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362
6363out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006364 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365
6366 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006367
6368dma_error:
Matt Carlson0d681b22011-07-27 14:20:49 +00006369 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Alexander Duyckf4188d82009-12-02 16:48:38 +00006370 dev_kfree_skb(skb);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006371 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006372 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373}
6374
Matt Carlson6e01b202011-08-19 13:58:20 +00006375static void tg3_mac_loopback(struct tg3 *tp, bool enable)
6376{
6377 if (enable) {
6378 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
6379 MAC_MODE_PORT_MODE_MASK);
6380
6381 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6382
6383 if (!tg3_flag(tp, 5705_PLUS))
6384 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
6385
6386 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
6387 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
6388 else
6389 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
6390 } else {
6391 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6392
6393 if (tg3_flag(tp, 5705_PLUS) ||
6394 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
6395 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
6396 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
6397 }
6398
6399 tw32(MAC_MODE, tp->mac_mode);
6400 udelay(40);
6401}
6402
Matt Carlson941ec902011-08-19 13:58:23 +00006403static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006404{
Matt Carlson941ec902011-08-19 13:58:23 +00006405 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006406
6407 tg3_phy_toggle_apd(tp, false);
6408 tg3_phy_toggle_automdix(tp, 0);
6409
Matt Carlson941ec902011-08-19 13:58:23 +00006410 if (extlpbk && tg3_phy_set_extloopbk(tp))
6411 return -EIO;
6412
6413 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006414 switch (speed) {
6415 case SPEED_10:
6416 break;
6417 case SPEED_100:
6418 bmcr |= BMCR_SPEED100;
6419 break;
6420 case SPEED_1000:
6421 default:
6422 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
6423 speed = SPEED_100;
6424 bmcr |= BMCR_SPEED100;
6425 } else {
6426 speed = SPEED_1000;
6427 bmcr |= BMCR_SPEED1000;
6428 }
6429 }
6430
Matt Carlson941ec902011-08-19 13:58:23 +00006431 if (extlpbk) {
6432 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
6433 tg3_readphy(tp, MII_CTRL1000, &val);
6434 val |= CTL1000_AS_MASTER |
6435 CTL1000_ENABLE_MASTER;
6436 tg3_writephy(tp, MII_CTRL1000, val);
6437 } else {
6438 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
6439 MII_TG3_FET_PTEST_TRIM_2;
6440 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
6441 }
6442 } else
6443 bmcr |= BMCR_LOOPBACK;
6444
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006445 tg3_writephy(tp, MII_BMCR, bmcr);
6446
6447 /* The write needs to be flushed for the FETs */
6448 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
6449 tg3_readphy(tp, MII_BMCR, &bmcr);
6450
6451 udelay(40);
6452
6453 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
6454 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00006455 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006456 MII_TG3_FET_PTEST_FRC_TX_LINK |
6457 MII_TG3_FET_PTEST_FRC_TX_LOCK);
6458
6459 /* The write needs to be flushed for the AC131 */
6460 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
6461 }
6462
6463 /* Reset to prevent losing 1st rx packet intermittently */
6464 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
6465 tg3_flag(tp, 5780_CLASS)) {
6466 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
6467 udelay(10);
6468 tw32_f(MAC_RX_MODE, tp->rx_mode);
6469 }
6470
6471 mac_mode = tp->mac_mode &
6472 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
6473 if (speed == SPEED_1000)
6474 mac_mode |= MAC_MODE_PORT_MODE_GMII;
6475 else
6476 mac_mode |= MAC_MODE_PORT_MODE_MII;
6477
6478 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
6479 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
6480
6481 if (masked_phy_id == TG3_PHY_ID_BCM5401)
6482 mac_mode &= ~MAC_MODE_LINK_POLARITY;
6483 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
6484 mac_mode |= MAC_MODE_LINK_POLARITY;
6485
6486 tg3_writephy(tp, MII_TG3_EXT_CTRL,
6487 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
6488 }
6489
6490 tw32(MAC_MODE, mac_mode);
6491 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00006492
6493 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006494}
6495
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006496static void tg3_set_loopback(struct net_device *dev, u32 features)
6497{
6498 struct tg3 *tp = netdev_priv(dev);
6499
6500 if (features & NETIF_F_LOOPBACK) {
6501 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6502 return;
6503
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006504 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006505 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006506 netif_carrier_on(tp->dev);
6507 spin_unlock_bh(&tp->lock);
6508 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6509 } else {
6510 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6511 return;
6512
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006513 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006514 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006515 /* Force link status check */
6516 tg3_setup_phy(tp, 1);
6517 spin_unlock_bh(&tp->lock);
6518 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6519 }
6520}
6521
Michał Mirosławdc668912011-04-07 03:35:07 +00006522static u32 tg3_fix_features(struct net_device *dev, u32 features)
6523{
6524 struct tg3 *tp = netdev_priv(dev);
6525
Joe Perches63c3a662011-04-26 08:12:10 +00006526 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006527 features &= ~NETIF_F_ALL_TSO;
6528
6529 return features;
6530}
6531
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006532static int tg3_set_features(struct net_device *dev, u32 features)
6533{
6534 u32 changed = dev->features ^ features;
6535
6536 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6537 tg3_set_loopback(dev, features);
6538
6539 return 0;
6540}
6541
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6543 int new_mtu)
6544{
6545 dev->mtu = new_mtu;
6546
Michael Chanef7f5ec2005-07-25 12:32:25 -07006547 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006548 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006549 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006550 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006551 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006552 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006553 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006554 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006555 if (tg3_flag(tp, 5780_CLASS)) {
6556 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00006557 netdev_update_features(dev);
6558 }
Joe Perches63c3a662011-04-26 08:12:10 +00006559 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561}
6562
6563static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6564{
6565 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006566 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567
6568 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6569 return -EINVAL;
6570
6571 if (!netif_running(dev)) {
6572 /* We'll just catch it later when the
6573 * device is up'd.
6574 */
6575 tg3_set_mtu(dev, tp, new_mtu);
6576 return 0;
6577 }
6578
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006579 tg3_phy_stop(tp);
6580
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006582
6583 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584
Michael Chan944d9802005-05-29 14:57:48 -07006585 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586
6587 tg3_set_mtu(dev, tp, new_mtu);
6588
Michael Chanb9ec6c12006-07-25 16:37:27 -07006589 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590
Michael Chanb9ec6c12006-07-25 16:37:27 -07006591 if (!err)
6592 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593
David S. Millerf47c11e2005-06-24 20:18:35 -07006594 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006596 if (!err)
6597 tg3_phy_start(tp);
6598
Michael Chanb9ec6c12006-07-25 16:37:27 -07006599 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600}
6601
Matt Carlson21f581a2009-08-28 14:00:25 +00006602static void tg3_rx_prodring_free(struct tg3 *tp,
6603 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 int i;
6606
Matt Carlson8fea32b2010-09-15 08:59:58 +00006607 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006608 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006609 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006610 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6611 tp->rx_pkt_map_sz);
6612
Joe Perches63c3a662011-04-26 08:12:10 +00006613 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006614 for (i = tpr->rx_jmb_cons_idx;
6615 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006616 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006617 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6618 TG3_RX_JMB_MAP_SZ);
6619 }
6620 }
6621
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006622 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624
Matt Carlson2c49a442010-09-30 10:34:35 +00006625 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006626 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6627 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628
Joe Perches63c3a662011-04-26 08:12:10 +00006629 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006630 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006631 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6632 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633 }
6634}
6635
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006636/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 *
6638 * The chip has been shut down and the driver detached from
6639 * the networking, so no interrupts or new tx packets will
6640 * end up in the driver. tp->{tx,}lock are held and thus
6641 * we may not sleep.
6642 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006643static int tg3_rx_prodring_alloc(struct tg3 *tp,
6644 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645{
Matt Carlson287be122009-08-28 13:58:46 +00006646 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006648 tpr->rx_std_cons_idx = 0;
6649 tpr->rx_std_prod_idx = 0;
6650 tpr->rx_jmb_cons_idx = 0;
6651 tpr->rx_jmb_prod_idx = 0;
6652
Matt Carlson8fea32b2010-09-15 08:59:58 +00006653 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006654 memset(&tpr->rx_std_buffers[0], 0,
6655 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006656 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006657 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006658 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006659 goto done;
6660 }
6661
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006663 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664
Matt Carlson287be122009-08-28 13:58:46 +00006665 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00006666 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006667 tp->dev->mtu > ETH_DATA_LEN)
6668 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6669 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006670
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 /* Initialize invariants of the rings, we only set this
6672 * stuff once. This works because the card does not
6673 * write into the rx buffer posting rings.
6674 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006675 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676 struct tg3_rx_buffer_desc *rxd;
6677
Matt Carlson21f581a2009-08-28 14:00:25 +00006678 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006679 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6681 rxd->opaque = (RXD_OPAQUE_RING_STD |
6682 (i << RXD_OPAQUE_INDEX_SHIFT));
6683 }
6684
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006685 /* Now allocate fresh SKBs for each rx ring. */
6686 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006687 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006688 netdev_warn(tp->dev,
6689 "Using a smaller RX standard ring. Only "
6690 "%d out of %d buffers were allocated "
6691 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006692 if (i == 0)
6693 goto initfail;
6694 tp->rx_pending = i;
6695 break;
6696 }
6697 }
6698
Joe Perches63c3a662011-04-26 08:12:10 +00006699 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006700 goto done;
6701
Matt Carlson2c49a442010-09-30 10:34:35 +00006702 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006703
Joe Perches63c3a662011-04-26 08:12:10 +00006704 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00006705 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706
Matt Carlson2c49a442010-09-30 10:34:35 +00006707 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006708 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709
Matt Carlson0d86df82010-02-17 15:17:00 +00006710 rxd = &tpr->rx_jmb[i].std;
6711 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6712 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6713 RXD_FLAG_JUMBO;
6714 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6715 (i << RXD_OPAQUE_INDEX_SHIFT));
6716 }
6717
6718 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6719 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006720 netdev_warn(tp->dev,
6721 "Using a smaller RX jumbo ring. Only %d "
6722 "out of %d buffers were allocated "
6723 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006724 if (i == 0)
6725 goto initfail;
6726 tp->rx_jumbo_pending = i;
6727 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 }
6729 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006730
6731done:
Michael Chan32d8c572006-07-25 16:38:29 -07006732 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006733
6734initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006735 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006736 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737}
6738
Matt Carlson21f581a2009-08-28 14:00:25 +00006739static void tg3_rx_prodring_fini(struct tg3 *tp,
6740 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741{
Matt Carlson21f581a2009-08-28 14:00:25 +00006742 kfree(tpr->rx_std_buffers);
6743 tpr->rx_std_buffers = NULL;
6744 kfree(tpr->rx_jmb_buffers);
6745 tpr->rx_jmb_buffers = NULL;
6746 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006747 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6748 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006749 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006751 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006752 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6753 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006754 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006756}
6757
Matt Carlson21f581a2009-08-28 14:00:25 +00006758static int tg3_rx_prodring_init(struct tg3 *tp,
6759 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006760{
Matt Carlson2c49a442010-09-30 10:34:35 +00006761 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6762 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006763 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006764 return -ENOMEM;
6765
Matt Carlson4bae65c2010-11-24 08:31:52 +00006766 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6767 TG3_RX_STD_RING_BYTES(tp),
6768 &tpr->rx_std_mapping,
6769 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006770 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006771 goto err_out;
6772
Joe Perches63c3a662011-04-26 08:12:10 +00006773 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006774 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006775 GFP_KERNEL);
6776 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006777 goto err_out;
6778
Matt Carlson4bae65c2010-11-24 08:31:52 +00006779 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6780 TG3_RX_JMB_RING_BYTES(tp),
6781 &tpr->rx_jmb_mapping,
6782 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006783 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006784 goto err_out;
6785 }
6786
6787 return 0;
6788
6789err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006790 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006791 return -ENOMEM;
6792}
6793
6794/* Free up pending packets in all rx/tx rings.
6795 *
6796 * The chip has been shut down and the driver detached from
6797 * the networking, so no interrupts or new tx packets will
6798 * end up in the driver. tp->{tx,}lock is not held and we are not
6799 * in an interrupt context and thus may sleep.
6800 */
6801static void tg3_free_rings(struct tg3 *tp)
6802{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006803 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006804
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006805 for (j = 0; j < tp->irq_cnt; j++) {
6806 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006807
Matt Carlson8fea32b2010-09-15 08:59:58 +00006808 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006809
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006810 if (!tnapi->tx_buffers)
6811 continue;
6812
Matt Carlson0d681b22011-07-27 14:20:49 +00006813 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
6814 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006815
Matt Carlson0d681b22011-07-27 14:20:49 +00006816 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006817 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006818
Matt Carlson0d681b22011-07-27 14:20:49 +00006819 tg3_tx_skb_unmap(tnapi, i, skb_shinfo(skb)->nr_frags);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006820
6821 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006822 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006823 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006824}
6825
6826/* Initialize tx/rx rings for packet processing.
6827 *
6828 * The chip has been shut down and the driver detached from
6829 * the networking, so no interrupts or new tx packets will
6830 * end up in the driver. tp->{tx,}lock are held and thus
6831 * we may not sleep.
6832 */
6833static int tg3_init_rings(struct tg3 *tp)
6834{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006835 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006836
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006837 /* Free up all the SKBs. */
6838 tg3_free_rings(tp);
6839
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006840 for (i = 0; i < tp->irq_cnt; i++) {
6841 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006842
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006843 tnapi->last_tag = 0;
6844 tnapi->last_irq_tag = 0;
6845 tnapi->hw_status->status = 0;
6846 tnapi->hw_status->status_tag = 0;
6847 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6848
6849 tnapi->tx_prod = 0;
6850 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006851 if (tnapi->tx_ring)
6852 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006853
6854 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006855 if (tnapi->rx_rcb)
6856 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006857
Matt Carlson8fea32b2010-09-15 08:59:58 +00006858 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006859 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006860 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006861 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006862 }
Matt Carlson72334482009-08-28 14:03:01 +00006863
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006864 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006865}
6866
6867/*
6868 * Must not be invoked with interrupt sources disabled and
6869 * the hardware shutdown down.
6870 */
6871static void tg3_free_consistent(struct tg3 *tp)
6872{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006873 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006874
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006875 for (i = 0; i < tp->irq_cnt; i++) {
6876 struct tg3_napi *tnapi = &tp->napi[i];
6877
6878 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006879 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006880 tnapi->tx_ring, tnapi->tx_desc_mapping);
6881 tnapi->tx_ring = NULL;
6882 }
6883
6884 kfree(tnapi->tx_buffers);
6885 tnapi->tx_buffers = NULL;
6886
6887 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006888 dma_free_coherent(&tp->pdev->dev,
6889 TG3_RX_RCB_RING_BYTES(tp),
6890 tnapi->rx_rcb,
6891 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006892 tnapi->rx_rcb = NULL;
6893 }
6894
Matt Carlson8fea32b2010-09-15 08:59:58 +00006895 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6896
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006897 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006898 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6899 tnapi->hw_status,
6900 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006901 tnapi->hw_status = NULL;
6902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006904
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006906 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6907 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 tp->hw_stats = NULL;
6909 }
6910}
6911
6912/*
6913 * Must not be invoked with interrupt sources disabled and
6914 * the hardware shutdown down. Can sleep.
6915 */
6916static int tg3_alloc_consistent(struct tg3 *tp)
6917{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006918 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006919
Matt Carlson4bae65c2010-11-24 08:31:52 +00006920 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6921 sizeof(struct tg3_hw_stats),
6922 &tp->stats_mapping,
6923 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924 if (!tp->hw_stats)
6925 goto err_out;
6926
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6928
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006929 for (i = 0; i < tp->irq_cnt; i++) {
6930 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006931 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006932
Matt Carlson4bae65c2010-11-24 08:31:52 +00006933 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6934 TG3_HW_STATUS_SIZE,
6935 &tnapi->status_mapping,
6936 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006937 if (!tnapi->hw_status)
6938 goto err_out;
6939
6940 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006941 sblk = tnapi->hw_status;
6942
Matt Carlson8fea32b2010-09-15 08:59:58 +00006943 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6944 goto err_out;
6945
Matt Carlson19cfaec2009-12-03 08:36:20 +00006946 /* If multivector TSS is enabled, vector 0 does not handle
6947 * tx interrupts. Don't allocate any resources for it.
6948 */
Joe Perches63c3a662011-04-26 08:12:10 +00006949 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
6950 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006951 tnapi->tx_buffers = kzalloc(
6952 sizeof(struct tg3_tx_ring_info) *
6953 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006954 if (!tnapi->tx_buffers)
6955 goto err_out;
6956
Matt Carlson4bae65c2010-11-24 08:31:52 +00006957 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6958 TG3_TX_RING_BYTES,
6959 &tnapi->tx_desc_mapping,
6960 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006961 if (!tnapi->tx_ring)
6962 goto err_out;
6963 }
6964
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006965 /*
6966 * When RSS is enabled, the status block format changes
6967 * slightly. The "rx_jumbo_consumer", "reserved",
6968 * and "rx_mini_consumer" members get mapped to the
6969 * other three rx return ring producer indexes.
6970 */
6971 switch (i) {
6972 default:
6973 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6974 break;
6975 case 2:
6976 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6977 break;
6978 case 3:
6979 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6980 break;
6981 case 4:
6982 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6983 break;
6984 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006985
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006986 /*
6987 * If multivector RSS is enabled, vector 0 does not handle
6988 * rx or tx interrupts. Don't allocate any resources for it.
6989 */
Joe Perches63c3a662011-04-26 08:12:10 +00006990 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006991 continue;
6992
Matt Carlson4bae65c2010-11-24 08:31:52 +00006993 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6994 TG3_RX_RCB_RING_BYTES(tp),
6995 &tnapi->rx_rcb_mapping,
6996 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006997 if (!tnapi->rx_rcb)
6998 goto err_out;
6999
7000 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007001 }
7002
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003 return 0;
7004
7005err_out:
7006 tg3_free_consistent(tp);
7007 return -ENOMEM;
7008}
7009
7010#define MAX_WAIT_CNT 1000
7011
7012/* To stop a block, clear the enable bit and poll till it
7013 * clears. tp->lock is held.
7014 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007015static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016{
7017 unsigned int i;
7018 u32 val;
7019
Joe Perches63c3a662011-04-26 08:12:10 +00007020 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021 switch (ofs) {
7022 case RCVLSC_MODE:
7023 case DMAC_MODE:
7024 case MBFREE_MODE:
7025 case BUFMGR_MODE:
7026 case MEMARB_MODE:
7027 /* We can't enable/disable these bits of the
7028 * 5705/5750, just say success.
7029 */
7030 return 0;
7031
7032 default:
7033 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 }
7036
7037 val = tr32(ofs);
7038 val &= ~enable_bit;
7039 tw32_f(ofs, val);
7040
7041 for (i = 0; i < MAX_WAIT_CNT; i++) {
7042 udelay(100);
7043 val = tr32(ofs);
7044 if ((val & enable_bit) == 0)
7045 break;
7046 }
7047
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007048 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007049 dev_err(&tp->pdev->dev,
7050 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7051 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052 return -ENODEV;
7053 }
7054
7055 return 0;
7056}
7057
7058/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007059static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060{
7061 int i, err;
7062
7063 tg3_disable_ints(tp);
7064
7065 tp->rx_mode &= ~RX_MODE_ENABLE;
7066 tw32_f(MAC_RX_MODE, tp->rx_mode);
7067 udelay(10);
7068
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007069 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7070 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7071 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7072 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7073 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7074 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007076 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7077 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7078 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7079 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7080 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7081 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7082 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
7084 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7085 tw32_f(MAC_MODE, tp->mac_mode);
7086 udelay(40);
7087
7088 tp->tx_mode &= ~TX_MODE_ENABLE;
7089 tw32_f(MAC_TX_MODE, tp->tx_mode);
7090
7091 for (i = 0; i < MAX_WAIT_CNT; i++) {
7092 udelay(100);
7093 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7094 break;
7095 }
7096 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007097 dev_err(&tp->pdev->dev,
7098 "%s timed out, TX_MODE_ENABLE will not clear "
7099 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007100 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101 }
7102
Michael Chane6de8ad2005-05-05 14:42:41 -07007103 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007104 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7105 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106
7107 tw32(FTQ_RESET, 0xffffffff);
7108 tw32(FTQ_RESET, 0x00000000);
7109
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007110 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7111 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007113 for (i = 0; i < tp->irq_cnt; i++) {
7114 struct tg3_napi *tnapi = &tp->napi[i];
7115 if (tnapi->hw_status)
7116 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 if (tp->hw_stats)
7119 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7120
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 return err;
7122}
7123
Matt Carlson0d3031d2007-10-10 18:02:43 -07007124static void tg3_ape_send_event(struct tg3 *tp, u32 event)
7125{
7126 int i;
7127 u32 apedata;
7128
Matt Carlsondc6d0742010-09-15 08:59:55 +00007129 /* NCSI does not support APE events */
Joe Perches63c3a662011-04-26 08:12:10 +00007130 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsondc6d0742010-09-15 08:59:55 +00007131 return;
7132
Matt Carlson0d3031d2007-10-10 18:02:43 -07007133 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
7134 if (apedata != APE_SEG_SIG_MAGIC)
7135 return;
7136
7137 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07007138 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007139 return;
7140
7141 /* Wait for up to 1 millisecond for APE to service previous event. */
7142 for (i = 0; i < 10; i++) {
7143 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
7144 return;
7145
7146 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
7147
7148 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
7149 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
7150 event | APE_EVENT_STATUS_EVENT_PENDING);
7151
7152 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
7153
7154 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
7155 break;
7156
7157 udelay(100);
7158 }
7159
7160 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
7161 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
7162}
7163
7164static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
7165{
7166 u32 event;
7167 u32 apedata;
7168
Joe Perches63c3a662011-04-26 08:12:10 +00007169 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007170 return;
7171
7172 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00007173 case RESET_KIND_INIT:
7174 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
7175 APE_HOST_SEG_SIG_MAGIC);
7176 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
7177 APE_HOST_SEG_LEN_MAGIC);
7178 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
7179 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
7180 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00007181 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00007182 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
7183 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00007184 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
7185 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07007186
Matt Carlson33f401a2010-04-05 10:19:27 +00007187 event = APE_EVENT_STATUS_STATE_START;
7188 break;
7189 case RESET_KIND_SHUTDOWN:
7190 /* With the interface we are currently using,
7191 * APE does not track driver state. Wiping
7192 * out the HOST SEGMENT SIGNATURE forces
7193 * the APE to assume OS absent status.
7194 */
7195 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08007196
Matt Carlsondc6d0742010-09-15 08:59:55 +00007197 if (device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007198 tg3_flag(tp, WOL_ENABLE)) {
Matt Carlsondc6d0742010-09-15 08:59:55 +00007199 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
7200 TG3_APE_HOST_WOL_SPEED_AUTO);
7201 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
7202 } else
7203 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
7204
7205 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
7206
Matt Carlson33f401a2010-04-05 10:19:27 +00007207 event = APE_EVENT_STATUS_STATE_UNLOAD;
7208 break;
7209 case RESET_KIND_SUSPEND:
7210 event = APE_EVENT_STATUS_STATE_SUSPEND;
7211 break;
7212 default:
7213 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007214 }
7215
7216 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
7217
7218 tg3_ape_send_event(tp, event);
7219}
7220
Michael Chane6af3012005-04-21 17:12:05 -07007221/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
7223{
David S. Millerf49639e2006-06-09 11:58:36 -07007224 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
7225 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226
Joe Perches63c3a662011-04-26 08:12:10 +00007227 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 switch (kind) {
7229 case RESET_KIND_INIT:
7230 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7231 DRV_STATE_START);
7232 break;
7233
7234 case RESET_KIND_SHUTDOWN:
7235 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7236 DRV_STATE_UNLOAD);
7237 break;
7238
7239 case RESET_KIND_SUSPEND:
7240 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7241 DRV_STATE_SUSPEND);
7242 break;
7243
7244 default:
7245 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007248
7249 if (kind == RESET_KIND_INIT ||
7250 kind == RESET_KIND_SUSPEND)
7251 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252}
7253
7254/* tp->lock is held. */
7255static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
7256{
Joe Perches63c3a662011-04-26 08:12:10 +00007257 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258 switch (kind) {
7259 case RESET_KIND_INIT:
7260 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7261 DRV_STATE_START_DONE);
7262 break;
7263
7264 case RESET_KIND_SHUTDOWN:
7265 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7266 DRV_STATE_UNLOAD_DONE);
7267 break;
7268
7269 default:
7270 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007273
7274 if (kind == RESET_KIND_SHUTDOWN)
7275 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276}
7277
7278/* tp->lock is held. */
7279static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
7280{
Joe Perches63c3a662011-04-26 08:12:10 +00007281 if (tg3_flag(tp, ENABLE_ASF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 switch (kind) {
7283 case RESET_KIND_INIT:
7284 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7285 DRV_STATE_START);
7286 break;
7287
7288 case RESET_KIND_SHUTDOWN:
7289 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7290 DRV_STATE_UNLOAD);
7291 break;
7292
7293 case RESET_KIND_SUSPEND:
7294 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7295 DRV_STATE_SUSPEND);
7296 break;
7297
7298 default:
7299 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301 }
7302}
7303
Michael Chan7a6f4362006-09-27 16:03:31 -07007304static int tg3_poll_fw(struct tg3 *tp)
7305{
7306 int i;
7307 u32 val;
7308
Michael Chanb5d37722006-09-27 16:06:21 -07007309 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08007310 /* Wait up to 20ms for init done. */
7311 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07007312 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
7313 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08007314 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07007315 }
7316 return -ENODEV;
7317 }
7318
Michael Chan7a6f4362006-09-27 16:03:31 -07007319 /* Wait for firmware initialization to complete. */
7320 for (i = 0; i < 100000; i++) {
7321 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
7322 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
7323 break;
7324 udelay(10);
7325 }
7326
7327 /* Chip might not be fitted with firmware. Some Sun onboard
7328 * parts are configured like that. So don't signal the timeout
7329 * of the above loop as an error, but do report the lack of
7330 * running firmware once.
7331 */
Joe Perches63c3a662011-04-26 08:12:10 +00007332 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
7333 tg3_flag_set(tp, NO_FWARE_REPORTED);
Michael Chan7a6f4362006-09-27 16:03:31 -07007334
Joe Perches05dbe002010-02-17 19:44:19 +00007335 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07007336 }
7337
Matt Carlson6b10c162010-02-12 14:47:08 +00007338 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7339 /* The 57765 A0 needs a little more
7340 * time to do some important work.
7341 */
7342 mdelay(10);
7343 }
7344
Michael Chan7a6f4362006-09-27 16:03:31 -07007345 return 0;
7346}
7347
Michael Chanee6a99b2007-07-18 21:49:10 -07007348/* Save PCI command register before chip reset */
7349static void tg3_save_pci_state(struct tg3 *tp)
7350{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007351 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007352}
7353
7354/* Restore PCI state after chip reset */
7355static void tg3_restore_pci_state(struct tg3 *tp)
7356{
7357 u32 val;
7358
7359 /* Re-enable indirect register accesses. */
7360 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7361 tp->misc_host_ctrl);
7362
7363 /* Set MAX PCI retry to zero. */
7364 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7365 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007366 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007367 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007368 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007369 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007370 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00007371 PCISTATE_ALLOW_APE_SHMEM_WR |
7372 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007373 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7374
Matt Carlson8a6eac92007-10-21 16:17:55 -07007375 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007376
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007377 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007378 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007379 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007380 else {
7381 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7382 tp->pci_cacheline_sz);
7383 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7384 tp->pci_lat_timer);
7385 }
Michael Chan114342f2007-10-15 02:12:26 -07007386 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007387
Michael Chanee6a99b2007-07-18 21:49:10 -07007388 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007389 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007390 u16 pcix_cmd;
7391
7392 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7393 &pcix_cmd);
7394 pcix_cmd &= ~PCI_X_CMD_ERO;
7395 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7396 pcix_cmd);
7397 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007398
Joe Perches63c3a662011-04-26 08:12:10 +00007399 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007400
7401 /* Chip reset on 5780 will reset MSI enable bit,
7402 * so need to restore it.
7403 */
Joe Perches63c3a662011-04-26 08:12:10 +00007404 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007405 u16 ctrl;
7406
7407 pci_read_config_word(tp->pdev,
7408 tp->msi_cap + PCI_MSI_FLAGS,
7409 &ctrl);
7410 pci_write_config_word(tp->pdev,
7411 tp->msi_cap + PCI_MSI_FLAGS,
7412 ctrl | PCI_MSI_FLAGS_ENABLE);
7413 val = tr32(MSGINT_MODE);
7414 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7415 }
7416 }
7417}
7418
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419static void tg3_stop_fw(struct tg3 *);
7420
7421/* tp->lock is held. */
7422static int tg3_chip_reset(struct tg3 *tp)
7423{
7424 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007425 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007426 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427
David S. Millerf49639e2006-06-09 11:58:36 -07007428 tg3_nvram_lock(tp);
7429
Matt Carlson77b483f2008-08-15 14:07:24 -07007430 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7431
David S. Millerf49639e2006-06-09 11:58:36 -07007432 /* No matching tg3_nvram_unlock() after this because
7433 * chip reset below will undo the nvram lock.
7434 */
7435 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436
Michael Chanee6a99b2007-07-18 21:49:10 -07007437 /* GRC_MISC_CFG core clock reset will clear the memory
7438 * enable bit in PCI register 4 and the MSI enable bit
7439 * on some chips, so we save relevant registers here.
7440 */
7441 tg3_save_pci_state(tp);
7442
Michael Chand9ab5ad2006-03-20 22:27:35 -08007443 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007444 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007445 tw32(GRC_FASTBOOT_PC, 0);
7446
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447 /*
7448 * We must avoid the readl() that normally takes place.
7449 * It locks machines, causes machine checks, and other
7450 * fun things. So, temporarily disable the 5701
7451 * hardware workaround, while we do the reset.
7452 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007453 write_op = tp->write32;
7454 if (write_op == tg3_write_flush_reg32)
7455 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456
Michael Chand18edcb2007-03-24 20:57:11 -07007457 /* Prevent the irq handler from reading or writing PCI registers
7458 * during chip reset when the memory enable bit in the PCI command
7459 * register may be cleared. The chip does not generate interrupt
7460 * at this time, but the irq handler may still be called due to irq
7461 * sharing or irqpoll.
7462 */
Joe Perches63c3a662011-04-26 08:12:10 +00007463 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007464 for (i = 0; i < tp->irq_cnt; i++) {
7465 struct tg3_napi *tnapi = &tp->napi[i];
7466 if (tnapi->hw_status) {
7467 tnapi->hw_status->status = 0;
7468 tnapi->hw_status->status_tag = 0;
7469 }
7470 tnapi->last_tag = 0;
7471 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007472 }
Michael Chand18edcb2007-03-24 20:57:11 -07007473 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007474
7475 for (i = 0; i < tp->irq_cnt; i++)
7476 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007477
Matt Carlson255ca312009-08-25 10:07:27 +00007478 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7479 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7480 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7481 }
7482
Linus Torvalds1da177e2005-04-16 15:20:36 -07007483 /* do the reset */
7484 val = GRC_MISC_CFG_CORECLK_RESET;
7485
Joe Perches63c3a662011-04-26 08:12:10 +00007486 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007487 /* Force PCIe 1.0a mode */
7488 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007489 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007490 tr32(TG3_PCIE_PHY_TSTCTL) ==
7491 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7492 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7493
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7495 tw32(GRC_MISC_CFG, (1 << 29));
7496 val |= (1 << 29);
7497 }
7498 }
7499
Michael Chanb5d37722006-09-27 16:06:21 -07007500 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7501 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7502 tw32(GRC_VCPU_EXT_CTRL,
7503 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7504 }
7505
Matt Carlsonf37500d2010-08-02 11:25:59 +00007506 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007507 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007509
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510 tw32(GRC_MISC_CFG, val);
7511
Michael Chan1ee582d2005-08-09 20:16:46 -07007512 /* restore 5701 hardware bug workaround write method */
7513 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514
7515 /* Unfortunately, we have to delay before the PCI read back.
7516 * Some 575X chips even will not respond to a PCI cfg access
7517 * when the reset command is given to the chip.
7518 *
7519 * How do these hardware designers expect things to work
7520 * properly if the PCI write is posted for a long period
7521 * of time? It is always necessary to have some method by
7522 * which a register read back can occur to push the write
7523 * out which does the reset.
7524 *
7525 * For most tg3 variants the trick below was working.
7526 * Ho hum...
7527 */
7528 udelay(120);
7529
7530 /* Flush PCI posted writes. The normal MMIO registers
7531 * are inaccessible at this time so this is the only
7532 * way to make this reliably (actually, this is no longer
7533 * the case, see above). I tried to use indirect
7534 * register read/write but this upset some 5701 variants.
7535 */
7536 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7537
7538 udelay(120);
7539
Jon Mason708ebb32011-06-27 12:56:50 +00007540 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007541 u16 val16;
7542
Linus Torvalds1da177e2005-04-16 15:20:36 -07007543 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7544 int i;
7545 u32 cfg_val;
7546
7547 /* Wait for link training to complete. */
7548 for (i = 0; i < 5000; i++)
7549 udelay(100);
7550
7551 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7552 pci_write_config_dword(tp->pdev, 0xc4,
7553 cfg_val | (1 << 15));
7554 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007555
Matt Carlsone7126992009-08-25 10:08:16 +00007556 /* Clear the "no snoop" and "relaxed ordering" bits. */
7557 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007558 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007559 &val16);
7560 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7561 PCI_EXP_DEVCTL_NOSNOOP_EN);
7562 /*
7563 * Older PCIe devices only support the 128 byte
7564 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007565 */
Joe Perches63c3a662011-04-26 08:12:10 +00007566 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007567 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007568 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007569 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007570 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007571
Matt Carlsoncf790032010-11-24 08:31:48 +00007572 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007573
7574 /* Clear error status */
7575 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007576 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007577 PCI_EXP_DEVSTA_CED |
7578 PCI_EXP_DEVSTA_NFED |
7579 PCI_EXP_DEVSTA_FED |
7580 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 }
7582
Michael Chanee6a99b2007-07-18 21:49:10 -07007583 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007584
Joe Perches63c3a662011-04-26 08:12:10 +00007585 tg3_flag_clear(tp, CHIP_RESETTING);
7586 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007587
Michael Chanee6a99b2007-07-18 21:49:10 -07007588 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007589 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007590 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007591 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592
7593 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7594 tg3_stop_fw(tp);
7595 tw32(0x5000, 0x400);
7596 }
7597
7598 tw32(GRC_MODE, tp->grc_mode);
7599
7600 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007601 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007602
7603 tw32(0xc4, val | (1 << 15));
7604 }
7605
7606 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7607 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7608 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7609 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7610 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7611 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7612 }
7613
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007614 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007615 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007616 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007617 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007618 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007619 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007621 val = 0;
7622
7623 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007624 udelay(40);
7625
Matt Carlson77b483f2008-08-15 14:07:24 -07007626 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7627
Michael Chan7a6f4362006-09-27 16:03:31 -07007628 err = tg3_poll_fw(tp);
7629 if (err)
7630 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631
Matt Carlson0a9140c2009-08-28 12:27:50 +00007632 tg3_mdio_start(tp);
7633
Joe Perches63c3a662011-04-26 08:12:10 +00007634 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007635 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7636 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007637 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007638 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639
7640 tw32(0x7c00, val | (1 << 25));
7641 }
7642
Matt Carlsond78b59f2011-04-05 14:22:46 +00007643 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7644 val = tr32(TG3_CPMU_CLCK_ORIDE);
7645 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7646 }
7647
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007649 tg3_flag_clear(tp, ENABLE_ASF);
7650 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7652 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7653 u32 nic_cfg;
7654
7655 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7656 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007657 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007658 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007659 if (tg3_flag(tp, 5750_PLUS))
7660 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661 }
7662 }
7663
7664 return 0;
7665}
7666
7667/* tp->lock is held. */
7668static void tg3_stop_fw(struct tg3 *tp)
7669{
Joe Perches63c3a662011-04-26 08:12:10 +00007670 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007671 /* Wait for RX cpu to ACK the previous event. */
7672 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673
7674 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007675
7676 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007677
Matt Carlson7c5026a2008-05-02 16:49:29 -07007678 /* Wait for RX cpu to ACK this event. */
7679 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007680 }
7681}
7682
7683/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007684static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685{
7686 int err;
7687
7688 tg3_stop_fw(tp);
7689
Michael Chan944d9802005-05-29 14:57:48 -07007690 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007691
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007692 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693 err = tg3_chip_reset(tp);
7694
Matt Carlsondaba2a62009-04-20 06:58:52 +00007695 __tg3_set_mac_addr(tp, 0);
7696
Michael Chan944d9802005-05-29 14:57:48 -07007697 tg3_write_sig_legacy(tp, kind);
7698 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699
7700 if (err)
7701 return err;
7702
7703 return 0;
7704}
7705
Linus Torvalds1da177e2005-04-16 15:20:36 -07007706#define RX_CPU_SCRATCH_BASE 0x30000
7707#define RX_CPU_SCRATCH_SIZE 0x04000
7708#define TX_CPU_SCRATCH_BASE 0x34000
7709#define TX_CPU_SCRATCH_SIZE 0x04000
7710
7711/* tp->lock is held. */
7712static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7713{
7714 int i;
7715
Joe Perches63c3a662011-04-26 08:12:10 +00007716 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717
Michael Chanb5d37722006-09-27 16:06:21 -07007718 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7719 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7720
7721 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7722 return 0;
7723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007724 if (offset == RX_CPU_BASE) {
7725 for (i = 0; i < 10000; i++) {
7726 tw32(offset + CPU_STATE, 0xffffffff);
7727 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7728 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7729 break;
7730 }
7731
7732 tw32(offset + CPU_STATE, 0xffffffff);
7733 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7734 udelay(10);
7735 } else {
7736 for (i = 0; i < 10000; i++) {
7737 tw32(offset + CPU_STATE, 0xffffffff);
7738 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7739 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7740 break;
7741 }
7742 }
7743
7744 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007745 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7746 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007747 return -ENODEV;
7748 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007749
7750 /* Clear firmware's nvram arbitration. */
Joe Perches63c3a662011-04-26 08:12:10 +00007751 if (tg3_flag(tp, NVRAM))
Michael Chanec41c7d2006-01-17 02:40:55 -08007752 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007753 return 0;
7754}
7755
7756struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007757 unsigned int fw_base;
7758 unsigned int fw_len;
7759 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007760};
7761
7762/* tp->lock is held. */
7763static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7764 int cpu_scratch_size, struct fw_info *info)
7765{
Michael Chanec41c7d2006-01-17 02:40:55 -08007766 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767 void (*write_op)(struct tg3 *, u32, u32);
7768
Joe Perches63c3a662011-04-26 08:12:10 +00007769 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007770 netdev_err(tp->dev,
7771 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007772 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773 return -EINVAL;
7774 }
7775
Joe Perches63c3a662011-04-26 08:12:10 +00007776 if (tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007777 write_op = tg3_write_mem;
7778 else
7779 write_op = tg3_write_indirect_reg32;
7780
Michael Chan1b628152005-05-29 14:59:49 -07007781 /* It is possible that bootcode is still loading at this point.
7782 * Get the nvram lock first before halting the cpu.
7783 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007784 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007786 if (!lock_err)
7787 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788 if (err)
7789 goto out;
7790
7791 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7792 write_op(tp, cpu_scratch_base + i, 0);
7793 tw32(cpu_base + CPU_STATE, 0xffffffff);
7794 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007795 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007797 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007798 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007799 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800
7801 err = 0;
7802
7803out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804 return err;
7805}
7806
7807/* tp->lock is held. */
7808static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7809{
7810 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007811 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812 int err, i;
7813
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007814 fw_data = (void *)tp->fw->data;
7815
7816 /* Firmware blob starts with version numbers, followed by
7817 start address and length. We are setting complete length.
7818 length = end_address_of_bss - start_address_of_text.
7819 Remainder is the blob to be loaded contiguously
7820 from start address. */
7821
7822 info.fw_base = be32_to_cpu(fw_data[1]);
7823 info.fw_len = tp->fw->size - 12;
7824 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825
7826 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7827 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7828 &info);
7829 if (err)
7830 return err;
7831
7832 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7833 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7834 &info);
7835 if (err)
7836 return err;
7837
7838 /* Now startup only the RX cpu. */
7839 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007840 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007841
7842 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007843 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007844 break;
7845 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7846 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007847 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007848 udelay(1000);
7849 }
7850 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007851 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7852 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007853 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854 return -ENODEV;
7855 }
7856 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7857 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7858
7859 return 0;
7860}
7861
Linus Torvalds1da177e2005-04-16 15:20:36 -07007862/* tp->lock is held. */
7863static int tg3_load_tso_firmware(struct tg3 *tp)
7864{
7865 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007866 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7868 int err, i;
7869
Joe Perches63c3a662011-04-26 08:12:10 +00007870 if (tg3_flag(tp, HW_TSO_1) ||
7871 tg3_flag(tp, HW_TSO_2) ||
7872 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873 return 0;
7874
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007875 fw_data = (void *)tp->fw->data;
7876
7877 /* Firmware blob starts with version numbers, followed by
7878 start address and length. We are setting complete length.
7879 length = end_address_of_bss - start_address_of_text.
7880 Remainder is the blob to be loaded contiguously
7881 from start address. */
7882
7883 info.fw_base = be32_to_cpu(fw_data[1]);
7884 cpu_scratch_size = tp->fw_len;
7885 info.fw_len = tp->fw->size - 12;
7886 info.fw_data = &fw_data[3];
7887
Linus Torvalds1da177e2005-04-16 15:20:36 -07007888 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007889 cpu_base = RX_CPU_BASE;
7890 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007891 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007892 cpu_base = TX_CPU_BASE;
7893 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7894 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7895 }
7896
7897 err = tg3_load_firmware_cpu(tp, cpu_base,
7898 cpu_scratch_base, cpu_scratch_size,
7899 &info);
7900 if (err)
7901 return err;
7902
7903 /* Now startup the cpu. */
7904 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007905 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906
7907 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007908 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007909 break;
7910 tw32(cpu_base + CPU_STATE, 0xffffffff);
7911 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007912 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007913 udelay(1000);
7914 }
7915 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007916 netdev_err(tp->dev,
7917 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007918 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 return -ENODEV;
7920 }
7921 tw32(cpu_base + CPU_STATE, 0xffffffff);
7922 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7923 return 0;
7924}
7925
Linus Torvalds1da177e2005-04-16 15:20:36 -07007926
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927static int tg3_set_mac_addr(struct net_device *dev, void *p)
7928{
7929 struct tg3 *tp = netdev_priv(dev);
7930 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007931 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932
Michael Chanf9804dd2005-09-27 12:13:10 -07007933 if (!is_valid_ether_addr(addr->sa_data))
7934 return -EINVAL;
7935
Linus Torvalds1da177e2005-04-16 15:20:36 -07007936 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7937
Michael Chane75f7c92006-03-20 21:33:26 -08007938 if (!netif_running(dev))
7939 return 0;
7940
Joe Perches63c3a662011-04-26 08:12:10 +00007941 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007942 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007943
Michael Chan986e0ae2007-05-05 12:10:20 -07007944 addr0_high = tr32(MAC_ADDR_0_HIGH);
7945 addr0_low = tr32(MAC_ADDR_0_LOW);
7946 addr1_high = tr32(MAC_ADDR_1_HIGH);
7947 addr1_low = tr32(MAC_ADDR_1_LOW);
7948
7949 /* Skip MAC addr 1 if ASF is using it. */
7950 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7951 !(addr1_high == 0 && addr1_low == 0))
7952 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007953 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007954 spin_lock_bh(&tp->lock);
7955 __tg3_set_mac_addr(tp, skip_mac_1);
7956 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957
Michael Chanb9ec6c12006-07-25 16:37:27 -07007958 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007959}
7960
7961/* tp->lock is held. */
7962static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7963 dma_addr_t mapping, u32 maxlen_flags,
7964 u32 nic_addr)
7965{
7966 tg3_write_mem(tp,
7967 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7968 ((u64) mapping >> 32));
7969 tg3_write_mem(tp,
7970 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7971 ((u64) mapping & 0xffffffff));
7972 tg3_write_mem(tp,
7973 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7974 maxlen_flags);
7975
Joe Perches63c3a662011-04-26 08:12:10 +00007976 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007977 tg3_write_mem(tp,
7978 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7979 nic_addr);
7980}
7981
7982static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007983static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007984{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007985 int i;
7986
Joe Perches63c3a662011-04-26 08:12:10 +00007987 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007988 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7989 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7990 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007991 } else {
7992 tw32(HOSTCC_TXCOL_TICKS, 0);
7993 tw32(HOSTCC_TXMAX_FRAMES, 0);
7994 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007995 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007996
Joe Perches63c3a662011-04-26 08:12:10 +00007997 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007998 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7999 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8000 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8001 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008002 tw32(HOSTCC_RXCOL_TICKS, 0);
8003 tw32(HOSTCC_RXMAX_FRAMES, 0);
8004 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008005 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008006
Joe Perches63c3a662011-04-26 08:12:10 +00008007 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008008 u32 val = ec->stats_block_coalesce_usecs;
8009
Matt Carlsonb6080e12009-09-01 13:12:00 +00008010 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8011 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8012
David S. Miller15f98502005-05-18 22:49:26 -07008013 if (!netif_carrier_ok(tp->dev))
8014 val = 0;
8015
8016 tw32(HOSTCC_STAT_COAL_TICKS, val);
8017 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008018
8019 for (i = 0; i < tp->irq_cnt - 1; i++) {
8020 u32 reg;
8021
8022 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8023 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008024 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8025 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008026 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8027 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008028
Joe Perches63c3a662011-04-26 08:12:10 +00008029 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008030 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8031 tw32(reg, ec->tx_coalesce_usecs);
8032 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8033 tw32(reg, ec->tx_max_coalesced_frames);
8034 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8035 tw32(reg, ec->tx_max_coalesced_frames_irq);
8036 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008037 }
8038
8039 for (; i < tp->irq_max - 1; i++) {
8040 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008041 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008042 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008043
Joe Perches63c3a662011-04-26 08:12:10 +00008044 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008045 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8046 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8047 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8048 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008049 }
David S. Miller15f98502005-05-18 22:49:26 -07008050}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008051
8052/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008053static void tg3_rings_reset(struct tg3 *tp)
8054{
8055 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008056 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008057 struct tg3_napi *tnapi = &tp->napi[0];
8058
8059 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008060 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008061 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008062 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008063 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00008064 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8065 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008066 else
8067 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8068
8069 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8070 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8071 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8072 BDINFO_FLAGS_DISABLED);
8073
8074
8075 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008076 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008077 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008078 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008079 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008080 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8081 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00008082 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8083 else
8084 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8085
8086 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8087 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8088 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8089 BDINFO_FLAGS_DISABLED);
8090
8091 /* Disable interrupts */
8092 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008093 tp->napi[0].chk_msi_cnt = 0;
8094 tp->napi[0].last_rx_cons = 0;
8095 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008096
8097 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008098 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008099 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008100 tp->napi[i].tx_prod = 0;
8101 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008102 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008103 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008104 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8105 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008106 tp->napi[0].chk_msi_cnt = 0;
8107 tp->napi[i].last_rx_cons = 0;
8108 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008109 }
Joe Perches63c3a662011-04-26 08:12:10 +00008110 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008111 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008112 } else {
8113 tp->napi[0].tx_prod = 0;
8114 tp->napi[0].tx_cons = 0;
8115 tw32_mailbox(tp->napi[0].prodmbox, 0);
8116 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8117 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008118
8119 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008120 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008121 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8122 for (i = 0; i < 16; i++)
8123 tw32_tx_mbox(mbox + i * 8, 0);
8124 }
8125
8126 txrcb = NIC_SRAM_SEND_RCB;
8127 rxrcb = NIC_SRAM_RCV_RET_RCB;
8128
8129 /* Clear status block in ram. */
8130 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8131
8132 /* Set status block DMA address */
8133 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8134 ((u64) tnapi->status_mapping >> 32));
8135 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8136 ((u64) tnapi->status_mapping & 0xffffffff));
8137
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008138 if (tnapi->tx_ring) {
8139 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8140 (TG3_TX_RING_SIZE <<
8141 BDINFO_FLAGS_MAXLEN_SHIFT),
8142 NIC_SRAM_TX_BUFFER_DESC);
8143 txrcb += TG3_BDINFO_SIZE;
8144 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008145
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008146 if (tnapi->rx_rcb) {
8147 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008148 (tp->rx_ret_ring_mask + 1) <<
8149 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008150 rxrcb += TG3_BDINFO_SIZE;
8151 }
8152
8153 stblk = HOSTCC_STATBLCK_RING1;
8154
8155 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8156 u64 mapping = (u64)tnapi->status_mapping;
8157 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8158 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8159
8160 /* Clear status block in ram. */
8161 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8162
Matt Carlson19cfaec2009-12-03 08:36:20 +00008163 if (tnapi->tx_ring) {
8164 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8165 (TG3_TX_RING_SIZE <<
8166 BDINFO_FLAGS_MAXLEN_SHIFT),
8167 NIC_SRAM_TX_BUFFER_DESC);
8168 txrcb += TG3_BDINFO_SIZE;
8169 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008170
8171 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008172 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008173 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8174
8175 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008176 rxrcb += TG3_BDINFO_SIZE;
8177 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008178}
8179
Matt Carlsoneb07a942011-04-20 07:57:36 +00008180static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8181{
8182 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8183
Joe Perches63c3a662011-04-26 08:12:10 +00008184 if (!tg3_flag(tp, 5750_PLUS) ||
8185 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008186 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
8187 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8188 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8189 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8190 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8191 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8192 else
8193 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8194
8195 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8196 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8197
8198 val = min(nic_rep_thresh, host_rep_thresh);
8199 tw32(RCVBDI_STD_THRESH, val);
8200
Joe Perches63c3a662011-04-26 08:12:10 +00008201 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008202 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8203
Joe Perches63c3a662011-04-26 08:12:10 +00008204 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008205 return;
8206
Joe Perches63c3a662011-04-26 08:12:10 +00008207 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008208 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
8209 else
8210 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
8211
8212 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8213
8214 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8215 tw32(RCVBDI_JUMBO_THRESH, val);
8216
Joe Perches63c3a662011-04-26 08:12:10 +00008217 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008218 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8219}
8220
Matt Carlson2d31eca2009-09-01 12:53:31 +00008221/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008222static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008223{
8224 u32 val, rdmac_mode;
8225 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008226 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227
8228 tg3_disable_ints(tp);
8229
8230 tg3_stop_fw(tp);
8231
8232 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8233
Joe Perches63c3a662011-04-26 08:12:10 +00008234 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008235 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008236
Matt Carlson699c0192010-12-06 08:28:51 +00008237 /* Enable MAC control of LPI */
8238 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8239 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8240 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8241 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8242
8243 tw32_f(TG3_CPMU_EEE_CTRL,
8244 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8245
Matt Carlsona386b902010-12-06 08:28:53 +00008246 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8247 TG3_CPMU_EEEMD_LPI_IN_TX |
8248 TG3_CPMU_EEEMD_LPI_IN_RX |
8249 TG3_CPMU_EEEMD_EEE_ENABLE;
8250
8251 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8252 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8253
Joe Perches63c3a662011-04-26 08:12:10 +00008254 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008255 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8256
8257 tw32_f(TG3_CPMU_EEE_MODE, val);
8258
8259 tw32_f(TG3_CPMU_EEE_DBTMR1,
8260 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8261 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8262
8263 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008264 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008265 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008266 }
8267
Matt Carlson603f1172010-02-12 14:47:10 +00008268 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008269 tg3_phy_reset(tp);
8270
Linus Torvalds1da177e2005-04-16 15:20:36 -07008271 err = tg3_chip_reset(tp);
8272 if (err)
8273 return err;
8274
8275 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8276
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008277 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008278 val = tr32(TG3_CPMU_CTRL);
8279 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8280 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008281
8282 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8283 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8284 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8285 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8286
8287 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8288 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8289 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8290 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8291
8292 val = tr32(TG3_CPMU_HST_ACC);
8293 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8294 val |= CPMU_HST_ACC_MACCLK_6_25;
8295 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008296 }
8297
Matt Carlson33466d92009-04-20 06:57:41 +00008298 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8299 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8300 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8301 PCIE_PWR_MGMT_L1_THRESH_4MS;
8302 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008303
8304 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8305 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8306
8307 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008308
Matt Carlsonf40386c2009-11-02 14:24:02 +00008309 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8310 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008311 }
8312
Joe Perches63c3a662011-04-26 08:12:10 +00008313 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b05902010-01-20 16:58:02 +00008314 u32 grc_mode = tr32(GRC_MODE);
8315
8316 /* Access the lower 1K of PL PCIE block registers. */
8317 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8318 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8319
8320 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8321 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8322 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8323
8324 tw32(GRC_MODE, grc_mode);
8325 }
8326
Matt Carlson5093eed2010-11-24 08:31:45 +00008327 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8328 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8329 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008330
Matt Carlson5093eed2010-11-24 08:31:45 +00008331 /* Access the lower 1K of PL PCIE block registers. */
8332 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8333 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008334
Matt Carlson5093eed2010-11-24 08:31:45 +00008335 val = tr32(TG3_PCIE_TLDLPL_PORT +
8336 TG3_PCIE_PL_LO_PHYCTL5);
8337 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8338 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008339
Matt Carlson5093eed2010-11-24 08:31:45 +00008340 tw32(GRC_MODE, grc_mode);
8341 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008342
Matt Carlson1ff30a52011-05-19 12:12:46 +00008343 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8344 u32 grc_mode = tr32(GRC_MODE);
8345
8346 /* Access the lower 1K of DL PCIE block registers. */
8347 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8348 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8349
8350 val = tr32(TG3_PCIE_TLDLPL_PORT +
8351 TG3_PCIE_DL_LO_FTSMAX);
8352 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8353 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8354 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8355
8356 tw32(GRC_MODE, grc_mode);
8357 }
8358
Matt Carlsona977dbe2010-04-12 06:58:26 +00008359 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8360 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8361 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8362 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008363 }
8364
Linus Torvalds1da177e2005-04-16 15:20:36 -07008365 /* This works around an issue with Athlon chipsets on
8366 * B3 tigon3 silicon. This bit has no effect on any
8367 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008368 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008369 */
Joe Perches63c3a662011-04-26 08:12:10 +00008370 if (!tg3_flag(tp, CPMU_PRESENT)) {
8371 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008372 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8373 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008375
8376 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008377 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378 val = tr32(TG3PCI_PCISTATE);
8379 val |= PCISTATE_RETRY_SAME_DMA;
8380 tw32(TG3PCI_PCISTATE, val);
8381 }
8382
Joe Perches63c3a662011-04-26 08:12:10 +00008383 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008384 /* Allow reads and writes to the
8385 * APE register and memory space.
8386 */
8387 val = tr32(TG3PCI_PCISTATE);
8388 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00008389 PCISTATE_ALLOW_APE_SHMEM_WR |
8390 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008391 tw32(TG3PCI_PCISTATE, val);
8392 }
8393
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8395 /* Enable some hw fixes. */
8396 val = tr32(TG3PCI_MSI_DATA);
8397 val |= (1 << 26) | (1 << 28) | (1 << 29);
8398 tw32(TG3PCI_MSI_DATA, val);
8399 }
8400
8401 /* Descriptor ring init may make accesses to the
8402 * NIC SRAM area to setup the TX descriptors, so we
8403 * can only do this after the hardware has been
8404 * successfully reset.
8405 */
Michael Chan32d8c572006-07-25 16:38:29 -07008406 err = tg3_init_rings(tp);
8407 if (err)
8408 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008409
Joe Perches63c3a662011-04-26 08:12:10 +00008410 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008411 val = tr32(TG3PCI_DMA_RW_CTRL) &
8412 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008413 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8414 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008415 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8416 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8417 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008418 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8419 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8420 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008421 /* This value is determined during the probe time DMA
8422 * engine test, tg3_test_dma.
8423 */
8424 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008426
8427 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8428 GRC_MODE_4X_NIC_SEND_RINGS |
8429 GRC_MODE_NO_TX_PHDR_CSUM |
8430 GRC_MODE_NO_RX_PHDR_CSUM);
8431 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008432
8433 /* Pseudo-header checksum is done by hardware logic and not
8434 * the offload processers, so make the chip do the pseudo-
8435 * header checksums on receive. For transmit it is more
8436 * convenient to do the pseudo-header checksum in software
8437 * as Linux does that on transmit for us in all cases.
8438 */
8439 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008440
8441 tw32(GRC_MODE,
8442 tp->grc_mode |
8443 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8444
8445 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8446 val = tr32(GRC_MISC_CFG);
8447 val &= ~0xff;
8448 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8449 tw32(GRC_MISC_CFG, val);
8450
8451 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008452 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008453 /* Do nothing. */
8454 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8455 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8456 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8457 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8458 else
8459 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8460 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8461 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008462 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463 int fw_len;
8464
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008465 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8467 tw32(BUFMGR_MB_POOL_ADDR,
8468 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8469 tw32(BUFMGR_MB_POOL_SIZE,
8470 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008472
Michael Chan0f893dc2005-07-25 12:30:38 -07008473 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008474 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8475 tp->bufmgr_config.mbuf_read_dma_low_water);
8476 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8477 tp->bufmgr_config.mbuf_mac_rx_low_water);
8478 tw32(BUFMGR_MB_HIGH_WATER,
8479 tp->bufmgr_config.mbuf_high_water);
8480 } else {
8481 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8482 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8483 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8484 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8485 tw32(BUFMGR_MB_HIGH_WATER,
8486 tp->bufmgr_config.mbuf_high_water_jumbo);
8487 }
8488 tw32(BUFMGR_DMA_LOW_WATER,
8489 tp->bufmgr_config.dma_low_water);
8490 tw32(BUFMGR_DMA_HIGH_WATER,
8491 tp->bufmgr_config.dma_high_water);
8492
Matt Carlsond309a462010-09-30 10:34:31 +00008493 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8494 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8495 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008496 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8497 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8498 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8499 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008500 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 for (i = 0; i < 2000; i++) {
8502 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8503 break;
8504 udelay(10);
8505 }
8506 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008507 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008508 return -ENODEV;
8509 }
8510
Matt Carlsoneb07a942011-04-20 07:57:36 +00008511 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8512 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008513
Matt Carlsoneb07a942011-04-20 07:57:36 +00008514 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008515
8516 /* Initialize TG3_BDINFO's at:
8517 * RCVDBDI_STD_BD: standard eth size rx ring
8518 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8519 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8520 *
8521 * like so:
8522 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8523 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8524 * ring attribute flags
8525 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8526 *
8527 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8528 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8529 *
8530 * The size of each ring is fixed in the firmware, but the location is
8531 * configurable.
8532 */
8533 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008534 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008535 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008536 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008537 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008538 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8539 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008541 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008542 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8544 BDINFO_FLAGS_DISABLED);
8545
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008546 /* Program the jumbo buffer descriptor ring control
8547 * blocks on those devices that have them.
8548 */
Matt Carlsona0512942011-07-27 14:20:54 +00008549 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008550 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551
Joe Perches63c3a662011-04-26 08:12:10 +00008552 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008553 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008554 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008555 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008556 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008557 val = TG3_RX_JMB_RING_SIZE(tp) <<
8558 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008559 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008560 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008561 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008562 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008563 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8564 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565 } else {
8566 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8567 BDINFO_FLAGS_DISABLED);
8568 }
8569
Joe Perches63c3a662011-04-26 08:12:10 +00008570 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008571 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008572 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008573 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008574 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008575 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8576 val |= (TG3_RX_STD_DMA_SZ << 2);
8577 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008578 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008579 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008580 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008581
8582 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583
Matt Carlson411da642009-11-13 13:03:46 +00008584 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008585 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008586
Joe Perches63c3a662011-04-26 08:12:10 +00008587 tpr->rx_jmb_prod_idx =
8588 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008589 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590
Matt Carlson2d31eca2009-09-01 12:53:31 +00008591 tg3_rings_reset(tp);
8592
Linus Torvalds1da177e2005-04-16 15:20:36 -07008593 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008594 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008595
8596 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008597 tw32(MAC_RX_MTU_SIZE,
8598 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008599
8600 /* The slot time is changed by tg3_setup_phy if we
8601 * run at gigabit with half duplex.
8602 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008603 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8604 (6 << TX_LENGTHS_IPG_SHIFT) |
8605 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8606
8607 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8608 val |= tr32(MAC_TX_LENGTHS) &
8609 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8610 TX_LENGTHS_CNT_DWN_VAL_MSK);
8611
8612 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008613
8614 /* Receive rules. */
8615 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8616 tw32(RCVLPC_CONFIG, 0x0181);
8617
8618 /* Calculate RDMAC_MODE setting early, we need it to determine
8619 * the RCVLPC_STATE_ENABLE mask.
8620 */
8621 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8622 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8623 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8624 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8625 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008626
Matt Carlsondeabaac2010-11-24 08:31:50 +00008627 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008628 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8629
Matt Carlson57e69832008-05-25 23:48:31 -07008630 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008631 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8632 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008633 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8634 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8635 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8636
Matt Carlsonc5908932011-03-09 16:58:25 +00008637 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8638 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008639 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008640 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008641 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8642 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008643 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008644 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8645 }
8646 }
8647
Joe Perches63c3a662011-04-26 08:12:10 +00008648 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008649 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8650
Joe Perches63c3a662011-04-26 08:12:10 +00008651 if (tg3_flag(tp, HW_TSO_1) ||
8652 tg3_flag(tp, HW_TSO_2) ||
8653 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008654 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8655
Matt Carlson108a6c12011-05-19 12:12:47 +00008656 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008657 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008658 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8659 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008660
Matt Carlsonf2096f92011-04-05 14:22:48 +00008661 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8662 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8663
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008664 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8665 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8666 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8667 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008668 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008669 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008670 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8671 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008672 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8673 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8674 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8675 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8676 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8677 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008678 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008679 tw32(TG3_RDMA_RSRVCTRL_REG,
8680 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8681 }
8682
Matt Carlsond78b59f2011-04-05 14:22:46 +00008683 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8684 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008685 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8686 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8687 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8688 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8689 }
8690
Linus Torvalds1da177e2005-04-16 15:20:36 -07008691 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008692 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008693 val = tr32(RCVLPC_STATS_ENABLE);
8694 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8695 tw32(RCVLPC_STATS_ENABLE, val);
8696 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008697 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008698 val = tr32(RCVLPC_STATS_ENABLE);
8699 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8700 tw32(RCVLPC_STATS_ENABLE, val);
8701 } else {
8702 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8703 }
8704 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8705 tw32(SNDDATAI_STATSENAB, 0xffffff);
8706 tw32(SNDDATAI_STATSCTRL,
8707 (SNDDATAI_SCTRL_ENABLE |
8708 SNDDATAI_SCTRL_FASTUPD));
8709
8710 /* Setup host coalescing engine. */
8711 tw32(HOSTCC_MODE, 0);
8712 for (i = 0; i < 2000; i++) {
8713 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8714 break;
8715 udelay(10);
8716 }
8717
Michael Chand244c892005-07-05 14:42:33 -07008718 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008719
Joe Perches63c3a662011-04-26 08:12:10 +00008720 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008721 /* Status/statistics block address. See tg3_timer,
8722 * the tg3_periodic_fetch_stats call there, and
8723 * tg3_get_stats to see how this works for 5705/5750 chips.
8724 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008725 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8726 ((u64) tp->stats_mapping >> 32));
8727 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8728 ((u64) tp->stats_mapping & 0xffffffff));
8729 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008730
Linus Torvalds1da177e2005-04-16 15:20:36 -07008731 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008732
8733 /* Clear statistics and status block memory areas */
8734 for (i = NIC_SRAM_STATS_BLK;
8735 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8736 i += sizeof(u32)) {
8737 tg3_write_mem(tp, i, 0);
8738 udelay(40);
8739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008740 }
8741
8742 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8743
8744 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8745 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008746 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008747 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8748
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008749 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8750 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008751 /* reset to prevent losing 1st rx packet intermittently */
8752 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8753 udelay(10);
8754 }
8755
Matt Carlson3bda1252008-08-15 14:08:22 -07008756 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00008757 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8758 MAC_MODE_FHDE_ENABLE;
8759 if (tg3_flag(tp, ENABLE_APE))
8760 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008761 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008762 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008763 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8764 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008765 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8766 udelay(40);
8767
Michael Chan314fba32005-04-21 17:07:04 -07008768 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008769 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008770 * register to preserve the GPIO settings for LOMs. The GPIOs,
8771 * whether used as inputs or outputs, are set by boot code after
8772 * reset.
8773 */
Joe Perches63c3a662011-04-26 08:12:10 +00008774 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008775 u32 gpio_mask;
8776
Michael Chan9d26e212006-12-07 00:21:14 -08008777 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8778 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8779 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008780
8781 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8782 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8783 GRC_LCLCTRL_GPIO_OUTPUT3;
8784
Michael Chanaf36e6b2006-03-23 01:28:06 -08008785 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8786 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8787
Gary Zambranoaaf84462007-05-05 11:51:45 -07008788 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008789 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8790
8791 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008792 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008793 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8794 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008796 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8797 udelay(100);
8798
Joe Perches63c3a662011-04-26 08:12:10 +00008799 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008800 val = tr32(MSGINT_MODE);
8801 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8802 tw32(MSGINT_MODE, val);
8803 }
8804
Joe Perches63c3a662011-04-26 08:12:10 +00008805 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008806 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8807 udelay(40);
8808 }
8809
8810 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8811 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8812 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8813 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8814 WDMAC_MODE_LNGREAD_ENAB);
8815
Matt Carlsonc5908932011-03-09 16:58:25 +00008816 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8817 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008818 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008819 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8820 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8821 /* nothing */
8822 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008823 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008824 val |= WDMAC_MODE_RX_ACCEL;
8825 }
8826 }
8827
Michael Chand9ab5ad2006-03-20 22:27:35 -08008828 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008829 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008830 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008831
Matt Carlson788a0352009-11-02 14:26:03 +00008832 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8833 val |= WDMAC_MODE_BURST_ALL_DATA;
8834
Linus Torvalds1da177e2005-04-16 15:20:36 -07008835 tw32_f(WDMAC_MODE, val);
8836 udelay(40);
8837
Joe Perches63c3a662011-04-26 08:12:10 +00008838 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008839 u16 pcix_cmd;
8840
8841 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8842 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008843 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008844 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8845 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008846 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008847 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8848 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008849 }
Matt Carlson9974a352007-10-07 23:27:28 -07008850 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8851 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008852 }
8853
8854 tw32_f(RDMAC_MODE, rdmac_mode);
8855 udelay(40);
8856
8857 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008858 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008859 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008860
8861 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8862 tw32(SNDDATAC_MODE,
8863 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8864 else
8865 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8866
Linus Torvalds1da177e2005-04-16 15:20:36 -07008867 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8868 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008869 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008870 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008871 val |= RCVDBDI_MODE_LRG_RING_SZ;
8872 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008873 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008874 if (tg3_flag(tp, HW_TSO_1) ||
8875 tg3_flag(tp, HW_TSO_2) ||
8876 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008877 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008878 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008879 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008880 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8881 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008882 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8883
8884 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8885 err = tg3_load_5701_a0_firmware_fix(tp);
8886 if (err)
8887 return err;
8888 }
8889
Joe Perches63c3a662011-04-26 08:12:10 +00008890 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008891 err = tg3_load_tso_firmware(tp);
8892 if (err)
8893 return err;
8894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008895
8896 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008897
Joe Perches63c3a662011-04-26 08:12:10 +00008898 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008899 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8900 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008901
8902 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8903 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8904 tp->tx_mode &= ~val;
8905 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8906 }
8907
Linus Torvalds1da177e2005-04-16 15:20:36 -07008908 tw32_f(MAC_TX_MODE, tp->tx_mode);
8909 udelay(100);
8910
Joe Perches63c3a662011-04-26 08:12:10 +00008911 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson9d53fa12011-07-20 10:20:54 +00008912 int i = 0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008913 u32 reg = MAC_RSS_INDIR_TBL_0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008914
Matt Carlson9d53fa12011-07-20 10:20:54 +00008915 if (tp->irq_cnt == 2) {
8916 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i += 8) {
8917 tw32(reg, 0x0);
8918 reg += 4;
8919 }
8920 } else {
8921 u32 val;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008922
Matt Carlson9d53fa12011-07-20 10:20:54 +00008923 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8924 val = i % (tp->irq_cnt - 1);
8925 i++;
8926 for (; i % 8; i++) {
8927 val <<= 4;
8928 val |= (i % (tp->irq_cnt - 1));
8929 }
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008930 tw32(reg, val);
8931 reg += 4;
8932 }
8933 }
8934
8935 /* Setup the "secret" hash key. */
8936 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8937 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8938 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8939 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8940 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8941 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8942 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8943 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8944 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8945 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8946 }
8947
Linus Torvalds1da177e2005-04-16 15:20:36 -07008948 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008949 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008950 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8951
Joe Perches63c3a662011-04-26 08:12:10 +00008952 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008953 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8954 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8955 RX_MODE_RSS_IPV6_HASH_EN |
8956 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8957 RX_MODE_RSS_IPV4_HASH_EN |
8958 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8959
Linus Torvalds1da177e2005-04-16 15:20:36 -07008960 tw32_f(MAC_RX_MODE, tp->rx_mode);
8961 udelay(10);
8962
Linus Torvalds1da177e2005-04-16 15:20:36 -07008963 tw32(MAC_LED_CTRL, tp->led_ctrl);
8964
8965 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008966 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008967 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8968 udelay(10);
8969 }
8970 tw32_f(MAC_RX_MODE, tp->rx_mode);
8971 udelay(10);
8972
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008973 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008974 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008975 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008976 /* Set drive transmission level to 1.2V */
8977 /* only if the signal pre-emphasis bit is not set */
8978 val = tr32(MAC_SERDES_CFG);
8979 val &= 0xfffff000;
8980 val |= 0x880;
8981 tw32(MAC_SERDES_CFG, val);
8982 }
8983 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8984 tw32(MAC_SERDES_CFG, 0x616000);
8985 }
8986
8987 /* Prevent chip from dropping frames when flow control
8988 * is enabled.
8989 */
Matt Carlson666bc832010-01-20 16:58:03 +00008990 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8991 val = 1;
8992 else
8993 val = 2;
8994 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008995
8996 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008997 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008998 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00008999 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009000 }
9001
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009002 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009003 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009004 u32 tmp;
9005
9006 tmp = tr32(SERDES_RX_CTRL);
9007 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9008 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9009 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9010 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9011 }
9012
Joe Perches63c3a662011-04-26 08:12:10 +00009013 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00009014 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
9015 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07009016 tp->link_config.speed = tp->link_config.orig_speed;
9017 tp->link_config.duplex = tp->link_config.orig_duplex;
9018 tp->link_config.autoneg = tp->link_config.orig_autoneg;
9019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009020
Matt Carlsondd477002008-05-25 23:45:58 -07009021 err = tg3_setup_phy(tp, 0);
9022 if (err)
9023 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009024
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009025 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9026 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009027 u32 tmp;
9028
9029 /* Clear CRC stats. */
9030 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9031 tg3_writephy(tp, MII_TG3_TEST1,
9032 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009033 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009035 }
9036 }
9037
9038 __tg3_set_rx_mode(tp->dev);
9039
9040 /* Initialize receive rules. */
9041 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9042 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9043 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9044 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9045
Joe Perches63c3a662011-04-26 08:12:10 +00009046 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009047 limit = 8;
9048 else
9049 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009050 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009051 limit -= 4;
9052 switch (limit) {
9053 case 16:
9054 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9055 case 15:
9056 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9057 case 14:
9058 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9059 case 13:
9060 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9061 case 12:
9062 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9063 case 11:
9064 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9065 case 10:
9066 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9067 case 9:
9068 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9069 case 8:
9070 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9071 case 7:
9072 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9073 case 6:
9074 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9075 case 5:
9076 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9077 case 4:
9078 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9079 case 3:
9080 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9081 case 2:
9082 case 1:
9083
9084 default:
9085 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009087
Joe Perches63c3a662011-04-26 08:12:10 +00009088 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009089 /* Write our heartbeat update interval to APE. */
9090 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9091 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009092
Linus Torvalds1da177e2005-04-16 15:20:36 -07009093 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9094
Linus Torvalds1da177e2005-04-16 15:20:36 -07009095 return 0;
9096}
9097
9098/* Called at device open time to get the chip ready for
9099 * packet processing. Invoked with tp->lock held.
9100 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009101static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009102{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009103 tg3_switch_clocks(tp);
9104
9105 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9106
Matt Carlson2f751b62008-08-04 23:17:34 -07009107 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009108}
9109
9110#define TG3_STAT_ADD32(PSTAT, REG) \
9111do { u32 __val = tr32(REG); \
9112 (PSTAT)->low += __val; \
9113 if ((PSTAT)->low < __val) \
9114 (PSTAT)->high += 1; \
9115} while (0)
9116
9117static void tg3_periodic_fetch_stats(struct tg3 *tp)
9118{
9119 struct tg3_hw_stats *sp = tp->hw_stats;
9120
9121 if (!netif_carrier_ok(tp->dev))
9122 return;
9123
9124 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9125 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9126 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9127 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9128 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9129 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9130 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9131 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9132 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9133 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9134 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9135 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9136 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9137
9138 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9139 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9140 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9141 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9142 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9143 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9144 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9145 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9146 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9147 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9148 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9149 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9150 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9151 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009152
9153 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009154 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9155 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9156 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009157 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9158 } else {
9159 u32 val = tr32(HOSTCC_FLOW_ATTN);
9160 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9161 if (val) {
9162 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9163 sp->rx_discards.low += val;
9164 if (sp->rx_discards.low < val)
9165 sp->rx_discards.high += 1;
9166 }
9167 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9168 }
Michael Chan463d3052006-05-22 16:36:27 -07009169 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009170}
9171
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009172static void tg3_chk_missed_msi(struct tg3 *tp)
9173{
9174 u32 i;
9175
9176 for (i = 0; i < tp->irq_cnt; i++) {
9177 struct tg3_napi *tnapi = &tp->napi[i];
9178
9179 if (tg3_has_work(tnapi)) {
9180 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9181 tnapi->last_tx_cons == tnapi->tx_cons) {
9182 if (tnapi->chk_msi_cnt < 1) {
9183 tnapi->chk_msi_cnt++;
9184 return;
9185 }
9186 tw32_mailbox(tnapi->int_mbox,
9187 tnapi->last_tag << 24);
9188 }
9189 }
9190 tnapi->chk_msi_cnt = 0;
9191 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9192 tnapi->last_tx_cons = tnapi->tx_cons;
9193 }
9194}
9195
Linus Torvalds1da177e2005-04-16 15:20:36 -07009196static void tg3_timer(unsigned long __opaque)
9197{
9198 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009199
Michael Chanf475f162006-03-27 23:20:14 -08009200 if (tp->irq_sync)
9201 goto restart_timer;
9202
David S. Millerf47c11e2005-06-24 20:18:35 -07009203 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009204
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009205 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
9206 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
9207 tg3_chk_missed_msi(tp);
9208
Joe Perches63c3a662011-04-26 08:12:10 +00009209 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009210 /* All of this garbage is because when using non-tagged
9211 * IRQ status the mailbox/status_block protocol the chip
9212 * uses with the cpu is race prone.
9213 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009214 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009215 tw32(GRC_LOCAL_CTRL,
9216 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9217 } else {
9218 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009219 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009221
David S. Millerfac9b832005-05-18 22:46:34 -07009222 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +00009223 tg3_flag_set(tp, RESTART_TIMER);
David S. Millerf47c11e2005-06-24 20:18:35 -07009224 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07009225 schedule_work(&tp->reset_task);
9226 return;
9227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009228 }
9229
Linus Torvalds1da177e2005-04-16 15:20:36 -07009230 /* This part only runs once per second. */
9231 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009232 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009233 tg3_periodic_fetch_stats(tp);
9234
Matt Carlsonb0c59432011-05-19 12:12:48 +00009235 if (tp->setlpicnt && !--tp->setlpicnt)
9236 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009237
Joe Perches63c3a662011-04-26 08:12:10 +00009238 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009239 u32 mac_stat;
9240 int phy_event;
9241
9242 mac_stat = tr32(MAC_STATUS);
9243
9244 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009245 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009246 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9247 phy_event = 1;
9248 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9249 phy_event = 1;
9250
9251 if (phy_event)
9252 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009253 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009254 u32 mac_stat = tr32(MAC_STATUS);
9255 int need_setup = 0;
9256
9257 if (netif_carrier_ok(tp->dev) &&
9258 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9259 need_setup = 1;
9260 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009261 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009262 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9263 MAC_STATUS_SIGNAL_DET))) {
9264 need_setup = 1;
9265 }
9266 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009267 if (!tp->serdes_counter) {
9268 tw32_f(MAC_MODE,
9269 (tp->mac_mode &
9270 ~MAC_MODE_PORT_MODE_MASK));
9271 udelay(40);
9272 tw32_f(MAC_MODE, tp->mac_mode);
9273 udelay(40);
9274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009275 tg3_setup_phy(tp, 0);
9276 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009277 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009278 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009279 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009281
9282 tp->timer_counter = tp->timer_multiplier;
9283 }
9284
Michael Chan130b8e42006-09-27 16:00:40 -07009285 /* Heartbeat is only sent once every 2 seconds.
9286 *
9287 * The heartbeat is to tell the ASF firmware that the host
9288 * driver is still alive. In the event that the OS crashes,
9289 * ASF needs to reset the hardware to free up the FIFO space
9290 * that may be filled with rx packets destined for the host.
9291 * If the FIFO is full, ASF will no longer function properly.
9292 *
9293 * Unintended resets have been reported on real time kernels
9294 * where the timer doesn't run on time. Netpoll will also have
9295 * same problem.
9296 *
9297 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9298 * to check the ring condition when the heartbeat is expiring
9299 * before doing the reset. This will prevent most unintended
9300 * resets.
9301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009302 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009303 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009304 tg3_wait_for_event_ack(tp);
9305
Michael Chanbbadf502006-04-06 21:46:34 -07009306 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009307 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009308 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009309 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9310 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009311
9312 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009313 }
9314 tp->asf_counter = tp->asf_multiplier;
9315 }
9316
David S. Millerf47c11e2005-06-24 20:18:35 -07009317 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009318
Michael Chanf475f162006-03-27 23:20:14 -08009319restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009320 tp->timer.expires = jiffies + tp->timer_offset;
9321 add_timer(&tp->timer);
9322}
9323
Matt Carlson4f125f42009-09-01 12:55:02 +00009324static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009325{
David Howells7d12e782006-10-05 14:55:46 +01009326 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009327 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009328 char *name;
9329 struct tg3_napi *tnapi = &tp->napi[irq_num];
9330
9331 if (tp->irq_cnt == 1)
9332 name = tp->dev->name;
9333 else {
9334 name = &tnapi->irq_lbl[0];
9335 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9336 name[IFNAMSIZ-1] = 0;
9337 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009338
Joe Perches63c3a662011-04-26 08:12:10 +00009339 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009340 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009341 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009342 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009343 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009344 } else {
9345 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009346 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009347 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009348 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009349 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009350
9351 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009352}
9353
Michael Chan79381092005-04-21 17:13:59 -07009354static int tg3_test_interrupt(struct tg3 *tp)
9355{
Matt Carlson09943a12009-08-28 14:01:57 +00009356 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009357 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009358 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009359 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009360
Michael Chand4bc3922005-05-29 14:59:20 -07009361 if (!netif_running(dev))
9362 return -ENODEV;
9363
Michael Chan79381092005-04-21 17:13:59 -07009364 tg3_disable_ints(tp);
9365
Matt Carlson4f125f42009-09-01 12:55:02 +00009366 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009367
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009368 /*
9369 * Turn off MSI one shot mode. Otherwise this test has no
9370 * observable way to know whether the interrupt was delivered.
9371 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009372 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009373 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9374 tw32(MSGINT_MODE, val);
9375 }
9376
Matt Carlson4f125f42009-09-01 12:55:02 +00009377 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009378 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009379 if (err)
9380 return err;
9381
Matt Carlson898a56f2009-08-28 14:02:40 +00009382 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009383 tg3_enable_ints(tp);
9384
9385 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009386 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009387
9388 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009389 u32 int_mbox, misc_host_ctrl;
9390
Matt Carlson898a56f2009-08-28 14:02:40 +00009391 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009392 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9393
9394 if ((int_mbox != 0) ||
9395 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9396 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009397 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009398 }
9399
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009400 if (tg3_flag(tp, 57765_PLUS) &&
9401 tnapi->hw_status->status_tag != tnapi->last_tag)
9402 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9403
Michael Chan79381092005-04-21 17:13:59 -07009404 msleep(10);
9405 }
9406
9407 tg3_disable_ints(tp);
9408
Matt Carlson4f125f42009-09-01 12:55:02 +00009409 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009410
Matt Carlson4f125f42009-09-01 12:55:02 +00009411 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009412
9413 if (err)
9414 return err;
9415
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009416 if (intr_ok) {
9417 /* Reenable MSI one shot mode. */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009418 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009419 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9420 tw32(MSGINT_MODE, val);
9421 }
Michael Chan79381092005-04-21 17:13:59 -07009422 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009423 }
Michael Chan79381092005-04-21 17:13:59 -07009424
9425 return -EIO;
9426}
9427
9428/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9429 * successfully restored
9430 */
9431static int tg3_test_msi(struct tg3 *tp)
9432{
Michael Chan79381092005-04-21 17:13:59 -07009433 int err;
9434 u16 pci_cmd;
9435
Joe Perches63c3a662011-04-26 08:12:10 +00009436 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009437 return 0;
9438
9439 /* Turn off SERR reporting in case MSI terminates with Master
9440 * Abort.
9441 */
9442 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9443 pci_write_config_word(tp->pdev, PCI_COMMAND,
9444 pci_cmd & ~PCI_COMMAND_SERR);
9445
9446 err = tg3_test_interrupt(tp);
9447
9448 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9449
9450 if (!err)
9451 return 0;
9452
9453 /* other failures */
9454 if (err != -EIO)
9455 return err;
9456
9457 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009458 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9459 "to INTx mode. Please report this failure to the PCI "
9460 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009461
Matt Carlson4f125f42009-09-01 12:55:02 +00009462 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009463
Michael Chan79381092005-04-21 17:13:59 -07009464 pci_disable_msi(tp->pdev);
9465
Joe Perches63c3a662011-04-26 08:12:10 +00009466 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009467 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009468
Matt Carlson4f125f42009-09-01 12:55:02 +00009469 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009470 if (err)
9471 return err;
9472
9473 /* Need to reset the chip because the MSI cycle may have terminated
9474 * with Master Abort.
9475 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009476 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009477
Michael Chan944d9802005-05-29 14:57:48 -07009478 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009479 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009480
David S. Millerf47c11e2005-06-24 20:18:35 -07009481 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009482
9483 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009484 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009485
9486 return err;
9487}
9488
Matt Carlson9e9fd122009-01-19 16:57:45 -08009489static int tg3_request_firmware(struct tg3 *tp)
9490{
9491 const __be32 *fw_data;
9492
9493 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009494 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9495 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009496 return -ENOENT;
9497 }
9498
9499 fw_data = (void *)tp->fw->data;
9500
9501 /* Firmware blob starts with version numbers, followed by
9502 * start address and _full_ length including BSS sections
9503 * (which must be longer than the actual data, of course
9504 */
9505
9506 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9507 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009508 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9509 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009510 release_firmware(tp->fw);
9511 tp->fw = NULL;
9512 return -EINVAL;
9513 }
9514
9515 /* We no longer need firmware; we have it. */
9516 tp->fw_needed = NULL;
9517 return 0;
9518}
9519
Matt Carlson679563f2009-09-01 12:55:46 +00009520static bool tg3_enable_msix(struct tg3 *tp)
9521{
9522 int i, rc, cpus = num_online_cpus();
9523 struct msix_entry msix_ent[tp->irq_max];
9524
9525 if (cpus == 1)
9526 /* Just fallback to the simpler MSI mode. */
9527 return false;
9528
9529 /*
9530 * We want as many rx rings enabled as there are cpus.
9531 * The first MSIX vector only deals with link interrupts, etc,
9532 * so we add one to the number of vectors we are requesting.
9533 */
9534 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9535
9536 for (i = 0; i < tp->irq_max; i++) {
9537 msix_ent[i].entry = i;
9538 msix_ent[i].vector = 0;
9539 }
9540
9541 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009542 if (rc < 0) {
9543 return false;
9544 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009545 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9546 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009547 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9548 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009549 tp->irq_cnt = rc;
9550 }
9551
9552 for (i = 0; i < tp->irq_max; i++)
9553 tp->napi[i].irq_vec = msix_ent[i].vector;
9554
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009555 netif_set_real_num_tx_queues(tp->dev, 1);
9556 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9557 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9558 pci_disable_msix(tp->pdev);
9559 return false;
9560 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009561
9562 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009563 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009564
9565 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9566 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009567 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009568 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9569 }
9570 }
Matt Carlson2430b032010-06-05 17:24:34 +00009571
Matt Carlson679563f2009-09-01 12:55:46 +00009572 return true;
9573}
9574
Matt Carlson07b01732009-08-28 14:01:15 +00009575static void tg3_ints_init(struct tg3 *tp)
9576{
Joe Perches63c3a662011-04-26 08:12:10 +00009577 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9578 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009579 /* All MSI supporting chips should support tagged
9580 * status. Assert that this is the case.
9581 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009582 netdev_warn(tp->dev,
9583 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009584 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009585 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009586
Joe Perches63c3a662011-04-26 08:12:10 +00009587 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9588 tg3_flag_set(tp, USING_MSIX);
9589 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9590 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009591
Joe Perches63c3a662011-04-26 08:12:10 +00009592 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009593 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009594 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009595 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009596 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9597 }
9598defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009599 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009600 tp->irq_cnt = 1;
9601 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009602 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009603 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009604 }
Matt Carlson07b01732009-08-28 14:01:15 +00009605}
9606
9607static void tg3_ints_fini(struct tg3 *tp)
9608{
Joe Perches63c3a662011-04-26 08:12:10 +00009609 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009610 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009611 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009612 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009613 tg3_flag_clear(tp, USING_MSI);
9614 tg3_flag_clear(tp, USING_MSIX);
9615 tg3_flag_clear(tp, ENABLE_RSS);
9616 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009617}
9618
Linus Torvalds1da177e2005-04-16 15:20:36 -07009619static int tg3_open(struct net_device *dev)
9620{
9621 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009622 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009623
Matt Carlson9e9fd122009-01-19 16:57:45 -08009624 if (tp->fw_needed) {
9625 err = tg3_request_firmware(tp);
9626 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9627 if (err)
9628 return err;
9629 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009630 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009631 tg3_flag_clear(tp, TSO_CAPABLE);
9632 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009633 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009634 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009635 }
9636 }
9637
Michael Chanc49a1562006-12-17 17:07:29 -08009638 netif_carrier_off(tp->dev);
9639
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009640 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009641 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009642 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009643
9644 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009645
Linus Torvalds1da177e2005-04-16 15:20:36 -07009646 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009647 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009648
David S. Millerf47c11e2005-06-24 20:18:35 -07009649 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009650
Matt Carlson679563f2009-09-01 12:55:46 +00009651 /*
9652 * Setup interrupts first so we know how
9653 * many NAPI resources to allocate
9654 */
9655 tg3_ints_init(tp);
9656
Linus Torvalds1da177e2005-04-16 15:20:36 -07009657 /* The placement of this call is tied
9658 * to the setup and use of Host TX descriptors.
9659 */
9660 err = tg3_alloc_consistent(tp);
9661 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009662 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009663
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009664 tg3_napi_init(tp);
9665
Matt Carlsonfed97812009-09-01 13:10:19 +00009666 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009667
Matt Carlson4f125f42009-09-01 12:55:02 +00009668 for (i = 0; i < tp->irq_cnt; i++) {
9669 struct tg3_napi *tnapi = &tp->napi[i];
9670 err = tg3_request_irq(tp, i);
9671 if (err) {
9672 for (i--; i >= 0; i--)
9673 free_irq(tnapi->irq_vec, tnapi);
9674 break;
9675 }
9676 }
Matt Carlson07b01732009-08-28 14:01:15 +00009677
9678 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009679 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009680
David S. Millerf47c11e2005-06-24 20:18:35 -07009681 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009682
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009683 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009684 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009685 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009686 tg3_free_rings(tp);
9687 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009688 if (tg3_flag(tp, TAGGED_STATUS) &&
9689 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9690 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765)
David S. Millerfac9b832005-05-18 22:46:34 -07009691 tp->timer_offset = HZ;
9692 else
9693 tp->timer_offset = HZ / 10;
9694
9695 BUG_ON(tp->timer_offset > HZ);
9696 tp->timer_counter = tp->timer_multiplier =
9697 (HZ / tp->timer_offset);
9698 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009699 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009700
9701 init_timer(&tp->timer);
9702 tp->timer.expires = jiffies + tp->timer_offset;
9703 tp->timer.data = (unsigned long) tp;
9704 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705 }
9706
David S. Millerf47c11e2005-06-24 20:18:35 -07009707 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009708
Matt Carlson07b01732009-08-28 14:01:15 +00009709 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009710 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009711
Joe Perches63c3a662011-04-26 08:12:10 +00009712 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009713 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009714
Michael Chan79381092005-04-21 17:13:59 -07009715 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009716 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009717 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009718 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009719 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009720
Matt Carlson679563f2009-09-01 12:55:46 +00009721 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009722 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009723
Joe Perches63c3a662011-04-26 08:12:10 +00009724 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009725 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009726
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009727 tw32(PCIE_TRANSACTION_CFG,
9728 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009729 }
Michael Chan79381092005-04-21 17:13:59 -07009730 }
9731
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009732 tg3_phy_start(tp);
9733
David S. Millerf47c11e2005-06-24 20:18:35 -07009734 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735
Michael Chan79381092005-04-21 17:13:59 -07009736 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009737 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009738 tg3_enable_ints(tp);
9739
David S. Millerf47c11e2005-06-24 20:18:35 -07009740 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009741
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009742 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009743
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009744 /*
9745 * Reset loopback feature if it was turned on while the device was down
9746 * make sure that it's installed properly now.
9747 */
9748 if (dev->features & NETIF_F_LOOPBACK)
9749 tg3_set_loopback(dev, dev->features);
9750
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009752
Matt Carlson679563f2009-09-01 12:55:46 +00009753err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009754 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9755 struct tg3_napi *tnapi = &tp->napi[i];
9756 free_irq(tnapi->irq_vec, tnapi);
9757 }
Matt Carlson07b01732009-08-28 14:01:15 +00009758
Matt Carlson679563f2009-09-01 12:55:46 +00009759err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009760 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009761 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009762 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009763
9764err_out1:
9765 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +00009766 tg3_frob_aux_power(tp, false);
9767 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +00009768 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009769}
9770
Eric Dumazet511d2222010-07-07 20:44:24 +00009771static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9772 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009773static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9774
9775static int tg3_close(struct net_device *dev)
9776{
Matt Carlson4f125f42009-09-01 12:55:02 +00009777 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009778 struct tg3 *tp = netdev_priv(dev);
9779
Matt Carlsonfed97812009-09-01 13:10:19 +00009780 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009781 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009782
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009783 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784
9785 del_timer_sync(&tp->timer);
9786
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009787 tg3_phy_stop(tp);
9788
David S. Millerf47c11e2005-06-24 20:18:35 -07009789 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790
9791 tg3_disable_ints(tp);
9792
Michael Chan944d9802005-05-29 14:57:48 -07009793 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009794 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009795 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796
David S. Millerf47c11e2005-06-24 20:18:35 -07009797 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009798
Matt Carlson4f125f42009-09-01 12:55:02 +00009799 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9800 struct tg3_napi *tnapi = &tp->napi[i];
9801 free_irq(tnapi->irq_vec, tnapi);
9802 }
Matt Carlson07b01732009-08-28 14:01:15 +00009803
9804 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805
Eric Dumazet511d2222010-07-07 20:44:24 +00009806 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9807
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9809 sizeof(tp->estats_prev));
9810
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009811 tg3_napi_fini(tp);
9812
Linus Torvalds1da177e2005-04-16 15:20:36 -07009813 tg3_free_consistent(tp);
9814
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009815 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009816
9817 netif_carrier_off(tp->dev);
9818
Linus Torvalds1da177e2005-04-16 15:20:36 -07009819 return 0;
9820}
9821
Eric Dumazet511d2222010-07-07 20:44:24 +00009822static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009823{
9824 return ((u64)val->high << 32) | ((u64)val->low);
9825}
9826
Eric Dumazet511d2222010-07-07 20:44:24 +00009827static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009828{
9829 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9830
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009831 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009832 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9833 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009834 u32 val;
9835
David S. Millerf47c11e2005-06-24 20:18:35 -07009836 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009837 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9838 tg3_writephy(tp, MII_TG3_TEST1,
9839 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009840 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009841 } else
9842 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009843 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009844
9845 tp->phy_crc_errors += val;
9846
9847 return tp->phy_crc_errors;
9848 }
9849
9850 return get_stat64(&hw_stats->rx_fcs_errors);
9851}
9852
9853#define ESTAT_ADD(member) \
9854 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009855 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009856
9857static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9858{
9859 struct tg3_ethtool_stats *estats = &tp->estats;
9860 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9861 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9862
9863 if (!hw_stats)
9864 return old_estats;
9865
9866 ESTAT_ADD(rx_octets);
9867 ESTAT_ADD(rx_fragments);
9868 ESTAT_ADD(rx_ucast_packets);
9869 ESTAT_ADD(rx_mcast_packets);
9870 ESTAT_ADD(rx_bcast_packets);
9871 ESTAT_ADD(rx_fcs_errors);
9872 ESTAT_ADD(rx_align_errors);
9873 ESTAT_ADD(rx_xon_pause_rcvd);
9874 ESTAT_ADD(rx_xoff_pause_rcvd);
9875 ESTAT_ADD(rx_mac_ctrl_rcvd);
9876 ESTAT_ADD(rx_xoff_entered);
9877 ESTAT_ADD(rx_frame_too_long_errors);
9878 ESTAT_ADD(rx_jabbers);
9879 ESTAT_ADD(rx_undersize_packets);
9880 ESTAT_ADD(rx_in_length_errors);
9881 ESTAT_ADD(rx_out_length_errors);
9882 ESTAT_ADD(rx_64_or_less_octet_packets);
9883 ESTAT_ADD(rx_65_to_127_octet_packets);
9884 ESTAT_ADD(rx_128_to_255_octet_packets);
9885 ESTAT_ADD(rx_256_to_511_octet_packets);
9886 ESTAT_ADD(rx_512_to_1023_octet_packets);
9887 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9888 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9889 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9890 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9891 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9892
9893 ESTAT_ADD(tx_octets);
9894 ESTAT_ADD(tx_collisions);
9895 ESTAT_ADD(tx_xon_sent);
9896 ESTAT_ADD(tx_xoff_sent);
9897 ESTAT_ADD(tx_flow_control);
9898 ESTAT_ADD(tx_mac_errors);
9899 ESTAT_ADD(tx_single_collisions);
9900 ESTAT_ADD(tx_mult_collisions);
9901 ESTAT_ADD(tx_deferred);
9902 ESTAT_ADD(tx_excessive_collisions);
9903 ESTAT_ADD(tx_late_collisions);
9904 ESTAT_ADD(tx_collide_2times);
9905 ESTAT_ADD(tx_collide_3times);
9906 ESTAT_ADD(tx_collide_4times);
9907 ESTAT_ADD(tx_collide_5times);
9908 ESTAT_ADD(tx_collide_6times);
9909 ESTAT_ADD(tx_collide_7times);
9910 ESTAT_ADD(tx_collide_8times);
9911 ESTAT_ADD(tx_collide_9times);
9912 ESTAT_ADD(tx_collide_10times);
9913 ESTAT_ADD(tx_collide_11times);
9914 ESTAT_ADD(tx_collide_12times);
9915 ESTAT_ADD(tx_collide_13times);
9916 ESTAT_ADD(tx_collide_14times);
9917 ESTAT_ADD(tx_collide_15times);
9918 ESTAT_ADD(tx_ucast_packets);
9919 ESTAT_ADD(tx_mcast_packets);
9920 ESTAT_ADD(tx_bcast_packets);
9921 ESTAT_ADD(tx_carrier_sense_errors);
9922 ESTAT_ADD(tx_discards);
9923 ESTAT_ADD(tx_errors);
9924
9925 ESTAT_ADD(dma_writeq_full);
9926 ESTAT_ADD(dma_write_prioq_full);
9927 ESTAT_ADD(rxbds_empty);
9928 ESTAT_ADD(rx_discards);
9929 ESTAT_ADD(rx_errors);
9930 ESTAT_ADD(rx_threshold_hit);
9931
9932 ESTAT_ADD(dma_readq_full);
9933 ESTAT_ADD(dma_read_prioq_full);
9934 ESTAT_ADD(tx_comp_queue_full);
9935
9936 ESTAT_ADD(ring_set_send_prod_index);
9937 ESTAT_ADD(ring_status_update);
9938 ESTAT_ADD(nic_irqs);
9939 ESTAT_ADD(nic_avoided_irqs);
9940 ESTAT_ADD(nic_tx_threshold_hit);
9941
Matt Carlson4452d092011-05-19 12:12:51 +00009942 ESTAT_ADD(mbuf_lwm_thresh_hit);
9943
Linus Torvalds1da177e2005-04-16 15:20:36 -07009944 return estats;
9945}
9946
Eric Dumazet511d2222010-07-07 20:44:24 +00009947static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9948 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009949{
9950 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009951 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009952 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9953
9954 if (!hw_stats)
9955 return old_stats;
9956
9957 stats->rx_packets = old_stats->rx_packets +
9958 get_stat64(&hw_stats->rx_ucast_packets) +
9959 get_stat64(&hw_stats->rx_mcast_packets) +
9960 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009961
Linus Torvalds1da177e2005-04-16 15:20:36 -07009962 stats->tx_packets = old_stats->tx_packets +
9963 get_stat64(&hw_stats->tx_ucast_packets) +
9964 get_stat64(&hw_stats->tx_mcast_packets) +
9965 get_stat64(&hw_stats->tx_bcast_packets);
9966
9967 stats->rx_bytes = old_stats->rx_bytes +
9968 get_stat64(&hw_stats->rx_octets);
9969 stats->tx_bytes = old_stats->tx_bytes +
9970 get_stat64(&hw_stats->tx_octets);
9971
9972 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009973 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009974 stats->tx_errors = old_stats->tx_errors +
9975 get_stat64(&hw_stats->tx_errors) +
9976 get_stat64(&hw_stats->tx_mac_errors) +
9977 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9978 get_stat64(&hw_stats->tx_discards);
9979
9980 stats->multicast = old_stats->multicast +
9981 get_stat64(&hw_stats->rx_mcast_packets);
9982 stats->collisions = old_stats->collisions +
9983 get_stat64(&hw_stats->tx_collisions);
9984
9985 stats->rx_length_errors = old_stats->rx_length_errors +
9986 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9987 get_stat64(&hw_stats->rx_undersize_packets);
9988
9989 stats->rx_over_errors = old_stats->rx_over_errors +
9990 get_stat64(&hw_stats->rxbds_empty);
9991 stats->rx_frame_errors = old_stats->rx_frame_errors +
9992 get_stat64(&hw_stats->rx_align_errors);
9993 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9994 get_stat64(&hw_stats->tx_discards);
9995 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9996 get_stat64(&hw_stats->tx_carrier_sense_errors);
9997
9998 stats->rx_crc_errors = old_stats->rx_crc_errors +
9999 calc_crc_errors(tp);
10000
John W. Linville4f63b872005-09-12 14:43:18 -070010001 stats->rx_missed_errors = old_stats->rx_missed_errors +
10002 get_stat64(&hw_stats->rx_discards);
10003
Eric Dumazetb0057c52010-10-10 19:55:52 +000010004 stats->rx_dropped = tp->rx_dropped;
10005
Linus Torvalds1da177e2005-04-16 15:20:36 -070010006 return stats;
10007}
10008
10009static inline u32 calc_crc(unsigned char *buf, int len)
10010{
10011 u32 reg;
10012 u32 tmp;
10013 int j, k;
10014
10015 reg = 0xffffffff;
10016
10017 for (j = 0; j < len; j++) {
10018 reg ^= buf[j];
10019
10020 for (k = 0; k < 8; k++) {
10021 tmp = reg & 0x01;
10022
10023 reg >>= 1;
10024
Matt Carlson859a5882010-04-05 10:19:28 +000010025 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010026 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010027 }
10028 }
10029
10030 return ~reg;
10031}
10032
10033static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
10034{
10035 /* accept or reject all multicast frames */
10036 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
10037 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
10038 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
10039 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
10040}
10041
10042static void __tg3_set_rx_mode(struct net_device *dev)
10043{
10044 struct tg3 *tp = netdev_priv(dev);
10045 u32 rx_mode;
10046
10047 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
10048 RX_MODE_KEEP_VLAN_TAG);
10049
Matt Carlsonbf933c82011-01-25 15:58:49 +000010050#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010051 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
10052 * flag clear.
10053 */
Joe Perches63c3a662011-04-26 08:12:10 +000010054 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010055 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
10056#endif
10057
10058 if (dev->flags & IFF_PROMISC) {
10059 /* Promiscuous mode. */
10060 rx_mode |= RX_MODE_PROMISC;
10061 } else if (dev->flags & IFF_ALLMULTI) {
10062 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010063 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +000010064 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010065 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010066 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010067 } else {
10068 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +000010069 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010070 u32 mc_filter[4] = { 0, };
10071 u32 regidx;
10072 u32 bit;
10073 u32 crc;
10074
Jiri Pirko22bedad2010-04-01 21:22:57 +000010075 netdev_for_each_mc_addr(ha, dev) {
10076 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010077 bit = ~crc & 0x7f;
10078 regidx = (bit & 0x60) >> 5;
10079 bit &= 0x1f;
10080 mc_filter[regidx] |= (1 << bit);
10081 }
10082
10083 tw32(MAC_HASH_REG_0, mc_filter[0]);
10084 tw32(MAC_HASH_REG_1, mc_filter[1]);
10085 tw32(MAC_HASH_REG_2, mc_filter[2]);
10086 tw32(MAC_HASH_REG_3, mc_filter[3]);
10087 }
10088
10089 if (rx_mode != tp->rx_mode) {
10090 tp->rx_mode = rx_mode;
10091 tw32_f(MAC_RX_MODE, rx_mode);
10092 udelay(10);
10093 }
10094}
10095
10096static void tg3_set_rx_mode(struct net_device *dev)
10097{
10098 struct tg3 *tp = netdev_priv(dev);
10099
Michael Chane75f7c92006-03-20 21:33:26 -080010100 if (!netif_running(dev))
10101 return;
10102
David S. Millerf47c11e2005-06-24 20:18:35 -070010103 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010104 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -070010105 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010106}
10107
Linus Torvalds1da177e2005-04-16 15:20:36 -070010108static int tg3_get_regs_len(struct net_device *dev)
10109{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010110 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111}
10112
10113static void tg3_get_regs(struct net_device *dev,
10114 struct ethtool_regs *regs, void *_p)
10115{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010116 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010117
10118 regs->version = 0;
10119
Matt Carlson97bd8e42011-04-13 11:05:04 +000010120 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121
Matt Carlson80096062010-08-02 11:26:06 +000010122 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010123 return;
10124
David S. Millerf47c11e2005-06-24 20:18:35 -070010125 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010126
Matt Carlson97bd8e42011-04-13 11:05:04 +000010127 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010128
David S. Millerf47c11e2005-06-24 20:18:35 -070010129 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010130}
10131
10132static int tg3_get_eeprom_len(struct net_device *dev)
10133{
10134 struct tg3 *tp = netdev_priv(dev);
10135
10136 return tp->nvram_size;
10137}
10138
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10140{
10141 struct tg3 *tp = netdev_priv(dev);
10142 int ret;
10143 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010144 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010145 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010146
Joe Perches63c3a662011-04-26 08:12:10 +000010147 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010148 return -EINVAL;
10149
Matt Carlson80096062010-08-02 11:26:06 +000010150 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010151 return -EAGAIN;
10152
Linus Torvalds1da177e2005-04-16 15:20:36 -070010153 offset = eeprom->offset;
10154 len = eeprom->len;
10155 eeprom->len = 0;
10156
10157 eeprom->magic = TG3_EEPROM_MAGIC;
10158
10159 if (offset & 3) {
10160 /* adjustments to start on required 4 byte boundary */
10161 b_offset = offset & 3;
10162 b_count = 4 - b_offset;
10163 if (b_count > len) {
10164 /* i.e. offset=1 len=2 */
10165 b_count = len;
10166 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010167 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010168 if (ret)
10169 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010170 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010171 len -= b_count;
10172 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010173 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010174 }
10175
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010176 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010177 pd = &data[eeprom->len];
10178 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010179 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010180 if (ret) {
10181 eeprom->len += i;
10182 return ret;
10183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010184 memcpy(pd + i, &val, 4);
10185 }
10186 eeprom->len += i;
10187
10188 if (len & 3) {
10189 /* read last bytes not ending on 4 byte boundary */
10190 pd = &data[eeprom->len];
10191 b_count = len & 3;
10192 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010193 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010194 if (ret)
10195 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010196 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010197 eeprom->len += b_count;
10198 }
10199 return 0;
10200}
10201
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010202static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010203
10204static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10205{
10206 struct tg3 *tp = netdev_priv(dev);
10207 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010208 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010209 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010210 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010211
Matt Carlson80096062010-08-02 11:26:06 +000010212 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010213 return -EAGAIN;
10214
Joe Perches63c3a662011-04-26 08:12:10 +000010215 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010216 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010217 return -EINVAL;
10218
10219 offset = eeprom->offset;
10220 len = eeprom->len;
10221
10222 if ((b_offset = (offset & 3))) {
10223 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010224 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010225 if (ret)
10226 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010227 len += b_offset;
10228 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010229 if (len < 4)
10230 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010231 }
10232
10233 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010234 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010235 /* adjustments to end on required 4 byte boundary */
10236 odd_len = 1;
10237 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010238 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010239 if (ret)
10240 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010241 }
10242
10243 buf = data;
10244 if (b_offset || odd_len) {
10245 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010246 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010247 return -ENOMEM;
10248 if (b_offset)
10249 memcpy(buf, &start, 4);
10250 if (odd_len)
10251 memcpy(buf+len-4, &end, 4);
10252 memcpy(buf + b_offset, data, eeprom->len);
10253 }
10254
10255 ret = tg3_nvram_write_block(tp, offset, len, buf);
10256
10257 if (buf != data)
10258 kfree(buf);
10259
10260 return ret;
10261}
10262
10263static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10264{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010265 struct tg3 *tp = netdev_priv(dev);
10266
Joe Perches63c3a662011-04-26 08:12:10 +000010267 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010268 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010269 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010270 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010271 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10272 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010273 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010274
Linus Torvalds1da177e2005-04-16 15:20:36 -070010275 cmd->supported = (SUPPORTED_Autoneg);
10276
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010277 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010278 cmd->supported |= (SUPPORTED_1000baseT_Half |
10279 SUPPORTED_1000baseT_Full);
10280
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010281 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010282 cmd->supported |= (SUPPORTED_100baseT_Half |
10283 SUPPORTED_100baseT_Full |
10284 SUPPORTED_10baseT_Half |
10285 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010286 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010287 cmd->port = PORT_TP;
10288 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010289 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010290 cmd->port = PORT_FIBRE;
10291 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010292
Linus Torvalds1da177e2005-04-16 15:20:36 -070010293 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010294 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10295 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10296 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10297 cmd->advertising |= ADVERTISED_Pause;
10298 } else {
10299 cmd->advertising |= ADVERTISED_Pause |
10300 ADVERTISED_Asym_Pause;
10301 }
10302 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10303 cmd->advertising |= ADVERTISED_Asym_Pause;
10304 }
10305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010306 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010307 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010308 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +000010309 } else {
David Decotigny70739492011-04-27 18:32:40 +000010310 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010311 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010312 }
Matt Carlson882e9792009-09-01 13:21:36 +000010313 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010314 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010315 cmd->autoneg = tp->link_config.autoneg;
10316 cmd->maxtxpkt = 0;
10317 cmd->maxrxpkt = 0;
10318 return 0;
10319}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010320
Linus Torvalds1da177e2005-04-16 15:20:36 -070010321static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10322{
10323 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010324 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010325
Joe Perches63c3a662011-04-26 08:12:10 +000010326 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010327 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010328 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010329 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010330 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10331 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010332 }
10333
Matt Carlson7e5856b2009-02-25 14:23:01 +000010334 if (cmd->autoneg != AUTONEG_ENABLE &&
10335 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010336 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010337
10338 if (cmd->autoneg == AUTONEG_DISABLE &&
10339 cmd->duplex != DUPLEX_FULL &&
10340 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010341 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010342
Matt Carlson7e5856b2009-02-25 14:23:01 +000010343 if (cmd->autoneg == AUTONEG_ENABLE) {
10344 u32 mask = ADVERTISED_Autoneg |
10345 ADVERTISED_Pause |
10346 ADVERTISED_Asym_Pause;
10347
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010348 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010349 mask |= ADVERTISED_1000baseT_Half |
10350 ADVERTISED_1000baseT_Full;
10351
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010352 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010353 mask |= ADVERTISED_100baseT_Half |
10354 ADVERTISED_100baseT_Full |
10355 ADVERTISED_10baseT_Half |
10356 ADVERTISED_10baseT_Full |
10357 ADVERTISED_TP;
10358 else
10359 mask |= ADVERTISED_FIBRE;
10360
10361 if (cmd->advertising & ~mask)
10362 return -EINVAL;
10363
10364 mask &= (ADVERTISED_1000baseT_Half |
10365 ADVERTISED_1000baseT_Full |
10366 ADVERTISED_100baseT_Half |
10367 ADVERTISED_100baseT_Full |
10368 ADVERTISED_10baseT_Half |
10369 ADVERTISED_10baseT_Full);
10370
10371 cmd->advertising &= mask;
10372 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010373 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010374 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010375 return -EINVAL;
10376
10377 if (cmd->duplex != DUPLEX_FULL)
10378 return -EINVAL;
10379 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010380 if (speed != SPEED_100 &&
10381 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010382 return -EINVAL;
10383 }
10384 }
10385
David S. Millerf47c11e2005-06-24 20:18:35 -070010386 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010387
10388 tp->link_config.autoneg = cmd->autoneg;
10389 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010390 tp->link_config.advertising = (cmd->advertising |
10391 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010392 tp->link_config.speed = SPEED_INVALID;
10393 tp->link_config.duplex = DUPLEX_INVALID;
10394 } else {
10395 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010396 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010397 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010398 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010399
Michael Chan24fcad62006-12-17 17:06:46 -080010400 tp->link_config.orig_speed = tp->link_config.speed;
10401 tp->link_config.orig_duplex = tp->link_config.duplex;
10402 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10403
Linus Torvalds1da177e2005-04-16 15:20:36 -070010404 if (netif_running(dev))
10405 tg3_setup_phy(tp, 1);
10406
David S. Millerf47c11e2005-06-24 20:18:35 -070010407 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010408
Linus Torvalds1da177e2005-04-16 15:20:36 -070010409 return 0;
10410}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010411
Linus Torvalds1da177e2005-04-16 15:20:36 -070010412static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10413{
10414 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010415
Linus Torvalds1da177e2005-04-16 15:20:36 -070010416 strcpy(info->driver, DRV_MODULE_NAME);
10417 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010418 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010419 strcpy(info->bus_info, pci_name(tp->pdev));
10420}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010421
Linus Torvalds1da177e2005-04-16 15:20:36 -070010422static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10423{
10424 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010425
Joe Perches63c3a662011-04-26 08:12:10 +000010426 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010427 wol->supported = WAKE_MAGIC;
10428 else
10429 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010430 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010431 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010432 wol->wolopts = WAKE_MAGIC;
10433 memset(&wol->sopass, 0, sizeof(wol->sopass));
10434}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010435
Linus Torvalds1da177e2005-04-16 15:20:36 -070010436static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10437{
10438 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010439 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010440
Linus Torvalds1da177e2005-04-16 15:20:36 -070010441 if (wol->wolopts & ~WAKE_MAGIC)
10442 return -EINVAL;
10443 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010444 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010445 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010446
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010447 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10448
David S. Millerf47c11e2005-06-24 20:18:35 -070010449 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010450 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010451 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010452 else
Joe Perches63c3a662011-04-26 08:12:10 +000010453 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010454 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010455
Linus Torvalds1da177e2005-04-16 15:20:36 -070010456 return 0;
10457}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010458
Linus Torvalds1da177e2005-04-16 15:20:36 -070010459static u32 tg3_get_msglevel(struct net_device *dev)
10460{
10461 struct tg3 *tp = netdev_priv(dev);
10462 return tp->msg_enable;
10463}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010464
Linus Torvalds1da177e2005-04-16 15:20:36 -070010465static void tg3_set_msglevel(struct net_device *dev, u32 value)
10466{
10467 struct tg3 *tp = netdev_priv(dev);
10468 tp->msg_enable = value;
10469}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010470
Linus Torvalds1da177e2005-04-16 15:20:36 -070010471static int tg3_nway_reset(struct net_device *dev)
10472{
10473 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010474 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010475
Linus Torvalds1da177e2005-04-16 15:20:36 -070010476 if (!netif_running(dev))
10477 return -EAGAIN;
10478
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010479 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010480 return -EINVAL;
10481
Joe Perches63c3a662011-04-26 08:12:10 +000010482 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010483 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010484 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010485 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010486 } else {
10487 u32 bmcr;
10488
10489 spin_lock_bh(&tp->lock);
10490 r = -EINVAL;
10491 tg3_readphy(tp, MII_BMCR, &bmcr);
10492 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10493 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010494 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010495 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10496 BMCR_ANENABLE);
10497 r = 0;
10498 }
10499 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010500 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010501
Linus Torvalds1da177e2005-04-16 15:20:36 -070010502 return r;
10503}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010504
Linus Torvalds1da177e2005-04-16 15:20:36 -070010505static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10506{
10507 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010508
Matt Carlson2c49a442010-09-30 10:34:35 +000010509 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010510 ering->rx_mini_max_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010511 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010512 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010513 else
10514 ering->rx_jumbo_max_pending = 0;
10515
10516 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010517
10518 ering->rx_pending = tp->rx_pending;
10519 ering->rx_mini_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010520 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010521 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10522 else
10523 ering->rx_jumbo_pending = 0;
10524
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010525 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010526}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010527
Linus Torvalds1da177e2005-04-16 15:20:36 -070010528static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10529{
10530 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010531 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010532
Matt Carlson2c49a442010-09-30 10:34:35 +000010533 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10534 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010535 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10536 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010537 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010538 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010539 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010540
Michael Chanbbe832c2005-06-24 20:20:04 -070010541 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010542 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010543 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010544 irq_sync = 1;
10545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010546
Michael Chanbbe832c2005-06-24 20:20:04 -070010547 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010548
Linus Torvalds1da177e2005-04-16 15:20:36 -070010549 tp->rx_pending = ering->rx_pending;
10550
Joe Perches63c3a662011-04-26 08:12:10 +000010551 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010552 tp->rx_pending > 63)
10553 tp->rx_pending = 63;
10554 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010555
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010556 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010557 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010558
10559 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010560 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010561 err = tg3_restart_hw(tp, 1);
10562 if (!err)
10563 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010564 }
10565
David S. Millerf47c11e2005-06-24 20:18:35 -070010566 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010567
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010568 if (irq_sync && !err)
10569 tg3_phy_start(tp);
10570
Michael Chanb9ec6c12006-07-25 16:37:27 -070010571 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010572}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010573
Linus Torvalds1da177e2005-04-16 15:20:36 -070010574static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10575{
10576 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010577
Joe Perches63c3a662011-04-26 08:12:10 +000010578 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010579
Steve Glendinninge18ce342008-12-16 02:00:00 -080010580 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010581 epause->rx_pause = 1;
10582 else
10583 epause->rx_pause = 0;
10584
Steve Glendinninge18ce342008-12-16 02:00:00 -080010585 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010586 epause->tx_pause = 1;
10587 else
10588 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010589}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010590
Linus Torvalds1da177e2005-04-16 15:20:36 -070010591static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10592{
10593 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010594 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010595
Joe Perches63c3a662011-04-26 08:12:10 +000010596 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010597 u32 newadv;
10598 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010599
Matt Carlson27121682010-02-17 15:16:57 +000010600 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010601
Matt Carlson27121682010-02-17 15:16:57 +000010602 if (!(phydev->supported & SUPPORTED_Pause) ||
10603 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010604 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010605 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010606
Matt Carlson27121682010-02-17 15:16:57 +000010607 tp->link_config.flowctrl = 0;
10608 if (epause->rx_pause) {
10609 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010610
Matt Carlson27121682010-02-17 15:16:57 +000010611 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010612 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010613 newadv = ADVERTISED_Pause;
10614 } else
10615 newadv = ADVERTISED_Pause |
10616 ADVERTISED_Asym_Pause;
10617 } else if (epause->tx_pause) {
10618 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10619 newadv = ADVERTISED_Asym_Pause;
10620 } else
10621 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010622
Matt Carlson27121682010-02-17 15:16:57 +000010623 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010624 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010625 else
Joe Perches63c3a662011-04-26 08:12:10 +000010626 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010627
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010628 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010629 u32 oldadv = phydev->advertising &
10630 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10631 if (oldadv != newadv) {
10632 phydev->advertising &=
10633 ~(ADVERTISED_Pause |
10634 ADVERTISED_Asym_Pause);
10635 phydev->advertising |= newadv;
10636 if (phydev->autoneg) {
10637 /*
10638 * Always renegotiate the link to
10639 * inform our link partner of our
10640 * flow control settings, even if the
10641 * flow control is forced. Let
10642 * tg3_adjust_link() do the final
10643 * flow control setup.
10644 */
10645 return phy_start_aneg(phydev);
10646 }
10647 }
10648
10649 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010650 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010651 } else {
10652 tp->link_config.orig_advertising &=
10653 ~(ADVERTISED_Pause |
10654 ADVERTISED_Asym_Pause);
10655 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010656 }
10657 } else {
10658 int irq_sync = 0;
10659
10660 if (netif_running(dev)) {
10661 tg3_netif_stop(tp);
10662 irq_sync = 1;
10663 }
10664
10665 tg3_full_lock(tp, irq_sync);
10666
10667 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010668 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010669 else
Joe Perches63c3a662011-04-26 08:12:10 +000010670 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010671 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010672 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010673 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010674 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010675 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010676 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010677 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010678 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010679
10680 if (netif_running(dev)) {
10681 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10682 err = tg3_restart_hw(tp, 1);
10683 if (!err)
10684 tg3_netif_start(tp);
10685 }
10686
10687 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010689
Michael Chanb9ec6c12006-07-25 16:37:27 -070010690 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010691}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010692
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010693static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010694{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010695 switch (sset) {
10696 case ETH_SS_TEST:
10697 return TG3_NUM_TEST;
10698 case ETH_SS_STATS:
10699 return TG3_NUM_STATS;
10700 default:
10701 return -EOPNOTSUPP;
10702 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010703}
10704
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010705static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010706{
10707 switch (stringset) {
10708 case ETH_SS_STATS:
10709 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10710 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010711 case ETH_SS_TEST:
10712 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10713 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010714 default:
10715 WARN_ON(1); /* we need a WARN() */
10716 break;
10717 }
10718}
10719
stephen hemminger81b87092011-04-04 08:43:50 +000010720static int tg3_set_phys_id(struct net_device *dev,
10721 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010722{
10723 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010724
10725 if (!netif_running(tp->dev))
10726 return -EAGAIN;
10727
stephen hemminger81b87092011-04-04 08:43:50 +000010728 switch (state) {
10729 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010730 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010731
stephen hemminger81b87092011-04-04 08:43:50 +000010732 case ETHTOOL_ID_ON:
10733 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10734 LED_CTRL_1000MBPS_ON |
10735 LED_CTRL_100MBPS_ON |
10736 LED_CTRL_10MBPS_ON |
10737 LED_CTRL_TRAFFIC_OVERRIDE |
10738 LED_CTRL_TRAFFIC_BLINK |
10739 LED_CTRL_TRAFFIC_LED);
10740 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010741
stephen hemminger81b87092011-04-04 08:43:50 +000010742 case ETHTOOL_ID_OFF:
10743 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10744 LED_CTRL_TRAFFIC_OVERRIDE);
10745 break;
Michael Chan4009a932005-09-05 17:52:54 -070010746
stephen hemminger81b87092011-04-04 08:43:50 +000010747 case ETHTOOL_ID_INACTIVE:
10748 tw32(MAC_LED_CTRL, tp->led_ctrl);
10749 break;
Michael Chan4009a932005-09-05 17:52:54 -070010750 }
stephen hemminger81b87092011-04-04 08:43:50 +000010751
Michael Chan4009a932005-09-05 17:52:54 -070010752 return 0;
10753}
10754
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010755static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010756 struct ethtool_stats *estats, u64 *tmp_stats)
10757{
10758 struct tg3 *tp = netdev_priv(dev);
10759 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10760}
10761
Matt Carlson535a4902011-07-20 10:20:56 +000010762static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000010763{
10764 int i;
10765 __be32 *buf;
10766 u32 offset = 0, len = 0;
10767 u32 magic, val;
10768
Joe Perches63c3a662011-04-26 08:12:10 +000010769 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010770 return NULL;
10771
10772 if (magic == TG3_EEPROM_MAGIC) {
10773 for (offset = TG3_NVM_DIR_START;
10774 offset < TG3_NVM_DIR_END;
10775 offset += TG3_NVM_DIRENT_SIZE) {
10776 if (tg3_nvram_read(tp, offset, &val))
10777 return NULL;
10778
10779 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10780 TG3_NVM_DIRTYPE_EXTVPD)
10781 break;
10782 }
10783
10784 if (offset != TG3_NVM_DIR_END) {
10785 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10786 if (tg3_nvram_read(tp, offset + 4, &offset))
10787 return NULL;
10788
10789 offset = tg3_nvram_logical_addr(tp, offset);
10790 }
10791 }
10792
10793 if (!offset || !len) {
10794 offset = TG3_NVM_VPD_OFF;
10795 len = TG3_NVM_VPD_LEN;
10796 }
10797
10798 buf = kmalloc(len, GFP_KERNEL);
10799 if (buf == NULL)
10800 return NULL;
10801
10802 if (magic == TG3_EEPROM_MAGIC) {
10803 for (i = 0; i < len; i += 4) {
10804 /* The data is in little-endian format in NVRAM.
10805 * Use the big-endian read routines to preserve
10806 * the byte order as it exists in NVRAM.
10807 */
10808 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10809 goto error;
10810 }
10811 } else {
10812 u8 *ptr;
10813 ssize_t cnt;
10814 unsigned int pos = 0;
10815
10816 ptr = (u8 *)&buf[0];
10817 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10818 cnt = pci_read_vpd(tp->pdev, pos,
10819 len - pos, ptr);
10820 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10821 cnt = 0;
10822 else if (cnt < 0)
10823 goto error;
10824 }
10825 if (pos != len)
10826 goto error;
10827 }
10828
Matt Carlson535a4902011-07-20 10:20:56 +000010829 *vpdlen = len;
10830
Matt Carlsonc3e94502011-04-13 11:05:08 +000010831 return buf;
10832
10833error:
10834 kfree(buf);
10835 return NULL;
10836}
10837
Michael Chan566f86a2005-05-29 14:56:58 -070010838#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010839#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10840#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10841#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010842#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10843#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000010844#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070010845#define NVRAM_SELFBOOT_HW_SIZE 0x20
10846#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010847
10848static int tg3_test_nvram(struct tg3 *tp)
10849{
Matt Carlson535a4902011-07-20 10:20:56 +000010850 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010851 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010852 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010853
Joe Perches63c3a662011-04-26 08:12:10 +000010854 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010855 return 0;
10856
Matt Carlsone4f34112009-02-25 14:25:00 +000010857 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010858 return -EIO;
10859
Michael Chan1b277772006-03-20 22:27:48 -080010860 if (magic == TG3_EEPROM_MAGIC)
10861 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010862 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010863 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10864 TG3_EEPROM_SB_FORMAT_1) {
10865 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10866 case TG3_EEPROM_SB_REVISION_0:
10867 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10868 break;
10869 case TG3_EEPROM_SB_REVISION_2:
10870 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10871 break;
10872 case TG3_EEPROM_SB_REVISION_3:
10873 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10874 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010875 case TG3_EEPROM_SB_REVISION_4:
10876 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10877 break;
10878 case TG3_EEPROM_SB_REVISION_5:
10879 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10880 break;
10881 case TG3_EEPROM_SB_REVISION_6:
10882 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10883 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010884 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010885 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010886 }
10887 } else
Michael Chan1b277772006-03-20 22:27:48 -080010888 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010889 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10890 size = NVRAM_SELFBOOT_HW_SIZE;
10891 else
Michael Chan1b277772006-03-20 22:27:48 -080010892 return -EIO;
10893
10894 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010895 if (buf == NULL)
10896 return -ENOMEM;
10897
Michael Chan1b277772006-03-20 22:27:48 -080010898 err = -EIO;
10899 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010900 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10901 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010902 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010903 }
Michael Chan1b277772006-03-20 22:27:48 -080010904 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010905 goto out;
10906
Michael Chan1b277772006-03-20 22:27:48 -080010907 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010908 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010909 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010910 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010911 u8 *buf8 = (u8 *) buf, csum8 = 0;
10912
Al Virob9fc7dc2007-12-17 22:59:57 -080010913 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010914 TG3_EEPROM_SB_REVISION_2) {
10915 /* For rev 2, the csum doesn't include the MBA. */
10916 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10917 csum8 += buf8[i];
10918 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10919 csum8 += buf8[i];
10920 } else {
10921 for (i = 0; i < size; i++)
10922 csum8 += buf8[i];
10923 }
Michael Chan1b277772006-03-20 22:27:48 -080010924
Adrian Bunkad96b482006-04-05 22:21:04 -070010925 if (csum8 == 0) {
10926 err = 0;
10927 goto out;
10928 }
10929
10930 err = -EIO;
10931 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010932 }
Michael Chan566f86a2005-05-29 14:56:58 -070010933
Al Virob9fc7dc2007-12-17 22:59:57 -080010934 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010935 TG3_EEPROM_MAGIC_HW) {
10936 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010937 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010938 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010939
10940 /* Separate the parity bits and the data bytes. */
10941 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10942 if ((i == 0) || (i == 8)) {
10943 int l;
10944 u8 msk;
10945
10946 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10947 parity[k++] = buf8[i] & msk;
10948 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010949 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010950 int l;
10951 u8 msk;
10952
10953 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10954 parity[k++] = buf8[i] & msk;
10955 i++;
10956
10957 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10958 parity[k++] = buf8[i] & msk;
10959 i++;
10960 }
10961 data[j++] = buf8[i];
10962 }
10963
10964 err = -EIO;
10965 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10966 u8 hw8 = hweight8(data[i]);
10967
10968 if ((hw8 & 0x1) && parity[i])
10969 goto out;
10970 else if (!(hw8 & 0x1) && !parity[i])
10971 goto out;
10972 }
10973 err = 0;
10974 goto out;
10975 }
10976
Matt Carlson01c3a392011-03-09 16:58:20 +000010977 err = -EIO;
10978
Michael Chan566f86a2005-05-29 14:56:58 -070010979 /* Bootstrap checksum at offset 0x10 */
10980 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010981 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010982 goto out;
10983
10984 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10985 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010986 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010987 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010988
Matt Carlsonc3e94502011-04-13 11:05:08 +000010989 kfree(buf);
10990
Matt Carlson535a4902011-07-20 10:20:56 +000010991 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000010992 if (!buf)
10993 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010994
Matt Carlson535a4902011-07-20 10:20:56 +000010995 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000010996 if (i > 0) {
10997 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10998 if (j < 0)
10999 goto out;
11000
Matt Carlson535a4902011-07-20 10:20:56 +000011001 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011002 goto out;
11003
11004 i += PCI_VPD_LRDT_TAG_SIZE;
11005 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11006 PCI_VPD_RO_KEYWORD_CHKSUM);
11007 if (j > 0) {
11008 u8 csum8 = 0;
11009
11010 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11011
11012 for (i = 0; i <= j; i++)
11013 csum8 += ((u8 *)buf)[i];
11014
11015 if (csum8)
11016 goto out;
11017 }
11018 }
11019
Michael Chan566f86a2005-05-29 14:56:58 -070011020 err = 0;
11021
11022out:
11023 kfree(buf);
11024 return err;
11025}
11026
Michael Chanca430072005-05-29 14:57:23 -070011027#define TG3_SERDES_TIMEOUT_SEC 2
11028#define TG3_COPPER_TIMEOUT_SEC 6
11029
11030static int tg3_test_link(struct tg3 *tp)
11031{
11032 int i, max;
11033
11034 if (!netif_running(tp->dev))
11035 return -ENODEV;
11036
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011037 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011038 max = TG3_SERDES_TIMEOUT_SEC;
11039 else
11040 max = TG3_COPPER_TIMEOUT_SEC;
11041
11042 for (i = 0; i < max; i++) {
11043 if (netif_carrier_ok(tp->dev))
11044 return 0;
11045
11046 if (msleep_interruptible(1000))
11047 break;
11048 }
11049
11050 return -EIO;
11051}
11052
Michael Chana71116d2005-05-29 14:58:11 -070011053/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011054static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011055{
Michael Chanb16250e2006-09-27 16:10:14 -070011056 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011057 u32 offset, read_mask, write_mask, val, save_val, read_val;
11058 static struct {
11059 u16 offset;
11060 u16 flags;
11061#define TG3_FL_5705 0x1
11062#define TG3_FL_NOT_5705 0x2
11063#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011064#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011065 u32 read_mask;
11066 u32 write_mask;
11067 } reg_tbl[] = {
11068 /* MAC Control Registers */
11069 { MAC_MODE, TG3_FL_NOT_5705,
11070 0x00000000, 0x00ef6f8c },
11071 { MAC_MODE, TG3_FL_5705,
11072 0x00000000, 0x01ef6b8c },
11073 { MAC_STATUS, TG3_FL_NOT_5705,
11074 0x03800107, 0x00000000 },
11075 { MAC_STATUS, TG3_FL_5705,
11076 0x03800100, 0x00000000 },
11077 { MAC_ADDR_0_HIGH, 0x0000,
11078 0x00000000, 0x0000ffff },
11079 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011080 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011081 { MAC_RX_MTU_SIZE, 0x0000,
11082 0x00000000, 0x0000ffff },
11083 { MAC_TX_MODE, 0x0000,
11084 0x00000000, 0x00000070 },
11085 { MAC_TX_LENGTHS, 0x0000,
11086 0x00000000, 0x00003fff },
11087 { MAC_RX_MODE, TG3_FL_NOT_5705,
11088 0x00000000, 0x000007fc },
11089 { MAC_RX_MODE, TG3_FL_5705,
11090 0x00000000, 0x000007dc },
11091 { MAC_HASH_REG_0, 0x0000,
11092 0x00000000, 0xffffffff },
11093 { MAC_HASH_REG_1, 0x0000,
11094 0x00000000, 0xffffffff },
11095 { MAC_HASH_REG_2, 0x0000,
11096 0x00000000, 0xffffffff },
11097 { MAC_HASH_REG_3, 0x0000,
11098 0x00000000, 0xffffffff },
11099
11100 /* Receive Data and Receive BD Initiator Control Registers. */
11101 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11102 0x00000000, 0xffffffff },
11103 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11104 0x00000000, 0xffffffff },
11105 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11106 0x00000000, 0x00000003 },
11107 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11108 0x00000000, 0xffffffff },
11109 { RCVDBDI_STD_BD+0, 0x0000,
11110 0x00000000, 0xffffffff },
11111 { RCVDBDI_STD_BD+4, 0x0000,
11112 0x00000000, 0xffffffff },
11113 { RCVDBDI_STD_BD+8, 0x0000,
11114 0x00000000, 0xffff0002 },
11115 { RCVDBDI_STD_BD+0xc, 0x0000,
11116 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011117
Michael Chana71116d2005-05-29 14:58:11 -070011118 /* Receive BD Initiator Control Registers. */
11119 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11120 0x00000000, 0xffffffff },
11121 { RCVBDI_STD_THRESH, TG3_FL_5705,
11122 0x00000000, 0x000003ff },
11123 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11124 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011125
Michael Chana71116d2005-05-29 14:58:11 -070011126 /* Host Coalescing Control Registers. */
11127 { HOSTCC_MODE, TG3_FL_NOT_5705,
11128 0x00000000, 0x00000004 },
11129 { HOSTCC_MODE, TG3_FL_5705,
11130 0x00000000, 0x000000f6 },
11131 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11132 0x00000000, 0xffffffff },
11133 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11134 0x00000000, 0x000003ff },
11135 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11136 0x00000000, 0xffffffff },
11137 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11138 0x00000000, 0x000003ff },
11139 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11140 0x00000000, 0xffffffff },
11141 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11142 0x00000000, 0x000000ff },
11143 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11144 0x00000000, 0xffffffff },
11145 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11146 0x00000000, 0x000000ff },
11147 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11148 0x00000000, 0xffffffff },
11149 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11150 0x00000000, 0xffffffff },
11151 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11152 0x00000000, 0xffffffff },
11153 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11154 0x00000000, 0x000000ff },
11155 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11156 0x00000000, 0xffffffff },
11157 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11158 0x00000000, 0x000000ff },
11159 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11160 0x00000000, 0xffffffff },
11161 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11162 0x00000000, 0xffffffff },
11163 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11164 0x00000000, 0xffffffff },
11165 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11166 0x00000000, 0xffffffff },
11167 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11168 0x00000000, 0xffffffff },
11169 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11170 0xffffffff, 0x00000000 },
11171 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11172 0xffffffff, 0x00000000 },
11173
11174 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011175 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011176 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011177 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011178 0x00000000, 0x007fffff },
11179 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11180 0x00000000, 0x0000003f },
11181 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11182 0x00000000, 0x000001ff },
11183 { BUFMGR_MB_HIGH_WATER, 0x0000,
11184 0x00000000, 0x000001ff },
11185 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11186 0xffffffff, 0x00000000 },
11187 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11188 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011189
Michael Chana71116d2005-05-29 14:58:11 -070011190 /* Mailbox Registers */
11191 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11192 0x00000000, 0x000001ff },
11193 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11194 0x00000000, 0x000001ff },
11195 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11196 0x00000000, 0x000007ff },
11197 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11198 0x00000000, 0x000001ff },
11199
11200 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11201 };
11202
Michael Chanb16250e2006-09-27 16:10:14 -070011203 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011204 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011205 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011206 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011207 is_5750 = 1;
11208 }
Michael Chana71116d2005-05-29 14:58:11 -070011209
11210 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11211 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11212 continue;
11213
11214 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11215 continue;
11216
Joe Perches63c3a662011-04-26 08:12:10 +000011217 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011218 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11219 continue;
11220
Michael Chanb16250e2006-09-27 16:10:14 -070011221 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11222 continue;
11223
Michael Chana71116d2005-05-29 14:58:11 -070011224 offset = (u32) reg_tbl[i].offset;
11225 read_mask = reg_tbl[i].read_mask;
11226 write_mask = reg_tbl[i].write_mask;
11227
11228 /* Save the original register content */
11229 save_val = tr32(offset);
11230
11231 /* Determine the read-only value. */
11232 read_val = save_val & read_mask;
11233
11234 /* Write zero to the register, then make sure the read-only bits
11235 * are not changed and the read/write bits are all zeros.
11236 */
11237 tw32(offset, 0);
11238
11239 val = tr32(offset);
11240
11241 /* Test the read-only and read/write bits. */
11242 if (((val & read_mask) != read_val) || (val & write_mask))
11243 goto out;
11244
11245 /* Write ones to all the bits defined by RdMask and WrMask, then
11246 * make sure the read-only bits are not changed and the
11247 * read/write bits are all ones.
11248 */
11249 tw32(offset, read_mask | write_mask);
11250
11251 val = tr32(offset);
11252
11253 /* Test the read-only bits. */
11254 if ((val & read_mask) != read_val)
11255 goto out;
11256
11257 /* Test the read/write bits. */
11258 if ((val & write_mask) != write_mask)
11259 goto out;
11260
11261 tw32(offset, save_val);
11262 }
11263
11264 return 0;
11265
11266out:
Michael Chan9f88f292006-12-07 00:22:54 -080011267 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011268 netdev_err(tp->dev,
11269 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011270 tw32(offset, save_val);
11271 return -EIO;
11272}
11273
Michael Chan7942e1d2005-05-29 14:58:36 -070011274static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11275{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011276 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011277 int i;
11278 u32 j;
11279
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011280 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011281 for (j = 0; j < len; j += 4) {
11282 u32 val;
11283
11284 tg3_write_mem(tp, offset + j, test_pattern[i]);
11285 tg3_read_mem(tp, offset + j, &val);
11286 if (val != test_pattern[i])
11287 return -EIO;
11288 }
11289 }
11290 return 0;
11291}
11292
11293static int tg3_test_memory(struct tg3 *tp)
11294{
11295 static struct mem_entry {
11296 u32 offset;
11297 u32 len;
11298 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011299 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011300 { 0x00002000, 0x1c000},
11301 { 0xffffffff, 0x00000}
11302 }, mem_tbl_5705[] = {
11303 { 0x00000100, 0x0000c},
11304 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011305 { 0x00004000, 0x00800},
11306 { 0x00006000, 0x01000},
11307 { 0x00008000, 0x02000},
11308 { 0x00010000, 0x0e000},
11309 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011310 }, mem_tbl_5755[] = {
11311 { 0x00000200, 0x00008},
11312 { 0x00004000, 0x00800},
11313 { 0x00006000, 0x00800},
11314 { 0x00008000, 0x02000},
11315 { 0x00010000, 0x0c000},
11316 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011317 }, mem_tbl_5906[] = {
11318 { 0x00000200, 0x00008},
11319 { 0x00004000, 0x00400},
11320 { 0x00006000, 0x00400},
11321 { 0x00008000, 0x01000},
11322 { 0x00010000, 0x01000},
11323 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011324 }, mem_tbl_5717[] = {
11325 { 0x00000200, 0x00008},
11326 { 0x00010000, 0x0a000},
11327 { 0x00020000, 0x13c00},
11328 { 0xffffffff, 0x00000}
11329 }, mem_tbl_57765[] = {
11330 { 0x00000200, 0x00008},
11331 { 0x00004000, 0x00800},
11332 { 0x00006000, 0x09800},
11333 { 0x00010000, 0x0a000},
11334 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011335 };
11336 struct mem_entry *mem_tbl;
11337 int err = 0;
11338 int i;
11339
Joe Perches63c3a662011-04-26 08:12:10 +000011340 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011341 mem_tbl = mem_tbl_5717;
11342 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11343 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011344 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011345 mem_tbl = mem_tbl_5755;
11346 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11347 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011348 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011349 mem_tbl = mem_tbl_5705;
11350 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011351 mem_tbl = mem_tbl_570x;
11352
11353 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011354 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11355 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011356 break;
11357 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011358
Michael Chan7942e1d2005-05-29 14:58:36 -070011359 return err;
11360}
11361
Matt Carlsonbb158d62011-04-25 12:42:47 +000011362#define TG3_TSO_MSS 500
11363
11364#define TG3_TSO_IP_HDR_LEN 20
11365#define TG3_TSO_TCP_HDR_LEN 20
11366#define TG3_TSO_TCP_OPT_LEN 12
11367
11368static const u8 tg3_tso_header[] = {
113690x08, 0x00,
113700x45, 0x00, 0x00, 0x00,
113710x00, 0x00, 0x40, 0x00,
113720x40, 0x06, 0x00, 0x00,
113730x0a, 0x00, 0x00, 0x01,
113740x0a, 0x00, 0x00, 0x02,
113750x0d, 0x00, 0xe0, 0x00,
113760x00, 0x00, 0x01, 0x00,
113770x00, 0x00, 0x02, 0x00,
113780x80, 0x10, 0x10, 0x00,
113790x14, 0x09, 0x00, 0x00,
113800x01, 0x01, 0x08, 0x0a,
113810x11, 0x11, 0x11, 0x11,
113820x11, 0x11, 0x11, 0x11,
11383};
Michael Chan9f40dea2005-09-05 17:53:06 -070011384
Matt Carlson28a45952011-08-19 13:58:22 +000011385static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011386{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011387 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011388 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011389 u32 budget;
Michael Chanc76949a2005-05-29 14:58:59 -070011390 struct sk_buff *skb, *rx_skb;
11391 u8 *tx_data;
11392 dma_addr_t map;
11393 int num_pkts, tx_len, rx_len, i, err;
11394 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011395 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011396 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011397
Matt Carlsonc8873402010-02-12 14:47:11 +000011398 tnapi = &tp->napi[0];
11399 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011400 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011401 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011402 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011403 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011404 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011405 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011406 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011407
Michael Chanc76949a2005-05-29 14:58:59 -070011408 err = -EIO;
11409
Matt Carlson4852a862011-04-13 11:05:07 +000011410 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011411 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011412 if (!skb)
11413 return -ENOMEM;
11414
Michael Chanc76949a2005-05-29 14:58:59 -070011415 tx_data = skb_put(skb, tx_len);
11416 memcpy(tx_data, tp->dev->dev_addr, 6);
11417 memset(tx_data + 6, 0x0, 8);
11418
Matt Carlson4852a862011-04-13 11:05:07 +000011419 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011420
Matt Carlson28a45952011-08-19 13:58:22 +000011421 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011422 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11423
11424 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11425 TG3_TSO_TCP_OPT_LEN;
11426
11427 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11428 sizeof(tg3_tso_header));
11429 mss = TG3_TSO_MSS;
11430
11431 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11432 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11433
11434 /* Set the total length field in the IP header */
11435 iph->tot_len = htons((u16)(mss + hdr_len));
11436
11437 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11438 TXD_FLAG_CPU_POST_DMA);
11439
Joe Perches63c3a662011-04-26 08:12:10 +000011440 if (tg3_flag(tp, HW_TSO_1) ||
11441 tg3_flag(tp, HW_TSO_2) ||
11442 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011443 struct tcphdr *th;
11444 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11445 th = (struct tcphdr *)&tx_data[val];
11446 th->check = 0;
11447 } else
11448 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11449
Joe Perches63c3a662011-04-26 08:12:10 +000011450 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011451 mss |= (hdr_len & 0xc) << 12;
11452 if (hdr_len & 0x10)
11453 base_flags |= 0x00000010;
11454 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011455 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011456 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011457 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011458 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11459 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11460 } else {
11461 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11462 }
11463
11464 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11465 } else {
11466 num_pkts = 1;
11467 data_off = ETH_HLEN;
11468 }
11469
11470 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011471 tx_data[i] = (u8) (i & 0xff);
11472
Alexander Duyckf4188d82009-12-02 16:48:38 +000011473 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11474 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011475 dev_kfree_skb(skb);
11476 return -EIO;
11477 }
Michael Chanc76949a2005-05-29 14:58:59 -070011478
Matt Carlson0d681b22011-07-27 14:20:49 +000011479 val = tnapi->tx_prod;
11480 tnapi->tx_buffers[val].skb = skb;
11481 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11482
Michael Chanc76949a2005-05-29 14:58:59 -070011483 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011484 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011485
11486 udelay(10);
11487
Matt Carlson898a56f2009-08-28 14:02:40 +000011488 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011489
Matt Carlson84b67b22011-07-27 14:20:52 +000011490 budget = tg3_tx_avail(tnapi);
11491 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011492 base_flags | TXD_FLAG_END, mss, 0)) {
11493 tnapi->tx_buffers[val].skb = NULL;
11494 dev_kfree_skb(skb);
11495 return -EIO;
11496 }
Michael Chanc76949a2005-05-29 14:58:59 -070011497
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011498 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011499
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011500 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11501 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011502
11503 udelay(10);
11504
Matt Carlson303fc922009-11-02 14:27:34 +000011505 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11506 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011507 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011508 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011509
11510 udelay(10);
11511
Matt Carlson898a56f2009-08-28 14:02:40 +000011512 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11513 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011514 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011515 (rx_idx == (rx_start_idx + num_pkts)))
11516 break;
11517 }
11518
Matt Carlson0d681b22011-07-27 14:20:49 +000011519 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, 0);
Michael Chanc76949a2005-05-29 14:58:59 -070011520 dev_kfree_skb(skb);
11521
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011522 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011523 goto out;
11524
11525 if (rx_idx != rx_start_idx + num_pkts)
11526 goto out;
11527
Matt Carlsonbb158d62011-04-25 12:42:47 +000011528 val = data_off;
11529 while (rx_idx != rx_start_idx) {
11530 desc = &rnapi->rx_rcb[rx_start_idx++];
11531 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11532 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011533
Matt Carlsonbb158d62011-04-25 12:42:47 +000011534 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11535 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011536 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011537
Matt Carlsonbb158d62011-04-25 12:42:47 +000011538 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11539 - ETH_FCS_LEN;
11540
Matt Carlson28a45952011-08-19 13:58:22 +000011541 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011542 if (rx_len != tx_len)
11543 goto out;
11544
11545 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11546 if (opaque_key != RXD_OPAQUE_RING_STD)
11547 goto out;
11548 } else {
11549 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11550 goto out;
11551 }
11552 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11553 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011554 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011555 goto out;
11556 }
11557
11558 if (opaque_key == RXD_OPAQUE_RING_STD) {
11559 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11560 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11561 mapping);
11562 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
11563 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11564 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11565 mapping);
11566 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011567 goto out;
11568
Matt Carlsonbb158d62011-04-25 12:42:47 +000011569 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11570 PCI_DMA_FROMDEVICE);
11571
11572 for (i = data_off; i < rx_len; i++, val++) {
11573 if (*(rx_skb->data + i) != (u8) (val & 0xff))
11574 goto out;
11575 }
Matt Carlson4852a862011-04-13 11:05:07 +000011576 }
11577
Michael Chanc76949a2005-05-29 14:58:59 -070011578 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011579
Michael Chanc76949a2005-05-29 14:58:59 -070011580 /* tg3_free_rings will unmap and free the rx_skb */
11581out:
11582 return err;
11583}
11584
Matt Carlson00c266b2011-04-25 12:42:46 +000011585#define TG3_STD_LOOPBACK_FAILED 1
11586#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011587#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011588#define TG3_LOOPBACK_FAILED \
11589 (TG3_STD_LOOPBACK_FAILED | \
11590 TG3_JMB_LOOPBACK_FAILED | \
11591 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011592
Matt Carlson941ec902011-08-19 13:58:23 +000011593static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011594{
Matt Carlson28a45952011-08-19 13:58:22 +000011595 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011596 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011597
Matt Carlsonab789042011-01-25 15:58:54 +000011598 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11599 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11600
Matt Carlson28a45952011-08-19 13:58:22 +000011601 if (!netif_running(tp->dev)) {
11602 data[0] = TG3_LOOPBACK_FAILED;
11603 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011604 if (do_extlpbk)
11605 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011606 goto done;
11607 }
11608
Michael Chanb9ec6c12006-07-25 16:37:27 -070011609 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011610 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011611 data[0] = TG3_LOOPBACK_FAILED;
11612 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011613 if (do_extlpbk)
11614 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011615 goto done;
11616 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011617
Joe Perches63c3a662011-04-26 08:12:10 +000011618 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011619 int i;
11620
11621 /* Reroute all rx packets to the 1st queue */
11622 for (i = MAC_RSS_INDIR_TBL_0;
11623 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11624 tw32(i, 0x0);
11625 }
11626
Matt Carlson6e01b202011-08-19 13:58:20 +000011627 /* HW errata - mac loopback fails in some cases on 5780.
11628 * Normal traffic and PHY loopback are not affected by
11629 * errata. Also, the MAC loopback test is deprecated for
11630 * all newer ASIC revisions.
11631 */
11632 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11633 !tg3_flag(tp, CPMU_PRESENT)) {
11634 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011635
Matt Carlson28a45952011-08-19 13:58:22 +000011636 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11637 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011638
11639 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011640 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11641 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011642
11643 tg3_mac_loopback(tp, false);
11644 }
Matt Carlson4852a862011-04-13 11:05:07 +000011645
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011646 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011647 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011648 int i;
11649
Matt Carlson941ec902011-08-19 13:58:23 +000011650 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011651
11652 /* Wait for link */
11653 for (i = 0; i < 100; i++) {
11654 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11655 break;
11656 mdelay(1);
11657 }
11658
Matt Carlson28a45952011-08-19 13:58:22 +000011659 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11660 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011661 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011662 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11663 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011664 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011665 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11666 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011667
Matt Carlson941ec902011-08-19 13:58:23 +000011668 if (do_extlpbk) {
11669 tg3_phy_lpbk_set(tp, 0, true);
11670
11671 /* All link indications report up, but the hardware
11672 * isn't really ready for about 20 msec. Double it
11673 * to be sure.
11674 */
11675 mdelay(40);
11676
11677 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11678 data[2] |= TG3_STD_LOOPBACK_FAILED;
11679 if (tg3_flag(tp, TSO_CAPABLE) &&
11680 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11681 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11682 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11683 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11684 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11685 }
11686
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011687 /* Re-enable gphy autopowerdown. */
11688 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11689 tg3_phy_toggle_apd(tp, true);
11690 }
Matt Carlson6833c042008-11-21 17:18:59 -080011691
Matt Carlson941ec902011-08-19 13:58:23 +000011692 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011693
Matt Carlsonab789042011-01-25 15:58:54 +000011694done:
11695 tp->phy_flags |= eee_cap;
11696
Michael Chan9f40dea2005-09-05 17:53:06 -070011697 return err;
11698}
11699
Michael Chan4cafd3f2005-05-29 14:56:34 -070011700static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11701 u64 *data)
11702{
Michael Chan566f86a2005-05-29 14:56:58 -070011703 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011704 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011705
Matt Carlsonbed98292011-07-13 09:27:29 +000011706 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11707 tg3_power_up(tp)) {
11708 etest->flags |= ETH_TEST_FL_FAILED;
11709 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11710 return;
11711 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011712
Michael Chan566f86a2005-05-29 14:56:58 -070011713 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11714
11715 if (tg3_test_nvram(tp) != 0) {
11716 etest->flags |= ETH_TEST_FL_FAILED;
11717 data[0] = 1;
11718 }
Matt Carlson941ec902011-08-19 13:58:23 +000011719 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011720 etest->flags |= ETH_TEST_FL_FAILED;
11721 data[1] = 1;
11722 }
Michael Chana71116d2005-05-29 14:58:11 -070011723 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011724 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011725
Michael Chanbbe832c2005-06-24 20:20:04 -070011726 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011727 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011728 tg3_netif_stop(tp);
11729 irq_sync = 1;
11730 }
11731
11732 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011733
11734 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011735 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011736 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011737 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011738 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011739 if (!err)
11740 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011741
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011742 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011743 tg3_phy_reset(tp);
11744
Michael Chana71116d2005-05-29 14:58:11 -070011745 if (tg3_test_registers(tp) != 0) {
11746 etest->flags |= ETH_TEST_FL_FAILED;
11747 data[2] = 1;
11748 }
Matt Carlson28a45952011-08-19 13:58:22 +000011749
Michael Chan7942e1d2005-05-29 14:58:36 -070011750 if (tg3_test_memory(tp) != 0) {
11751 etest->flags |= ETH_TEST_FL_FAILED;
11752 data[3] = 1;
11753 }
Matt Carlson28a45952011-08-19 13:58:22 +000011754
Matt Carlson941ec902011-08-19 13:58:23 +000011755 if (doextlpbk)
11756 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
11757
11758 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070011759 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011760
David S. Millerf47c11e2005-06-24 20:18:35 -070011761 tg3_full_unlock(tp);
11762
Michael Chand4bc3922005-05-29 14:59:20 -070011763 if (tg3_test_interrupt(tp) != 0) {
11764 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011765 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070011766 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011767
11768 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011769
Michael Chana71116d2005-05-29 14:58:11 -070011770 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11771 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011772 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011773 err2 = tg3_restart_hw(tp, 1);
11774 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011775 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011776 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011777
11778 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011779
11780 if (irq_sync && !err2)
11781 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011782 }
Matt Carlson80096062010-08-02 11:26:06 +000011783 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011784 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011785
Michael Chan4cafd3f2005-05-29 14:56:34 -070011786}
11787
Linus Torvalds1da177e2005-04-16 15:20:36 -070011788static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11789{
11790 struct mii_ioctl_data *data = if_mii(ifr);
11791 struct tg3 *tp = netdev_priv(dev);
11792 int err;
11793
Joe Perches63c3a662011-04-26 08:12:10 +000011794 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011795 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011796 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011797 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011798 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011799 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011800 }
11801
Matt Carlson33f401a2010-04-05 10:19:27 +000011802 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011803 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011804 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011805
11806 /* fallthru */
11807 case SIOCGMIIREG: {
11808 u32 mii_regval;
11809
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011810 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011811 break; /* We have no PHY */
11812
Matt Carlson34eea5a2011-04-20 07:57:38 +000011813 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011814 return -EAGAIN;
11815
David S. Millerf47c11e2005-06-24 20:18:35 -070011816 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011817 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011818 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011819
11820 data->val_out = mii_regval;
11821
11822 return err;
11823 }
11824
11825 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011826 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011827 break; /* We have no PHY */
11828
Matt Carlson34eea5a2011-04-20 07:57:38 +000011829 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011830 return -EAGAIN;
11831
David S. Millerf47c11e2005-06-24 20:18:35 -070011832 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011833 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011834 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011835
11836 return err;
11837
11838 default:
11839 /* do nothing */
11840 break;
11841 }
11842 return -EOPNOTSUPP;
11843}
11844
David S. Miller15f98502005-05-18 22:49:26 -070011845static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11846{
11847 struct tg3 *tp = netdev_priv(dev);
11848
11849 memcpy(ec, &tp->coal, sizeof(*ec));
11850 return 0;
11851}
11852
Michael Chand244c892005-07-05 14:42:33 -070011853static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11854{
11855 struct tg3 *tp = netdev_priv(dev);
11856 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11857 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11858
Joe Perches63c3a662011-04-26 08:12:10 +000011859 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011860 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11861 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11862 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11863 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11864 }
11865
11866 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11867 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11868 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11869 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11870 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11871 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11872 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11873 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11874 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11875 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11876 return -EINVAL;
11877
11878 /* No rx interrupts will be generated if both are zero */
11879 if ((ec->rx_coalesce_usecs == 0) &&
11880 (ec->rx_max_coalesced_frames == 0))
11881 return -EINVAL;
11882
11883 /* No tx interrupts will be generated if both are zero */
11884 if ((ec->tx_coalesce_usecs == 0) &&
11885 (ec->tx_max_coalesced_frames == 0))
11886 return -EINVAL;
11887
11888 /* Only copy relevant parameters, ignore all others. */
11889 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11890 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11891 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11892 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11893 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11894 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11895 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11896 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11897 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11898
11899 if (netif_running(dev)) {
11900 tg3_full_lock(tp, 0);
11901 __tg3_set_coalesce(tp, &tp->coal);
11902 tg3_full_unlock(tp);
11903 }
11904 return 0;
11905}
11906
Jeff Garzik7282d492006-09-13 14:30:00 -040011907static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011908 .get_settings = tg3_get_settings,
11909 .set_settings = tg3_set_settings,
11910 .get_drvinfo = tg3_get_drvinfo,
11911 .get_regs_len = tg3_get_regs_len,
11912 .get_regs = tg3_get_regs,
11913 .get_wol = tg3_get_wol,
11914 .set_wol = tg3_set_wol,
11915 .get_msglevel = tg3_get_msglevel,
11916 .set_msglevel = tg3_set_msglevel,
11917 .nway_reset = tg3_nway_reset,
11918 .get_link = ethtool_op_get_link,
11919 .get_eeprom_len = tg3_get_eeprom_len,
11920 .get_eeprom = tg3_get_eeprom,
11921 .set_eeprom = tg3_set_eeprom,
11922 .get_ringparam = tg3_get_ringparam,
11923 .set_ringparam = tg3_set_ringparam,
11924 .get_pauseparam = tg3_get_pauseparam,
11925 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011926 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011927 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011928 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011929 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011930 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011931 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011932 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011933};
11934
11935static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11936{
Michael Chan1b277772006-03-20 22:27:48 -080011937 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011938
11939 tp->nvram_size = EEPROM_CHIP_SIZE;
11940
Matt Carlsone4f34112009-02-25 14:25:00 +000011941 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011942 return;
11943
Michael Chanb16250e2006-09-27 16:10:14 -070011944 if ((magic != TG3_EEPROM_MAGIC) &&
11945 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11946 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011947 return;
11948
11949 /*
11950 * Size the chip by reading offsets at increasing powers of two.
11951 * When we encounter our validation signature, we know the addressing
11952 * has wrapped around, and thus have our chip size.
11953 */
Michael Chan1b277772006-03-20 22:27:48 -080011954 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011955
11956 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011957 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011958 return;
11959
Michael Chan18201802006-03-20 22:29:15 -080011960 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011961 break;
11962
11963 cursize <<= 1;
11964 }
11965
11966 tp->nvram_size = cursize;
11967}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011968
Linus Torvalds1da177e2005-04-16 15:20:36 -070011969static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11970{
11971 u32 val;
11972
Joe Perches63c3a662011-04-26 08:12:10 +000011973 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011974 return;
11975
11976 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011977 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011978 tg3_get_eeprom_size(tp);
11979 return;
11980 }
11981
Matt Carlson6d348f22009-02-25 14:25:52 +000011982 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011983 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011984 /* This is confusing. We want to operate on the
11985 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11986 * call will read from NVRAM and byteswap the data
11987 * according to the byteswapping settings for all
11988 * other register accesses. This ensures the data we
11989 * want will always reside in the lower 16-bits.
11990 * However, the data in NVRAM is in LE format, which
11991 * means the data from the NVRAM read will always be
11992 * opposite the endianness of the CPU. The 16-bit
11993 * byteswap then brings the data to CPU endianness.
11994 */
11995 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011996 return;
11997 }
11998 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011999 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012000}
12001
12002static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12003{
12004 u32 nvcfg1;
12005
12006 nvcfg1 = tr32(NVRAM_CFG1);
12007 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012008 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012009 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012010 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12011 tw32(NVRAM_CFG1, nvcfg1);
12012 }
12013
Matt Carlson6ff6f812011-05-19 12:12:54 +000012014 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012015 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012016 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012017 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12018 tp->nvram_jedecnum = JEDEC_ATMEL;
12019 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012020 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012021 break;
12022 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12023 tp->nvram_jedecnum = JEDEC_ATMEL;
12024 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12025 break;
12026 case FLASH_VENDOR_ATMEL_EEPROM:
12027 tp->nvram_jedecnum = JEDEC_ATMEL;
12028 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012029 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012030 break;
12031 case FLASH_VENDOR_ST:
12032 tp->nvram_jedecnum = JEDEC_ST;
12033 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012034 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012035 break;
12036 case FLASH_VENDOR_SAIFUN:
12037 tp->nvram_jedecnum = JEDEC_SAIFUN;
12038 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12039 break;
12040 case FLASH_VENDOR_SST_SMALL:
12041 case FLASH_VENDOR_SST_LARGE:
12042 tp->nvram_jedecnum = JEDEC_SST;
12043 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12044 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012045 }
Matt Carlson8590a602009-08-28 12:29:16 +000012046 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012047 tp->nvram_jedecnum = JEDEC_ATMEL;
12048 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012049 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012050 }
12051}
12052
Matt Carlsona1b950d2009-09-01 13:20:17 +000012053static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12054{
12055 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12056 case FLASH_5752PAGE_SIZE_256:
12057 tp->nvram_pagesize = 256;
12058 break;
12059 case FLASH_5752PAGE_SIZE_512:
12060 tp->nvram_pagesize = 512;
12061 break;
12062 case FLASH_5752PAGE_SIZE_1K:
12063 tp->nvram_pagesize = 1024;
12064 break;
12065 case FLASH_5752PAGE_SIZE_2K:
12066 tp->nvram_pagesize = 2048;
12067 break;
12068 case FLASH_5752PAGE_SIZE_4K:
12069 tp->nvram_pagesize = 4096;
12070 break;
12071 case FLASH_5752PAGE_SIZE_264:
12072 tp->nvram_pagesize = 264;
12073 break;
12074 case FLASH_5752PAGE_SIZE_528:
12075 tp->nvram_pagesize = 528;
12076 break;
12077 }
12078}
12079
Michael Chan361b4ac2005-04-21 17:11:21 -070012080static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12081{
12082 u32 nvcfg1;
12083
12084 nvcfg1 = tr32(NVRAM_CFG1);
12085
Michael Chane6af3012005-04-21 17:12:05 -070012086 /* NVRAM protection for TPM */
12087 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012088 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012089
Michael Chan361b4ac2005-04-21 17:11:21 -070012090 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012091 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12092 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12093 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012094 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012095 break;
12096 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12097 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012098 tg3_flag_set(tp, NVRAM_BUFFERED);
12099 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012100 break;
12101 case FLASH_5752VENDOR_ST_M45PE10:
12102 case FLASH_5752VENDOR_ST_M45PE20:
12103 case FLASH_5752VENDOR_ST_M45PE40:
12104 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012105 tg3_flag_set(tp, NVRAM_BUFFERED);
12106 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012107 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012108 }
12109
Joe Perches63c3a662011-04-26 08:12:10 +000012110 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012111 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012112 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012113 /* For eeprom, set pagesize to maximum eeprom size */
12114 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12115
12116 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12117 tw32(NVRAM_CFG1, nvcfg1);
12118 }
12119}
12120
Michael Chand3c7b882006-03-23 01:28:25 -080012121static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12122{
Matt Carlson989a9d22007-05-05 11:51:05 -070012123 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012124
12125 nvcfg1 = tr32(NVRAM_CFG1);
12126
12127 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012128 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012129 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012130 protect = 1;
12131 }
Michael Chand3c7b882006-03-23 01:28:25 -080012132
Matt Carlson989a9d22007-05-05 11:51:05 -070012133 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12134 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012135 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12136 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12137 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12138 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12139 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012140 tg3_flag_set(tp, NVRAM_BUFFERED);
12141 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012142 tp->nvram_pagesize = 264;
12143 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12144 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12145 tp->nvram_size = (protect ? 0x3e200 :
12146 TG3_NVRAM_SIZE_512KB);
12147 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12148 tp->nvram_size = (protect ? 0x1f200 :
12149 TG3_NVRAM_SIZE_256KB);
12150 else
12151 tp->nvram_size = (protect ? 0x1f200 :
12152 TG3_NVRAM_SIZE_128KB);
12153 break;
12154 case FLASH_5752VENDOR_ST_M45PE10:
12155 case FLASH_5752VENDOR_ST_M45PE20:
12156 case FLASH_5752VENDOR_ST_M45PE40:
12157 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012158 tg3_flag_set(tp, NVRAM_BUFFERED);
12159 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012160 tp->nvram_pagesize = 256;
12161 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12162 tp->nvram_size = (protect ?
12163 TG3_NVRAM_SIZE_64KB :
12164 TG3_NVRAM_SIZE_128KB);
12165 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12166 tp->nvram_size = (protect ?
12167 TG3_NVRAM_SIZE_64KB :
12168 TG3_NVRAM_SIZE_256KB);
12169 else
12170 tp->nvram_size = (protect ?
12171 TG3_NVRAM_SIZE_128KB :
12172 TG3_NVRAM_SIZE_512KB);
12173 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012174 }
12175}
12176
Michael Chan1b277772006-03-20 22:27:48 -080012177static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12178{
12179 u32 nvcfg1;
12180
12181 nvcfg1 = tr32(NVRAM_CFG1);
12182
12183 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012184 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12185 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12186 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12187 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12188 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012189 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012190 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012191
Matt Carlson8590a602009-08-28 12:29:16 +000012192 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12193 tw32(NVRAM_CFG1, nvcfg1);
12194 break;
12195 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12196 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12197 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12198 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12199 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012200 tg3_flag_set(tp, NVRAM_BUFFERED);
12201 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012202 tp->nvram_pagesize = 264;
12203 break;
12204 case FLASH_5752VENDOR_ST_M45PE10:
12205 case FLASH_5752VENDOR_ST_M45PE20:
12206 case FLASH_5752VENDOR_ST_M45PE40:
12207 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012208 tg3_flag_set(tp, NVRAM_BUFFERED);
12209 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012210 tp->nvram_pagesize = 256;
12211 break;
Michael Chan1b277772006-03-20 22:27:48 -080012212 }
12213}
12214
Matt Carlson6b91fa02007-10-10 18:01:09 -070012215static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12216{
12217 u32 nvcfg1, protect = 0;
12218
12219 nvcfg1 = tr32(NVRAM_CFG1);
12220
12221 /* NVRAM protection for TPM */
12222 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012223 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012224 protect = 1;
12225 }
12226
12227 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12228 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012229 case FLASH_5761VENDOR_ATMEL_ADB021D:
12230 case FLASH_5761VENDOR_ATMEL_ADB041D:
12231 case FLASH_5761VENDOR_ATMEL_ADB081D:
12232 case FLASH_5761VENDOR_ATMEL_ADB161D:
12233 case FLASH_5761VENDOR_ATMEL_MDB021D:
12234 case FLASH_5761VENDOR_ATMEL_MDB041D:
12235 case FLASH_5761VENDOR_ATMEL_MDB081D:
12236 case FLASH_5761VENDOR_ATMEL_MDB161D:
12237 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012238 tg3_flag_set(tp, NVRAM_BUFFERED);
12239 tg3_flag_set(tp, FLASH);
12240 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012241 tp->nvram_pagesize = 256;
12242 break;
12243 case FLASH_5761VENDOR_ST_A_M45PE20:
12244 case FLASH_5761VENDOR_ST_A_M45PE40:
12245 case FLASH_5761VENDOR_ST_A_M45PE80:
12246 case FLASH_5761VENDOR_ST_A_M45PE16:
12247 case FLASH_5761VENDOR_ST_M_M45PE20:
12248 case FLASH_5761VENDOR_ST_M_M45PE40:
12249 case FLASH_5761VENDOR_ST_M_M45PE80:
12250 case FLASH_5761VENDOR_ST_M_M45PE16:
12251 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012252 tg3_flag_set(tp, NVRAM_BUFFERED);
12253 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012254 tp->nvram_pagesize = 256;
12255 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012256 }
12257
12258 if (protect) {
12259 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12260 } else {
12261 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012262 case FLASH_5761VENDOR_ATMEL_ADB161D:
12263 case FLASH_5761VENDOR_ATMEL_MDB161D:
12264 case FLASH_5761VENDOR_ST_A_M45PE16:
12265 case FLASH_5761VENDOR_ST_M_M45PE16:
12266 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12267 break;
12268 case FLASH_5761VENDOR_ATMEL_ADB081D:
12269 case FLASH_5761VENDOR_ATMEL_MDB081D:
12270 case FLASH_5761VENDOR_ST_A_M45PE80:
12271 case FLASH_5761VENDOR_ST_M_M45PE80:
12272 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12273 break;
12274 case FLASH_5761VENDOR_ATMEL_ADB041D:
12275 case FLASH_5761VENDOR_ATMEL_MDB041D:
12276 case FLASH_5761VENDOR_ST_A_M45PE40:
12277 case FLASH_5761VENDOR_ST_M_M45PE40:
12278 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12279 break;
12280 case FLASH_5761VENDOR_ATMEL_ADB021D:
12281 case FLASH_5761VENDOR_ATMEL_MDB021D:
12282 case FLASH_5761VENDOR_ST_A_M45PE20:
12283 case FLASH_5761VENDOR_ST_M_M45PE20:
12284 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12285 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012286 }
12287 }
12288}
12289
Michael Chanb5d37722006-09-27 16:06:21 -070012290static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12291{
12292 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012293 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012294 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12295}
12296
Matt Carlson321d32a2008-11-21 17:22:19 -080012297static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12298{
12299 u32 nvcfg1;
12300
12301 nvcfg1 = tr32(NVRAM_CFG1);
12302
12303 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12304 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12305 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12306 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012307 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012308 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12309
12310 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12311 tw32(NVRAM_CFG1, nvcfg1);
12312 return;
12313 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12314 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12315 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12316 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12317 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12318 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12319 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12320 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012321 tg3_flag_set(tp, NVRAM_BUFFERED);
12322 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012323
12324 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12325 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12326 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12327 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12328 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12329 break;
12330 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12331 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12332 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12333 break;
12334 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12335 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12336 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12337 break;
12338 }
12339 break;
12340 case FLASH_5752VENDOR_ST_M45PE10:
12341 case FLASH_5752VENDOR_ST_M45PE20:
12342 case FLASH_5752VENDOR_ST_M45PE40:
12343 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012344 tg3_flag_set(tp, NVRAM_BUFFERED);
12345 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012346
12347 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12348 case FLASH_5752VENDOR_ST_M45PE10:
12349 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12350 break;
12351 case FLASH_5752VENDOR_ST_M45PE20:
12352 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12353 break;
12354 case FLASH_5752VENDOR_ST_M45PE40:
12355 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12356 break;
12357 }
12358 break;
12359 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012360 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012361 return;
12362 }
12363
Matt Carlsona1b950d2009-09-01 13:20:17 +000012364 tg3_nvram_get_pagesize(tp, nvcfg1);
12365 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012366 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012367}
12368
12369
12370static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12371{
12372 u32 nvcfg1;
12373
12374 nvcfg1 = tr32(NVRAM_CFG1);
12375
12376 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12377 case FLASH_5717VENDOR_ATMEL_EEPROM:
12378 case FLASH_5717VENDOR_MICRO_EEPROM:
12379 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012380 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012381 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12382
12383 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12384 tw32(NVRAM_CFG1, nvcfg1);
12385 return;
12386 case FLASH_5717VENDOR_ATMEL_MDB011D:
12387 case FLASH_5717VENDOR_ATMEL_ADB011B:
12388 case FLASH_5717VENDOR_ATMEL_ADB011D:
12389 case FLASH_5717VENDOR_ATMEL_MDB021D:
12390 case FLASH_5717VENDOR_ATMEL_ADB021B:
12391 case FLASH_5717VENDOR_ATMEL_ADB021D:
12392 case FLASH_5717VENDOR_ATMEL_45USPT:
12393 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012394 tg3_flag_set(tp, NVRAM_BUFFERED);
12395 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012396
12397 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12398 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012399 /* Detect size with tg3_nvram_get_size() */
12400 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012401 case FLASH_5717VENDOR_ATMEL_ADB021B:
12402 case FLASH_5717VENDOR_ATMEL_ADB021D:
12403 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12404 break;
12405 default:
12406 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12407 break;
12408 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012409 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012410 case FLASH_5717VENDOR_ST_M_M25PE10:
12411 case FLASH_5717VENDOR_ST_A_M25PE10:
12412 case FLASH_5717VENDOR_ST_M_M45PE10:
12413 case FLASH_5717VENDOR_ST_A_M45PE10:
12414 case FLASH_5717VENDOR_ST_M_M25PE20:
12415 case FLASH_5717VENDOR_ST_A_M25PE20:
12416 case FLASH_5717VENDOR_ST_M_M45PE20:
12417 case FLASH_5717VENDOR_ST_A_M45PE20:
12418 case FLASH_5717VENDOR_ST_25USPT:
12419 case FLASH_5717VENDOR_ST_45USPT:
12420 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012421 tg3_flag_set(tp, NVRAM_BUFFERED);
12422 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012423
12424 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12425 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012426 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012427 /* Detect size with tg3_nvram_get_size() */
12428 break;
12429 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012430 case FLASH_5717VENDOR_ST_A_M45PE20:
12431 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12432 break;
12433 default:
12434 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12435 break;
12436 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012437 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012438 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012439 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012440 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012441 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012442
12443 tg3_nvram_get_pagesize(tp, nvcfg1);
12444 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012445 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012446}
12447
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012448static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12449{
12450 u32 nvcfg1, nvmpinstrp;
12451
12452 nvcfg1 = tr32(NVRAM_CFG1);
12453 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12454
12455 switch (nvmpinstrp) {
12456 case FLASH_5720_EEPROM_HD:
12457 case FLASH_5720_EEPROM_LD:
12458 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012459 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012460
12461 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12462 tw32(NVRAM_CFG1, nvcfg1);
12463 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12464 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12465 else
12466 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12467 return;
12468 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12469 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12470 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12471 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12472 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12473 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12474 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12475 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12476 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12477 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12478 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12479 case FLASH_5720VENDOR_ATMEL_45USPT:
12480 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012481 tg3_flag_set(tp, NVRAM_BUFFERED);
12482 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012483
12484 switch (nvmpinstrp) {
12485 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12486 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12487 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12488 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12489 break;
12490 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12491 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12492 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12493 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12494 break;
12495 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12496 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12497 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12498 break;
12499 default:
12500 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12501 break;
12502 }
12503 break;
12504 case FLASH_5720VENDOR_M_ST_M25PE10:
12505 case FLASH_5720VENDOR_M_ST_M45PE10:
12506 case FLASH_5720VENDOR_A_ST_M25PE10:
12507 case FLASH_5720VENDOR_A_ST_M45PE10:
12508 case FLASH_5720VENDOR_M_ST_M25PE20:
12509 case FLASH_5720VENDOR_M_ST_M45PE20:
12510 case FLASH_5720VENDOR_A_ST_M25PE20:
12511 case FLASH_5720VENDOR_A_ST_M45PE20:
12512 case FLASH_5720VENDOR_M_ST_M25PE40:
12513 case FLASH_5720VENDOR_M_ST_M45PE40:
12514 case FLASH_5720VENDOR_A_ST_M25PE40:
12515 case FLASH_5720VENDOR_A_ST_M45PE40:
12516 case FLASH_5720VENDOR_M_ST_M25PE80:
12517 case FLASH_5720VENDOR_M_ST_M45PE80:
12518 case FLASH_5720VENDOR_A_ST_M25PE80:
12519 case FLASH_5720VENDOR_A_ST_M45PE80:
12520 case FLASH_5720VENDOR_ST_25USPT:
12521 case FLASH_5720VENDOR_ST_45USPT:
12522 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012523 tg3_flag_set(tp, NVRAM_BUFFERED);
12524 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012525
12526 switch (nvmpinstrp) {
12527 case FLASH_5720VENDOR_M_ST_M25PE20:
12528 case FLASH_5720VENDOR_M_ST_M45PE20:
12529 case FLASH_5720VENDOR_A_ST_M25PE20:
12530 case FLASH_5720VENDOR_A_ST_M45PE20:
12531 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12532 break;
12533 case FLASH_5720VENDOR_M_ST_M25PE40:
12534 case FLASH_5720VENDOR_M_ST_M45PE40:
12535 case FLASH_5720VENDOR_A_ST_M25PE40:
12536 case FLASH_5720VENDOR_A_ST_M45PE40:
12537 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12538 break;
12539 case FLASH_5720VENDOR_M_ST_M25PE80:
12540 case FLASH_5720VENDOR_M_ST_M45PE80:
12541 case FLASH_5720VENDOR_A_ST_M25PE80:
12542 case FLASH_5720VENDOR_A_ST_M45PE80:
12543 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12544 break;
12545 default:
12546 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12547 break;
12548 }
12549 break;
12550 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012551 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012552 return;
12553 }
12554
12555 tg3_nvram_get_pagesize(tp, nvcfg1);
12556 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012557 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012558}
12559
Linus Torvalds1da177e2005-04-16 15:20:36 -070012560/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12561static void __devinit tg3_nvram_init(struct tg3 *tp)
12562{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012563 tw32_f(GRC_EEPROM_ADDR,
12564 (EEPROM_ADDR_FSM_RESET |
12565 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12566 EEPROM_ADDR_CLKPERD_SHIFT)));
12567
Michael Chan9d57f012006-12-07 00:23:25 -080012568 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012569
12570 /* Enable seeprom accesses. */
12571 tw32_f(GRC_LOCAL_CTRL,
12572 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12573 udelay(100);
12574
12575 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12576 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012577 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012578
Michael Chanec41c7d2006-01-17 02:40:55 -080012579 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012580 netdev_warn(tp->dev,
12581 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012582 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012583 return;
12584 }
Michael Chane6af3012005-04-21 17:12:05 -070012585 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012586
Matt Carlson989a9d22007-05-05 11:51:05 -070012587 tp->nvram_size = 0;
12588
Michael Chan361b4ac2005-04-21 17:11:21 -070012589 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12590 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012591 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12592 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012593 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012594 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12595 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012596 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012597 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12598 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012599 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12600 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012601 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12602 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012603 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012604 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12605 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012606 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012607 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12608 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012609 else
12610 tg3_get_nvram_info(tp);
12611
Matt Carlson989a9d22007-05-05 11:51:05 -070012612 if (tp->nvram_size == 0)
12613 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012614
Michael Chane6af3012005-04-21 17:12:05 -070012615 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012616 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012617
12618 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012619 tg3_flag_clear(tp, NVRAM);
12620 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012621
12622 tg3_get_eeprom_size(tp);
12623 }
12624}
12625
Linus Torvalds1da177e2005-04-16 15:20:36 -070012626static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12627 u32 offset, u32 len, u8 *buf)
12628{
12629 int i, j, rc = 0;
12630 u32 val;
12631
12632 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012633 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012634 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012635
12636 addr = offset + i;
12637
12638 memcpy(&data, buf + i, 4);
12639
Matt Carlson62cedd12009-04-20 14:52:29 -070012640 /*
12641 * The SEEPROM interface expects the data to always be opposite
12642 * the native endian format. We accomplish this by reversing
12643 * all the operations that would have been performed on the
12644 * data from a call to tg3_nvram_read_be32().
12645 */
12646 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012647
12648 val = tr32(GRC_EEPROM_ADDR);
12649 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12650
12651 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12652 EEPROM_ADDR_READ);
12653 tw32(GRC_EEPROM_ADDR, val |
12654 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12655 (addr & EEPROM_ADDR_ADDR_MASK) |
12656 EEPROM_ADDR_START |
12657 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012658
Michael Chan9d57f012006-12-07 00:23:25 -080012659 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012660 val = tr32(GRC_EEPROM_ADDR);
12661
12662 if (val & EEPROM_ADDR_COMPLETE)
12663 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012664 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012665 }
12666 if (!(val & EEPROM_ADDR_COMPLETE)) {
12667 rc = -EBUSY;
12668 break;
12669 }
12670 }
12671
12672 return rc;
12673}
12674
12675/* offset and length are dword aligned */
12676static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12677 u8 *buf)
12678{
12679 int ret = 0;
12680 u32 pagesize = tp->nvram_pagesize;
12681 u32 pagemask = pagesize - 1;
12682 u32 nvram_cmd;
12683 u8 *tmp;
12684
12685 tmp = kmalloc(pagesize, GFP_KERNEL);
12686 if (tmp == NULL)
12687 return -ENOMEM;
12688
12689 while (len) {
12690 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012691 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012692
12693 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012694
Linus Torvalds1da177e2005-04-16 15:20:36 -070012695 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012696 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12697 (__be32 *) (tmp + j));
12698 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012699 break;
12700 }
12701 if (ret)
12702 break;
12703
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012704 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012705 size = pagesize;
12706 if (len < size)
12707 size = len;
12708
12709 len -= size;
12710
12711 memcpy(tmp + page_off, buf, size);
12712
12713 offset = offset + (pagesize - page_off);
12714
Michael Chane6af3012005-04-21 17:12:05 -070012715 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012716
12717 /*
12718 * Before we can erase the flash page, we need
12719 * to issue a special "write enable" command.
12720 */
12721 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12722
12723 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12724 break;
12725
12726 /* Erase the target page */
12727 tw32(NVRAM_ADDR, phy_addr);
12728
12729 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12730 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12731
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012732 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012733 break;
12734
12735 /* Issue another write enable to start the write. */
12736 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12737
12738 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12739 break;
12740
12741 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012742 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012743
Al Virob9fc7dc2007-12-17 22:59:57 -080012744 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012745
Al Virob9fc7dc2007-12-17 22:59:57 -080012746 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012747
12748 tw32(NVRAM_ADDR, phy_addr + j);
12749
12750 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12751 NVRAM_CMD_WR;
12752
12753 if (j == 0)
12754 nvram_cmd |= NVRAM_CMD_FIRST;
12755 else if (j == (pagesize - 4))
12756 nvram_cmd |= NVRAM_CMD_LAST;
12757
12758 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12759 break;
12760 }
12761 if (ret)
12762 break;
12763 }
12764
12765 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12766 tg3_nvram_exec_cmd(tp, nvram_cmd);
12767
12768 kfree(tmp);
12769
12770 return ret;
12771}
12772
12773/* offset and length are dword aligned */
12774static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12775 u8 *buf)
12776{
12777 int i, ret = 0;
12778
12779 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012780 u32 page_off, phy_addr, nvram_cmd;
12781 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012782
12783 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012784 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012785
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012786 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012787
Michael Chan18201802006-03-20 22:29:15 -080012788 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012789
12790 tw32(NVRAM_ADDR, phy_addr);
12791
12792 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12793
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012794 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012795 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012796 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012797 nvram_cmd |= NVRAM_CMD_LAST;
12798
12799 if (i == (len - 4))
12800 nvram_cmd |= NVRAM_CMD_LAST;
12801
Matt Carlson321d32a2008-11-21 17:22:19 -080012802 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012803 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012804 (tp->nvram_jedecnum == JEDEC_ST) &&
12805 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012806
12807 if ((ret = tg3_nvram_exec_cmd(tp,
12808 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12809 NVRAM_CMD_DONE)))
12810
12811 break;
12812 }
Joe Perches63c3a662011-04-26 08:12:10 +000012813 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012814 /* We always do complete word writes to eeprom. */
12815 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12816 }
12817
12818 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12819 break;
12820 }
12821 return ret;
12822}
12823
12824/* offset and length are dword aligned */
12825static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12826{
12827 int ret;
12828
Joe Perches63c3a662011-04-26 08:12:10 +000012829 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012830 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12831 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012832 udelay(40);
12833 }
12834
Joe Perches63c3a662011-04-26 08:12:10 +000012835 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012836 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012837 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012838 u32 grc_mode;
12839
Michael Chanec41c7d2006-01-17 02:40:55 -080012840 ret = tg3_nvram_lock(tp);
12841 if (ret)
12842 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012843
Michael Chane6af3012005-04-21 17:12:05 -070012844 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012845 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012846 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012847
12848 grc_mode = tr32(GRC_MODE);
12849 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12850
Joe Perches63c3a662011-04-26 08:12:10 +000012851 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012852 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12853 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012854 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012855 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12856 buf);
12857 }
12858
12859 grc_mode = tr32(GRC_MODE);
12860 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12861
Michael Chane6af3012005-04-21 17:12:05 -070012862 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012863 tg3_nvram_unlock(tp);
12864 }
12865
Joe Perches63c3a662011-04-26 08:12:10 +000012866 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012867 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012868 udelay(40);
12869 }
12870
12871 return ret;
12872}
12873
12874struct subsys_tbl_ent {
12875 u16 subsys_vendor, subsys_devid;
12876 u32 phy_id;
12877};
12878
Matt Carlson24daf2b2010-02-17 15:17:02 +000012879static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012880 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012881 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012882 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012883 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012884 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012885 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012886 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012887 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12888 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12889 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012890 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012891 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012892 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012893 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12894 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12895 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012896 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012897 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012898 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012899 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012900 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012901 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012902 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012903
12904 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012905 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012906 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012907 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012908 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012909 { TG3PCI_SUBVENDOR_ID_3COM,
12910 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12911 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012912 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012913 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012914 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012915
12916 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012917 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012918 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012919 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012920 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012921 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012922 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012923 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012924 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012925
12926 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012927 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012928 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012929 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012930 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012931 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12932 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12933 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012934 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012935 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012936 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012937
12938 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012939 { TG3PCI_SUBVENDOR_ID_IBM,
12940 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012941};
12942
Matt Carlson24daf2b2010-02-17 15:17:02 +000012943static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012944{
12945 int i;
12946
12947 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12948 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12949 tp->pdev->subsystem_vendor) &&
12950 (subsys_id_to_phy_id[i].subsys_devid ==
12951 tp->pdev->subsystem_device))
12952 return &subsys_id_to_phy_id[i];
12953 }
12954 return NULL;
12955}
12956
Michael Chan7d0c41e2005-04-21 17:06:20 -070012957static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012958{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012959 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070012960
Matt Carlson79eb6902010-02-17 15:17:03 +000012961 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012962 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12963
Gary Zambranoa85feb82007-05-05 11:52:19 -070012964 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012965 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12966 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012967
Michael Chanb5d37722006-09-27 16:06:21 -070012968 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012969 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012970 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12971 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012972 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012973 val = tr32(VCPU_CFGSHDW);
12974 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012975 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012976 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012977 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012978 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012979 device_set_wakeup_enable(&tp->pdev->dev, true);
12980 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012981 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012982 }
12983
Linus Torvalds1da177e2005-04-16 15:20:36 -070012984 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12985 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12986 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012987 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012988 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012989
12990 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12991 tp->nic_sram_data_cfg = nic_cfg;
12992
12993 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12994 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000012995 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12996 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
12997 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012998 (ver > 0) && (ver < 0x100))
12999 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13000
Matt Carlsona9daf362008-05-25 23:49:44 -070013001 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13002 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13003
Linus Torvalds1da177e2005-04-16 15:20:36 -070013004 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13005 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13006 eeprom_phy_serdes = 1;
13007
13008 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13009 if (nic_phy_id != 0) {
13010 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13011 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13012
13013 eeprom_phy_id = (id1 >> 16) << 10;
13014 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13015 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13016 } else
13017 eeprom_phy_id = 0;
13018
Michael Chan7d0c41e2005-04-21 17:06:20 -070013019 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013020 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013021 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013022 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013023 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013024 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013025 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013026
Joe Perches63c3a662011-04-26 08:12:10 +000013027 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013028 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13029 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013030 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013031 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13032
13033 switch (led_cfg) {
13034 default:
13035 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13036 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13037 break;
13038
13039 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13040 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13041 break;
13042
13043 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13044 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013045
13046 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13047 * read on some older 5700/5701 bootcode.
13048 */
13049 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13050 ASIC_REV_5700 ||
13051 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13052 ASIC_REV_5701)
13053 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13054
Linus Torvalds1da177e2005-04-16 15:20:36 -070013055 break;
13056
13057 case SHASTA_EXT_LED_SHARED:
13058 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13059 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13060 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13061 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13062 LED_CTRL_MODE_PHY_2);
13063 break;
13064
13065 case SHASTA_EXT_LED_MAC:
13066 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13067 break;
13068
13069 case SHASTA_EXT_LED_COMBO:
13070 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13071 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13072 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13073 LED_CTRL_MODE_PHY_2);
13074 break;
13075
Stephen Hemminger855e1112008-04-16 16:37:28 -070013076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013077
13078 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13079 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13080 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13081 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13082
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013083 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13084 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013085
Michael Chan9d26e212006-12-07 00:21:14 -080013086 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013087 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013088 if ((tp->pdev->subsystem_vendor ==
13089 PCI_VENDOR_ID_ARIMA) &&
13090 (tp->pdev->subsystem_device == 0x205a ||
13091 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013092 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013093 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013094 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13095 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013097
13098 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013099 tg3_flag_set(tp, ENABLE_ASF);
13100 if (tg3_flag(tp, 5750_PLUS))
13101 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013102 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013103
13104 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013105 tg3_flag(tp, 5750_PLUS))
13106 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013107
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013108 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013109 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013110 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013111
Joe Perches63c3a662011-04-26 08:12:10 +000013112 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013113 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013114 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013115 device_set_wakeup_enable(&tp->pdev->dev, true);
13116 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013117
Linus Torvalds1da177e2005-04-16 15:20:36 -070013118 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013119 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013120
13121 /* serdes signal pre-emphasis in register 0x590 set by */
13122 /* bootcode if bit 18 is set */
13123 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013124 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013125
Joe Perches63c3a662011-04-26 08:12:10 +000013126 if ((tg3_flag(tp, 57765_PLUS) ||
13127 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13128 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013129 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013130 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013131
Joe Perches63c3a662011-04-26 08:12:10 +000013132 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013133 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013134 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013135 u32 cfg3;
13136
13137 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13138 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013139 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013140 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013141
Matt Carlson14417062010-02-17 15:16:59 +000013142 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013143 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013144 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013145 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013146 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013147 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013148 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013149done:
Joe Perches63c3a662011-04-26 08:12:10 +000013150 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013151 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013152 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013153 else
13154 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013155}
13156
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013157static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13158{
13159 int i;
13160 u32 val;
13161
13162 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13163 tw32(OTP_CTRL, cmd);
13164
13165 /* Wait for up to 1 ms for command to execute. */
13166 for (i = 0; i < 100; i++) {
13167 val = tr32(OTP_STATUS);
13168 if (val & OTP_STATUS_CMD_DONE)
13169 break;
13170 udelay(10);
13171 }
13172
13173 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13174}
13175
13176/* Read the gphy configuration from the OTP region of the chip. The gphy
13177 * configuration is a 32-bit value that straddles the alignment boundary.
13178 * We do two 32-bit reads and then shift and merge the results.
13179 */
13180static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13181{
13182 u32 bhalf_otp, thalf_otp;
13183
13184 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13185
13186 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13187 return 0;
13188
13189 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13190
13191 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13192 return 0;
13193
13194 thalf_otp = tr32(OTP_READ_DATA);
13195
13196 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13197
13198 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13199 return 0;
13200
13201 bhalf_otp = tr32(OTP_READ_DATA);
13202
13203 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13204}
13205
Matt Carlsone256f8a2011-03-09 16:58:24 +000013206static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13207{
13208 u32 adv = ADVERTISED_Autoneg |
13209 ADVERTISED_Pause;
13210
13211 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13212 adv |= ADVERTISED_1000baseT_Half |
13213 ADVERTISED_1000baseT_Full;
13214
13215 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13216 adv |= ADVERTISED_100baseT_Half |
13217 ADVERTISED_100baseT_Full |
13218 ADVERTISED_10baseT_Half |
13219 ADVERTISED_10baseT_Full |
13220 ADVERTISED_TP;
13221 else
13222 adv |= ADVERTISED_FIBRE;
13223
13224 tp->link_config.advertising = adv;
13225 tp->link_config.speed = SPEED_INVALID;
13226 tp->link_config.duplex = DUPLEX_INVALID;
13227 tp->link_config.autoneg = AUTONEG_ENABLE;
13228 tp->link_config.active_speed = SPEED_INVALID;
13229 tp->link_config.active_duplex = DUPLEX_INVALID;
13230 tp->link_config.orig_speed = SPEED_INVALID;
13231 tp->link_config.orig_duplex = DUPLEX_INVALID;
13232 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13233}
13234
Michael Chan7d0c41e2005-04-21 17:06:20 -070013235static int __devinit tg3_phy_probe(struct tg3 *tp)
13236{
13237 u32 hw_phy_id_1, hw_phy_id_2;
13238 u32 hw_phy_id, hw_phy_id_masked;
13239 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013240
Matt Carlsone256f8a2011-03-09 16:58:24 +000013241 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013242 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013243 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13244
Joe Perches63c3a662011-04-26 08:12:10 +000013245 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013246 return tg3_phy_init(tp);
13247
Linus Torvalds1da177e2005-04-16 15:20:36 -070013248 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013249 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013250 */
13251 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013252 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013253 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013254 } else {
13255 /* Now read the physical PHY_ID from the chip and verify
13256 * that it is sane. If it doesn't look good, we fall back
13257 * to either the hard-coded table based PHY_ID and failing
13258 * that the value found in the eeprom area.
13259 */
13260 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13261 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13262
13263 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13264 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13265 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13266
Matt Carlson79eb6902010-02-17 15:17:03 +000013267 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013268 }
13269
Matt Carlson79eb6902010-02-17 15:17:03 +000013270 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013271 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013272 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013273 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013274 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013275 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013276 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013277 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013278 /* Do nothing, phy ID already set up in
13279 * tg3_get_eeprom_hw_cfg().
13280 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013281 } else {
13282 struct subsys_tbl_ent *p;
13283
13284 /* No eeprom signature? Try the hardcoded
13285 * subsys device table.
13286 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013287 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013288 if (!p)
13289 return -ENODEV;
13290
13291 tp->phy_id = p->phy_id;
13292 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013293 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013294 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013295 }
13296 }
13297
Matt Carlsona6b68da2010-12-06 08:28:52 +000013298 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013299 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13300 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13301 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013302 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13303 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13304 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013305 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13306
Matt Carlsone256f8a2011-03-09 16:58:24 +000013307 tg3_phy_init_link_config(tp);
13308
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013309 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013310 !tg3_flag(tp, ENABLE_APE) &&
13311 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013312 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013313
13314 tg3_readphy(tp, MII_BMSR, &bmsr);
13315 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13316 (bmsr & BMSR_LSTATUS))
13317 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013318
Linus Torvalds1da177e2005-04-16 15:20:36 -070013319 err = tg3_phy_reset(tp);
13320 if (err)
13321 return err;
13322
Matt Carlson42b64a42011-05-19 12:12:49 +000013323 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013324
Michael Chan3600d912006-12-07 00:21:48 -080013325 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13326 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13327 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13328 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013329 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13330 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013331
13332 tg3_writephy(tp, MII_BMCR,
13333 BMCR_ANENABLE | BMCR_ANRESTART);
13334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013335 }
13336
13337skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013338 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013339 err = tg3_init_5401phy_dsp(tp);
13340 if (err)
13341 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013342
Linus Torvalds1da177e2005-04-16 15:20:36 -070013343 err = tg3_init_5401phy_dsp(tp);
13344 }
13345
Linus Torvalds1da177e2005-04-16 15:20:36 -070013346 return err;
13347}
13348
Matt Carlson184b8902010-04-05 10:19:25 +000013349static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013350{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013351 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013352 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013353 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013354 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013355
Matt Carlson535a4902011-07-20 10:20:56 +000013356 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013357 if (!vpd_data)
13358 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013359
Matt Carlson535a4902011-07-20 10:20:56 +000013360 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013361 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013362 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013363
13364 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13365 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13366 i += PCI_VPD_LRDT_TAG_SIZE;
13367
Matt Carlson535a4902011-07-20 10:20:56 +000013368 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013369 goto out_not_found;
13370
Matt Carlson184b8902010-04-05 10:19:25 +000013371 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13372 PCI_VPD_RO_KEYWORD_MFR_ID);
13373 if (j > 0) {
13374 len = pci_vpd_info_field_size(&vpd_data[j]);
13375
13376 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13377 if (j + len > block_end || len != 4 ||
13378 memcmp(&vpd_data[j], "1028", 4))
13379 goto partno;
13380
13381 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13382 PCI_VPD_RO_KEYWORD_VENDOR0);
13383 if (j < 0)
13384 goto partno;
13385
13386 len = pci_vpd_info_field_size(&vpd_data[j]);
13387
13388 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13389 if (j + len > block_end)
13390 goto partno;
13391
13392 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013393 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013394 }
13395
13396partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013397 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13398 PCI_VPD_RO_KEYWORD_PARTNO);
13399 if (i < 0)
13400 goto out_not_found;
13401
13402 len = pci_vpd_info_field_size(&vpd_data[i]);
13403
13404 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13405 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013406 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013407 goto out_not_found;
13408
13409 memcpy(tp->board_part_number, &vpd_data[i], len);
13410
Linus Torvalds1da177e2005-04-16 15:20:36 -070013411out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013412 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013413 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013414 return;
13415
13416out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013417 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13418 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13419 strcpy(tp->board_part_number, "BCM5717");
13420 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13421 strcpy(tp->board_part_number, "BCM5718");
13422 else
13423 goto nomatch;
13424 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13425 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13426 strcpy(tp->board_part_number, "BCM57780");
13427 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13428 strcpy(tp->board_part_number, "BCM57760");
13429 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13430 strcpy(tp->board_part_number, "BCM57790");
13431 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13432 strcpy(tp->board_part_number, "BCM57788");
13433 else
13434 goto nomatch;
13435 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13436 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13437 strcpy(tp->board_part_number, "BCM57761");
13438 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13439 strcpy(tp->board_part_number, "BCM57765");
13440 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13441 strcpy(tp->board_part_number, "BCM57781");
13442 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13443 strcpy(tp->board_part_number, "BCM57785");
13444 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13445 strcpy(tp->board_part_number, "BCM57791");
13446 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13447 strcpy(tp->board_part_number, "BCM57795");
13448 else
13449 goto nomatch;
13450 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013451 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013452 } else {
13453nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013454 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013456}
13457
Matt Carlson9c8a6202007-10-21 16:16:08 -070013458static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13459{
13460 u32 val;
13461
Matt Carlsone4f34112009-02-25 14:25:00 +000013462 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013463 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013464 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013465 val != 0)
13466 return 0;
13467
13468 return 1;
13469}
13470
Matt Carlsonacd9c112009-02-25 14:26:33 +000013471static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13472{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013473 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013474 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013475 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013476
13477 if (tg3_nvram_read(tp, 0xc, &offset) ||
13478 tg3_nvram_read(tp, 0x4, &start))
13479 return;
13480
13481 offset = tg3_nvram_logical_addr(tp, offset);
13482
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013483 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013484 return;
13485
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013486 if ((val & 0xfc000000) == 0x0c000000) {
13487 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013488 return;
13489
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013490 if (val == 0)
13491 newver = true;
13492 }
13493
Matt Carlson75f99362010-04-05 10:19:24 +000013494 dst_off = strlen(tp->fw_ver);
13495
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013496 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013497 if (TG3_VER_SIZE - dst_off < 16 ||
13498 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013499 return;
13500
13501 offset = offset + ver_offset - start;
13502 for (i = 0; i < 16; i += 4) {
13503 __be32 v;
13504 if (tg3_nvram_read_be32(tp, offset + i, &v))
13505 return;
13506
Matt Carlson75f99362010-04-05 10:19:24 +000013507 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013508 }
13509 } else {
13510 u32 major, minor;
13511
13512 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13513 return;
13514
13515 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13516 TG3_NVM_BCVER_MAJSFT;
13517 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013518 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13519 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013520 }
13521}
13522
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013523static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13524{
13525 u32 val, major, minor;
13526
13527 /* Use native endian representation */
13528 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13529 return;
13530
13531 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13532 TG3_NVM_HWSB_CFG1_MAJSFT;
13533 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13534 TG3_NVM_HWSB_CFG1_MINSFT;
13535
13536 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13537}
13538
Matt Carlsondfe00d72008-11-21 17:19:41 -080013539static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13540{
13541 u32 offset, major, minor, build;
13542
Matt Carlson75f99362010-04-05 10:19:24 +000013543 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013544
13545 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13546 return;
13547
13548 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13549 case TG3_EEPROM_SB_REVISION_0:
13550 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13551 break;
13552 case TG3_EEPROM_SB_REVISION_2:
13553 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13554 break;
13555 case TG3_EEPROM_SB_REVISION_3:
13556 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13557 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013558 case TG3_EEPROM_SB_REVISION_4:
13559 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13560 break;
13561 case TG3_EEPROM_SB_REVISION_5:
13562 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13563 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013564 case TG3_EEPROM_SB_REVISION_6:
13565 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13566 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013567 default:
13568 return;
13569 }
13570
Matt Carlsone4f34112009-02-25 14:25:00 +000013571 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013572 return;
13573
13574 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13575 TG3_EEPROM_SB_EDH_BLD_SHFT;
13576 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13577 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13578 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13579
13580 if (minor > 99 || build > 26)
13581 return;
13582
Matt Carlson75f99362010-04-05 10:19:24 +000013583 offset = strlen(tp->fw_ver);
13584 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13585 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013586
13587 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013588 offset = strlen(tp->fw_ver);
13589 if (offset < TG3_VER_SIZE - 1)
13590 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013591 }
13592}
13593
Matt Carlsonacd9c112009-02-25 14:26:33 +000013594static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013595{
13596 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013597 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013598
13599 for (offset = TG3_NVM_DIR_START;
13600 offset < TG3_NVM_DIR_END;
13601 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013602 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013603 return;
13604
13605 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13606 break;
13607 }
13608
13609 if (offset == TG3_NVM_DIR_END)
13610 return;
13611
Joe Perches63c3a662011-04-26 08:12:10 +000013612 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013613 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013614 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013615 return;
13616
Matt Carlsone4f34112009-02-25 14:25:00 +000013617 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013618 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013619 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013620 return;
13621
13622 offset += val - start;
13623
Matt Carlsonacd9c112009-02-25 14:26:33 +000013624 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013625
Matt Carlsonacd9c112009-02-25 14:26:33 +000013626 tp->fw_ver[vlen++] = ',';
13627 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013628
13629 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013630 __be32 v;
13631 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013632 return;
13633
Al Virob9fc7dc2007-12-17 22:59:57 -080013634 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013635
Matt Carlsonacd9c112009-02-25 14:26:33 +000013636 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13637 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013638 break;
13639 }
13640
Matt Carlsonacd9c112009-02-25 14:26:33 +000013641 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13642 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013643 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013644}
13645
Matt Carlson7fd76442009-02-25 14:27:20 +000013646static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13647{
13648 int vlen;
13649 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013650 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013651
Joe Perches63c3a662011-04-26 08:12:10 +000013652 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013653 return;
13654
13655 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13656 if (apedata != APE_SEG_SIG_MAGIC)
13657 return;
13658
13659 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13660 if (!(apedata & APE_FW_STATUS_READY))
13661 return;
13662
13663 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13664
Matt Carlsondc6d0742010-09-15 08:59:55 +000013665 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013666 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013667 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013668 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013669 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013670 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013671
Matt Carlson7fd76442009-02-25 14:27:20 +000013672 vlen = strlen(tp->fw_ver);
13673
Matt Carlsonecc79642010-08-02 11:26:01 +000013674 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13675 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013676 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13677 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13678 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13679 (apedata & APE_FW_VERSION_BLDMSK));
13680}
13681
Matt Carlsonacd9c112009-02-25 14:26:33 +000013682static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13683{
13684 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013685 bool vpd_vers = false;
13686
13687 if (tp->fw_ver[0] != 0)
13688 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013689
Joe Perches63c3a662011-04-26 08:12:10 +000013690 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013691 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013692 return;
13693 }
13694
Matt Carlsonacd9c112009-02-25 14:26:33 +000013695 if (tg3_nvram_read(tp, 0, &val))
13696 return;
13697
13698 if (val == TG3_EEPROM_MAGIC)
13699 tg3_read_bc_ver(tp);
13700 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13701 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013702 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13703 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013704 else
13705 return;
13706
Matt Carlsonc9cab242011-07-13 09:27:27 +000013707 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013708 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013709
Matt Carlsonc9cab242011-07-13 09:27:27 +000013710 if (tg3_flag(tp, ENABLE_APE)) {
13711 if (tg3_flag(tp, ENABLE_ASF))
13712 tg3_read_dash_ver(tp);
13713 } else if (tg3_flag(tp, ENABLE_ASF)) {
13714 tg3_read_mgmtfw_ver(tp);
13715 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013716
Matt Carlson75f99362010-04-05 10:19:24 +000013717done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013718 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013719}
13720
Michael Chan7544b092007-05-05 13:08:32 -070013721static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13722
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013723static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13724{
Joe Perches63c3a662011-04-26 08:12:10 +000013725 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013726 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013727 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013728 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013729 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013730 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013731}
13732
Matt Carlson41434702011-03-09 16:58:22 +000013733static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013734 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13735 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13736 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13737 { },
13738};
13739
Linus Torvalds1da177e2005-04-16 15:20:36 -070013740static int __devinit tg3_get_invariants(struct tg3 *tp)
13741{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013742 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013743 u32 pci_state_reg, grc_misc_cfg;
13744 u32 val;
13745 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013746 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013747
Linus Torvalds1da177e2005-04-16 15:20:36 -070013748 /* Force memory write invalidate off. If we leave it on,
13749 * then on 5700_BX chips we have to enable a workaround.
13750 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13751 * to match the cacheline size. The Broadcom driver have this
13752 * workaround but turns MWI off all the times so never uses
13753 * it. This seems to suggest that the workaround is insufficient.
13754 */
13755 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13756 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13757 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13758
Matt Carlson16821282011-07-13 09:27:28 +000013759 /* Important! -- Make sure register accesses are byteswapped
13760 * correctly. Also, for those chips that require it, make
13761 * sure that indirect register accesses are enabled before
13762 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013763 */
13764 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13765 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013766 tp->misc_host_ctrl |= (misc_ctrl_reg &
13767 MISC_HOST_CTRL_CHIPREV);
13768 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13769 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013770
13771 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13772 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013773 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13774 u32 prod_id_asic_rev;
13775
Matt Carlson5001e2f2009-11-13 13:03:51 +000013776 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13777 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013778 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13779 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013780 pci_read_config_dword(tp->pdev,
13781 TG3PCI_GEN2_PRODID_ASICREV,
13782 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013783 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13784 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13785 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13786 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13787 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13788 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13789 pci_read_config_dword(tp->pdev,
13790 TG3PCI_GEN15_PRODID_ASICREV,
13791 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013792 else
13793 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13794 &prod_id_asic_rev);
13795
Matt Carlson321d32a2008-11-21 17:22:19 -080013796 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013798
Michael Chanff645be2005-04-21 17:09:53 -070013799 /* Wrong chip ID in 5752 A0. This code can be removed later
13800 * as A0 is not in production.
13801 */
13802 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13803 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13804
Michael Chan68929142005-08-09 20:17:14 -070013805 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13806 * we need to disable memory and use config. cycles
13807 * only to access all registers. The 5702/03 chips
13808 * can mistakenly decode the special cycles from the
13809 * ICH chipsets as memory write cycles, causing corruption
13810 * of register and memory space. Only certain ICH bridges
13811 * will drive special cycles with non-zero data during the
13812 * address phase which can fall within the 5703's address
13813 * range. This is not an ICH bug as the PCI spec allows
13814 * non-zero address during special cycles. However, only
13815 * these ICH bridges are known to drive non-zero addresses
13816 * during special cycles.
13817 *
13818 * Since special cycles do not cross PCI bridges, we only
13819 * enable this workaround if the 5703 is on the secondary
13820 * bus of these ICH bridges.
13821 */
13822 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13823 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13824 static struct tg3_dev_id {
13825 u32 vendor;
13826 u32 device;
13827 u32 rev;
13828 } ich_chipsets[] = {
13829 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13830 PCI_ANY_ID },
13831 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13832 PCI_ANY_ID },
13833 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13834 0xa },
13835 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13836 PCI_ANY_ID },
13837 { },
13838 };
13839 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13840 struct pci_dev *bridge = NULL;
13841
13842 while (pci_id->vendor != 0) {
13843 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13844 bridge);
13845 if (!bridge) {
13846 pci_id++;
13847 continue;
13848 }
13849 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013850 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013851 continue;
13852 }
13853 if (bridge->subordinate &&
13854 (bridge->subordinate->number ==
13855 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013856 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013857 pci_dev_put(bridge);
13858 break;
13859 }
13860 }
13861 }
13862
Matt Carlson6ff6f812011-05-19 12:12:54 +000013863 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013864 static struct tg3_dev_id {
13865 u32 vendor;
13866 u32 device;
13867 } bridge_chipsets[] = {
13868 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13869 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13870 { },
13871 };
13872 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13873 struct pci_dev *bridge = NULL;
13874
13875 while (pci_id->vendor != 0) {
13876 bridge = pci_get_device(pci_id->vendor,
13877 pci_id->device,
13878 bridge);
13879 if (!bridge) {
13880 pci_id++;
13881 continue;
13882 }
13883 if (bridge->subordinate &&
13884 (bridge->subordinate->number <=
13885 tp->pdev->bus->number) &&
13886 (bridge->subordinate->subordinate >=
13887 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013888 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013889 pci_dev_put(bridge);
13890 break;
13891 }
13892 }
13893 }
13894
Michael Chan4a29cc22006-03-19 13:21:12 -080013895 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13896 * DMA addresses > 40-bit. This bridge may have other additional
13897 * 57xx devices behind it in some 4-port NIC designs for example.
13898 * Any tg3 device found behind the bridge will also need the 40-bit
13899 * DMA workaround.
13900 */
Michael Chana4e2b342005-10-26 15:46:52 -070013901 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13902 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013903 tg3_flag_set(tp, 5780_CLASS);
13904 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013905 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013906 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013907 struct pci_dev *bridge = NULL;
13908
13909 do {
13910 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13911 PCI_DEVICE_ID_SERVERWORKS_EPB,
13912 bridge);
13913 if (bridge && bridge->subordinate &&
13914 (bridge->subordinate->number <=
13915 tp->pdev->bus->number) &&
13916 (bridge->subordinate->subordinate >=
13917 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013918 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013919 pci_dev_put(bridge);
13920 break;
13921 }
13922 } while (bridge);
13923 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013924
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013925 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000013926 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070013927 tp->pdev_peer = tg3_find_peer(tp);
13928
Matt Carlsonc885e822010-08-02 11:25:57 +000013929 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013930 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13931 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013932 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013933
13934 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013935 tg3_flag(tp, 5717_PLUS))
13936 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013937
Matt Carlson321d32a2008-11-21 17:22:19 -080013938 /* Intentionally exclude ASIC_REV_5906 */
13939 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013940 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013941 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013942 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013943 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013944 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013945 tg3_flag(tp, 57765_PLUS))
13946 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013947
13948 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13949 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013950 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013951 tg3_flag(tp, 5755_PLUS) ||
13952 tg3_flag(tp, 5780_CLASS))
13953 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013954
Matt Carlson6ff6f812011-05-19 12:12:54 +000013955 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013956 tg3_flag(tp, 5750_PLUS))
13957 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013958
Matt Carlson507399f2009-11-13 13:03:37 +000013959 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000013960 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000013961 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013962 else if (tg3_flag(tp, 57765_PLUS))
13963 tg3_flag_set(tp, HW_TSO_3);
13964 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013965 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013966 tg3_flag_set(tp, HW_TSO_2);
13967 else if (tg3_flag(tp, 5750_PLUS)) {
13968 tg3_flag_set(tp, HW_TSO_1);
13969 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013970 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13971 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013972 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013973 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13974 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13975 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013976 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013977 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13978 tp->fw_needed = FIRMWARE_TG3TSO5;
13979 else
13980 tp->fw_needed = FIRMWARE_TG3TSO;
13981 }
13982
Matt Carlsondabc5c62011-05-19 12:12:52 +000013983 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013984 if (tg3_flag(tp, HW_TSO_1) ||
13985 tg3_flag(tp, HW_TSO_2) ||
13986 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsondabc5c62011-05-19 12:12:52 +000013987 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13988 tg3_flag_set(tp, TSO_CAPABLE);
13989 else {
13990 tg3_flag_clear(tp, TSO_CAPABLE);
13991 tg3_flag_clear(tp, TSO_BUG);
13992 tp->fw_needed = NULL;
13993 }
13994
13995 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
13996 tp->fw_needed = FIRMWARE_TG3;
13997
Matt Carlson507399f2009-11-13 13:03:37 +000013998 tp->irq_max = 1;
13999
Joe Perches63c3a662011-04-26 08:12:10 +000014000 if (tg3_flag(tp, 5750_PLUS)) {
14001 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014002 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14003 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14004 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14005 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14006 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014007 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014008
Joe Perches63c3a662011-04-26 08:12:10 +000014009 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014010 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014011 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014013
Joe Perches63c3a662011-04-26 08:12:10 +000014014 if (tg3_flag(tp, 57765_PLUS)) {
14015 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014016 tp->irq_max = TG3_IRQ_MAX_VECS;
14017 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014018 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014019
Matt Carlson2ffcc982011-05-19 12:12:44 +000014020 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014021 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014022
Matt Carlsone31aa982011-07-27 14:20:53 +000014023 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
14024 tg3_flag_set(tp, 4K_FIFO_LIMIT);
14025
Joe Perches63c3a662011-04-26 08:12:10 +000014026 if (tg3_flag(tp, 5717_PLUS))
14027 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014028
Joe Perches63c3a662011-04-26 08:12:10 +000014029 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014030 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014031 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014032
Joe Perches63c3a662011-04-26 08:12:10 +000014033 if (!tg3_flag(tp, 5705_PLUS) ||
14034 tg3_flag(tp, 5780_CLASS) ||
14035 tg3_flag(tp, USE_JUMBO_BDFLAG))
14036 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014037
Matt Carlson52f44902008-11-21 17:17:04 -080014038 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14039 &pci_state_reg);
14040
Jon Mason708ebb32011-06-27 12:56:50 +000014041 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014042 u16 lnkctl;
14043
Joe Perches63c3a662011-04-26 08:12:10 +000014044 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014045
Matt Carlsoncf790032010-11-24 08:31:48 +000014046 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000014047 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14048 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000014049 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000014050
14051 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014052
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014053 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +000014054 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014055 &lnkctl);
14056 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014057 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14058 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014059 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014060 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014061 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014062 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014063 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014064 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14065 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014066 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b05902010-01-20 16:58:02 +000014067 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014068 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014069 }
Matt Carlson52f44902008-11-21 17:17:04 -080014070 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000014071 /* BCM5785 devices are effectively PCIe devices, and should
14072 * follow PCIe codepaths, but do not have a PCIe capabilities
14073 * section.
14074 */
Joe Perches63c3a662011-04-26 08:12:10 +000014075 tg3_flag_set(tp, PCI_EXPRESS);
14076 } else if (!tg3_flag(tp, 5705_PLUS) ||
14077 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014078 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14079 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014080 dev_err(&tp->pdev->dev,
14081 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014082 return -EIO;
14083 }
14084
14085 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014086 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014088
Michael Chan399de502005-10-03 14:02:39 -070014089 /* If we have an AMD 762 or VIA K8T800 chipset, write
14090 * reordering to the mailbox registers done by the host
14091 * controller can cause major troubles. We read back from
14092 * every mailbox register write to force the writes to be
14093 * posted to the chip in order.
14094 */
Matt Carlson41434702011-03-09 16:58:22 +000014095 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014096 !tg3_flag(tp, PCI_EXPRESS))
14097 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014098
Matt Carlson69fc4052008-12-21 20:19:57 -080014099 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14100 &tp->pci_cacheline_sz);
14101 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14102 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014103 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14104 tp->pci_lat_timer < 64) {
14105 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014106 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14107 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014108 }
14109
Matt Carlson16821282011-07-13 09:27:28 +000014110 /* Important! -- It is critical that the PCI-X hw workaround
14111 * situation is decided before the first MMIO register access.
14112 */
Matt Carlson52f44902008-11-21 17:17:04 -080014113 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14114 /* 5700 BX chips need to have their TX producer index
14115 * mailboxes written twice to workaround a bug.
14116 */
Joe Perches63c3a662011-04-26 08:12:10 +000014117 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014118
Matt Carlson52f44902008-11-21 17:17:04 -080014119 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014120 *
14121 * The workaround is to use indirect register accesses
14122 * for all chip writes not to mailbox registers.
14123 */
Joe Perches63c3a662011-04-26 08:12:10 +000014124 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014125 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014126
Joe Perches63c3a662011-04-26 08:12:10 +000014127 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014128
14129 /* The chip can have it's power management PCI config
14130 * space registers clobbered due to this bug.
14131 * So explicitly force the chip into D0 here.
14132 */
Matt Carlson9974a352007-10-07 23:27:28 -070014133 pci_read_config_dword(tp->pdev,
14134 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014135 &pm_reg);
14136 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14137 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014138 pci_write_config_dword(tp->pdev,
14139 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014140 pm_reg);
14141
14142 /* Also, force SERR#/PERR# in PCI command. */
14143 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14144 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14145 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14146 }
14147 }
14148
Linus Torvalds1da177e2005-04-16 15:20:36 -070014149 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014150 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014151 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014152 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014153
14154 /* Chip-specific fixup from Broadcom driver */
14155 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14156 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14157 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14158 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14159 }
14160
Michael Chan1ee582d2005-08-09 20:16:46 -070014161 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014162 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014163 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014164 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014165 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014166 tp->write32_tx_mbox = tg3_write32;
14167 tp->write32_rx_mbox = tg3_write32;
14168
14169 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014170 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014171 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014172 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014173 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014174 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14175 /*
14176 * Back to back register writes can cause problems on these
14177 * chips, the workaround is to read back all reg writes
14178 * except those to mailbox regs.
14179 *
14180 * See tg3_write_indirect_reg32().
14181 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014182 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014183 }
14184
Joe Perches63c3a662011-04-26 08:12:10 +000014185 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014186 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014187 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014188 tp->write32_rx_mbox = tg3_write_flush_reg32;
14189 }
Michael Chan20094932005-08-09 20:16:32 -070014190
Joe Perches63c3a662011-04-26 08:12:10 +000014191 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014192 tp->read32 = tg3_read_indirect_reg32;
14193 tp->write32 = tg3_write_indirect_reg32;
14194 tp->read32_mbox = tg3_read_indirect_mbox;
14195 tp->write32_mbox = tg3_write_indirect_mbox;
14196 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14197 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14198
14199 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014200 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014201
14202 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14203 pci_cmd &= ~PCI_COMMAND_MEMORY;
14204 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14205 }
Michael Chanb5d37722006-09-27 16:06:21 -070014206 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14207 tp->read32_mbox = tg3_read32_mbox_5906;
14208 tp->write32_mbox = tg3_write32_mbox_5906;
14209 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14210 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14211 }
Michael Chan68929142005-08-09 20:17:14 -070014212
Michael Chanbbadf502006-04-06 21:46:34 -070014213 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014214 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014215 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014216 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014217 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014218
Matt Carlson16821282011-07-13 09:27:28 +000014219 /* The memory arbiter has to be enabled in order for SRAM accesses
14220 * to succeed. Normally on powerup the tg3 chip firmware will make
14221 * sure it is enabled, but other entities such as system netboot
14222 * code might disable it.
14223 */
14224 val = tr32(MEMARB_MODE);
14225 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14226
Matt Carlson69f11c92011-07-13 09:27:30 +000014227 if (tg3_flag(tp, PCIX_MODE)) {
14228 pci_read_config_dword(tp->pdev,
14229 tp->pcix_cap + PCI_X_STATUS, &val);
14230 tp->pci_fn = val & 0x7;
14231 } else {
14232 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14233 }
14234
Michael Chan7d0c41e2005-04-21 17:06:20 -070014235 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014236 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014237 * determined before calling tg3_set_power_state() so that
14238 * we know whether or not to switch out of Vaux power.
14239 * When the flag is set, it means that GPIO1 is used for eeprom
14240 * write protect and also implies that it is a LOM where GPIOs
14241 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014242 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014243 tg3_get_eeprom_hw_cfg(tp);
14244
Joe Perches63c3a662011-04-26 08:12:10 +000014245 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014246 /* Allow reads and writes to the
14247 * APE register and memory space.
14248 */
14249 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +000014250 PCISTATE_ALLOW_APE_SHMEM_WR |
14251 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014252 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14253 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014254
14255 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014256 }
14257
Matt Carlson9936bcf2007-10-10 18:03:07 -070014258 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014259 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014260 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014261 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014262 tg3_flag(tp, 57765_PLUS))
14263 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014264
Matt Carlson16821282011-07-13 09:27:28 +000014265 /* Set up tp->grc_local_ctrl before calling
14266 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14267 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014268 * It is also used as eeprom write protect on LOMs.
14269 */
14270 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014271 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014272 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014273 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14274 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014275 /* Unused GPIO3 must be driven as output on 5752 because there
14276 * are no pull-up resistors on unused GPIO pins.
14277 */
14278 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14279 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014280
Matt Carlson321d32a2008-11-21 17:22:19 -080014281 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014282 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14283 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080014284 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14285
Matt Carlson8d519ab2009-04-20 06:58:01 +000014286 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14287 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014288 /* Turn off the debug UART. */
14289 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014290 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014291 /* Keep VMain power. */
14292 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14293 GRC_LCLCTRL_GPIO_OUTPUT0;
14294 }
14295
Matt Carlson16821282011-07-13 09:27:28 +000014296 /* Switch out of Vaux if it is a NIC */
14297 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014298
Linus Torvalds1da177e2005-04-16 15:20:36 -070014299 /* Derive initial jumbo mode from MTU assigned in
14300 * ether_setup() via the alloc_etherdev() call
14301 */
Joe Perches63c3a662011-04-26 08:12:10 +000014302 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14303 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014304
14305 /* Determine WakeOnLan speed to use. */
14306 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14307 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14308 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14309 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014310 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014311 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014312 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014313 }
14314
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014315 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014316 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014317
Linus Torvalds1da177e2005-04-16 15:20:36 -070014318 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014319 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14320 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014321 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014322 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014323 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14324 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14325 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014326
14327 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14328 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014329 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014330 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014331 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014332
Joe Perches63c3a662011-04-26 08:12:10 +000014333 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014334 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014335 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014336 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014337 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014338 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014339 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014340 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14341 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014342 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14343 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014344 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014345 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014346 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014347 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014348 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014350
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014351 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14352 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14353 tp->phy_otp = tg3_read_otp_phycfg(tp);
14354 if (tp->phy_otp == 0)
14355 tp->phy_otp = TG3_OTP_DEFAULT;
14356 }
14357
Joe Perches63c3a662011-04-26 08:12:10 +000014358 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014359 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14360 else
14361 tp->mi_mode = MAC_MI_MODE_BASE;
14362
Linus Torvalds1da177e2005-04-16 15:20:36 -070014363 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014364 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14365 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14366 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14367
Matt Carlson4d958472011-04-20 07:57:35 +000014368 /* Set these bits to enable statistics workaround. */
14369 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14370 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14371 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14372 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14373 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14374 }
14375
Matt Carlson321d32a2008-11-21 17:22:19 -080014376 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14377 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014378 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014379
Matt Carlson158d7ab2008-05-29 01:37:54 -070014380 err = tg3_mdio_init(tp);
14381 if (err)
14382 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014383
14384 /* Initialize data/descriptor byte/word swapping. */
14385 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014386 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14387 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14388 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14389 GRC_MODE_B2HRX_ENABLE |
14390 GRC_MODE_HTX2B_ENABLE |
14391 GRC_MODE_HOST_STACKUP);
14392 else
14393 val &= GRC_MODE_HOST_STACKUP;
14394
Linus Torvalds1da177e2005-04-16 15:20:36 -070014395 tw32(GRC_MODE, val | tp->grc_mode);
14396
14397 tg3_switch_clocks(tp);
14398
14399 /* Clear this out for sanity. */
14400 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14401
14402 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14403 &pci_state_reg);
14404 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014405 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014406 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14407
14408 if (chiprevid == CHIPREV_ID_5701_A0 ||
14409 chiprevid == CHIPREV_ID_5701_B0 ||
14410 chiprevid == CHIPREV_ID_5701_B2 ||
14411 chiprevid == CHIPREV_ID_5701_B5) {
14412 void __iomem *sram_base;
14413
14414 /* Write some dummy words into the SRAM status block
14415 * area, see if it reads back correctly. If the return
14416 * value is bad, force enable the PCIX workaround.
14417 */
14418 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14419
14420 writel(0x00000000, sram_base);
14421 writel(0x00000000, sram_base + 4);
14422 writel(0xffffffff, sram_base + 4);
14423 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014424 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014425 }
14426 }
14427
14428 udelay(50);
14429 tg3_nvram_init(tp);
14430
14431 grc_misc_cfg = tr32(GRC_MISC_CFG);
14432 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14433
Linus Torvalds1da177e2005-04-16 15:20:36 -070014434 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14435 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14436 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014437 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014438
Joe Perches63c3a662011-04-26 08:12:10 +000014439 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014440 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014441 tg3_flag_set(tp, TAGGED_STATUS);
14442 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014443 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14444 HOSTCC_MODE_CLRTICK_TXBD);
14445
14446 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14447 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14448 tp->misc_host_ctrl);
14449 }
14450
Matt Carlson3bda1252008-08-15 14:08:22 -070014451 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014452 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014453 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014454 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014455 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014456
Linus Torvalds1da177e2005-04-16 15:20:36 -070014457 /* these are limited to 10/100 only */
14458 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14459 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14460 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14461 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14462 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14463 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14464 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14465 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14466 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014467 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14468 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014469 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014470 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14471 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014472 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14473 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014474
14475 err = tg3_phy_probe(tp);
14476 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014477 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014478 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014479 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014480 }
14481
Matt Carlson184b8902010-04-05 10:19:25 +000014482 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014483 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014484
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014485 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14486 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014487 } else {
14488 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014489 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014490 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014491 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014492 }
14493
14494 /* 5700 {AX,BX} chips have a broken status block link
14495 * change bit implementation, so we must use the
14496 * status register in those cases.
14497 */
14498 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014499 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014500 else
Joe Perches63c3a662011-04-26 08:12:10 +000014501 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014502
14503 /* The led_ctrl is set during tg3_phy_probe, here we might
14504 * have to force the link status polling mechanism based
14505 * upon subsystem IDs.
14506 */
14507 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014508 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014509 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14510 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014511 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014512 }
14513
14514 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014515 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014516 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014517 else
Joe Perches63c3a662011-04-26 08:12:10 +000014518 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014519
Matt Carlsonbf933c82011-01-25 15:58:49 +000014520 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014521 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014522 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014523 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014524 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014525#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014526 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014527#endif
14528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014529
Matt Carlson2c49a442010-09-30 10:34:35 +000014530 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14531 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014532 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14533
Matt Carlson2c49a442010-09-30 10:34:35 +000014534 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014535
14536 /* Increment the rx prod index on the rx std ring by at most
14537 * 8 for these chips to workaround hw errata.
14538 */
14539 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14540 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14541 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14542 tp->rx_std_max_post = 8;
14543
Joe Perches63c3a662011-04-26 08:12:10 +000014544 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014545 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14546 PCIE_PWR_MGMT_L1_THRESH_MSK;
14547
Linus Torvalds1da177e2005-04-16 15:20:36 -070014548 return err;
14549}
14550
David S. Miller49b6e95f2007-03-29 01:38:42 -070014551#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014552static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14553{
14554 struct net_device *dev = tp->dev;
14555 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014556 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014557 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014558 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014559
David S. Miller49b6e95f2007-03-29 01:38:42 -070014560 addr = of_get_property(dp, "local-mac-address", &len);
14561 if (addr && len == 6) {
14562 memcpy(dev->dev_addr, addr, 6);
14563 memcpy(dev->perm_addr, dev->dev_addr, 6);
14564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014565 }
14566 return -ENODEV;
14567}
14568
14569static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14570{
14571 struct net_device *dev = tp->dev;
14572
14573 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014574 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014575 return 0;
14576}
14577#endif
14578
14579static int __devinit tg3_get_device_address(struct tg3 *tp)
14580{
14581 struct net_device *dev = tp->dev;
14582 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014583 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014584
David S. Miller49b6e95f2007-03-29 01:38:42 -070014585#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014586 if (!tg3_get_macaddr_sparc(tp))
14587 return 0;
14588#endif
14589
14590 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014591 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014592 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014593 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14594 mac_offset = 0xcc;
14595 if (tg3_nvram_lock(tp))
14596 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14597 else
14598 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014599 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014600 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014601 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014602 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014603 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014604 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014605 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014606
14607 /* First try to get it from MAC address mailbox. */
14608 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14609 if ((hi >> 16) == 0x484b) {
14610 dev->dev_addr[0] = (hi >> 8) & 0xff;
14611 dev->dev_addr[1] = (hi >> 0) & 0xff;
14612
14613 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14614 dev->dev_addr[2] = (lo >> 24) & 0xff;
14615 dev->dev_addr[3] = (lo >> 16) & 0xff;
14616 dev->dev_addr[4] = (lo >> 8) & 0xff;
14617 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014618
Michael Chan008652b2006-03-27 23:14:53 -080014619 /* Some old bootcode may report a 0 MAC address in SRAM */
14620 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14621 }
14622 if (!addr_ok) {
14623 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014624 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014625 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014626 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014627 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14628 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014629 }
14630 /* Finally just fetch it out of the MAC control regs. */
14631 else {
14632 hi = tr32(MAC_ADDR_0_HIGH);
14633 lo = tr32(MAC_ADDR_0_LOW);
14634
14635 dev->dev_addr[5] = lo & 0xff;
14636 dev->dev_addr[4] = (lo >> 8) & 0xff;
14637 dev->dev_addr[3] = (lo >> 16) & 0xff;
14638 dev->dev_addr[2] = (lo >> 24) & 0xff;
14639 dev->dev_addr[1] = hi & 0xff;
14640 dev->dev_addr[0] = (hi >> 8) & 0xff;
14641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014642 }
14643
14644 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014645#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014646 if (!tg3_get_default_macaddr_sparc(tp))
14647 return 0;
14648#endif
14649 return -EINVAL;
14650 }
John W. Linville2ff43692005-09-12 14:44:20 -070014651 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014652 return 0;
14653}
14654
David S. Miller59e6b432005-05-18 22:50:10 -070014655#define BOUNDARY_SINGLE_CACHELINE 1
14656#define BOUNDARY_MULTI_CACHELINE 2
14657
14658static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14659{
14660 int cacheline_size;
14661 u8 byte;
14662 int goal;
14663
14664 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14665 if (byte == 0)
14666 cacheline_size = 1024;
14667 else
14668 cacheline_size = (int) byte * 4;
14669
14670 /* On 5703 and later chips, the boundary bits have no
14671 * effect.
14672 */
14673 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14674 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014675 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014676 goto out;
14677
14678#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14679 goal = BOUNDARY_MULTI_CACHELINE;
14680#else
14681#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14682 goal = BOUNDARY_SINGLE_CACHELINE;
14683#else
14684 goal = 0;
14685#endif
14686#endif
14687
Joe Perches63c3a662011-04-26 08:12:10 +000014688 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014689 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14690 goto out;
14691 }
14692
David S. Miller59e6b432005-05-18 22:50:10 -070014693 if (!goal)
14694 goto out;
14695
14696 /* PCI controllers on most RISC systems tend to disconnect
14697 * when a device tries to burst across a cache-line boundary.
14698 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14699 *
14700 * Unfortunately, for PCI-E there are only limited
14701 * write-side controls for this, and thus for reads
14702 * we will still get the disconnects. We'll also waste
14703 * these PCI cycles for both read and write for chips
14704 * other than 5700 and 5701 which do not implement the
14705 * boundary bits.
14706 */
Joe Perches63c3a662011-04-26 08:12:10 +000014707 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014708 switch (cacheline_size) {
14709 case 16:
14710 case 32:
14711 case 64:
14712 case 128:
14713 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14714 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14715 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14716 } else {
14717 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14718 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14719 }
14720 break;
14721
14722 case 256:
14723 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14724 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14725 break;
14726
14727 default:
14728 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14729 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14730 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014731 }
Joe Perches63c3a662011-04-26 08:12:10 +000014732 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014733 switch (cacheline_size) {
14734 case 16:
14735 case 32:
14736 case 64:
14737 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14738 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14739 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14740 break;
14741 }
14742 /* fallthrough */
14743 case 128:
14744 default:
14745 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14746 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14747 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014748 }
David S. Miller59e6b432005-05-18 22:50:10 -070014749 } else {
14750 switch (cacheline_size) {
14751 case 16:
14752 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14753 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14754 DMA_RWCTRL_WRITE_BNDRY_16);
14755 break;
14756 }
14757 /* fallthrough */
14758 case 32:
14759 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14760 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14761 DMA_RWCTRL_WRITE_BNDRY_32);
14762 break;
14763 }
14764 /* fallthrough */
14765 case 64:
14766 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14767 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14768 DMA_RWCTRL_WRITE_BNDRY_64);
14769 break;
14770 }
14771 /* fallthrough */
14772 case 128:
14773 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14774 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14775 DMA_RWCTRL_WRITE_BNDRY_128);
14776 break;
14777 }
14778 /* fallthrough */
14779 case 256:
14780 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14781 DMA_RWCTRL_WRITE_BNDRY_256);
14782 break;
14783 case 512:
14784 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14785 DMA_RWCTRL_WRITE_BNDRY_512);
14786 break;
14787 case 1024:
14788 default:
14789 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14790 DMA_RWCTRL_WRITE_BNDRY_1024);
14791 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014792 }
David S. Miller59e6b432005-05-18 22:50:10 -070014793 }
14794
14795out:
14796 return val;
14797}
14798
Linus Torvalds1da177e2005-04-16 15:20:36 -070014799static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14800{
14801 struct tg3_internal_buffer_desc test_desc;
14802 u32 sram_dma_descs;
14803 int i, ret;
14804
14805 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14806
14807 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14808 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14809 tw32(RDMAC_STATUS, 0);
14810 tw32(WDMAC_STATUS, 0);
14811
14812 tw32(BUFMGR_MODE, 0);
14813 tw32(FTQ_RESET, 0);
14814
14815 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14816 test_desc.addr_lo = buf_dma & 0xffffffff;
14817 test_desc.nic_mbuf = 0x00002100;
14818 test_desc.len = size;
14819
14820 /*
14821 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14822 * the *second* time the tg3 driver was getting loaded after an
14823 * initial scan.
14824 *
14825 * Broadcom tells me:
14826 * ...the DMA engine is connected to the GRC block and a DMA
14827 * reset may affect the GRC block in some unpredictable way...
14828 * The behavior of resets to individual blocks has not been tested.
14829 *
14830 * Broadcom noted the GRC reset will also reset all sub-components.
14831 */
14832 if (to_device) {
14833 test_desc.cqid_sqid = (13 << 8) | 2;
14834
14835 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14836 udelay(40);
14837 } else {
14838 test_desc.cqid_sqid = (16 << 8) | 7;
14839
14840 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14841 udelay(40);
14842 }
14843 test_desc.flags = 0x00000005;
14844
14845 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14846 u32 val;
14847
14848 val = *(((u32 *)&test_desc) + i);
14849 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14850 sram_dma_descs + (i * sizeof(u32)));
14851 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14852 }
14853 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14854
Matt Carlson859a5882010-04-05 10:19:28 +000014855 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014856 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014857 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014858 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014859
14860 ret = -ENODEV;
14861 for (i = 0; i < 40; i++) {
14862 u32 val;
14863
14864 if (to_device)
14865 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14866 else
14867 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14868 if ((val & 0xffff) == sram_dma_descs) {
14869 ret = 0;
14870 break;
14871 }
14872
14873 udelay(100);
14874 }
14875
14876 return ret;
14877}
14878
David S. Millerded73402005-05-23 13:59:47 -070014879#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014880
Matt Carlson41434702011-03-09 16:58:22 +000014881static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014882 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14883 { },
14884};
14885
Linus Torvalds1da177e2005-04-16 15:20:36 -070014886static int __devinit tg3_test_dma(struct tg3 *tp)
14887{
14888 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014889 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014890 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014891
Matt Carlson4bae65c2010-11-24 08:31:52 +000014892 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14893 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014894 if (!buf) {
14895 ret = -ENOMEM;
14896 goto out_nofree;
14897 }
14898
14899 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14900 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14901
David S. Miller59e6b432005-05-18 22:50:10 -070014902 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014903
Joe Perches63c3a662011-04-26 08:12:10 +000014904 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014905 goto out;
14906
Joe Perches63c3a662011-04-26 08:12:10 +000014907 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014908 /* DMA read watermark not used on PCIE */
14909 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014910 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014911 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14912 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014913 tp->dma_rwctrl |= 0x003f0000;
14914 else
14915 tp->dma_rwctrl |= 0x003f000f;
14916 } else {
14917 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14918 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14919 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014920 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014921
Michael Chan4a29cc22006-03-19 13:21:12 -080014922 /* If the 5704 is behind the EPB bridge, we can
14923 * do the less restrictive ONE_DMA workaround for
14924 * better performance.
14925 */
Joe Perches63c3a662011-04-26 08:12:10 +000014926 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014927 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14928 tp->dma_rwctrl |= 0x8000;
14929 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014930 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14931
Michael Chan49afdeb2007-02-13 12:17:03 -080014932 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14933 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014934 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014935 tp->dma_rwctrl |=
14936 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14937 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14938 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014939 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14940 /* 5780 always in PCIX mode */
14941 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014942 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14943 /* 5714 always in PCIX mode */
14944 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014945 } else {
14946 tp->dma_rwctrl |= 0x001b000f;
14947 }
14948 }
14949
14950 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14951 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14952 tp->dma_rwctrl &= 0xfffffff0;
14953
14954 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14955 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14956 /* Remove this if it causes problems for some boards. */
14957 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14958
14959 /* On 5700/5701 chips, we need to set this bit.
14960 * Otherwise the chip will issue cacheline transactions
14961 * to streamable DMA memory with not all the byte
14962 * enables turned on. This is an error on several
14963 * RISC PCI controllers, in particular sparc64.
14964 *
14965 * On 5703/5704 chips, this bit has been reassigned
14966 * a different meaning. In particular, it is used
14967 * on those chips to enable a PCI-X workaround.
14968 */
14969 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14970 }
14971
14972 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14973
14974#if 0
14975 /* Unneeded, already done by tg3_get_invariants. */
14976 tg3_switch_clocks(tp);
14977#endif
14978
Linus Torvalds1da177e2005-04-16 15:20:36 -070014979 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14980 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14981 goto out;
14982
David S. Miller59e6b432005-05-18 22:50:10 -070014983 /* It is best to perform DMA test with maximum write burst size
14984 * to expose the 5700/5701 write DMA bug.
14985 */
14986 saved_dma_rwctrl = tp->dma_rwctrl;
14987 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14988 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14989
Linus Torvalds1da177e2005-04-16 15:20:36 -070014990 while (1) {
14991 u32 *p = buf, i;
14992
14993 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14994 p[i] = i;
14995
14996 /* Send the buffer to the chip. */
14997 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14998 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014999 dev_err(&tp->pdev->dev,
15000 "%s: Buffer write failed. err = %d\n",
15001 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015002 break;
15003 }
15004
15005#if 0
15006 /* validate data reached card RAM correctly. */
15007 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15008 u32 val;
15009 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15010 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015011 dev_err(&tp->pdev->dev,
15012 "%s: Buffer corrupted on device! "
15013 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015014 /* ret = -ENODEV here? */
15015 }
15016 p[i] = 0;
15017 }
15018#endif
15019 /* Now read it back. */
15020 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15021 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015022 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15023 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015024 break;
15025 }
15026
15027 /* Verify it. */
15028 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15029 if (p[i] == i)
15030 continue;
15031
David S. Miller59e6b432005-05-18 22:50:10 -070015032 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15033 DMA_RWCTRL_WRITE_BNDRY_16) {
15034 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015035 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15036 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15037 break;
15038 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015039 dev_err(&tp->pdev->dev,
15040 "%s: Buffer corrupted on read back! "
15041 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015042 ret = -ENODEV;
15043 goto out;
15044 }
15045 }
15046
15047 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15048 /* Success. */
15049 ret = 0;
15050 break;
15051 }
15052 }
David S. Miller59e6b432005-05-18 22:50:10 -070015053 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15054 DMA_RWCTRL_WRITE_BNDRY_16) {
15055 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015056 * now look for chipsets that are known to expose the
15057 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015058 */
Matt Carlson41434702011-03-09 16:58:22 +000015059 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015060 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15061 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000015062 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015063 /* Safe to use the calculated DMA boundary. */
15064 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000015065 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015066
David S. Miller59e6b432005-05-18 22:50:10 -070015067 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015069
15070out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015071 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015072out_nofree:
15073 return ret;
15074}
15075
Linus Torvalds1da177e2005-04-16 15:20:36 -070015076static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15077{
Joe Perches63c3a662011-04-26 08:12:10 +000015078 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015079 tp->bufmgr_config.mbuf_read_dma_low_water =
15080 DEFAULT_MB_RDMA_LOW_WATER_5705;
15081 tp->bufmgr_config.mbuf_mac_rx_low_water =
15082 DEFAULT_MB_MACRX_LOW_WATER_57765;
15083 tp->bufmgr_config.mbuf_high_water =
15084 DEFAULT_MB_HIGH_WATER_57765;
15085
15086 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15087 DEFAULT_MB_RDMA_LOW_WATER_5705;
15088 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15089 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15090 tp->bufmgr_config.mbuf_high_water_jumbo =
15091 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015092 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec172005-07-25 12:31:48 -070015093 tp->bufmgr_config.mbuf_read_dma_low_water =
15094 DEFAULT_MB_RDMA_LOW_WATER_5705;
15095 tp->bufmgr_config.mbuf_mac_rx_low_water =
15096 DEFAULT_MB_MACRX_LOW_WATER_5705;
15097 tp->bufmgr_config.mbuf_high_water =
15098 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015099 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15100 tp->bufmgr_config.mbuf_mac_rx_low_water =
15101 DEFAULT_MB_MACRX_LOW_WATER_5906;
15102 tp->bufmgr_config.mbuf_high_water =
15103 DEFAULT_MB_HIGH_WATER_5906;
15104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015105
Michael Chanfdfec172005-07-25 12:31:48 -070015106 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15107 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15108 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15109 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15110 tp->bufmgr_config.mbuf_high_water_jumbo =
15111 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15112 } else {
15113 tp->bufmgr_config.mbuf_read_dma_low_water =
15114 DEFAULT_MB_RDMA_LOW_WATER;
15115 tp->bufmgr_config.mbuf_mac_rx_low_water =
15116 DEFAULT_MB_MACRX_LOW_WATER;
15117 tp->bufmgr_config.mbuf_high_water =
15118 DEFAULT_MB_HIGH_WATER;
15119
15120 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15121 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15122 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15123 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15124 tp->bufmgr_config.mbuf_high_water_jumbo =
15125 DEFAULT_MB_HIGH_WATER_JUMBO;
15126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015127
15128 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15129 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15130}
15131
15132static char * __devinit tg3_phy_string(struct tg3 *tp)
15133{
Matt Carlson79eb6902010-02-17 15:17:03 +000015134 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15135 case TG3_PHY_ID_BCM5400: return "5400";
15136 case TG3_PHY_ID_BCM5401: return "5401";
15137 case TG3_PHY_ID_BCM5411: return "5411";
15138 case TG3_PHY_ID_BCM5701: return "5701";
15139 case TG3_PHY_ID_BCM5703: return "5703";
15140 case TG3_PHY_ID_BCM5704: return "5704";
15141 case TG3_PHY_ID_BCM5705: return "5705";
15142 case TG3_PHY_ID_BCM5750: return "5750";
15143 case TG3_PHY_ID_BCM5752: return "5752";
15144 case TG3_PHY_ID_BCM5714: return "5714";
15145 case TG3_PHY_ID_BCM5780: return "5780";
15146 case TG3_PHY_ID_BCM5755: return "5755";
15147 case TG3_PHY_ID_BCM5787: return "5787";
15148 case TG3_PHY_ID_BCM5784: return "5784";
15149 case TG3_PHY_ID_BCM5756: return "5722/5756";
15150 case TG3_PHY_ID_BCM5906: return "5906";
15151 case TG3_PHY_ID_BCM5761: return "5761";
15152 case TG3_PHY_ID_BCM5718C: return "5718C";
15153 case TG3_PHY_ID_BCM5718S: return "5718S";
15154 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015155 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015156 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015157 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015158 case 0: return "serdes";
15159 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015161}
15162
Michael Chanf9804dd2005-09-27 12:13:10 -070015163static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15164{
Joe Perches63c3a662011-04-26 08:12:10 +000015165 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015166 strcpy(str, "PCI Express");
15167 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015168 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015169 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15170
15171 strcpy(str, "PCIX:");
15172
15173 if ((clock_ctrl == 7) ||
15174 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15175 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15176 strcat(str, "133MHz");
15177 else if (clock_ctrl == 0)
15178 strcat(str, "33MHz");
15179 else if (clock_ctrl == 2)
15180 strcat(str, "50MHz");
15181 else if (clock_ctrl == 4)
15182 strcat(str, "66MHz");
15183 else if (clock_ctrl == 6)
15184 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015185 } else {
15186 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015187 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015188 strcat(str, "66MHz");
15189 else
15190 strcat(str, "33MHz");
15191 }
Joe Perches63c3a662011-04-26 08:12:10 +000015192 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015193 strcat(str, ":32-bit");
15194 else
15195 strcat(str, ":64-bit");
15196 return str;
15197}
15198
Michael Chan8c2dc7e2005-12-19 16:26:02 -080015199static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015200{
15201 struct pci_dev *peer;
15202 unsigned int func, devnr = tp->pdev->devfn & ~7;
15203
15204 for (func = 0; func < 8; func++) {
15205 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15206 if (peer && peer != tp->pdev)
15207 break;
15208 pci_dev_put(peer);
15209 }
Michael Chan16fe9d72005-12-13 21:09:54 -080015210 /* 5704 can be configured in single-port mode, set peer to
15211 * tp->pdev in that case.
15212 */
15213 if (!peer) {
15214 peer = tp->pdev;
15215 return peer;
15216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015217
15218 /*
15219 * We don't need to keep the refcount elevated; there's no way
15220 * to remove one half of this device without removing the other
15221 */
15222 pci_dev_put(peer);
15223
15224 return peer;
15225}
15226
David S. Miller15f98502005-05-18 22:49:26 -070015227static void __devinit tg3_init_coal(struct tg3 *tp)
15228{
15229 struct ethtool_coalesce *ec = &tp->coal;
15230
15231 memset(ec, 0, sizeof(*ec));
15232 ec->cmd = ETHTOOL_GCOALESCE;
15233 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15234 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15235 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15236 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15237 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15238 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15239 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15240 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15241 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15242
15243 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15244 HOSTCC_MODE_CLRTICK_TXBD)) {
15245 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15246 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15247 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15248 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15249 }
Michael Chand244c892005-07-05 14:42:33 -070015250
Joe Perches63c3a662011-04-26 08:12:10 +000015251 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015252 ec->rx_coalesce_usecs_irq = 0;
15253 ec->tx_coalesce_usecs_irq = 0;
15254 ec->stats_block_coalesce_usecs = 0;
15255 }
David S. Miller15f98502005-05-18 22:49:26 -070015256}
15257
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015258static const struct net_device_ops tg3_netdev_ops = {
15259 .ndo_open = tg3_open,
15260 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080015261 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000015262 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080015263 .ndo_validate_addr = eth_validate_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +000015264 .ndo_set_rx_mode = tg3_set_rx_mode,
Stephen Hemminger00829822008-11-20 20:14:53 -080015265 .ndo_set_mac_address = tg3_set_mac_addr,
15266 .ndo_do_ioctl = tg3_ioctl,
15267 .ndo_tx_timeout = tg3_tx_timeout,
15268 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000015269 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015270 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080015271#ifdef CONFIG_NET_POLL_CONTROLLER
15272 .ndo_poll_controller = tg3_poll_controller,
15273#endif
15274};
15275
Linus Torvalds1da177e2005-04-16 15:20:36 -070015276static int __devinit tg3_init_one(struct pci_dev *pdev,
15277 const struct pci_device_id *ent)
15278{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015279 struct net_device *dev;
15280 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015281 int i, err, pm_cap;
15282 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015283 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015284 u64 dma_mask, persist_dma_mask;
Matt Carlson0da06062011-05-19 12:12:53 +000015285 u32 features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015286
Joe Perches05dbe002010-02-17 19:44:19 +000015287 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015288
15289 err = pci_enable_device(pdev);
15290 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015291 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015292 return err;
15293 }
15294
Linus Torvalds1da177e2005-04-16 15:20:36 -070015295 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15296 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015297 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015298 goto err_out_disable_pdev;
15299 }
15300
15301 pci_set_master(pdev);
15302
15303 /* Find power-management capability. */
15304 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15305 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015306 dev_err(&pdev->dev,
15307 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015308 err = -EIO;
15309 goto err_out_free_res;
15310 }
15311
Matt Carlson16821282011-07-13 09:27:28 +000015312 err = pci_set_power_state(pdev, PCI_D0);
15313 if (err) {
15314 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15315 goto err_out_free_res;
15316 }
15317
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015318 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015319 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015320 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015321 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015322 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015323 }
15324
Linus Torvalds1da177e2005-04-16 15:20:36 -070015325 SET_NETDEV_DEV(dev, &pdev->dev);
15326
Linus Torvalds1da177e2005-04-16 15:20:36 -070015327 tp = netdev_priv(dev);
15328 tp->pdev = pdev;
15329 tp->dev = dev;
15330 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015331 tp->rx_mode = TG3_DEF_RX_MODE;
15332 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015333
Linus Torvalds1da177e2005-04-16 15:20:36 -070015334 if (tg3_debug > 0)
15335 tp->msg_enable = tg3_debug;
15336 else
15337 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15338
15339 /* The word/byte swap controls here control register access byte
15340 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15341 * setting below.
15342 */
15343 tp->misc_host_ctrl =
15344 MISC_HOST_CTRL_MASK_PCI_INT |
15345 MISC_HOST_CTRL_WORD_SWAP |
15346 MISC_HOST_CTRL_INDIR_ACCESS |
15347 MISC_HOST_CTRL_PCISTATE_RW;
15348
15349 /* The NONFRM (non-frame) byte/word swap controls take effect
15350 * on descriptor entries, anything which isn't packet data.
15351 *
15352 * The StrongARM chips on the board (one for tx, one for rx)
15353 * are running in big-endian mode.
15354 */
15355 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15356 GRC_MODE_WSWAP_NONFRM_DATA);
15357#ifdef __BIG_ENDIAN
15358 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15359#endif
15360 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015361 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015362 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015363
Matt Carlsond5fe4882008-11-21 17:20:32 -080015364 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015365 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015366 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015367 err = -ENOMEM;
15368 goto err_out_free_dev;
15369 }
15370
Matt Carlsonc9cab242011-07-13 09:27:27 +000015371 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15372 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15373 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15374 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15375 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15376 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15377 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15378 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15379 tg3_flag_set(tp, ENABLE_APE);
15380 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15381 if (!tp->aperegs) {
15382 dev_err(&pdev->dev,
15383 "Cannot map APE registers, aborting\n");
15384 err = -ENOMEM;
15385 goto err_out_iounmap;
15386 }
15387 }
15388
Linus Torvalds1da177e2005-04-16 15:20:36 -070015389 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15390 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015391
Linus Torvalds1da177e2005-04-16 15:20:36 -070015392 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015393 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015394 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015395 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015396
15397 err = tg3_get_invariants(tp);
15398 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015399 dev_err(&pdev->dev,
15400 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015401 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015402 }
15403
Michael Chan4a29cc22006-03-19 13:21:12 -080015404 /* The EPB bridge inside 5714, 5715, and 5780 and any
15405 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015406 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15407 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15408 * do DMA address check in tg3_start_xmit().
15409 */
Joe Perches63c3a662011-04-26 08:12:10 +000015410 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015411 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015412 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015413 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015414#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015415 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015416#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015417 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015418 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015419
15420 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015421 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015422 err = pci_set_dma_mask(pdev, dma_mask);
15423 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015424 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015425 err = pci_set_consistent_dma_mask(pdev,
15426 persist_dma_mask);
15427 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015428 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15429 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015430 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015431 }
15432 }
15433 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015434 if (err || dma_mask == DMA_BIT_MASK(32)) {
15435 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015436 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015437 dev_err(&pdev->dev,
15438 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015439 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015440 }
15441 }
15442
Michael Chanfdfec172005-07-25 12:31:48 -070015443 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015444
Matt Carlson0da06062011-05-19 12:12:53 +000015445 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15446
15447 /* 5700 B0 chips do not support checksumming correctly due
15448 * to hardware bugs.
15449 */
15450 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15451 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15452
15453 if (tg3_flag(tp, 5755_PLUS))
15454 features |= NETIF_F_IPV6_CSUM;
15455 }
15456
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015457 /* TSO is on by default on chips that support hardware TSO.
15458 * Firmware TSO on older chips gives lower performance, so it
15459 * is off by default, but can be enabled using ethtool.
15460 */
Joe Perches63c3a662011-04-26 08:12:10 +000015461 if ((tg3_flag(tp, HW_TSO_1) ||
15462 tg3_flag(tp, HW_TSO_2) ||
15463 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015464 (features & NETIF_F_IP_CSUM))
15465 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015466 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015467 if (features & NETIF_F_IPV6_CSUM)
15468 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015469 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015470 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015471 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15472 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015473 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015474 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015475 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015477
Matt Carlsond542fe22011-05-19 16:02:43 +000015478 dev->features |= features;
15479 dev->vlan_features |= features;
15480
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015481 /*
15482 * Add loopback capability only for a subset of devices that support
15483 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15484 * loopback for the remaining devices.
15485 */
15486 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15487 !tg3_flag(tp, CPMU_PRESENT))
15488 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015489 features |= NETIF_F_LOOPBACK;
15490
Matt Carlson0da06062011-05-19 12:12:53 +000015491 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015492
Linus Torvalds1da177e2005-04-16 15:20:36 -070015493 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015494 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015495 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015496 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015497 tp->rx_pending = 63;
15498 }
15499
Linus Torvalds1da177e2005-04-16 15:20:36 -070015500 err = tg3_get_device_address(tp);
15501 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015502 dev_err(&pdev->dev,
15503 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015504 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015505 }
15506
Matt Carlsonc88864d2007-11-12 21:07:01 -080015507 /*
15508 * Reset chip in case UNDI or EFI driver did not shutdown
15509 * DMA self test will enable WDMAC and we'll see (spurious)
15510 * pending DMA on the PCI bus at that point.
15511 */
15512 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15513 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15514 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15515 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15516 }
15517
15518 err = tg3_test_dma(tp);
15519 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015520 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015521 goto err_out_apeunmap;
15522 }
15523
Matt Carlson78f90dc2009-11-13 13:03:42 +000015524 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15525 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15526 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015527 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015528 struct tg3_napi *tnapi = &tp->napi[i];
15529
15530 tnapi->tp = tp;
15531 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15532
15533 tnapi->int_mbox = intmbx;
15534 if (i < 4)
15535 intmbx += 0x8;
15536 else
15537 intmbx += 0x4;
15538
15539 tnapi->consmbox = rcvmbx;
15540 tnapi->prodmbox = sndmbx;
15541
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015542 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015543 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015544 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015545 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015546
Joe Perches63c3a662011-04-26 08:12:10 +000015547 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015548 break;
15549
15550 /*
15551 * If we support MSIX, we'll be using RSS. If we're using
15552 * RSS, the first vector only handles link interrupts and the
15553 * remaining vectors handle rx and tx interrupts. Reuse the
15554 * mailbox values for the next iteration. The values we setup
15555 * above are still useful for the single vectored mode.
15556 */
15557 if (!i)
15558 continue;
15559
15560 rcvmbx += 0x8;
15561
15562 if (sndmbx & 0x4)
15563 sndmbx -= 0x4;
15564 else
15565 sndmbx += 0xc;
15566 }
15567
Matt Carlsonc88864d2007-11-12 21:07:01 -080015568 tg3_init_coal(tp);
15569
Michael Chanc49a1562006-12-17 17:07:29 -080015570 pci_set_drvdata(pdev, dev);
15571
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015572 if (tg3_flag(tp, 5717_PLUS)) {
15573 /* Resume a low-power mode */
15574 tg3_frob_aux_power(tp, false);
15575 }
15576
Linus Torvalds1da177e2005-04-16 15:20:36 -070015577 err = register_netdev(dev);
15578 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015579 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015580 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015581 }
15582
Joe Perches05dbe002010-02-17 19:44:19 +000015583 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15584 tp->board_part_number,
15585 tp->pci_chip_rev_id,
15586 tg3_bus_string(tp, str),
15587 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015588
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015589 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015590 struct phy_device *phydev;
15591 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015592 netdev_info(dev,
15593 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015594 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015595 } else {
15596 char *ethtype;
15597
15598 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15599 ethtype = "10/100Base-TX";
15600 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15601 ethtype = "1000Base-SX";
15602 else
15603 ethtype = "10/100/1000Base-T";
15604
Matt Carlson5129c3a2010-04-05 10:19:23 +000015605 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015606 "(WireSpeed[%d], EEE[%d])\n",
15607 tg3_phy_string(tp), ethtype,
15608 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15609 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015610 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015611
Joe Perches05dbe002010-02-17 19:44:19 +000015612 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015613 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015614 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015615 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015616 tg3_flag(tp, ENABLE_ASF) != 0,
15617 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015618 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15619 tp->dma_rwctrl,
15620 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15621 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015622
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015623 pci_save_state(pdev);
15624
Linus Torvalds1da177e2005-04-16 15:20:36 -070015625 return 0;
15626
Matt Carlson0d3031d2007-10-10 18:02:43 -070015627err_out_apeunmap:
15628 if (tp->aperegs) {
15629 iounmap(tp->aperegs);
15630 tp->aperegs = NULL;
15631 }
15632
Linus Torvalds1da177e2005-04-16 15:20:36 -070015633err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015634 if (tp->regs) {
15635 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015636 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015638
15639err_out_free_dev:
15640 free_netdev(dev);
15641
Matt Carlson16821282011-07-13 09:27:28 +000015642err_out_power_down:
15643 pci_set_power_state(pdev, PCI_D3hot);
15644
Linus Torvalds1da177e2005-04-16 15:20:36 -070015645err_out_free_res:
15646 pci_release_regions(pdev);
15647
15648err_out_disable_pdev:
15649 pci_disable_device(pdev);
15650 pci_set_drvdata(pdev, NULL);
15651 return err;
15652}
15653
15654static void __devexit tg3_remove_one(struct pci_dev *pdev)
15655{
15656 struct net_device *dev = pci_get_drvdata(pdev);
15657
15658 if (dev) {
15659 struct tg3 *tp = netdev_priv(dev);
15660
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015661 if (tp->fw)
15662 release_firmware(tp->fw);
15663
Tejun Heo23f333a2010-12-12 16:45:14 +010015664 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015665
Joe Perches63c3a662011-04-26 08:12:10 +000015666 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015667 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015668 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015669 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015670
Linus Torvalds1da177e2005-04-16 15:20:36 -070015671 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015672 if (tp->aperegs) {
15673 iounmap(tp->aperegs);
15674 tp->aperegs = NULL;
15675 }
Michael Chan68929142005-08-09 20:17:14 -070015676 if (tp->regs) {
15677 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015678 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015680 free_netdev(dev);
15681 pci_release_regions(pdev);
15682 pci_disable_device(pdev);
15683 pci_set_drvdata(pdev, NULL);
15684 }
15685}
15686
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015687#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015688static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015689{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015690 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015691 struct net_device *dev = pci_get_drvdata(pdev);
15692 struct tg3 *tp = netdev_priv(dev);
15693 int err;
15694
15695 if (!netif_running(dev))
15696 return 0;
15697
Tejun Heo23f333a2010-12-12 16:45:14 +010015698 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015699 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015700 tg3_netif_stop(tp);
15701
15702 del_timer_sync(&tp->timer);
15703
David S. Millerf47c11e2005-06-24 20:18:35 -070015704 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015705 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015706 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015707
15708 netif_device_detach(dev);
15709
David S. Millerf47c11e2005-06-24 20:18:35 -070015710 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015711 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015712 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015713 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015714
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015715 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015716 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015717 int err2;
15718
David S. Millerf47c11e2005-06-24 20:18:35 -070015719 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015720
Joe Perches63c3a662011-04-26 08:12:10 +000015721 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015722 err2 = tg3_restart_hw(tp, 1);
15723 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015724 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015725
15726 tp->timer.expires = jiffies + tp->timer_offset;
15727 add_timer(&tp->timer);
15728
15729 netif_device_attach(dev);
15730 tg3_netif_start(tp);
15731
Michael Chanb9ec6c12006-07-25 16:37:27 -070015732out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015733 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015734
15735 if (!err2)
15736 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015737 }
15738
15739 return err;
15740}
15741
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015742static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015743{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015744 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015745 struct net_device *dev = pci_get_drvdata(pdev);
15746 struct tg3 *tp = netdev_priv(dev);
15747 int err;
15748
15749 if (!netif_running(dev))
15750 return 0;
15751
Linus Torvalds1da177e2005-04-16 15:20:36 -070015752 netif_device_attach(dev);
15753
David S. Millerf47c11e2005-06-24 20:18:35 -070015754 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015755
Joe Perches63c3a662011-04-26 08:12:10 +000015756 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015757 err = tg3_restart_hw(tp, 1);
15758 if (err)
15759 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015760
15761 tp->timer.expires = jiffies + tp->timer_offset;
15762 add_timer(&tp->timer);
15763
Linus Torvalds1da177e2005-04-16 15:20:36 -070015764 tg3_netif_start(tp);
15765
Michael Chanb9ec6c12006-07-25 16:37:27 -070015766out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015767 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015768
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015769 if (!err)
15770 tg3_phy_start(tp);
15771
Michael Chanb9ec6c12006-07-25 16:37:27 -070015772 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015773}
15774
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015775static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015776#define TG3_PM_OPS (&tg3_pm_ops)
15777
15778#else
15779
15780#define TG3_PM_OPS NULL
15781
15782#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015783
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015784/**
15785 * tg3_io_error_detected - called when PCI error is detected
15786 * @pdev: Pointer to PCI device
15787 * @state: The current pci connection state
15788 *
15789 * This function is called after a PCI bus error affecting
15790 * this device has been detected.
15791 */
15792static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15793 pci_channel_state_t state)
15794{
15795 struct net_device *netdev = pci_get_drvdata(pdev);
15796 struct tg3 *tp = netdev_priv(netdev);
15797 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15798
15799 netdev_info(netdev, "PCI I/O error detected\n");
15800
15801 rtnl_lock();
15802
15803 if (!netif_running(netdev))
15804 goto done;
15805
15806 tg3_phy_stop(tp);
15807
15808 tg3_netif_stop(tp);
15809
15810 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015811 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015812
15813 /* Want to make sure that the reset task doesn't run */
15814 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015815 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15816 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015817
15818 netif_device_detach(netdev);
15819
15820 /* Clean up software state, even if MMIO is blocked */
15821 tg3_full_lock(tp, 0);
15822 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15823 tg3_full_unlock(tp);
15824
15825done:
15826 if (state == pci_channel_io_perm_failure)
15827 err = PCI_ERS_RESULT_DISCONNECT;
15828 else
15829 pci_disable_device(pdev);
15830
15831 rtnl_unlock();
15832
15833 return err;
15834}
15835
15836/**
15837 * tg3_io_slot_reset - called after the pci bus has been reset.
15838 * @pdev: Pointer to PCI device
15839 *
15840 * Restart the card from scratch, as if from a cold-boot.
15841 * At this point, the card has exprienced a hard reset,
15842 * followed by fixups by BIOS, and has its config space
15843 * set up identically to what it was at cold boot.
15844 */
15845static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15846{
15847 struct net_device *netdev = pci_get_drvdata(pdev);
15848 struct tg3 *tp = netdev_priv(netdev);
15849 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15850 int err;
15851
15852 rtnl_lock();
15853
15854 if (pci_enable_device(pdev)) {
15855 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15856 goto done;
15857 }
15858
15859 pci_set_master(pdev);
15860 pci_restore_state(pdev);
15861 pci_save_state(pdev);
15862
15863 if (!netif_running(netdev)) {
15864 rc = PCI_ERS_RESULT_RECOVERED;
15865 goto done;
15866 }
15867
15868 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000015869 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015870 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015871
15872 rc = PCI_ERS_RESULT_RECOVERED;
15873
15874done:
15875 rtnl_unlock();
15876
15877 return rc;
15878}
15879
15880/**
15881 * tg3_io_resume - called when traffic can start flowing again.
15882 * @pdev: Pointer to PCI device
15883 *
15884 * This callback is called when the error recovery driver tells
15885 * us that its OK to resume normal operation.
15886 */
15887static void tg3_io_resume(struct pci_dev *pdev)
15888{
15889 struct net_device *netdev = pci_get_drvdata(pdev);
15890 struct tg3 *tp = netdev_priv(netdev);
15891 int err;
15892
15893 rtnl_lock();
15894
15895 if (!netif_running(netdev))
15896 goto done;
15897
15898 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015899 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015900 err = tg3_restart_hw(tp, 1);
15901 tg3_full_unlock(tp);
15902 if (err) {
15903 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15904 goto done;
15905 }
15906
15907 netif_device_attach(netdev);
15908
15909 tp->timer.expires = jiffies + tp->timer_offset;
15910 add_timer(&tp->timer);
15911
15912 tg3_netif_start(tp);
15913
15914 tg3_phy_start(tp);
15915
15916done:
15917 rtnl_unlock();
15918}
15919
15920static struct pci_error_handlers tg3_err_handler = {
15921 .error_detected = tg3_io_error_detected,
15922 .slot_reset = tg3_io_slot_reset,
15923 .resume = tg3_io_resume
15924};
15925
Linus Torvalds1da177e2005-04-16 15:20:36 -070015926static struct pci_driver tg3_driver = {
15927 .name = DRV_MODULE_NAME,
15928 .id_table = tg3_pci_tbl,
15929 .probe = tg3_init_one,
15930 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015931 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015932 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015933};
15934
15935static int __init tg3_init(void)
15936{
Jeff Garzik29917622006-08-19 17:48:59 -040015937 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015938}
15939
15940static void __exit tg3_cleanup(void)
15941{
15942 pci_unregister_driver(&tg3_driver);
15943}
15944
15945module_init(tg3_init);
15946module_exit(tg3_cleanup);