blob: 45715b18e7bed0a940cbfb862eb31dd71ec80cce [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
Matt Carlson81389f52011-08-31 11:44:49 +0000190#if (NET_IP_ALIGN != 0)
191#define TG3_RX_OFFSET(tp) ((tp)->rx_offset)
192#else
193#define TG3_RX_OFFSET(tp) 0
194#endif
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000197#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Matt Carlsone31aa982011-07-27 14:20:53 +0000198#define TG3_TX_BD_DMA_MAX 4096
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Matt Carlsonad829262008-11-21 17:16:16 -0800200#define TG3_RAW_IP_ALIGN 2
201
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000202#define TG3_FW_UPDATE_TIMEOUT_SEC 5
203
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800204#define FIRMWARE_TG3 "tigon/tg3.bin"
205#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
206#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000209 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
212MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
213MODULE_LICENSE("GPL");
214MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800215MODULE_FIRMWARE(FIRMWARE_TG3);
216MODULE_FIRMWARE(FIRMWARE_TG3TSO);
217MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
220module_param(tg3_debug, int, 0);
221MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
222
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000223static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
291 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
292 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
293 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
294 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000295 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000296 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700297 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
298 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
299 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
300 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
301 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
302 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
303 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000304 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700305 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306};
307
308MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
309
Andreas Mohr50da8592006-08-14 23:54:30 -0700310static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000312} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 { "rx_octets" },
314 { "rx_fragments" },
315 { "rx_ucast_packets" },
316 { "rx_mcast_packets" },
317 { "rx_bcast_packets" },
318 { "rx_fcs_errors" },
319 { "rx_align_errors" },
320 { "rx_xon_pause_rcvd" },
321 { "rx_xoff_pause_rcvd" },
322 { "rx_mac_ctrl_rcvd" },
323 { "rx_xoff_entered" },
324 { "rx_frame_too_long_errors" },
325 { "rx_jabbers" },
326 { "rx_undersize_packets" },
327 { "rx_in_length_errors" },
328 { "rx_out_length_errors" },
329 { "rx_64_or_less_octet_packets" },
330 { "rx_65_to_127_octet_packets" },
331 { "rx_128_to_255_octet_packets" },
332 { "rx_256_to_511_octet_packets" },
333 { "rx_512_to_1023_octet_packets" },
334 { "rx_1024_to_1522_octet_packets" },
335 { "rx_1523_to_2047_octet_packets" },
336 { "rx_2048_to_4095_octet_packets" },
337 { "rx_4096_to_8191_octet_packets" },
338 { "rx_8192_to_9022_octet_packets" },
339
340 { "tx_octets" },
341 { "tx_collisions" },
342
343 { "tx_xon_sent" },
344 { "tx_xoff_sent" },
345 { "tx_flow_control" },
346 { "tx_mac_errors" },
347 { "tx_single_collisions" },
348 { "tx_mult_collisions" },
349 { "tx_deferred" },
350 { "tx_excessive_collisions" },
351 { "tx_late_collisions" },
352 { "tx_collide_2times" },
353 { "tx_collide_3times" },
354 { "tx_collide_4times" },
355 { "tx_collide_5times" },
356 { "tx_collide_6times" },
357 { "tx_collide_7times" },
358 { "tx_collide_8times" },
359 { "tx_collide_9times" },
360 { "tx_collide_10times" },
361 { "tx_collide_11times" },
362 { "tx_collide_12times" },
363 { "tx_collide_13times" },
364 { "tx_collide_14times" },
365 { "tx_collide_15times" },
366 { "tx_ucast_packets" },
367 { "tx_mcast_packets" },
368 { "tx_bcast_packets" },
369 { "tx_carrier_sense_errors" },
370 { "tx_discards" },
371 { "tx_errors" },
372
373 { "dma_writeq_full" },
374 { "dma_write_prioq_full" },
375 { "rxbds_empty" },
376 { "rx_discards" },
377 { "rx_errors" },
378 { "rx_threshold_hit" },
379
380 { "dma_readq_full" },
381 { "dma_read_prioq_full" },
382 { "tx_comp_queue_full" },
383
384 { "ring_set_send_prod_index" },
385 { "ring_status_update" },
386 { "nic_irqs" },
387 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000388 { "nic_tx_threshold_hit" },
389
390 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391};
392
Matt Carlson48fa55a2011-04-13 11:05:06 +0000393#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
394
395
Andreas Mohr50da8592006-08-14 23:54:30 -0700396static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700397 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000398} ethtool_test_keys[] = {
Matt Carlson28a45952011-08-19 13:58:22 +0000399 { "nvram test (online) " },
400 { "link test (online) " },
401 { "register test (offline)" },
402 { "memory test (offline)" },
403 { "mac loopback test (offline)" },
404 { "phy loopback test (offline)" },
Matt Carlson941ec902011-08-19 13:58:23 +0000405 { "ext loopback test (offline)" },
Matt Carlson28a45952011-08-19 13:58:22 +0000406 { "interrupt test (offline)" },
Michael Chan4cafd3f2005-05-29 14:56:34 -0700407};
408
Matt Carlson48fa55a2011-04-13 11:05:06 +0000409#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
410
411
Michael Chanb401e9e2005-12-19 16:27:04 -0800412static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
413{
414 writel(val, tp->regs + off);
415}
416
417static u32 tg3_read32(struct tg3 *tp, u32 off)
418{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000419 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800420}
421
Matt Carlson0d3031d2007-10-10 18:02:43 -0700422static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
423{
424 writel(val, tp->aperegs + off);
425}
426
427static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
428{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000429 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
433{
Michael Chan68929142005-08-09 20:17:14 -0700434 unsigned long flags;
435
436 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700437 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
438 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700439 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700440}
441
442static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
443{
444 writel(val, tp->regs + off);
445 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Michael Chan68929142005-08-09 20:17:14 -0700448static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
449{
450 unsigned long flags;
451 u32 val;
452
453 spin_lock_irqsave(&tp->indirect_lock, flags);
454 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
455 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
456 spin_unlock_irqrestore(&tp->indirect_lock, flags);
457 return val;
458}
459
460static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
461{
462 unsigned long flags;
463
464 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
465 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
466 TG3_64BIT_REG_LOW, val);
467 return;
468 }
Matt Carlson66711e62009-11-13 13:03:49 +0000469 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700470 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
471 TG3_64BIT_REG_LOW, val);
472 return;
473 }
474
475 spin_lock_irqsave(&tp->indirect_lock, flags);
476 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
477 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
478 spin_unlock_irqrestore(&tp->indirect_lock, flags);
479
480 /* In indirect mode when disabling interrupts, we also need
481 * to clear the interrupt bit in the GRC local ctrl register.
482 */
483 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
484 (val == 0x1)) {
485 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
486 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
487 }
488}
489
490static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
491{
492 unsigned long flags;
493 u32 val;
494
495 spin_lock_irqsave(&tp->indirect_lock, flags);
496 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
497 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
498 spin_unlock_irqrestore(&tp->indirect_lock, flags);
499 return val;
500}
501
Michael Chanb401e9e2005-12-19 16:27:04 -0800502/* usec_wait specifies the wait time in usec when writing to certain registers
503 * where it is unsafe to read back the register without some delay.
504 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
505 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
506 */
507static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Joe Perches63c3a662011-04-26 08:12:10 +0000509 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800510 /* Non-posted methods */
511 tp->write32(tp, off, val);
512 else {
513 /* Posted method */
514 tg3_write32(tp, off, val);
515 if (usec_wait)
516 udelay(usec_wait);
517 tp->read32(tp, off);
518 }
519 /* Wait again after the read for the posted method to guarantee that
520 * the wait time is met.
521 */
522 if (usec_wait)
523 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Michael Chan09ee9292005-08-09 20:17:00 -0700526static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
527{
528 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000529 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700530 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700531}
532
Michael Chan20094932005-08-09 20:16:32 -0700533static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 void __iomem *mbox = tp->regs + off;
536 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000537 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000539 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 readl(mbox);
541}
542
Michael Chanb5d37722006-09-27 16:06:21 -0700543static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
544{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000545 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700546}
547
548static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
549{
550 writel(val, tp->regs + off + GRCMBOX_BASE);
551}
552
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000553#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700554#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000555#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
556#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
557#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700558
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000559#define tw32(reg, val) tp->write32(tp, reg, val)
560#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
561#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
562#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
565{
Michael Chan68929142005-08-09 20:17:14 -0700566 unsigned long flags;
567
Matt Carlson6ff6f812011-05-19 12:12:54 +0000568 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700569 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
570 return;
571
Michael Chan68929142005-08-09 20:17:14 -0700572 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000573 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700574 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
575 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Michael Chanbbadf502006-04-06 21:46:34 -0700577 /* Always leave this as zero. */
578 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
579 } else {
580 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
581 tw32_f(TG3PCI_MEM_WIN_DATA, val);
582
583 /* Always leave this as zero. */
584 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
585 }
Michael Chan68929142005-08-09 20:17:14 -0700586 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
589static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
590{
Michael Chan68929142005-08-09 20:17:14 -0700591 unsigned long flags;
592
Matt Carlson6ff6f812011-05-19 12:12:54 +0000593 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700594 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
595 *val = 0;
596 return;
597 }
598
Michael Chan68929142005-08-09 20:17:14 -0700599 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000600 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700601 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
602 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Michael Chanbbadf502006-04-06 21:46:34 -0700604 /* Always leave this as zero. */
605 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
606 } else {
607 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
608 *val = tr32(TG3PCI_MEM_WIN_DATA);
609
610 /* Always leave this as zero. */
611 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
612 }
Michael Chan68929142005-08-09 20:17:14 -0700613 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Matt Carlson0d3031d2007-10-10 18:02:43 -0700616static void tg3_ape_lock_init(struct tg3 *tp)
617{
618 int i;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000619 u32 regbase, bit;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000620
621 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
622 regbase = TG3_APE_LOCK_GRANT;
623 else
624 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700625
626 /* Make sure the driver hasn't any stale locks. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000627 for (i = 0; i < 8; i++) {
628 if (i == TG3_APE_LOCK_GPIO)
629 continue;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000630 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000631 }
632
633 /* Clear the correct bit of the GPIO lock too. */
634 if (!tp->pci_fn)
635 bit = APE_LOCK_GRANT_DRIVER;
636 else
637 bit = 1 << tp->pci_fn;
638
639 tg3_ape_write32(tp, regbase + 4 * TG3_APE_LOCK_GPIO, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700640}
641
642static int tg3_ape_lock(struct tg3 *tp, int locknum)
643{
644 int i, off;
645 int ret = 0;
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000646 u32 status, req, gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700647
Joe Perches63c3a662011-04-26 08:12:10 +0000648 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700649 return 0;
650
651 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000652 case TG3_APE_LOCK_GPIO:
653 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
654 return 0;
Matt Carlson33f401a2010-04-05 10:19:27 +0000655 case TG3_APE_LOCK_GRC:
656 case TG3_APE_LOCK_MEM:
657 break;
658 default:
659 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700660 }
661
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000662 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
663 req = TG3_APE_LOCK_REQ;
664 gnt = TG3_APE_LOCK_GRANT;
665 } else {
666 req = TG3_APE_PER_LOCK_REQ;
667 gnt = TG3_APE_PER_LOCK_GRANT;
668 }
669
Matt Carlson0d3031d2007-10-10 18:02:43 -0700670 off = 4 * locknum;
671
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000672 if (locknum != TG3_APE_LOCK_GPIO || !tp->pci_fn)
673 bit = APE_LOCK_REQ_DRIVER;
674 else
675 bit = 1 << tp->pci_fn;
676
677 tg3_ape_write32(tp, req + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700678
679 /* Wait for up to 1 millisecond to acquire lock. */
680 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000681 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000682 if (status == bit)
Matt Carlson0d3031d2007-10-10 18:02:43 -0700683 break;
684 udelay(10);
685 }
686
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000687 if (status != bit) {
Matt Carlson0d3031d2007-10-10 18:02:43 -0700688 /* Revoke the lock request. */
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000689 tg3_ape_write32(tp, gnt + off, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700690 ret = -EBUSY;
691 }
692
693 return ret;
694}
695
696static void tg3_ape_unlock(struct tg3 *tp, int locknum)
697{
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000698 u32 gnt, bit;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700699
Joe Perches63c3a662011-04-26 08:12:10 +0000700 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700701 return;
702
703 switch (locknum) {
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000704 case TG3_APE_LOCK_GPIO:
705 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
706 return;
Matt Carlson33f401a2010-04-05 10:19:27 +0000707 case TG3_APE_LOCK_GRC:
708 case TG3_APE_LOCK_MEM:
709 break;
710 default:
711 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700712 }
713
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000714 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
715 gnt = TG3_APE_LOCK_GRANT;
716 else
717 gnt = TG3_APE_PER_LOCK_GRANT;
718
Matt Carlson6f5c8f832011-07-13 09:27:31 +0000719 if (locknum != TG3_APE_LOCK_GPIO || !tp->pci_fn)
720 bit = APE_LOCK_GRANT_DRIVER;
721 else
722 bit = 1 << tp->pci_fn;
723
724 tg3_ape_write32(tp, gnt + 4 * locknum, bit);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727static void tg3_disable_ints(struct tg3 *tp)
728{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000729 int i;
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 tw32(TG3PCI_MISC_HOST_CTRL,
732 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000733 for (i = 0; i < tp->irq_max; i++)
734 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737static void tg3_enable_ints(struct tg3 *tp)
738{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000739 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000740
Michael Chanbbe832c2005-06-24 20:20:04 -0700741 tp->irq_sync = 0;
742 wmb();
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 tw32(TG3PCI_MISC_HOST_CTRL,
745 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000746
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000747 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000748 for (i = 0; i < tp->irq_cnt; i++) {
749 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000750
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000751 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000752 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000753 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
754
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000755 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000756 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000757
758 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000759 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000760 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
761 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
762 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000763 tw32(HOSTCC_MODE, tp->coal_now);
764
765 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Matt Carlson17375d22009-08-28 14:02:18 +0000768static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700769{
Matt Carlson17375d22009-08-28 14:02:18 +0000770 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000771 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700772 unsigned int work_exists = 0;
773
774 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000775 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700776 if (sblk->status & SD_STATUS_LINK_CHG)
777 work_exists = 1;
778 }
779 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000780 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000781 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700782 work_exists = 1;
783
784 return work_exists;
785}
786
Matt Carlson17375d22009-08-28 14:02:18 +0000787/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700788 * similar to tg3_enable_ints, but it accurately determines whether there
789 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400790 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 */
Matt Carlson17375d22009-08-28 14:02:18 +0000792static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Matt Carlson17375d22009-08-28 14:02:18 +0000794 struct tg3 *tp = tnapi->tp;
795
Matt Carlson898a56f2009-08-28 14:02:40 +0000796 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 mmiowb();
798
David S. Millerfac9b832005-05-18 22:46:34 -0700799 /* When doing tagged status, this work check is unnecessary.
800 * The last_tag we write above tells the chip which piece of
801 * work we've completed.
802 */
Joe Perches63c3a662011-04-26 08:12:10 +0000803 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700804 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000805 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808static void tg3_switch_clocks(struct tg3 *tp)
809{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000810 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 u32 orig_clock_ctrl;
812
Joe Perches63c3a662011-04-26 08:12:10 +0000813 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700814 return;
815
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000816 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 orig_clock_ctrl = clock_ctrl;
819 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
820 CLOCK_CTRL_CLKRUN_OENABLE |
821 0x1f);
822 tp->pci_clock_ctrl = clock_ctrl;
823
Joe Perches63c3a662011-04-26 08:12:10 +0000824 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800826 tw32_wait_f(TG3PCI_CLOCK_CTRL,
827 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800830 tw32_wait_f(TG3PCI_CLOCK_CTRL,
831 clock_ctrl |
832 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
833 40);
834 tw32_wait_f(TG3PCI_CLOCK_CTRL,
835 clock_ctrl | (CLOCK_CTRL_ALTCLK),
836 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800838 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841#define PHY_BUSY_LOOPS 5000
842
843static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
844{
845 u32 frame_val;
846 unsigned int loops;
847 int ret;
848
849 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
850 tw32_f(MAC_MI_MODE,
851 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
852 udelay(80);
853 }
854
855 *val = 0x0;
856
Matt Carlson882e9792009-09-01 13:21:36 +0000857 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 MI_COM_PHY_ADDR_MASK);
859 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
860 MI_COM_REG_ADDR_MASK);
861 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 tw32_f(MAC_MI_COM, frame_val);
864
865 loops = PHY_BUSY_LOOPS;
866 while (loops != 0) {
867 udelay(10);
868 frame_val = tr32(MAC_MI_COM);
869
870 if ((frame_val & MI_COM_BUSY) == 0) {
871 udelay(5);
872 frame_val = tr32(MAC_MI_COM);
873 break;
874 }
875 loops -= 1;
876 }
877
878 ret = -EBUSY;
879 if (loops != 0) {
880 *val = frame_val & MI_COM_DATA_MASK;
881 ret = 0;
882 }
883
884 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
885 tw32_f(MAC_MI_MODE, tp->mi_mode);
886 udelay(80);
887 }
888
889 return ret;
890}
891
892static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
893{
894 u32 frame_val;
895 unsigned int loops;
896 int ret;
897
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000898 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +0000899 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -0700900 return 0;
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
903 tw32_f(MAC_MI_MODE,
904 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
905 udelay(80);
906 }
907
Matt Carlson882e9792009-09-01 13:21:36 +0000908 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 MI_COM_PHY_ADDR_MASK);
910 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
911 MI_COM_REG_ADDR_MASK);
912 frame_val |= (val & MI_COM_DATA_MASK);
913 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 tw32_f(MAC_MI_COM, frame_val);
916
917 loops = PHY_BUSY_LOOPS;
918 while (loops != 0) {
919 udelay(10);
920 frame_val = tr32(MAC_MI_COM);
921 if ((frame_val & MI_COM_BUSY) == 0) {
922 udelay(5);
923 frame_val = tr32(MAC_MI_COM);
924 break;
925 }
926 loops -= 1;
927 }
928
929 ret = -EBUSY;
930 if (loops != 0)
931 ret = 0;
932
933 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
934 tw32_f(MAC_MI_MODE, tp->mi_mode);
935 udelay(80);
936 }
937
938 return ret;
939}
940
Matt Carlsonb0988c12011-04-20 07:57:39 +0000941static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
942{
943 int err;
944
945 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
946 if (err)
947 goto done;
948
949 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
950 if (err)
951 goto done;
952
953 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
954 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
955 if (err)
956 goto done;
957
958 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
959
960done:
961 return err;
962}
963
964static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
965{
966 int err;
967
968 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
969 if (err)
970 goto done;
971
972 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
973 if (err)
974 goto done;
975
976 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
977 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
978 if (err)
979 goto done;
980
981 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
982
983done:
984 return err;
985}
986
987static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
988{
989 int err;
990
991 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
992 if (!err)
993 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
994
995 return err;
996}
997
998static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
999{
1000 int err;
1001
1002 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1003 if (!err)
1004 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1005
1006 return err;
1007}
1008
Matt Carlson15ee95c2011-04-20 07:57:40 +00001009static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
1010{
1011 int err;
1012
1013 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
1014 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
1015 MII_TG3_AUXCTL_SHDWSEL_MISC);
1016 if (!err)
1017 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
1018
1019 return err;
1020}
1021
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001022static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
1023{
1024 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
1025 set |= MII_TG3_AUXCTL_MISC_WREN;
1026
1027 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
1028}
1029
Matt Carlson1d36ba42011-04-20 07:57:42 +00001030#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
1031 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1032 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1033 MII_TG3_AUXCTL_ACTL_TX_6DB)
1034
1035#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1036 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1037 MII_TG3_AUXCTL_ACTL_TX_6DB);
1038
Matt Carlson95e28692008-05-25 23:44:14 -07001039static int tg3_bmcr_reset(struct tg3 *tp)
1040{
1041 u32 phy_control;
1042 int limit, err;
1043
1044 /* OK, reset it, and poll the BMCR_RESET bit until it
1045 * clears or we time out.
1046 */
1047 phy_control = BMCR_RESET;
1048 err = tg3_writephy(tp, MII_BMCR, phy_control);
1049 if (err != 0)
1050 return -EBUSY;
1051
1052 limit = 5000;
1053 while (limit--) {
1054 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1055 if (err != 0)
1056 return -EBUSY;
1057
1058 if ((phy_control & BMCR_RESET) == 0) {
1059 udelay(40);
1060 break;
1061 }
1062 udelay(10);
1063 }
Roel Kluind4675b52009-02-12 16:33:27 -08001064 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001065 return -EBUSY;
1066
1067 return 0;
1068}
1069
Matt Carlson158d7ab2008-05-29 01:37:54 -07001070static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1071{
Francois Romieu3d165432009-01-19 16:56:50 -08001072 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001073 u32 val;
1074
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001075 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001076
1077 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001078 val = -EIO;
1079
1080 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001081
1082 return val;
1083}
1084
1085static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1086{
Francois Romieu3d165432009-01-19 16:56:50 -08001087 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001088 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001089
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001090 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001091
1092 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001093 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001094
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001095 spin_unlock_bh(&tp->lock);
1096
1097 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001098}
1099
1100static int tg3_mdio_reset(struct mii_bus *bp)
1101{
1102 return 0;
1103}
1104
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001105static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001106{
1107 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001108 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001109
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001110 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001111 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001112 case PHY_ID_BCM50610:
1113 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001114 val = MAC_PHYCFG2_50610_LED_MODES;
1115 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001116 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001117 val = MAC_PHYCFG2_AC131_LED_MODES;
1118 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001119 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001120 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1121 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001122 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001123 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1124 break;
1125 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001126 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001127 }
1128
1129 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1130 tw32(MAC_PHYCFG2, val);
1131
1132 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001133 val &= ~(MAC_PHYCFG1_RGMII_INT |
1134 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1135 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001136 tw32(MAC_PHYCFG1, val);
1137
1138 return;
1139 }
1140
Joe Perches63c3a662011-04-26 08:12:10 +00001141 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001142 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1143 MAC_PHYCFG2_FMODE_MASK_MASK |
1144 MAC_PHYCFG2_GMODE_MASK_MASK |
1145 MAC_PHYCFG2_ACT_MASK_MASK |
1146 MAC_PHYCFG2_QUAL_MASK_MASK |
1147 MAC_PHYCFG2_INBAND_ENABLE;
1148
1149 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001150
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001151 val = tr32(MAC_PHYCFG1);
1152 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1153 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001154 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1155 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001156 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001157 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001158 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1159 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001160 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1161 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1162 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001163
Matt Carlsona9daf362008-05-25 23:49:44 -07001164 val = tr32(MAC_EXT_RGMII_MODE);
1165 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1166 MAC_RGMII_MODE_RX_QUALITY |
1167 MAC_RGMII_MODE_RX_ACTIVITY |
1168 MAC_RGMII_MODE_RX_ENG_DET |
1169 MAC_RGMII_MODE_TX_ENABLE |
1170 MAC_RGMII_MODE_TX_LOWPWR |
1171 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001172 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1173 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001174 val |= MAC_RGMII_MODE_RX_INT_B |
1175 MAC_RGMII_MODE_RX_QUALITY |
1176 MAC_RGMII_MODE_RX_ACTIVITY |
1177 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001178 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001179 val |= MAC_RGMII_MODE_TX_ENABLE |
1180 MAC_RGMII_MODE_TX_LOWPWR |
1181 MAC_RGMII_MODE_TX_RESET;
1182 }
1183 tw32(MAC_EXT_RGMII_MODE, val);
1184}
1185
Matt Carlson158d7ab2008-05-29 01:37:54 -07001186static void tg3_mdio_start(struct tg3 *tp)
1187{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001188 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1189 tw32_f(MAC_MI_MODE, tp->mi_mode);
1190 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001191
Joe Perches63c3a662011-04-26 08:12:10 +00001192 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001193 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1194 tg3_mdio_config_5785(tp);
1195}
1196
1197static int tg3_mdio_init(struct tg3 *tp)
1198{
1199 int i;
1200 u32 reg;
1201 struct phy_device *phydev;
1202
Joe Perches63c3a662011-04-26 08:12:10 +00001203 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001204 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001205
Matt Carlson69f11c92011-07-13 09:27:30 +00001206 tp->phy_addr = tp->pci_fn + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001207
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001208 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1209 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1210 else
1211 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1212 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001213 if (is_serdes)
1214 tp->phy_addr += 7;
1215 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001216 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001217
Matt Carlson158d7ab2008-05-29 01:37:54 -07001218 tg3_mdio_start(tp);
1219
Joe Perches63c3a662011-04-26 08:12:10 +00001220 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001221 return 0;
1222
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001223 tp->mdio_bus = mdiobus_alloc();
1224 if (tp->mdio_bus == NULL)
1225 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001226
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001227 tp->mdio_bus->name = "tg3 mdio bus";
1228 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001229 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001230 tp->mdio_bus->priv = tp;
1231 tp->mdio_bus->parent = &tp->pdev->dev;
1232 tp->mdio_bus->read = &tg3_mdio_read;
1233 tp->mdio_bus->write = &tg3_mdio_write;
1234 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001235 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001236 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001237
1238 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001239 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001240
1241 /* The bus registration will look for all the PHYs on the mdio bus.
1242 * Unfortunately, it does not ensure the PHY is powered up before
1243 * accessing the PHY ID registers. A chip reset is the
1244 * quickest way to bring the device back to an operational state..
1245 */
1246 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1247 tg3_bmcr_reset(tp);
1248
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001249 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001250 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001251 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001252 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001253 return i;
1254 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001255
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001256 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001257
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001258 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001259 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001260 mdiobus_unregister(tp->mdio_bus);
1261 mdiobus_free(tp->mdio_bus);
1262 return -ENODEV;
1263 }
1264
1265 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001266 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001267 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001268 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001269 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001270 case PHY_ID_BCM50610:
1271 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001272 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001273 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001274 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001275 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001276 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001277 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001278 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001279 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001280 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001281 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001282 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001283 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001284 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001285 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001286 case PHY_ID_RTL8201E:
1287 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001288 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e092009-11-02 14:31:11 +00001289 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001290 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001291 break;
1292 }
1293
Joe Perches63c3a662011-04-26 08:12:10 +00001294 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001295
1296 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1297 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001298
1299 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001300}
1301
1302static void tg3_mdio_fini(struct tg3 *tp)
1303{
Joe Perches63c3a662011-04-26 08:12:10 +00001304 if (tg3_flag(tp, MDIOBUS_INITED)) {
1305 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001306 mdiobus_unregister(tp->mdio_bus);
1307 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001308 }
1309}
1310
Matt Carlson95e28692008-05-25 23:44:14 -07001311/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001312static inline void tg3_generate_fw_event(struct tg3 *tp)
1313{
1314 u32 val;
1315
1316 val = tr32(GRC_RX_CPU_EVENT);
1317 val |= GRC_RX_CPU_DRIVER_EVENT;
1318 tw32_f(GRC_RX_CPU_EVENT, val);
1319
1320 tp->last_event_jiffies = jiffies;
1321}
1322
1323#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1324
1325/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001326static void tg3_wait_for_event_ack(struct tg3 *tp)
1327{
1328 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001329 unsigned int delay_cnt;
1330 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001331
Matt Carlson4ba526c2008-08-15 14:10:04 -07001332 /* If enough time has passed, no wait is necessary. */
1333 time_remain = (long)(tp->last_event_jiffies + 1 +
1334 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1335 (long)jiffies;
1336 if (time_remain < 0)
1337 return;
1338
1339 /* Check if we can shorten the wait time. */
1340 delay_cnt = jiffies_to_usecs(time_remain);
1341 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1342 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1343 delay_cnt = (delay_cnt >> 3) + 1;
1344
1345 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001346 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1347 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001348 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001349 }
1350}
1351
1352/* tp->lock is held. */
1353static void tg3_ump_link_report(struct tg3 *tp)
1354{
1355 u32 reg;
1356 u32 val;
1357
Joe Perches63c3a662011-04-26 08:12:10 +00001358 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001359 return;
1360
1361 tg3_wait_for_event_ack(tp);
1362
1363 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1364
1365 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1366
1367 val = 0;
1368 if (!tg3_readphy(tp, MII_BMCR, &reg))
1369 val = reg << 16;
1370 if (!tg3_readphy(tp, MII_BMSR, &reg))
1371 val |= (reg & 0xffff);
1372 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1373
1374 val = 0;
1375 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1376 val = reg << 16;
1377 if (!tg3_readphy(tp, MII_LPA, &reg))
1378 val |= (reg & 0xffff);
1379 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1380
1381 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001382 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001383 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1384 val = reg << 16;
1385 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1386 val |= (reg & 0xffff);
1387 }
1388 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1389
1390 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1391 val = reg << 16;
1392 else
1393 val = 0;
1394 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1395
Matt Carlson4ba526c2008-08-15 14:10:04 -07001396 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001397}
1398
1399static void tg3_link_report(struct tg3 *tp)
1400{
1401 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001402 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001403 tg3_ump_link_report(tp);
1404 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001405 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1406 (tp->link_config.active_speed == SPEED_1000 ?
1407 1000 :
1408 (tp->link_config.active_speed == SPEED_100 ?
1409 100 : 10)),
1410 (tp->link_config.active_duplex == DUPLEX_FULL ?
1411 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001412
Joe Perches05dbe002010-02-17 19:44:19 +00001413 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1414 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1415 "on" : "off",
1416 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1417 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001418
1419 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1420 netdev_info(tp->dev, "EEE is %s\n",
1421 tp->setlpicnt ? "enabled" : "disabled");
1422
Matt Carlson95e28692008-05-25 23:44:14 -07001423 tg3_ump_link_report(tp);
1424 }
1425}
1426
1427static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1428{
1429 u16 miireg;
1430
Steve Glendinninge18ce342008-12-16 02:00:00 -08001431 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001432 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001433 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001434 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001435 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001436 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1437 else
1438 miireg = 0;
1439
1440 return miireg;
1441}
1442
1443static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1444{
1445 u16 miireg;
1446
Steve Glendinninge18ce342008-12-16 02:00:00 -08001447 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001448 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001449 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001450 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001451 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001452 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1453 else
1454 miireg = 0;
1455
1456 return miireg;
1457}
1458
Matt Carlson95e28692008-05-25 23:44:14 -07001459static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1460{
1461 u8 cap = 0;
1462
1463 if (lcladv & ADVERTISE_1000XPAUSE) {
1464 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1465 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001466 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001467 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001468 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001469 } else {
1470 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001471 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001472 }
1473 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1474 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001475 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001476 }
1477
1478 return cap;
1479}
1480
Matt Carlsonf51f3562008-05-25 23:45:08 -07001481static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001482{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001483 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001484 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001485 u32 old_rx_mode = tp->rx_mode;
1486 u32 old_tx_mode = tp->tx_mode;
1487
Joe Perches63c3a662011-04-26 08:12:10 +00001488 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001489 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001490 else
1491 autoneg = tp->link_config.autoneg;
1492
Joe Perches63c3a662011-04-26 08:12:10 +00001493 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001494 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001495 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001496 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001497 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001498 } else
1499 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001500
Matt Carlsonf51f3562008-05-25 23:45:08 -07001501 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001502
Steve Glendinninge18ce342008-12-16 02:00:00 -08001503 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001504 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1505 else
1506 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1507
Matt Carlsonf51f3562008-05-25 23:45:08 -07001508 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001509 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001510
Steve Glendinninge18ce342008-12-16 02:00:00 -08001511 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001512 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1513 else
1514 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1515
Matt Carlsonf51f3562008-05-25 23:45:08 -07001516 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001517 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001518}
1519
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001520static void tg3_adjust_link(struct net_device *dev)
1521{
1522 u8 oldflowctrl, linkmesg = 0;
1523 u32 mac_mode, lcl_adv, rmt_adv;
1524 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001525 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001526
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001527 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001528
1529 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1530 MAC_MODE_HALF_DUPLEX);
1531
1532 oldflowctrl = tp->link_config.active_flowctrl;
1533
1534 if (phydev->link) {
1535 lcl_adv = 0;
1536 rmt_adv = 0;
1537
1538 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1539 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001540 else if (phydev->speed == SPEED_1000 ||
1541 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001542 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001543 else
1544 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001545
1546 if (phydev->duplex == DUPLEX_HALF)
1547 mac_mode |= MAC_MODE_HALF_DUPLEX;
1548 else {
1549 lcl_adv = tg3_advert_flowctrl_1000T(
1550 tp->link_config.flowctrl);
1551
1552 if (phydev->pause)
1553 rmt_adv = LPA_PAUSE_CAP;
1554 if (phydev->asym_pause)
1555 rmt_adv |= LPA_PAUSE_ASYM;
1556 }
1557
1558 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1559 } else
1560 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1561
1562 if (mac_mode != tp->mac_mode) {
1563 tp->mac_mode = mac_mode;
1564 tw32_f(MAC_MODE, tp->mac_mode);
1565 udelay(40);
1566 }
1567
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001568 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1569 if (phydev->speed == SPEED_10)
1570 tw32(MAC_MI_STAT,
1571 MAC_MI_STAT_10MBPS_MODE |
1572 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1573 else
1574 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1575 }
1576
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001577 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1578 tw32(MAC_TX_LENGTHS,
1579 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1580 (6 << TX_LENGTHS_IPG_SHIFT) |
1581 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1582 else
1583 tw32(MAC_TX_LENGTHS,
1584 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1585 (6 << TX_LENGTHS_IPG_SHIFT) |
1586 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1587
1588 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1589 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1590 phydev->speed != tp->link_config.active_speed ||
1591 phydev->duplex != tp->link_config.active_duplex ||
1592 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001593 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001594
1595 tp->link_config.active_speed = phydev->speed;
1596 tp->link_config.active_duplex = phydev->duplex;
1597
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001598 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001599
1600 if (linkmesg)
1601 tg3_link_report(tp);
1602}
1603
1604static int tg3_phy_init(struct tg3 *tp)
1605{
1606 struct phy_device *phydev;
1607
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001608 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001609 return 0;
1610
1611 /* Bring the PHY back to a known state. */
1612 tg3_bmcr_reset(tp);
1613
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001614 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001615
1616 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001617 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001618 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001619 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001620 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001621 return PTR_ERR(phydev);
1622 }
1623
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001624 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001625 switch (phydev->interface) {
1626 case PHY_INTERFACE_MODE_GMII:
1627 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001628 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001629 phydev->supported &= (PHY_GBIT_FEATURES |
1630 SUPPORTED_Pause |
1631 SUPPORTED_Asym_Pause);
1632 break;
1633 }
1634 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001635 case PHY_INTERFACE_MODE_MII:
1636 phydev->supported &= (PHY_BASIC_FEATURES |
1637 SUPPORTED_Pause |
1638 SUPPORTED_Asym_Pause);
1639 break;
1640 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001641 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001642 return -EINVAL;
1643 }
1644
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001645 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001646
1647 phydev->advertising = phydev->supported;
1648
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001649 return 0;
1650}
1651
1652static void tg3_phy_start(struct tg3 *tp)
1653{
1654 struct phy_device *phydev;
1655
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001656 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001657 return;
1658
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001659 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001660
Matt Carlson80096062010-08-02 11:26:06 +00001661 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1662 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001663 phydev->speed = tp->link_config.orig_speed;
1664 phydev->duplex = tp->link_config.orig_duplex;
1665 phydev->autoneg = tp->link_config.orig_autoneg;
1666 phydev->advertising = tp->link_config.orig_advertising;
1667 }
1668
1669 phy_start(phydev);
1670
1671 phy_start_aneg(phydev);
1672}
1673
1674static void tg3_phy_stop(struct tg3 *tp)
1675{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001676 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001677 return;
1678
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001679 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001680}
1681
1682static void tg3_phy_fini(struct tg3 *tp)
1683{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001684 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001685 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001686 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001687 }
1688}
1689
Matt Carlson941ec902011-08-19 13:58:23 +00001690static int tg3_phy_set_extloopbk(struct tg3 *tp)
1691{
1692 int err;
1693 u32 val;
1694
1695 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
1696 return 0;
1697
1698 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
1699 /* Cannot do read-modify-write on 5401 */
1700 err = tg3_phy_auxctl_write(tp,
1701 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1702 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
1703 0x4c20);
1704 goto done;
1705 }
1706
1707 err = tg3_phy_auxctl_read(tp,
1708 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1709 if (err)
1710 return err;
1711
1712 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
1713 err = tg3_phy_auxctl_write(tp,
1714 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
1715
1716done:
1717 return err;
1718}
1719
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001720static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1721{
1722 u32 phytest;
1723
1724 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1725 u32 phy;
1726
1727 tg3_writephy(tp, MII_TG3_FET_TEST,
1728 phytest | MII_TG3_FET_SHADOW_EN);
1729 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1730 if (enable)
1731 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1732 else
1733 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1734 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1735 }
1736 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1737 }
1738}
1739
Matt Carlson6833c042008-11-21 17:18:59 -08001740static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1741{
1742 u32 reg;
1743
Joe Perches63c3a662011-04-26 08:12:10 +00001744 if (!tg3_flag(tp, 5705_PLUS) ||
1745 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001746 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001747 return;
1748
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001749 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001750 tg3_phy_fet_toggle_apd(tp, enable);
1751 return;
1752 }
1753
Matt Carlson6833c042008-11-21 17:18:59 -08001754 reg = MII_TG3_MISC_SHDW_WREN |
1755 MII_TG3_MISC_SHDW_SCR5_SEL |
1756 MII_TG3_MISC_SHDW_SCR5_LPED |
1757 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1758 MII_TG3_MISC_SHDW_SCR5_SDTL |
1759 MII_TG3_MISC_SHDW_SCR5_C125OE;
1760 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1761 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1762
1763 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1764
1765
1766 reg = MII_TG3_MISC_SHDW_WREN |
1767 MII_TG3_MISC_SHDW_APD_SEL |
1768 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1769 if (enable)
1770 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1771
1772 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1773}
1774
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001775static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1776{
1777 u32 phy;
1778
Joe Perches63c3a662011-04-26 08:12:10 +00001779 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001780 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001781 return;
1782
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001783 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001784 u32 ephy;
1785
Matt Carlson535ef6e2009-08-25 10:09:36 +00001786 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1787 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1788
1789 tg3_writephy(tp, MII_TG3_FET_TEST,
1790 ephy | MII_TG3_FET_SHADOW_EN);
1791 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001792 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001793 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001794 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001795 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1796 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001797 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001798 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001799 }
1800 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00001801 int ret;
1802
1803 ret = tg3_phy_auxctl_read(tp,
1804 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
1805 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001806 if (enable)
1807 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1808 else
1809 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001810 tg3_phy_auxctl_write(tp,
1811 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001812 }
1813 }
1814}
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816static void tg3_phy_set_wirespeed(struct tg3 *tp)
1817{
Matt Carlson15ee95c2011-04-20 07:57:40 +00001818 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 u32 val;
1820
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001821 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return;
1823
Matt Carlson15ee95c2011-04-20 07:57:40 +00001824 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
1825 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001826 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
1827 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001830static void tg3_phy_apply_otp(struct tg3 *tp)
1831{
1832 u32 otp, phy;
1833
1834 if (!tp->phy_otp)
1835 return;
1836
1837 otp = tp->phy_otp;
1838
Matt Carlson1d36ba42011-04-20 07:57:42 +00001839 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
1840 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001841
1842 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1843 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1844 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1845
1846 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1847 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1848 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1849
1850 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1851 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1852 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1853
1854 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1855 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1856
1857 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1858 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1859
1860 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1861 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1862 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1863
Matt Carlson1d36ba42011-04-20 07:57:42 +00001864 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001865}
1866
Matt Carlson52b02d02010-10-14 10:37:41 +00001867static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1868{
1869 u32 val;
1870
1871 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1872 return;
1873
1874 tp->setlpicnt = 0;
1875
1876 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1877 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001878 tp->link_config.active_duplex == DUPLEX_FULL &&
1879 (tp->link_config.active_speed == SPEED_100 ||
1880 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001881 u32 eeectl;
1882
1883 if (tp->link_config.active_speed == SPEED_1000)
1884 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1885 else
1886 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1887
1888 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1889
Matt Carlson3110f5f52010-12-06 08:28:50 +00001890 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1891 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001892
Matt Carlsonb0c59432011-05-19 12:12:48 +00001893 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
1894 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00001895 tp->setlpicnt = 2;
1896 }
1897
1898 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00001899 if (current_link_up == 1 &&
1900 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
1901 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
1902 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1903 }
1904
Matt Carlson52b02d02010-10-14 10:37:41 +00001905 val = tr32(TG3_CPMU_EEE_MODE);
1906 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1907 }
1908}
1909
Matt Carlsonb0c59432011-05-19 12:12:48 +00001910static void tg3_phy_eee_enable(struct tg3 *tp)
1911{
1912 u32 val;
1913
1914 if (tp->link_config.active_speed == SPEED_1000 &&
1915 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1916 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
1917 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
1918 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00001919 val = MII_TG3_DSP_TAP26_ALNOKO |
1920 MII_TG3_DSP_TAP26_RMRXSTO;
1921 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00001922 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1923 }
1924
1925 val = tr32(TG3_CPMU_EEE_MODE);
1926 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
1927}
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929static int tg3_wait_macro_done(struct tg3 *tp)
1930{
1931 int limit = 100;
1932
1933 while (limit--) {
1934 u32 tmp32;
1935
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001936 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 if ((tmp32 & 0x1000) == 0)
1938 break;
1939 }
1940 }
Roel Kluind4675b52009-02-12 16:33:27 -08001941 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return -EBUSY;
1943
1944 return 0;
1945}
1946
1947static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1948{
1949 static const u32 test_pat[4][6] = {
1950 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1951 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1952 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1953 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1954 };
1955 int chan;
1956
1957 for (chan = 0; chan < 4; chan++) {
1958 int i;
1959
1960 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1961 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001962 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 for (i = 0; i < 6; i++)
1965 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1966 test_pat[chan][i]);
1967
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001968 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (tg3_wait_macro_done(tp)) {
1970 *resetp = 1;
1971 return -EBUSY;
1972 }
1973
1974 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1975 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001976 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (tg3_wait_macro_done(tp)) {
1978 *resetp = 1;
1979 return -EBUSY;
1980 }
1981
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001982 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (tg3_wait_macro_done(tp)) {
1984 *resetp = 1;
1985 return -EBUSY;
1986 }
1987
1988 for (i = 0; i < 6; i += 2) {
1989 u32 low, high;
1990
1991 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1992 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1993 tg3_wait_macro_done(tp)) {
1994 *resetp = 1;
1995 return -EBUSY;
1996 }
1997 low &= 0x7fff;
1998 high &= 0x000f;
1999 if (low != test_pat[chan][i] ||
2000 high != test_pat[chan][i+1]) {
2001 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2002 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2003 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2004
2005 return -EBUSY;
2006 }
2007 }
2008 }
2009
2010 return 0;
2011}
2012
2013static int tg3_phy_reset_chanpat(struct tg3 *tp)
2014{
2015 int chan;
2016
2017 for (chan = 0; chan < 4; chan++) {
2018 int i;
2019
2020 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2021 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002022 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 for (i = 0; i < 6; i++)
2024 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002025 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (tg3_wait_macro_done(tp))
2027 return -EBUSY;
2028 }
2029
2030 return 0;
2031}
2032
2033static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2034{
2035 u32 reg32, phy9_orig;
2036 int retries, do_phy_reset, err;
2037
2038 retries = 10;
2039 do_phy_reset = 1;
2040 do {
2041 if (do_phy_reset) {
2042 err = tg3_bmcr_reset(tp);
2043 if (err)
2044 return err;
2045 do_phy_reset = 0;
2046 }
2047
2048 /* Disable transmitter and interrupt. */
2049 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2050 continue;
2051
2052 reg32 |= 0x3000;
2053 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2054
2055 /* Set full-duplex, 1000 mbps. */
2056 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002057 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002060 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 continue;
2062
Matt Carlson221c5632011-06-13 13:39:01 +00002063 tg3_writephy(tp, MII_CTRL1000,
2064 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Matt Carlson1d36ba42011-04-20 07:57:42 +00002066 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2067 if (err)
2068 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002071 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2074 if (!err)
2075 break;
2076 } while (--retries);
2077
2078 err = tg3_phy_reset_chanpat(tp);
2079 if (err)
2080 return err;
2081
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002082 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002085 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Matt Carlson1d36ba42011-04-20 07:57:42 +00002087 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Matt Carlson221c5632011-06-13 13:39:01 +00002089 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2092 reg32 &= ~0x3000;
2093 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2094 } else if (!err)
2095 err = -EBUSY;
2096
2097 return err;
2098}
2099
2100/* This will reset the tigon3 PHY if there is no valid
2101 * link unless the FORCE argument is non-zero.
2102 */
2103static int tg3_phy_reset(struct tg3 *tp)
2104{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002105 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 int err;
2107
Michael Chan60189dd2006-12-17 17:08:07 -08002108 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002109 val = tr32(GRC_MISC_CFG);
2110 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2111 udelay(40);
2112 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002113 err = tg3_readphy(tp, MII_BMSR, &val);
2114 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (err != 0)
2116 return -EBUSY;
2117
Michael Chanc8e1e822006-04-29 18:55:17 -07002118 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2119 netif_carrier_off(tp->dev);
2120 tg3_link_report(tp);
2121 }
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2124 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2125 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2126 err = tg3_phy_reset_5703_4_5(tp);
2127 if (err)
2128 return err;
2129 goto out;
2130 }
2131
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002132 cpmuctrl = 0;
2133 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2134 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2135 cpmuctrl = tr32(TG3_CPMU_CTRL);
2136 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2137 tw32(TG3_CPMU_CTRL,
2138 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2139 }
2140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 err = tg3_bmcr_reset(tp);
2142 if (err)
2143 return err;
2144
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002145 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002146 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2147 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002148
2149 tw32(TG3_CPMU_CTRL, cpmuctrl);
2150 }
2151
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002152 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2153 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002154 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2155 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2156 CPMU_LSPD_1000MB_MACCLK_12_5) {
2157 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2158 udelay(40);
2159 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2160 }
2161 }
2162
Joe Perches63c3a662011-04-26 08:12:10 +00002163 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002164 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002165 return 0;
2166
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002167 tg3_phy_apply_otp(tp);
2168
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002169 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002170 tg3_phy_toggle_apd(tp, true);
2171 else
2172 tg3_phy_toggle_apd(tp, false);
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002175 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2176 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002177 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2178 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002179 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002181
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002182 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002183 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2184 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002186
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002187 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002188 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2189 tg3_phydsp_write(tp, 0x000a, 0x310b);
2190 tg3_phydsp_write(tp, 0x201f, 0x9506);
2191 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2192 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2193 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002194 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002195 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2196 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2197 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2198 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2199 tg3_writephy(tp, MII_TG3_TEST1,
2200 MII_TG3_TEST1_TRIM_EN | 0x4);
2201 } else
2202 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2203
2204 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2205 }
Michael Chanc424cb22006-04-29 18:56:34 -07002206 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 /* Set Extended packet length bit (bit 14) on all chips that */
2209 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002210 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002212 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002213 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002215 err = tg3_phy_auxctl_read(tp,
2216 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2217 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002218 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2219 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
2221
2222 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2223 * jumbo frames transmission.
2224 */
Joe Perches63c3a662011-04-26 08:12:10 +00002225 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002226 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002227 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002228 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
2230
Michael Chan715116a2006-09-27 16:09:25 -07002231 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002232 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002233 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002234 }
2235
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002236 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 tg3_phy_set_wirespeed(tp);
2238 return 0;
2239}
2240
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002241#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2242#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2243#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2244 TG3_GPIO_MSG_NEED_VAUX)
2245#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2246 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2247 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2248 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2249 (TG3_GPIO_MSG_DRVR_PRES << 12))
2250
2251#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2252 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2253 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2254 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2255 (TG3_GPIO_MSG_NEED_VAUX << 12))
2256
2257static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2258{
2259 u32 status, shift;
2260
2261 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2262 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2263 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2264 else
2265 status = tr32(TG3_CPMU_DRV_STATUS);
2266
2267 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2268 status &= ~(TG3_GPIO_MSG_MASK << shift);
2269 status |= (newstat << shift);
2270
2271 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2272 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2273 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2274 else
2275 tw32(TG3_CPMU_DRV_STATUS, status);
2276
2277 return status >> TG3_APE_GPIO_MSG_SHIFT;
2278}
2279
Matt Carlson520b2752011-06-13 13:39:02 +00002280static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2281{
2282 if (!tg3_flag(tp, IS_NIC))
2283 return 0;
2284
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002285 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2286 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2287 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2288 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2289 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002290
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002291 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2292
2293 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2294 TG3_GRC_LCLCTL_PWRSW_DELAY);
2295
2296 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2297 } else {
2298 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2299 TG3_GRC_LCLCTL_PWRSW_DELAY);
2300 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002301
Matt Carlson520b2752011-06-13 13:39:02 +00002302 return 0;
2303}
2304
2305static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2306{
2307 u32 grc_local_ctrl;
2308
2309 if (!tg3_flag(tp, IS_NIC) ||
2310 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2311 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2312 return;
2313
2314 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2315
2316 tw32_wait_f(GRC_LOCAL_CTRL,
2317 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2318 TG3_GRC_LCLCTL_PWRSW_DELAY);
2319
2320 tw32_wait_f(GRC_LOCAL_CTRL,
2321 grc_local_ctrl,
2322 TG3_GRC_LCLCTL_PWRSW_DELAY);
2323
2324 tw32_wait_f(GRC_LOCAL_CTRL,
2325 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2326 TG3_GRC_LCLCTL_PWRSW_DELAY);
2327}
2328
2329static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2330{
2331 if (!tg3_flag(tp, IS_NIC))
2332 return;
2333
2334 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2335 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2336 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2337 (GRC_LCLCTRL_GPIO_OE0 |
2338 GRC_LCLCTRL_GPIO_OE1 |
2339 GRC_LCLCTRL_GPIO_OE2 |
2340 GRC_LCLCTRL_GPIO_OUTPUT0 |
2341 GRC_LCLCTRL_GPIO_OUTPUT1),
2342 TG3_GRC_LCLCTL_PWRSW_DELAY);
2343 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2344 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2345 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2346 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2347 GRC_LCLCTRL_GPIO_OE1 |
2348 GRC_LCLCTRL_GPIO_OE2 |
2349 GRC_LCLCTRL_GPIO_OUTPUT0 |
2350 GRC_LCLCTRL_GPIO_OUTPUT1 |
2351 tp->grc_local_ctrl;
2352 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2353 TG3_GRC_LCLCTL_PWRSW_DELAY);
2354
2355 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2356 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2357 TG3_GRC_LCLCTL_PWRSW_DELAY);
2358
2359 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2360 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2361 TG3_GRC_LCLCTL_PWRSW_DELAY);
2362 } else {
2363 u32 no_gpio2;
2364 u32 grc_local_ctrl = 0;
2365
2366 /* Workaround to prevent overdrawing Amps. */
2367 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2368 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2369 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2370 grc_local_ctrl,
2371 TG3_GRC_LCLCTL_PWRSW_DELAY);
2372 }
2373
2374 /* On 5753 and variants, GPIO2 cannot be used. */
2375 no_gpio2 = tp->nic_sram_data_cfg &
2376 NIC_SRAM_DATA_CFG_NO_GPIO2;
2377
2378 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2379 GRC_LCLCTRL_GPIO_OE1 |
2380 GRC_LCLCTRL_GPIO_OE2 |
2381 GRC_LCLCTRL_GPIO_OUTPUT1 |
2382 GRC_LCLCTRL_GPIO_OUTPUT2;
2383 if (no_gpio2) {
2384 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2385 GRC_LCLCTRL_GPIO_OUTPUT2);
2386 }
2387 tw32_wait_f(GRC_LOCAL_CTRL,
2388 tp->grc_local_ctrl | grc_local_ctrl,
2389 TG3_GRC_LCLCTL_PWRSW_DELAY);
2390
2391 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2392
2393 tw32_wait_f(GRC_LOCAL_CTRL,
2394 tp->grc_local_ctrl | grc_local_ctrl,
2395 TG3_GRC_LCLCTL_PWRSW_DELAY);
2396
2397 if (!no_gpio2) {
2398 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2399 tw32_wait_f(GRC_LOCAL_CTRL,
2400 tp->grc_local_ctrl | grc_local_ctrl,
2401 TG3_GRC_LCLCTL_PWRSW_DELAY);
2402 }
2403 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002404}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002405
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002406static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002407{
2408 u32 msg = 0;
2409
2410 /* Serialize power state transitions */
2411 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2412 return;
2413
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002414 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002415 msg = TG3_GPIO_MSG_NEED_VAUX;
2416
2417 msg = tg3_set_function_status(tp, msg);
2418
2419 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2420 goto done;
2421
2422 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2423 tg3_pwrsrc_switch_to_vaux(tp);
2424 else
2425 tg3_pwrsrc_die_with_vmain(tp);
2426
2427done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002428 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002429}
2430
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002431static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
Matt Carlson683644b2011-03-09 16:58:23 +00002433 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Matt Carlson334355a2010-01-20 16:58:10 +00002435 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002436 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlson334355a2010-01-20 16:58:10 +00002437 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return;
2439
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002440 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2441 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2442 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002443 tg3_frob_aux_power_5717(tp, include_wol ?
2444 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002445 return;
2446 }
2447
2448 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002449 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002451 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002452
Michael Chanbc1c7562006-03-20 17:48:03 -08002453 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002454 if (dev_peer) {
2455 struct tg3 *tp_peer = netdev_priv(dev_peer);
2456
Joe Perches63c3a662011-04-26 08:12:10 +00002457 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002458 return;
2459
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002460 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002461 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002462 need_vaux = true;
2463 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002466 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2467 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002468 need_vaux = true;
2469
Matt Carlson520b2752011-06-13 13:39:02 +00002470 if (need_vaux)
2471 tg3_pwrsrc_switch_to_vaux(tp);
2472 else
2473 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002476static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2477{
2478 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2479 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002480 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002481 if (speed != SPEED_10)
2482 return 1;
2483 } else if (speed == SPEED_10)
2484 return 1;
2485
2486 return 0;
2487}
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489static int tg3_setup_phy(struct tg3 *, int);
2490
2491#define RESET_KIND_SHUTDOWN 0
2492#define RESET_KIND_INIT 1
2493#define RESET_KIND_SUSPEND 2
2494
2495static void tg3_write_sig_post_reset(struct tg3 *, int);
2496static int tg3_halt_cpu(struct tg3 *, u32);
2497
Matt Carlson0a459aa2008-11-03 16:54:15 -08002498static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002499{
Matt Carlsonce057f02007-11-12 21:08:03 -08002500 u32 val;
2501
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002502 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002503 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2504 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2505 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2506
2507 sg_dig_ctrl |=
2508 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2509 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2510 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2511 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002512 return;
Michael Chan51297242007-02-13 12:17:57 -08002513 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002514
Michael Chan60189dd2006-12-17 17:08:07 -08002515 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002516 tg3_bmcr_reset(tp);
2517 val = tr32(GRC_MISC_CFG);
2518 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2519 udelay(40);
2520 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002521 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002522 u32 phytest;
2523 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2524 u32 phy;
2525
2526 tg3_writephy(tp, MII_ADVERTISE, 0);
2527 tg3_writephy(tp, MII_BMCR,
2528 BMCR_ANENABLE | BMCR_ANRESTART);
2529
2530 tg3_writephy(tp, MII_TG3_FET_TEST,
2531 phytest | MII_TG3_FET_SHADOW_EN);
2532 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2533 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2534 tg3_writephy(tp,
2535 MII_TG3_FET_SHDW_AUXMODE4,
2536 phy);
2537 }
2538 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2539 }
2540 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002541 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002542 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2543 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002544
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002545 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2546 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2547 MII_TG3_AUXCTL_PCTL_VREG_11V;
2548 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002549 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002550
Michael Chan15c3b692006-03-22 01:06:52 -08002551 /* The PHY should not be powered down on some chips because
2552 * of bugs.
2553 */
2554 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2555 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2556 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002557 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002558 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002559
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002560 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2561 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002562 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2563 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2564 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2565 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2566 }
2567
Michael Chan15c3b692006-03-22 01:06:52 -08002568 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2569}
2570
Matt Carlson3f007892008-11-03 16:51:36 -08002571/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002572static int tg3_nvram_lock(struct tg3 *tp)
2573{
Joe Perches63c3a662011-04-26 08:12:10 +00002574 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002575 int i;
2576
2577 if (tp->nvram_lock_cnt == 0) {
2578 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2579 for (i = 0; i < 8000; i++) {
2580 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2581 break;
2582 udelay(20);
2583 }
2584 if (i == 8000) {
2585 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2586 return -ENODEV;
2587 }
2588 }
2589 tp->nvram_lock_cnt++;
2590 }
2591 return 0;
2592}
2593
2594/* tp->lock is held. */
2595static void tg3_nvram_unlock(struct tg3 *tp)
2596{
Joe Perches63c3a662011-04-26 08:12:10 +00002597 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002598 if (tp->nvram_lock_cnt > 0)
2599 tp->nvram_lock_cnt--;
2600 if (tp->nvram_lock_cnt == 0)
2601 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2602 }
2603}
2604
2605/* tp->lock is held. */
2606static void tg3_enable_nvram_access(struct tg3 *tp)
2607{
Joe Perches63c3a662011-04-26 08:12:10 +00002608 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002609 u32 nvaccess = tr32(NVRAM_ACCESS);
2610
2611 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2612 }
2613}
2614
2615/* tp->lock is held. */
2616static void tg3_disable_nvram_access(struct tg3 *tp)
2617{
Joe Perches63c3a662011-04-26 08:12:10 +00002618 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002619 u32 nvaccess = tr32(NVRAM_ACCESS);
2620
2621 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2622 }
2623}
2624
2625static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2626 u32 offset, u32 *val)
2627{
2628 u32 tmp;
2629 int i;
2630
2631 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2632 return -EINVAL;
2633
2634 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2635 EEPROM_ADDR_DEVID_MASK |
2636 EEPROM_ADDR_READ);
2637 tw32(GRC_EEPROM_ADDR,
2638 tmp |
2639 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2640 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2641 EEPROM_ADDR_ADDR_MASK) |
2642 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2643
2644 for (i = 0; i < 1000; i++) {
2645 tmp = tr32(GRC_EEPROM_ADDR);
2646
2647 if (tmp & EEPROM_ADDR_COMPLETE)
2648 break;
2649 msleep(1);
2650 }
2651 if (!(tmp & EEPROM_ADDR_COMPLETE))
2652 return -EBUSY;
2653
Matt Carlson62cedd12009-04-20 14:52:29 -07002654 tmp = tr32(GRC_EEPROM_DATA);
2655
2656 /*
2657 * The data will always be opposite the native endian
2658 * format. Perform a blind byteswap to compensate.
2659 */
2660 *val = swab32(tmp);
2661
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002662 return 0;
2663}
2664
2665#define NVRAM_CMD_TIMEOUT 10000
2666
2667static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2668{
2669 int i;
2670
2671 tw32(NVRAM_CMD, nvram_cmd);
2672 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2673 udelay(10);
2674 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2675 udelay(10);
2676 break;
2677 }
2678 }
2679
2680 if (i == NVRAM_CMD_TIMEOUT)
2681 return -EBUSY;
2682
2683 return 0;
2684}
2685
2686static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2687{
Joe Perches63c3a662011-04-26 08:12:10 +00002688 if (tg3_flag(tp, NVRAM) &&
2689 tg3_flag(tp, NVRAM_BUFFERED) &&
2690 tg3_flag(tp, FLASH) &&
2691 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002692 (tp->nvram_jedecnum == JEDEC_ATMEL))
2693
2694 addr = ((addr / tp->nvram_pagesize) <<
2695 ATMEL_AT45DB0X1B_PAGE_POS) +
2696 (addr % tp->nvram_pagesize);
2697
2698 return addr;
2699}
2700
2701static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2702{
Joe Perches63c3a662011-04-26 08:12:10 +00002703 if (tg3_flag(tp, NVRAM) &&
2704 tg3_flag(tp, NVRAM_BUFFERED) &&
2705 tg3_flag(tp, FLASH) &&
2706 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002707 (tp->nvram_jedecnum == JEDEC_ATMEL))
2708
2709 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2710 tp->nvram_pagesize) +
2711 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2712
2713 return addr;
2714}
2715
Matt Carlsone4f34112009-02-25 14:25:00 +00002716/* NOTE: Data read in from NVRAM is byteswapped according to
2717 * the byteswapping settings for all other register accesses.
2718 * tg3 devices are BE devices, so on a BE machine, the data
2719 * returned will be exactly as it is seen in NVRAM. On a LE
2720 * machine, the 32-bit value will be byteswapped.
2721 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002722static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2723{
2724 int ret;
2725
Joe Perches63c3a662011-04-26 08:12:10 +00002726 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002727 return tg3_nvram_read_using_eeprom(tp, offset, val);
2728
2729 offset = tg3_nvram_phys_addr(tp, offset);
2730
2731 if (offset > NVRAM_ADDR_MSK)
2732 return -EINVAL;
2733
2734 ret = tg3_nvram_lock(tp);
2735 if (ret)
2736 return ret;
2737
2738 tg3_enable_nvram_access(tp);
2739
2740 tw32(NVRAM_ADDR, offset);
2741 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2742 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2743
2744 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002745 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002746
2747 tg3_disable_nvram_access(tp);
2748
2749 tg3_nvram_unlock(tp);
2750
2751 return ret;
2752}
2753
Matt Carlsona9dc5292009-02-25 14:25:30 +00002754/* Ensures NVRAM data is in bytestream format. */
2755static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002756{
2757 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002758 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002759 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002760 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002761 return res;
2762}
2763
2764/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002765static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2766{
2767 u32 addr_high, addr_low;
2768 int i;
2769
2770 addr_high = ((tp->dev->dev_addr[0] << 8) |
2771 tp->dev->dev_addr[1]);
2772 addr_low = ((tp->dev->dev_addr[2] << 24) |
2773 (tp->dev->dev_addr[3] << 16) |
2774 (tp->dev->dev_addr[4] << 8) |
2775 (tp->dev->dev_addr[5] << 0));
2776 for (i = 0; i < 4; i++) {
2777 if (i == 1 && skip_mac_1)
2778 continue;
2779 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2780 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2781 }
2782
2783 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2784 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2785 for (i = 0; i < 12; i++) {
2786 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2787 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2788 }
2789 }
2790
2791 addr_high = (tp->dev->dev_addr[0] +
2792 tp->dev->dev_addr[1] +
2793 tp->dev->dev_addr[2] +
2794 tp->dev->dev_addr[3] +
2795 tp->dev->dev_addr[4] +
2796 tp->dev->dev_addr[5]) &
2797 TX_BACKOFF_SEED_MASK;
2798 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2799}
2800
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002801static void tg3_enable_register_access(struct tg3 *tp)
2802{
2803 /*
2804 * Make sure register accesses (indirect or otherwise) will function
2805 * correctly.
2806 */
2807 pci_write_config_dword(tp->pdev,
2808 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2809}
2810
2811static int tg3_power_up(struct tg3 *tp)
2812{
Matt Carlsonbed98292011-07-13 09:27:29 +00002813 int err;
2814
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002815 tg3_enable_register_access(tp);
2816
Matt Carlsonbed98292011-07-13 09:27:29 +00002817 err = pci_set_power_state(tp->pdev, PCI_D0);
2818 if (!err) {
2819 /* Switch out of Vaux if it is a NIC */
2820 tg3_pwrsrc_switch_to_vmain(tp);
2821 } else {
2822 netdev_err(tp->dev, "Transition to D0 failed\n");
2823 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002824
Matt Carlsonbed98292011-07-13 09:27:29 +00002825 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002826}
2827
2828static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
2830 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002831 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002833 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002834
2835 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00002836 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002837 u16 lnkctl;
2838
2839 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00002840 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002841 &lnkctl);
2842 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2843 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00002844 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002845 lnkctl);
2846 }
2847
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2849 tw32(TG3PCI_MISC_HOST_CTRL,
2850 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2851
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002852 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00002853 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002854
Joe Perches63c3a662011-04-26 08:12:10 +00002855 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002856 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002857 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002858 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002859 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002860 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002861
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002862 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002863
Matt Carlson80096062010-08-02 11:26:06 +00002864 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002865
2866 tp->link_config.orig_speed = phydev->speed;
2867 tp->link_config.orig_duplex = phydev->duplex;
2868 tp->link_config.orig_autoneg = phydev->autoneg;
2869 tp->link_config.orig_advertising = phydev->advertising;
2870
2871 advertising = ADVERTISED_TP |
2872 ADVERTISED_Pause |
2873 ADVERTISED_Autoneg |
2874 ADVERTISED_10baseT_Half;
2875
Joe Perches63c3a662011-04-26 08:12:10 +00002876 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
2877 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002878 advertising |=
2879 ADVERTISED_100baseT_Half |
2880 ADVERTISED_100baseT_Full |
2881 ADVERTISED_10baseT_Full;
2882 else
2883 advertising |= ADVERTISED_10baseT_Full;
2884 }
2885
2886 phydev->advertising = advertising;
2887
2888 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002889
2890 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002891 if (phyid != PHY_ID_BCMAC131) {
2892 phyid &= PHY_BCM_OUI_MASK;
2893 if (phyid == PHY_BCM_OUI_1 ||
2894 phyid == PHY_BCM_OUI_2 ||
2895 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002896 do_low_power = true;
2897 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002898 }
Matt Carlsondd477002008-05-25 23:45:58 -07002899 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002900 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002901
Matt Carlson80096062010-08-02 11:26:06 +00002902 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2903 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002904 tp->link_config.orig_speed = tp->link_config.speed;
2905 tp->link_config.orig_duplex = tp->link_config.duplex;
2906 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002909 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002910 tp->link_config.speed = SPEED_10;
2911 tp->link_config.duplex = DUPLEX_HALF;
2912 tp->link_config.autoneg = AUTONEG_ENABLE;
2913 tg3_setup_phy(tp, 0);
2914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 }
2916
Michael Chanb5d37722006-09-27 16:06:21 -07002917 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2918 u32 val;
2919
2920 val = tr32(GRC_VCPU_EXT_CTRL);
2921 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00002922 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002923 int i;
2924 u32 val;
2925
2926 for (i = 0; i < 200; i++) {
2927 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2928 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2929 break;
2930 msleep(1);
2931 }
2932 }
Joe Perches63c3a662011-04-26 08:12:10 +00002933 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07002934 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2935 WOL_DRV_STATE_SHUTDOWN |
2936 WOL_DRV_WOL |
2937 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002938
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002939 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 u32 mac_mode;
2941
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002942 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002943 if (do_low_power &&
2944 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
2945 tg3_phy_auxctl_write(tp,
2946 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
2947 MII_TG3_AUXCTL_PCTL_WOL_EN |
2948 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2949 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07002950 udelay(40);
2951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002953 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002954 mac_mode = MAC_MODE_PORT_MODE_GMII;
2955 else
2956 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002958 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2959 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2960 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00002961 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002962 SPEED_100 : SPEED_10;
2963 if (tg3_5700_link_polarity(tp, speed))
2964 mac_mode |= MAC_MODE_LINK_POLARITY;
2965 else
2966 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 } else {
2969 mac_mode = MAC_MODE_PORT_MODE_TBI;
2970 }
2971
Joe Perches63c3a662011-04-26 08:12:10 +00002972 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 tw32(MAC_LED_CTRL, tp->led_ctrl);
2974
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002975 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00002976 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
2977 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002978 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Joe Perches63c3a662011-04-26 08:12:10 +00002980 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002981 mac_mode |= MAC_MODE_APE_TX_EN |
2982 MAC_MODE_APE_RX_EN |
2983 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 tw32_f(MAC_MODE, mac_mode);
2986 udelay(100);
2987
2988 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2989 udelay(10);
2990 }
2991
Joe Perches63c3a662011-04-26 08:12:10 +00002992 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2994 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2995 u32 base_val;
2996
2997 base_val = tp->pci_clock_ctrl;
2998 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2999 CLOCK_CTRL_TXCLK_DISABLE);
3000
Michael Chanb401e9e2005-12-19 16:27:04 -08003001 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3002 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003003 } else if (tg3_flag(tp, 5780_CLASS) ||
3004 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003005 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003006 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003007 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 u32 newbits1, newbits2;
3009
3010 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3011 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3012 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3013 CLOCK_CTRL_TXCLK_DISABLE |
3014 CLOCK_CTRL_ALTCLK);
3015 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003016 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 newbits1 = CLOCK_CTRL_625_CORE;
3018 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3019 } else {
3020 newbits1 = CLOCK_CTRL_ALTCLK;
3021 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3022 }
3023
Michael Chanb401e9e2005-12-19 16:27:04 -08003024 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3025 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Michael Chanb401e9e2005-12-19 16:27:04 -08003027 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3028 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Joe Perches63c3a662011-04-26 08:12:10 +00003030 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 u32 newbits3;
3032
3033 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3034 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3035 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3036 CLOCK_CTRL_TXCLK_DISABLE |
3037 CLOCK_CTRL_44MHZ_CORE);
3038 } else {
3039 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3040 }
3041
Michael Chanb401e9e2005-12-19 16:27:04 -08003042 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3043 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 }
3045 }
3046
Joe Perches63c3a662011-04-26 08:12:10 +00003047 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003048 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003049
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003050 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
3052 /* Workaround for unstable PLL clock */
3053 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3054 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3055 u32 val = tr32(0x7d00);
3056
3057 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3058 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003059 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003060 int err;
3061
3062 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003064 if (!err)
3065 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 }
3068
Michael Chanbbadf502006-04-06 21:46:34 -07003069 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 return 0;
3072}
3073
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003074static void tg3_power_down(struct tg3 *tp)
3075{
3076 tg3_power_down_prepare(tp);
3077
Joe Perches63c3a662011-04-26 08:12:10 +00003078 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003079 pci_set_power_state(tp->pdev, PCI_D3hot);
3080}
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3083{
3084 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3085 case MII_TG3_AUX_STAT_10HALF:
3086 *speed = SPEED_10;
3087 *duplex = DUPLEX_HALF;
3088 break;
3089
3090 case MII_TG3_AUX_STAT_10FULL:
3091 *speed = SPEED_10;
3092 *duplex = DUPLEX_FULL;
3093 break;
3094
3095 case MII_TG3_AUX_STAT_100HALF:
3096 *speed = SPEED_100;
3097 *duplex = DUPLEX_HALF;
3098 break;
3099
3100 case MII_TG3_AUX_STAT_100FULL:
3101 *speed = SPEED_100;
3102 *duplex = DUPLEX_FULL;
3103 break;
3104
3105 case MII_TG3_AUX_STAT_1000HALF:
3106 *speed = SPEED_1000;
3107 *duplex = DUPLEX_HALF;
3108 break;
3109
3110 case MII_TG3_AUX_STAT_1000FULL:
3111 *speed = SPEED_1000;
3112 *duplex = DUPLEX_FULL;
3113 break;
3114
3115 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003116 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003117 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3118 SPEED_10;
3119 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3120 DUPLEX_HALF;
3121 break;
3122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 *speed = SPEED_INVALID;
3124 *duplex = DUPLEX_INVALID;
3125 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127}
3128
Matt Carlson42b64a42011-05-19 12:12:49 +00003129static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
Matt Carlson42b64a42011-05-19 12:12:49 +00003131 int err = 0;
3132 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Matt Carlson42b64a42011-05-19 12:12:49 +00003134 new_adv = ADVERTISE_CSMA;
3135 if (advertise & ADVERTISED_10baseT_Half)
3136 new_adv |= ADVERTISE_10HALF;
3137 if (advertise & ADVERTISED_10baseT_Full)
3138 new_adv |= ADVERTISE_10FULL;
3139 if (advertise & ADVERTISED_100baseT_Half)
3140 new_adv |= ADVERTISE_100HALF;
3141 if (advertise & ADVERTISED_100baseT_Full)
3142 new_adv |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
Matt Carlson42b64a42011-05-19 12:12:49 +00003144 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Matt Carlson42b64a42011-05-19 12:12:49 +00003146 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3147 if (err)
3148 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Matt Carlson42b64a42011-05-19 12:12:49 +00003150 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3151 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003152
Matt Carlson42b64a42011-05-19 12:12:49 +00003153 new_adv = 0;
3154 if (advertise & ADVERTISED_1000baseT_Half)
Matt Carlson221c5632011-06-13 13:39:01 +00003155 new_adv |= ADVERTISE_1000HALF;
Matt Carlson42b64a42011-05-19 12:12:49 +00003156 if (advertise & ADVERTISED_1000baseT_Full)
Matt Carlson221c5632011-06-13 13:39:01 +00003157 new_adv |= ADVERTISE_1000FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003158
Matt Carlson42b64a42011-05-19 12:12:49 +00003159 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3160 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
Matt Carlson221c5632011-06-13 13:39:01 +00003161 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Matt Carlson221c5632011-06-13 13:39:01 +00003163 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
Matt Carlson42b64a42011-05-19 12:12:49 +00003164 if (err)
3165 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003166
Matt Carlson42b64a42011-05-19 12:12:49 +00003167 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3168 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Matt Carlson42b64a42011-05-19 12:12:49 +00003170 tw32(TG3_CPMU_EEE_MODE,
3171 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003172
Matt Carlson42b64a42011-05-19 12:12:49 +00003173 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3174 if (!err) {
3175 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003176
Matt Carlsona6b68da2010-12-06 08:28:52 +00003177 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003178 /* Advertise 100-BaseTX EEE ability */
3179 if (advertise & ADVERTISED_100baseT_Full)
3180 val |= MDIO_AN_EEE_ADV_100TX;
3181 /* Advertise 1000-BaseT EEE ability */
3182 if (advertise & ADVERTISED_1000baseT_Full)
3183 val |= MDIO_AN_EEE_ADV_1000T;
3184 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003185 if (err)
3186 val = 0;
3187
3188 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3189 case ASIC_REV_5717:
3190 case ASIC_REV_57765:
3191 case ASIC_REV_5719:
3192 /* If we advertised any eee advertisements above... */
3193 if (val)
3194 val = MII_TG3_DSP_TAP26_ALNOKO |
3195 MII_TG3_DSP_TAP26_RMRXSTO |
3196 MII_TG3_DSP_TAP26_OPCSINPT;
3197 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3198 /* Fall through */
3199 case ASIC_REV_5720:
3200 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3201 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3202 MII_TG3_DSP_CH34TP2_HIBW01);
3203 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003204
Matt Carlson42b64a42011-05-19 12:12:49 +00003205 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3206 if (!err)
3207 err = err2;
3208 }
3209
3210done:
3211 return err;
3212}
3213
3214static void tg3_phy_copper_begin(struct tg3 *tp)
3215{
3216 u32 new_adv;
3217 int i;
3218
3219 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3220 new_adv = ADVERTISED_10baseT_Half |
3221 ADVERTISED_10baseT_Full;
3222 if (tg3_flag(tp, WOL_SPEED_100MB))
3223 new_adv |= ADVERTISED_100baseT_Half |
3224 ADVERTISED_100baseT_Full;
3225
3226 tg3_phy_autoneg_cfg(tp, new_adv,
3227 FLOW_CTRL_TX | FLOW_CTRL_RX);
3228 } else if (tp->link_config.speed == SPEED_INVALID) {
3229 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3230 tp->link_config.advertising &=
3231 ~(ADVERTISED_1000baseT_Half |
3232 ADVERTISED_1000baseT_Full);
3233
3234 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3235 tp->link_config.flowctrl);
3236 } else {
3237 /* Asking for a specific link mode. */
3238 if (tp->link_config.speed == SPEED_1000) {
3239 if (tp->link_config.duplex == DUPLEX_FULL)
3240 new_adv = ADVERTISED_1000baseT_Full;
3241 else
3242 new_adv = ADVERTISED_1000baseT_Half;
3243 } else if (tp->link_config.speed == SPEED_100) {
3244 if (tp->link_config.duplex == DUPLEX_FULL)
3245 new_adv = ADVERTISED_100baseT_Full;
3246 else
3247 new_adv = ADVERTISED_100baseT_Half;
3248 } else {
3249 if (tp->link_config.duplex == DUPLEX_FULL)
3250 new_adv = ADVERTISED_10baseT_Full;
3251 else
3252 new_adv = ADVERTISED_10baseT_Half;
3253 }
3254
3255 tg3_phy_autoneg_cfg(tp, new_adv,
3256 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003257 }
3258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3260 tp->link_config.speed != SPEED_INVALID) {
3261 u32 bmcr, orig_bmcr;
3262
3263 tp->link_config.active_speed = tp->link_config.speed;
3264 tp->link_config.active_duplex = tp->link_config.duplex;
3265
3266 bmcr = 0;
3267 switch (tp->link_config.speed) {
3268 default:
3269 case SPEED_10:
3270 break;
3271
3272 case SPEED_100:
3273 bmcr |= BMCR_SPEED100;
3274 break;
3275
3276 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003277 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
3281 if (tp->link_config.duplex == DUPLEX_FULL)
3282 bmcr |= BMCR_FULLDPLX;
3283
3284 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3285 (bmcr != orig_bmcr)) {
3286 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3287 for (i = 0; i < 1500; i++) {
3288 u32 tmp;
3289
3290 udelay(10);
3291 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3292 tg3_readphy(tp, MII_BMSR, &tmp))
3293 continue;
3294 if (!(tmp & BMSR_LSTATUS)) {
3295 udelay(40);
3296 break;
3297 }
3298 }
3299 tg3_writephy(tp, MII_BMCR, bmcr);
3300 udelay(40);
3301 }
3302 } else {
3303 tg3_writephy(tp, MII_BMCR,
3304 BMCR_ANENABLE | BMCR_ANRESTART);
3305 }
3306}
3307
3308static int tg3_init_5401phy_dsp(struct tg3 *tp)
3309{
3310 int err;
3311
3312 /* Turn off tap power management. */
3313 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003314 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003316 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3317 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3318 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3319 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3320 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
3322 udelay(40);
3323
3324 return err;
3325}
3326
Michael Chan3600d912006-12-07 00:21:48 -08003327static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328{
Michael Chan3600d912006-12-07 00:21:48 -08003329 u32 adv_reg, all_mask = 0;
3330
3331 if (mask & ADVERTISED_10baseT_Half)
3332 all_mask |= ADVERTISE_10HALF;
3333 if (mask & ADVERTISED_10baseT_Full)
3334 all_mask |= ADVERTISE_10FULL;
3335 if (mask & ADVERTISED_100baseT_Half)
3336 all_mask |= ADVERTISE_100HALF;
3337 if (mask & ADVERTISED_100baseT_Full)
3338 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
3340 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3341 return 0;
3342
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003343 if ((adv_reg & ADVERTISE_ALL) != all_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 return 0;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003345
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003346 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 u32 tg3_ctrl;
3348
Michael Chan3600d912006-12-07 00:21:48 -08003349 all_mask = 0;
3350 if (mask & ADVERTISED_1000baseT_Half)
3351 all_mask |= ADVERTISE_1000HALF;
3352 if (mask & ADVERTISED_1000baseT_Full)
3353 all_mask |= ADVERTISE_1000FULL;
3354
Matt Carlson221c5632011-06-13 13:39:01 +00003355 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 return 0;
3357
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003358 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
3359 if (tg3_ctrl != all_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 return 0;
3361 }
3362 return 1;
3363}
3364
Matt Carlsonef167e22007-12-20 20:10:01 -08003365static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3366{
3367 u32 curadv, reqadv;
3368
3369 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3370 return 1;
3371
3372 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3373 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3374
3375 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3376 if (curadv != reqadv)
3377 return 0;
3378
Joe Perches63c3a662011-04-26 08:12:10 +00003379 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003380 tg3_readphy(tp, MII_LPA, rmtadv);
3381 } else {
3382 /* Reprogram the advertisement register, even if it
3383 * does not affect the current link. If the link
3384 * gets renegotiated in the future, we can save an
3385 * additional renegotiation cycle by advertising
3386 * it correctly in the first place.
3387 */
3388 if (curadv != reqadv) {
3389 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3390 ADVERTISE_PAUSE_ASYM);
3391 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3392 }
3393 }
3394
3395 return 1;
3396}
3397
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3399{
3400 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003401 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003402 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 u16 current_speed;
3404 u8 current_duplex;
3405 int i, err;
3406
3407 tw32(MAC_EVENT, 0);
3408
3409 tw32_f(MAC_STATUS,
3410 (MAC_STATUS_SYNC_CHANGED |
3411 MAC_STATUS_CFG_CHANGED |
3412 MAC_STATUS_MI_COMPLETION |
3413 MAC_STATUS_LNKSTATE_CHANGED));
3414 udelay(40);
3415
Matt Carlson8ef21422008-05-02 16:47:53 -07003416 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3417 tw32_f(MAC_MI_MODE,
3418 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3419 udelay(80);
3420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003422 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
3424 /* Some third-party PHYs need to be reset on link going
3425 * down.
3426 */
3427 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3428 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3429 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3430 netif_carrier_ok(tp->dev)) {
3431 tg3_readphy(tp, MII_BMSR, &bmsr);
3432 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3433 !(bmsr & BMSR_LSTATUS))
3434 force_reset = 1;
3435 }
3436 if (force_reset)
3437 tg3_phy_reset(tp);
3438
Matt Carlson79eb6902010-02-17 15:17:03 +00003439 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 tg3_readphy(tp, MII_BMSR, &bmsr);
3441 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003442 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 bmsr = 0;
3444
3445 if (!(bmsr & BMSR_LSTATUS)) {
3446 err = tg3_init_5401phy_dsp(tp);
3447 if (err)
3448 return err;
3449
3450 tg3_readphy(tp, MII_BMSR, &bmsr);
3451 for (i = 0; i < 1000; i++) {
3452 udelay(10);
3453 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3454 (bmsr & BMSR_LSTATUS)) {
3455 udelay(40);
3456 break;
3457 }
3458 }
3459
Matt Carlson79eb6902010-02-17 15:17:03 +00003460 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3461 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 !(bmsr & BMSR_LSTATUS) &&
3463 tp->link_config.active_speed == SPEED_1000) {
3464 err = tg3_phy_reset(tp);
3465 if (!err)
3466 err = tg3_init_5401phy_dsp(tp);
3467 if (err)
3468 return err;
3469 }
3470 }
3471 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3472 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3473 /* 5701 {A0,B0} CRC bug workaround */
3474 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003475 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3476 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3477 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 }
3479
3480 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003481 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3482 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003484 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003486 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3488
3489 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3490 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3491 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3492 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3493 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3494 else
3495 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3496 }
3497
3498 current_link_up = 0;
3499 current_speed = SPEED_INVALID;
3500 current_duplex = DUPLEX_INVALID;
3501
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003502 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003503 err = tg3_phy_auxctl_read(tp,
3504 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3505 &val);
3506 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003507 tg3_phy_auxctl_write(tp,
3508 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3509 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 goto relink;
3511 }
3512 }
3513
3514 bmsr = 0;
3515 for (i = 0; i < 100; i++) {
3516 tg3_readphy(tp, MII_BMSR, &bmsr);
3517 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3518 (bmsr & BMSR_LSTATUS))
3519 break;
3520 udelay(40);
3521 }
3522
3523 if (bmsr & BMSR_LSTATUS) {
3524 u32 aux_stat, bmcr;
3525
3526 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3527 for (i = 0; i < 2000; i++) {
3528 udelay(10);
3529 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3530 aux_stat)
3531 break;
3532 }
3533
3534 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3535 &current_speed,
3536 &current_duplex);
3537
3538 bmcr = 0;
3539 for (i = 0; i < 200; i++) {
3540 tg3_readphy(tp, MII_BMCR, &bmcr);
3541 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3542 continue;
3543 if (bmcr && bmcr != 0x7fff)
3544 break;
3545 udelay(10);
3546 }
3547
Matt Carlsonef167e22007-12-20 20:10:01 -08003548 lcl_adv = 0;
3549 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Matt Carlsonef167e22007-12-20 20:10:01 -08003551 tp->link_config.active_speed = current_speed;
3552 tp->link_config.active_duplex = current_duplex;
3553
3554 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3555 if ((bmcr & BMCR_ANENABLE) &&
3556 tg3_copper_is_advertising_all(tp,
3557 tp->link_config.advertising)) {
3558 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3559 &rmt_adv))
3560 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 }
3562 } else {
3563 if (!(bmcr & BMCR_ANENABLE) &&
3564 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003565 tp->link_config.duplex == current_duplex &&
3566 tp->link_config.flowctrl ==
3567 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
3570 }
3571
Matt Carlsonef167e22007-12-20 20:10:01 -08003572 if (current_link_up == 1 &&
3573 tp->link_config.active_duplex == DUPLEX_FULL)
3574 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 }
3576
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577relink:
Matt Carlson80096062010-08-02 11:26:06 +00003578 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 tg3_phy_copper_begin(tp);
3580
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003581 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00003582 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
3583 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 current_link_up = 1;
3585 }
3586
3587 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3588 if (current_link_up == 1) {
3589 if (tp->link_config.active_speed == SPEED_100 ||
3590 tp->link_config.active_speed == SPEED_10)
3591 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3592 else
3593 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003594 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003595 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3596 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3598
3599 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3600 if (tp->link_config.active_duplex == DUPLEX_HALF)
3601 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3602
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003604 if (current_link_up == 1 &&
3605 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003607 else
3608 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 }
3610
3611 /* ??? Without this setting Netgear GA302T PHY does not
3612 * ??? send/receive packets...
3613 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003614 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3616 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3617 tw32_f(MAC_MI_MODE, tp->mi_mode);
3618 udelay(80);
3619 }
3620
3621 tw32_f(MAC_MODE, tp->mac_mode);
3622 udelay(40);
3623
Matt Carlson52b02d02010-10-14 10:37:41 +00003624 tg3_phy_eee_adjust(tp, current_link_up);
3625
Joe Perches63c3a662011-04-26 08:12:10 +00003626 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 /* Polled via timer. */
3628 tw32_f(MAC_EVENT, 0);
3629 } else {
3630 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3631 }
3632 udelay(40);
3633
3634 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3635 current_link_up == 1 &&
3636 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00003637 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 udelay(120);
3639 tw32_f(MAC_STATUS,
3640 (MAC_STATUS_SYNC_CHANGED |
3641 MAC_STATUS_CFG_CHANGED));
3642 udelay(40);
3643 tg3_write_mem(tp,
3644 NIC_SRAM_FIRMWARE_MBOX,
3645 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3646 }
3647
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003648 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00003649 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003650 u16 oldlnkctl, newlnkctl;
3651
3652 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003653 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003654 &oldlnkctl);
3655 if (tp->link_config.active_speed == SPEED_100 ||
3656 tp->link_config.active_speed == SPEED_10)
3657 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3658 else
3659 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3660 if (newlnkctl != oldlnkctl)
3661 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003662 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003663 newlnkctl);
3664 }
3665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 if (current_link_up != netif_carrier_ok(tp->dev)) {
3667 if (current_link_up)
3668 netif_carrier_on(tp->dev);
3669 else
3670 netif_carrier_off(tp->dev);
3671 tg3_link_report(tp);
3672 }
3673
3674 return 0;
3675}
3676
3677struct tg3_fiber_aneginfo {
3678 int state;
3679#define ANEG_STATE_UNKNOWN 0
3680#define ANEG_STATE_AN_ENABLE 1
3681#define ANEG_STATE_RESTART_INIT 2
3682#define ANEG_STATE_RESTART 3
3683#define ANEG_STATE_DISABLE_LINK_OK 4
3684#define ANEG_STATE_ABILITY_DETECT_INIT 5
3685#define ANEG_STATE_ABILITY_DETECT 6
3686#define ANEG_STATE_ACK_DETECT_INIT 7
3687#define ANEG_STATE_ACK_DETECT 8
3688#define ANEG_STATE_COMPLETE_ACK_INIT 9
3689#define ANEG_STATE_COMPLETE_ACK 10
3690#define ANEG_STATE_IDLE_DETECT_INIT 11
3691#define ANEG_STATE_IDLE_DETECT 12
3692#define ANEG_STATE_LINK_OK 13
3693#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3694#define ANEG_STATE_NEXT_PAGE_WAIT 15
3695
3696 u32 flags;
3697#define MR_AN_ENABLE 0x00000001
3698#define MR_RESTART_AN 0x00000002
3699#define MR_AN_COMPLETE 0x00000004
3700#define MR_PAGE_RX 0x00000008
3701#define MR_NP_LOADED 0x00000010
3702#define MR_TOGGLE_TX 0x00000020
3703#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3704#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3705#define MR_LP_ADV_SYM_PAUSE 0x00000100
3706#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3707#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3708#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3709#define MR_LP_ADV_NEXT_PAGE 0x00001000
3710#define MR_TOGGLE_RX 0x00002000
3711#define MR_NP_RX 0x00004000
3712
3713#define MR_LINK_OK 0x80000000
3714
3715 unsigned long link_time, cur_time;
3716
3717 u32 ability_match_cfg;
3718 int ability_match_count;
3719
3720 char ability_match, idle_match, ack_match;
3721
3722 u32 txconfig, rxconfig;
3723#define ANEG_CFG_NP 0x00000080
3724#define ANEG_CFG_ACK 0x00000040
3725#define ANEG_CFG_RF2 0x00000020
3726#define ANEG_CFG_RF1 0x00000010
3727#define ANEG_CFG_PS2 0x00000001
3728#define ANEG_CFG_PS1 0x00008000
3729#define ANEG_CFG_HD 0x00004000
3730#define ANEG_CFG_FD 0x00002000
3731#define ANEG_CFG_INVAL 0x00001f06
3732
3733};
3734#define ANEG_OK 0
3735#define ANEG_DONE 1
3736#define ANEG_TIMER_ENAB 2
3737#define ANEG_FAILED -1
3738
3739#define ANEG_STATE_SETTLE_TIME 10000
3740
3741static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3742 struct tg3_fiber_aneginfo *ap)
3743{
Matt Carlson5be73b42007-12-20 20:09:29 -08003744 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 unsigned long delta;
3746 u32 rx_cfg_reg;
3747 int ret;
3748
3749 if (ap->state == ANEG_STATE_UNKNOWN) {
3750 ap->rxconfig = 0;
3751 ap->link_time = 0;
3752 ap->cur_time = 0;
3753 ap->ability_match_cfg = 0;
3754 ap->ability_match_count = 0;
3755 ap->ability_match = 0;
3756 ap->idle_match = 0;
3757 ap->ack_match = 0;
3758 }
3759 ap->cur_time++;
3760
3761 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3762 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3763
3764 if (rx_cfg_reg != ap->ability_match_cfg) {
3765 ap->ability_match_cfg = rx_cfg_reg;
3766 ap->ability_match = 0;
3767 ap->ability_match_count = 0;
3768 } else {
3769 if (++ap->ability_match_count > 1) {
3770 ap->ability_match = 1;
3771 ap->ability_match_cfg = rx_cfg_reg;
3772 }
3773 }
3774 if (rx_cfg_reg & ANEG_CFG_ACK)
3775 ap->ack_match = 1;
3776 else
3777 ap->ack_match = 0;
3778
3779 ap->idle_match = 0;
3780 } else {
3781 ap->idle_match = 1;
3782 ap->ability_match_cfg = 0;
3783 ap->ability_match_count = 0;
3784 ap->ability_match = 0;
3785 ap->ack_match = 0;
3786
3787 rx_cfg_reg = 0;
3788 }
3789
3790 ap->rxconfig = rx_cfg_reg;
3791 ret = ANEG_OK;
3792
Matt Carlson33f401a2010-04-05 10:19:27 +00003793 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 case ANEG_STATE_UNKNOWN:
3795 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3796 ap->state = ANEG_STATE_AN_ENABLE;
3797
3798 /* fallthru */
3799 case ANEG_STATE_AN_ENABLE:
3800 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3801 if (ap->flags & MR_AN_ENABLE) {
3802 ap->link_time = 0;
3803 ap->cur_time = 0;
3804 ap->ability_match_cfg = 0;
3805 ap->ability_match_count = 0;
3806 ap->ability_match = 0;
3807 ap->idle_match = 0;
3808 ap->ack_match = 0;
3809
3810 ap->state = ANEG_STATE_RESTART_INIT;
3811 } else {
3812 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3813 }
3814 break;
3815
3816 case ANEG_STATE_RESTART_INIT:
3817 ap->link_time = ap->cur_time;
3818 ap->flags &= ~(MR_NP_LOADED);
3819 ap->txconfig = 0;
3820 tw32(MAC_TX_AUTO_NEG, 0);
3821 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3822 tw32_f(MAC_MODE, tp->mac_mode);
3823 udelay(40);
3824
3825 ret = ANEG_TIMER_ENAB;
3826 ap->state = ANEG_STATE_RESTART;
3827
3828 /* fallthru */
3829 case ANEG_STATE_RESTART:
3830 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00003831 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00003833 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 break;
3836
3837 case ANEG_STATE_DISABLE_LINK_OK:
3838 ret = ANEG_DONE;
3839 break;
3840
3841 case ANEG_STATE_ABILITY_DETECT_INIT:
3842 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003843 ap->txconfig = ANEG_CFG_FD;
3844 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3845 if (flowctrl & ADVERTISE_1000XPAUSE)
3846 ap->txconfig |= ANEG_CFG_PS1;
3847 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3848 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3850 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3851 tw32_f(MAC_MODE, tp->mac_mode);
3852 udelay(40);
3853
3854 ap->state = ANEG_STATE_ABILITY_DETECT;
3855 break;
3856
3857 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00003858 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 break;
3861
3862 case ANEG_STATE_ACK_DETECT_INIT:
3863 ap->txconfig |= ANEG_CFG_ACK;
3864 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3865 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3866 tw32_f(MAC_MODE, tp->mac_mode);
3867 udelay(40);
3868
3869 ap->state = ANEG_STATE_ACK_DETECT;
3870
3871 /* fallthru */
3872 case ANEG_STATE_ACK_DETECT:
3873 if (ap->ack_match != 0) {
3874 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3875 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3876 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3877 } else {
3878 ap->state = ANEG_STATE_AN_ENABLE;
3879 }
3880 } else if (ap->ability_match != 0 &&
3881 ap->rxconfig == 0) {
3882 ap->state = ANEG_STATE_AN_ENABLE;
3883 }
3884 break;
3885
3886 case ANEG_STATE_COMPLETE_ACK_INIT:
3887 if (ap->rxconfig & ANEG_CFG_INVAL) {
3888 ret = ANEG_FAILED;
3889 break;
3890 }
3891 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3892 MR_LP_ADV_HALF_DUPLEX |
3893 MR_LP_ADV_SYM_PAUSE |
3894 MR_LP_ADV_ASYM_PAUSE |
3895 MR_LP_ADV_REMOTE_FAULT1 |
3896 MR_LP_ADV_REMOTE_FAULT2 |
3897 MR_LP_ADV_NEXT_PAGE |
3898 MR_TOGGLE_RX |
3899 MR_NP_RX);
3900 if (ap->rxconfig & ANEG_CFG_FD)
3901 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3902 if (ap->rxconfig & ANEG_CFG_HD)
3903 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3904 if (ap->rxconfig & ANEG_CFG_PS1)
3905 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3906 if (ap->rxconfig & ANEG_CFG_PS2)
3907 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3908 if (ap->rxconfig & ANEG_CFG_RF1)
3909 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3910 if (ap->rxconfig & ANEG_CFG_RF2)
3911 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3912 if (ap->rxconfig & ANEG_CFG_NP)
3913 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3914
3915 ap->link_time = ap->cur_time;
3916
3917 ap->flags ^= (MR_TOGGLE_TX);
3918 if (ap->rxconfig & 0x0008)
3919 ap->flags |= MR_TOGGLE_RX;
3920 if (ap->rxconfig & ANEG_CFG_NP)
3921 ap->flags |= MR_NP_RX;
3922 ap->flags |= MR_PAGE_RX;
3923
3924 ap->state = ANEG_STATE_COMPLETE_ACK;
3925 ret = ANEG_TIMER_ENAB;
3926 break;
3927
3928 case ANEG_STATE_COMPLETE_ACK:
3929 if (ap->ability_match != 0 &&
3930 ap->rxconfig == 0) {
3931 ap->state = ANEG_STATE_AN_ENABLE;
3932 break;
3933 }
3934 delta = ap->cur_time - ap->link_time;
3935 if (delta > ANEG_STATE_SETTLE_TIME) {
3936 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3937 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3938 } else {
3939 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3940 !(ap->flags & MR_NP_RX)) {
3941 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3942 } else {
3943 ret = ANEG_FAILED;
3944 }
3945 }
3946 }
3947 break;
3948
3949 case ANEG_STATE_IDLE_DETECT_INIT:
3950 ap->link_time = ap->cur_time;
3951 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3952 tw32_f(MAC_MODE, tp->mac_mode);
3953 udelay(40);
3954
3955 ap->state = ANEG_STATE_IDLE_DETECT;
3956 ret = ANEG_TIMER_ENAB;
3957 break;
3958
3959 case ANEG_STATE_IDLE_DETECT:
3960 if (ap->ability_match != 0 &&
3961 ap->rxconfig == 0) {
3962 ap->state = ANEG_STATE_AN_ENABLE;
3963 break;
3964 }
3965 delta = ap->cur_time - ap->link_time;
3966 if (delta > ANEG_STATE_SETTLE_TIME) {
3967 /* XXX another gem from the Broadcom driver :( */
3968 ap->state = ANEG_STATE_LINK_OK;
3969 }
3970 break;
3971
3972 case ANEG_STATE_LINK_OK:
3973 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3974 ret = ANEG_DONE;
3975 break;
3976
3977 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3978 /* ??? unimplemented */
3979 break;
3980
3981 case ANEG_STATE_NEXT_PAGE_WAIT:
3982 /* ??? unimplemented */
3983 break;
3984
3985 default:
3986 ret = ANEG_FAILED;
3987 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
3990 return ret;
3991}
3992
Matt Carlson5be73b42007-12-20 20:09:29 -08003993static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994{
3995 int res = 0;
3996 struct tg3_fiber_aneginfo aninfo;
3997 int status = ANEG_FAILED;
3998 unsigned int tick;
3999 u32 tmp;
4000
4001 tw32_f(MAC_TX_AUTO_NEG, 0);
4002
4003 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4004 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4005 udelay(40);
4006
4007 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4008 udelay(40);
4009
4010 memset(&aninfo, 0, sizeof(aninfo));
4011 aninfo.flags |= MR_AN_ENABLE;
4012 aninfo.state = ANEG_STATE_UNKNOWN;
4013 aninfo.cur_time = 0;
4014 tick = 0;
4015 while (++tick < 195000) {
4016 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4017 if (status == ANEG_DONE || status == ANEG_FAILED)
4018 break;
4019
4020 udelay(1);
4021 }
4022
4023 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4024 tw32_f(MAC_MODE, tp->mac_mode);
4025 udelay(40);
4026
Matt Carlson5be73b42007-12-20 20:09:29 -08004027 *txflags = aninfo.txconfig;
4028 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
4030 if (status == ANEG_DONE &&
4031 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4032 MR_LP_ADV_FULL_DUPLEX)))
4033 res = 1;
4034
4035 return res;
4036}
4037
4038static void tg3_init_bcm8002(struct tg3 *tp)
4039{
4040 u32 mac_status = tr32(MAC_STATUS);
4041 int i;
4042
4043 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004044 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 !(mac_status & MAC_STATUS_PCS_SYNCED))
4046 return;
4047
4048 /* Set PLL lock range. */
4049 tg3_writephy(tp, 0x16, 0x8007);
4050
4051 /* SW reset */
4052 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4053
4054 /* Wait for reset to complete. */
4055 /* XXX schedule_timeout() ... */
4056 for (i = 0; i < 500; i++)
4057 udelay(10);
4058
4059 /* Config mode; select PMA/Ch 1 regs. */
4060 tg3_writephy(tp, 0x10, 0x8411);
4061
4062 /* Enable auto-lock and comdet, select txclk for tx. */
4063 tg3_writephy(tp, 0x11, 0x0a10);
4064
4065 tg3_writephy(tp, 0x18, 0x00a0);
4066 tg3_writephy(tp, 0x16, 0x41ff);
4067
4068 /* Assert and deassert POR. */
4069 tg3_writephy(tp, 0x13, 0x0400);
4070 udelay(40);
4071 tg3_writephy(tp, 0x13, 0x0000);
4072
4073 tg3_writephy(tp, 0x11, 0x0a50);
4074 udelay(40);
4075 tg3_writephy(tp, 0x11, 0x0a10);
4076
4077 /* Wait for signal to stabilize */
4078 /* XXX schedule_timeout() ... */
4079 for (i = 0; i < 15000; i++)
4080 udelay(10);
4081
4082 /* Deselect the channel register so we can read the PHYID
4083 * later.
4084 */
4085 tg3_writephy(tp, 0x10, 0x8011);
4086}
4087
4088static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4089{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004090 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 u32 sg_dig_ctrl, sg_dig_status;
4092 u32 serdes_cfg, expected_sg_dig_ctrl;
4093 int workaround, port_a;
4094 int current_link_up;
4095
4096 serdes_cfg = 0;
4097 expected_sg_dig_ctrl = 0;
4098 workaround = 0;
4099 port_a = 1;
4100 current_link_up = 0;
4101
4102 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4103 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4104 workaround = 1;
4105 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4106 port_a = 0;
4107
4108 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4109 /* preserve bits 20-23 for voltage regulator */
4110 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4111 }
4112
4113 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4114
4115 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004116 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 if (workaround) {
4118 u32 val = serdes_cfg;
4119
4120 if (port_a)
4121 val |= 0xc010000;
4122 else
4123 val |= 0x4010000;
4124 tw32_f(MAC_SERDES_CFG, val);
4125 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004126
4127 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 }
4129 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4130 tg3_setup_flow_control(tp, 0, 0);
4131 current_link_up = 1;
4132 }
4133 goto out;
4134 }
4135
4136 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004137 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
Matt Carlson82cd3d12007-12-20 20:09:00 -08004139 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4140 if (flowctrl & ADVERTISE_1000XPAUSE)
4141 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4142 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4143 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
4145 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004146 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004147 tp->serdes_counter &&
4148 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4149 MAC_STATUS_RCVD_CFG)) ==
4150 MAC_STATUS_PCS_SYNCED)) {
4151 tp->serdes_counter--;
4152 current_link_up = 1;
4153 goto out;
4154 }
4155restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 if (workaround)
4157 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004158 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 udelay(5);
4160 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4161
Michael Chan3d3ebe72006-09-27 15:59:15 -07004162 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004163 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4165 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004166 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 mac_status = tr32(MAC_STATUS);
4168
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004169 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004171 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Matt Carlson82cd3d12007-12-20 20:09:00 -08004173 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4174 local_adv |= ADVERTISE_1000XPAUSE;
4175 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4176 local_adv |= ADVERTISE_1000XPSE_ASYM;
4177
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004178 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004179 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004180 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004181 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
4183 tg3_setup_flow_control(tp, local_adv, remote_adv);
4184 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004185 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004186 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004187 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004188 if (tp->serdes_counter)
4189 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 else {
4191 if (workaround) {
4192 u32 val = serdes_cfg;
4193
4194 if (port_a)
4195 val |= 0xc010000;
4196 else
4197 val |= 0x4010000;
4198
4199 tw32_f(MAC_SERDES_CFG, val);
4200 }
4201
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004202 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 udelay(40);
4204
4205 /* Link parallel detection - link is up */
4206 /* only if we have PCS_SYNC and not */
4207 /* receiving config code words */
4208 mac_status = tr32(MAC_STATUS);
4209 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4210 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4211 tg3_setup_flow_control(tp, 0, 0);
4212 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004213 tp->phy_flags |=
4214 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004215 tp->serdes_counter =
4216 SERDES_PARALLEL_DET_TIMEOUT;
4217 } else
4218 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 }
4220 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004221 } else {
4222 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004223 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 }
4225
4226out:
4227 return current_link_up;
4228}
4229
4230static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4231{
4232 int current_link_up = 0;
4233
Michael Chan5cf64b82007-05-05 12:11:21 -07004234 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
4237 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004238 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004240
Matt Carlson5be73b42007-12-20 20:09:29 -08004241 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4242 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Matt Carlson5be73b42007-12-20 20:09:29 -08004244 if (txflags & ANEG_CFG_PS1)
4245 local_adv |= ADVERTISE_1000XPAUSE;
4246 if (txflags & ANEG_CFG_PS2)
4247 local_adv |= ADVERTISE_1000XPSE_ASYM;
4248
4249 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4250 remote_adv |= LPA_1000XPAUSE;
4251 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4252 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
4254 tg3_setup_flow_control(tp, local_adv, remote_adv);
4255
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 current_link_up = 1;
4257 }
4258 for (i = 0; i < 30; i++) {
4259 udelay(20);
4260 tw32_f(MAC_STATUS,
4261 (MAC_STATUS_SYNC_CHANGED |
4262 MAC_STATUS_CFG_CHANGED));
4263 udelay(40);
4264 if ((tr32(MAC_STATUS) &
4265 (MAC_STATUS_SYNC_CHANGED |
4266 MAC_STATUS_CFG_CHANGED)) == 0)
4267 break;
4268 }
4269
4270 mac_status = tr32(MAC_STATUS);
4271 if (current_link_up == 0 &&
4272 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4273 !(mac_status & MAC_STATUS_RCVD_CFG))
4274 current_link_up = 1;
4275 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004276 tg3_setup_flow_control(tp, 0, 0);
4277
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 /* Forcing 1000FD link up. */
4279 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
4281 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4282 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004283
4284 tw32_f(MAC_MODE, tp->mac_mode);
4285 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 }
4287
4288out:
4289 return current_link_up;
4290}
4291
4292static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4293{
4294 u32 orig_pause_cfg;
4295 u16 orig_active_speed;
4296 u8 orig_active_duplex;
4297 u32 mac_status;
4298 int current_link_up;
4299 int i;
4300
Matt Carlson8d018622007-12-20 20:05:44 -08004301 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 orig_active_speed = tp->link_config.active_speed;
4303 orig_active_duplex = tp->link_config.active_duplex;
4304
Joe Perches63c3a662011-04-26 08:12:10 +00004305 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004307 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 mac_status = tr32(MAC_STATUS);
4309 mac_status &= (MAC_STATUS_PCS_SYNCED |
4310 MAC_STATUS_SIGNAL_DET |
4311 MAC_STATUS_CFG_CHANGED |
4312 MAC_STATUS_RCVD_CFG);
4313 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4314 MAC_STATUS_SIGNAL_DET)) {
4315 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4316 MAC_STATUS_CFG_CHANGED));
4317 return 0;
4318 }
4319 }
4320
4321 tw32_f(MAC_TX_AUTO_NEG, 0);
4322
4323 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4324 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4325 tw32_f(MAC_MODE, tp->mac_mode);
4326 udelay(40);
4327
Matt Carlson79eb6902010-02-17 15:17:03 +00004328 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 tg3_init_bcm8002(tp);
4330
4331 /* Enable link change event even when serdes polling. */
4332 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4333 udelay(40);
4334
4335 current_link_up = 0;
4336 mac_status = tr32(MAC_STATUS);
4337
Joe Perches63c3a662011-04-26 08:12:10 +00004338 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4340 else
4341 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4342
Matt Carlson898a56f2009-08-28 14:02:40 +00004343 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004345 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
4347 for (i = 0; i < 100; i++) {
4348 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4349 MAC_STATUS_CFG_CHANGED));
4350 udelay(5);
4351 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004352 MAC_STATUS_CFG_CHANGED |
4353 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 break;
4355 }
4356
4357 mac_status = tr32(MAC_STATUS);
4358 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4359 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004360 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4361 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 tw32_f(MAC_MODE, (tp->mac_mode |
4363 MAC_MODE_SEND_CONFIGS));
4364 udelay(1);
4365 tw32_f(MAC_MODE, tp->mac_mode);
4366 }
4367 }
4368
4369 if (current_link_up == 1) {
4370 tp->link_config.active_speed = SPEED_1000;
4371 tp->link_config.active_duplex = DUPLEX_FULL;
4372 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4373 LED_CTRL_LNKLED_OVERRIDE |
4374 LED_CTRL_1000MBPS_ON));
4375 } else {
4376 tp->link_config.active_speed = SPEED_INVALID;
4377 tp->link_config.active_duplex = DUPLEX_INVALID;
4378 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4379 LED_CTRL_LNKLED_OVERRIDE |
4380 LED_CTRL_TRAFFIC_OVERRIDE));
4381 }
4382
4383 if (current_link_up != netif_carrier_ok(tp->dev)) {
4384 if (current_link_up)
4385 netif_carrier_on(tp->dev);
4386 else
4387 netif_carrier_off(tp->dev);
4388 tg3_link_report(tp);
4389 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004390 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 if (orig_pause_cfg != now_pause_cfg ||
4392 orig_active_speed != tp->link_config.active_speed ||
4393 orig_active_duplex != tp->link_config.active_duplex)
4394 tg3_link_report(tp);
4395 }
4396
4397 return 0;
4398}
4399
Michael Chan747e8f82005-07-25 12:33:22 -07004400static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4401{
4402 int current_link_up, err = 0;
4403 u32 bmsr, bmcr;
4404 u16 current_speed;
4405 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004406 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004407
4408 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4409 tw32_f(MAC_MODE, tp->mac_mode);
4410 udelay(40);
4411
4412 tw32(MAC_EVENT, 0);
4413
4414 tw32_f(MAC_STATUS,
4415 (MAC_STATUS_SYNC_CHANGED |
4416 MAC_STATUS_CFG_CHANGED |
4417 MAC_STATUS_MI_COMPLETION |
4418 MAC_STATUS_LNKSTATE_CHANGED));
4419 udelay(40);
4420
4421 if (force_reset)
4422 tg3_phy_reset(tp);
4423
4424 current_link_up = 0;
4425 current_speed = SPEED_INVALID;
4426 current_duplex = DUPLEX_INVALID;
4427
4428 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4429 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004430 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4431 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4432 bmsr |= BMSR_LSTATUS;
4433 else
4434 bmsr &= ~BMSR_LSTATUS;
4435 }
Michael Chan747e8f82005-07-25 12:33:22 -07004436
4437 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4438
4439 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004440 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004441 /* do nothing, just check for link up at the end */
4442 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4443 u32 adv, new_adv;
4444
4445 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4446 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4447 ADVERTISE_1000XPAUSE |
4448 ADVERTISE_1000XPSE_ASYM |
4449 ADVERTISE_SLCT);
4450
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004451 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004452
4453 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4454 new_adv |= ADVERTISE_1000XHALF;
4455 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4456 new_adv |= ADVERTISE_1000XFULL;
4457
4458 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4459 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4460 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4461 tg3_writephy(tp, MII_BMCR, bmcr);
4462
4463 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004464 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004465 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004466
4467 return err;
4468 }
4469 } else {
4470 u32 new_bmcr;
4471
4472 bmcr &= ~BMCR_SPEED1000;
4473 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4474
4475 if (tp->link_config.duplex == DUPLEX_FULL)
4476 new_bmcr |= BMCR_FULLDPLX;
4477
4478 if (new_bmcr != bmcr) {
4479 /* BMCR_SPEED1000 is a reserved bit that needs
4480 * to be set on write.
4481 */
4482 new_bmcr |= BMCR_SPEED1000;
4483
4484 /* Force a linkdown */
4485 if (netif_carrier_ok(tp->dev)) {
4486 u32 adv;
4487
4488 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4489 adv &= ~(ADVERTISE_1000XFULL |
4490 ADVERTISE_1000XHALF |
4491 ADVERTISE_SLCT);
4492 tg3_writephy(tp, MII_ADVERTISE, adv);
4493 tg3_writephy(tp, MII_BMCR, bmcr |
4494 BMCR_ANRESTART |
4495 BMCR_ANENABLE);
4496 udelay(10);
4497 netif_carrier_off(tp->dev);
4498 }
4499 tg3_writephy(tp, MII_BMCR, new_bmcr);
4500 bmcr = new_bmcr;
4501 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4502 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004503 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4504 ASIC_REV_5714) {
4505 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4506 bmsr |= BMSR_LSTATUS;
4507 else
4508 bmsr &= ~BMSR_LSTATUS;
4509 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004510 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004511 }
4512 }
4513
4514 if (bmsr & BMSR_LSTATUS) {
4515 current_speed = SPEED_1000;
4516 current_link_up = 1;
4517 if (bmcr & BMCR_FULLDPLX)
4518 current_duplex = DUPLEX_FULL;
4519 else
4520 current_duplex = DUPLEX_HALF;
4521
Matt Carlsonef167e22007-12-20 20:10:01 -08004522 local_adv = 0;
4523 remote_adv = 0;
4524
Michael Chan747e8f82005-07-25 12:33:22 -07004525 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004526 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004527
4528 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4529 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4530 common = local_adv & remote_adv;
4531 if (common & (ADVERTISE_1000XHALF |
4532 ADVERTISE_1000XFULL)) {
4533 if (common & ADVERTISE_1000XFULL)
4534 current_duplex = DUPLEX_FULL;
4535 else
4536 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004537 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004538 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004539 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004540 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004541 }
Michael Chan747e8f82005-07-25 12:33:22 -07004542 }
4543 }
4544
Matt Carlsonef167e22007-12-20 20:10:01 -08004545 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4546 tg3_setup_flow_control(tp, local_adv, remote_adv);
4547
Michael Chan747e8f82005-07-25 12:33:22 -07004548 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4549 if (tp->link_config.active_duplex == DUPLEX_HALF)
4550 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4551
4552 tw32_f(MAC_MODE, tp->mac_mode);
4553 udelay(40);
4554
4555 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4556
4557 tp->link_config.active_speed = current_speed;
4558 tp->link_config.active_duplex = current_duplex;
4559
4560 if (current_link_up != netif_carrier_ok(tp->dev)) {
4561 if (current_link_up)
4562 netif_carrier_on(tp->dev);
4563 else {
4564 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004565 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004566 }
4567 tg3_link_report(tp);
4568 }
4569 return err;
4570}
4571
4572static void tg3_serdes_parallel_detect(struct tg3 *tp)
4573{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004574 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004575 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004576 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004577 return;
4578 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004579
Michael Chan747e8f82005-07-25 12:33:22 -07004580 if (!netif_carrier_ok(tp->dev) &&
4581 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4582 u32 bmcr;
4583
4584 tg3_readphy(tp, MII_BMCR, &bmcr);
4585 if (bmcr & BMCR_ANENABLE) {
4586 u32 phy1, phy2;
4587
4588 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004589 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4590 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004591
4592 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004593 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4594 MII_TG3_DSP_EXP1_INT_STAT);
4595 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4596 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004597
4598 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4599 /* We have signal detect and not receiving
4600 * config code words, link is up by parallel
4601 * detection.
4602 */
4603
4604 bmcr &= ~BMCR_ANENABLE;
4605 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4606 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004607 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004608 }
4609 }
Matt Carlson859a5882010-04-05 10:19:28 +00004610 } else if (netif_carrier_ok(tp->dev) &&
4611 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004612 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004613 u32 phy2;
4614
4615 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004616 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4617 MII_TG3_DSP_EXP1_INT_STAT);
4618 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004619 if (phy2 & 0x20) {
4620 u32 bmcr;
4621
4622 /* Config code words received, turn on autoneg. */
4623 tg3_readphy(tp, MII_BMCR, &bmcr);
4624 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4625
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004626 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004627
4628 }
4629 }
4630}
4631
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4633{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004634 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 int err;
4636
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004637 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004639 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004640 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00004641 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004644 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004645 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004646
4647 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4648 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4649 scale = 65;
4650 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4651 scale = 6;
4652 else
4653 scale = 12;
4654
4655 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4656 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4657 tw32(GRC_MISC_CFG, val);
4658 }
4659
Matt Carlsonf2096f92011-04-05 14:22:48 +00004660 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4661 (6 << TX_LENGTHS_IPG_SHIFT);
4662 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4663 val |= tr32(MAC_TX_LENGTHS) &
4664 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4665 TX_LENGTHS_CNT_DWN_VAL_MSK);
4666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 if (tp->link_config.active_speed == SPEED_1000 &&
4668 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004669 tw32(MAC_TX_LENGTHS, val |
4670 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004672 tw32(MAC_TX_LENGTHS, val |
4673 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
Joe Perches63c3a662011-04-26 08:12:10 +00004675 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 if (netif_carrier_ok(tp->dev)) {
4677 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004678 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 } else {
4680 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4681 }
4682 }
4683
Joe Perches63c3a662011-04-26 08:12:10 +00004684 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004685 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004686 if (!netif_carrier_ok(tp->dev))
4687 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4688 tp->pwrmgmt_thresh;
4689 else
4690 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4691 tw32(PCIE_PWR_MGMT_THRESH, val);
4692 }
4693
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 return err;
4695}
4696
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004697static inline int tg3_irq_sync(struct tg3 *tp)
4698{
4699 return tp->irq_sync;
4700}
4701
Matt Carlson97bd8e42011-04-13 11:05:04 +00004702static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4703{
4704 int i;
4705
4706 dst = (u32 *)((u8 *)dst + off);
4707 for (i = 0; i < len; i += sizeof(u32))
4708 *dst++ = tr32(off + i);
4709}
4710
4711static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4712{
4713 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4714 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4715 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4716 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4717 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4718 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4719 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4720 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4721 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4722 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4723 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4724 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4725 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4726 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4727 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4728 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4729 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4730 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4731 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4732
Joe Perches63c3a662011-04-26 08:12:10 +00004733 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004734 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4735
4736 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4737 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4738 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4739 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4740 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4741 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4742 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4743 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4744
Joe Perches63c3a662011-04-26 08:12:10 +00004745 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004746 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4747 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4748 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4749 }
4750
4751 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4752 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4753 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4754 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4755 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4756
Joe Perches63c3a662011-04-26 08:12:10 +00004757 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004758 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4759}
4760
4761static void tg3_dump_state(struct tg3 *tp)
4762{
4763 int i;
4764 u32 *regs;
4765
4766 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4767 if (!regs) {
4768 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4769 return;
4770 }
4771
Joe Perches63c3a662011-04-26 08:12:10 +00004772 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004773 /* Read up to but not including private PCI registers */
4774 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4775 regs[i / sizeof(u32)] = tr32(i);
4776 } else
4777 tg3_dump_legacy_regs(tp, regs);
4778
4779 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4780 if (!regs[i + 0] && !regs[i + 1] &&
4781 !regs[i + 2] && !regs[i + 3])
4782 continue;
4783
4784 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4785 i * 4,
4786 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4787 }
4788
4789 kfree(regs);
4790
4791 for (i = 0; i < tp->irq_cnt; i++) {
4792 struct tg3_napi *tnapi = &tp->napi[i];
4793
4794 /* SW status block */
4795 netdev_err(tp->dev,
4796 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4797 i,
4798 tnapi->hw_status->status,
4799 tnapi->hw_status->status_tag,
4800 tnapi->hw_status->rx_jumbo_consumer,
4801 tnapi->hw_status->rx_consumer,
4802 tnapi->hw_status->rx_mini_consumer,
4803 tnapi->hw_status->idx[0].rx_producer,
4804 tnapi->hw_status->idx[0].tx_consumer);
4805
4806 netdev_err(tp->dev,
4807 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4808 i,
4809 tnapi->last_tag, tnapi->last_irq_tag,
4810 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4811 tnapi->rx_rcb_ptr,
4812 tnapi->prodring.rx_std_prod_idx,
4813 tnapi->prodring.rx_std_cons_idx,
4814 tnapi->prodring.rx_jmb_prod_idx,
4815 tnapi->prodring.rx_jmb_cons_idx);
4816 }
4817}
4818
Michael Chandf3e6542006-05-26 17:48:07 -07004819/* This is called whenever we suspect that the system chipset is re-
4820 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4821 * is bogus tx completions. We try to recover by setting the
4822 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4823 * in the workqueue.
4824 */
4825static void tg3_tx_recover(struct tg3 *tp)
4826{
Joe Perches63c3a662011-04-26 08:12:10 +00004827 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07004828 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4829
Matt Carlson5129c3a2010-04-05 10:19:23 +00004830 netdev_warn(tp->dev,
4831 "The system may be re-ordering memory-mapped I/O "
4832 "cycles to the network device, attempting to recover. "
4833 "Please report the problem to the driver maintainer "
4834 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004835
4836 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00004837 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07004838 spin_unlock(&tp->lock);
4839}
4840
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004841static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004842{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004843 /* Tell compiler to fetch tx indices from memory. */
4844 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004845 return tnapi->tx_pending -
4846 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004847}
4848
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849/* Tigon3 never reports partial packet sends. So we do not
4850 * need special logic to handle SKBs that have not had all
4851 * of their frags sent yet, like SunGEM does.
4852 */
Matt Carlson17375d22009-08-28 14:02:18 +00004853static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854{
Matt Carlson17375d22009-08-28 14:02:18 +00004855 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004856 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004857 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004858 struct netdev_queue *txq;
4859 int index = tnapi - tp->napi;
4860
Joe Perches63c3a662011-04-26 08:12:10 +00004861 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004862 index--;
4863
4864 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
4866 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00004867 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004869 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870
Michael Chandf3e6542006-05-26 17:48:07 -07004871 if (unlikely(skb == NULL)) {
4872 tg3_tx_recover(tp);
4873 return;
4874 }
4875
Alexander Duyckf4188d82009-12-02 16:48:38 +00004876 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004877 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004878 skb_headlen(skb),
4879 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
4881 ri->skb = NULL;
4882
Matt Carlsone01ee142011-07-27 14:20:50 +00004883 while (ri->fragmented) {
4884 ri->fragmented = false;
4885 sw_idx = NEXT_TX(sw_idx);
4886 ri = &tnapi->tx_buffers[sw_idx];
4887 }
4888
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 sw_idx = NEXT_TX(sw_idx);
4890
4891 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004892 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004893 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4894 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004895
4896 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004897 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004898 skb_shinfo(skb)->frags[i].size,
4899 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00004900
4901 while (ri->fragmented) {
4902 ri->fragmented = false;
4903 sw_idx = NEXT_TX(sw_idx);
4904 ri = &tnapi->tx_buffers[sw_idx];
4905 }
4906
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 sw_idx = NEXT_TX(sw_idx);
4908 }
4909
David S. Millerf47c11e2005-06-24 20:18:35 -07004910 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004911
4912 if (unlikely(tx_bug)) {
4913 tg3_tx_recover(tp);
4914 return;
4915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 }
4917
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004918 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919
Michael Chan1b2a7202006-08-07 21:46:02 -07004920 /* Need to make the tx_cons update visible to tg3_start_xmit()
4921 * before checking for netif_queue_stopped(). Without the
4922 * memory barrier, there is a small possibility that tg3_start_xmit()
4923 * will miss it and cause the queue to be stopped forever.
4924 */
4925 smp_mb();
4926
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004927 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004928 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004929 __netif_tx_lock(txq, smp_processor_id());
4930 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004931 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004932 netif_tx_wake_queue(txq);
4933 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935}
4936
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004937static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4938{
4939 if (!ri->skb)
4940 return;
4941
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004942 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004943 map_sz, PCI_DMA_FROMDEVICE);
4944 dev_kfree_skb_any(ri->skb);
4945 ri->skb = NULL;
4946}
4947
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948/* Returns size of skb allocated or < 0 on error.
4949 *
4950 * We only need to fill in the address because the other members
4951 * of the RX descriptor are invariant, see tg3_init_rings.
4952 *
4953 * Note the purposeful assymetry of cpu vs. chip accesses. For
4954 * posting buffers we only dirty the first cache line of the RX
4955 * descriptor (containing the address). Whereas for the RX status
4956 * buffers the cpu only reads the last cacheline of the RX descriptor
4957 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4958 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004959static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004960 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961{
4962 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004963 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 struct sk_buff *skb;
4965 dma_addr_t mapping;
4966 int skb_size, dest_idx;
4967
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 switch (opaque_key) {
4969 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004970 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004971 desc = &tpr->rx_std[dest_idx];
4972 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004973 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 break;
4975
4976 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004977 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004978 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004979 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004980 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 break;
4982
4983 default:
4984 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
4987 /* Do not overwrite any of the map or rp information
4988 * until we are sure we can commit to a new buffer.
4989 *
4990 * Callers depend upon this behavior and assume that
4991 * we leave everything unchanged if we fail.
4992 */
Matt Carlson81389f52011-08-31 11:44:49 +00004993 skb = netdev_alloc_skb(tp->dev, skb_size + TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 if (skb == NULL)
4995 return -ENOMEM;
4996
Matt Carlson81389f52011-08-31 11:44:49 +00004997 skb_reserve(skb, TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
Matt Carlson287be122009-08-28 13:58:46 +00004999 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005001 if (pci_dma_mapping_error(tp->pdev, mapping)) {
5002 dev_kfree_skb(skb);
5003 return -EIO;
5004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
5006 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005007 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 desc->addr_hi = ((u64)mapping >> 32);
5010 desc->addr_lo = ((u64)mapping & 0xffffffff);
5011
5012 return skb_size;
5013}
5014
5015/* We only need to move over in the address because the other
5016 * members of the RX descriptor are invariant. See notes above
5017 * tg3_alloc_rx_skb for full details.
5018 */
Matt Carlsona3896162009-11-13 13:03:44 +00005019static void tg3_recycle_rx(struct tg3_napi *tnapi,
5020 struct tg3_rx_prodring_set *dpr,
5021 u32 opaque_key, int src_idx,
5022 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023{
Matt Carlson17375d22009-08-28 14:02:18 +00005024 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5026 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005027 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005028 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
5030 switch (opaque_key) {
5031 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005032 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005033 dest_desc = &dpr->rx_std[dest_idx];
5034 dest_map = &dpr->rx_std_buffers[dest_idx];
5035 src_desc = &spr->rx_std[src_idx];
5036 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 break;
5038
5039 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005040 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005041 dest_desc = &dpr->rx_jmb[dest_idx].std;
5042 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5043 src_desc = &spr->rx_jmb[src_idx].std;
5044 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 break;
5046
5047 default:
5048 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050
5051 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005052 dma_unmap_addr_set(dest_map, mapping,
5053 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 dest_desc->addr_hi = src_desc->addr_hi;
5055 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005056
5057 /* Ensure that the update to the skb happens after the physical
5058 * addresses have been transferred to the new BD location.
5059 */
5060 smp_wmb();
5061
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 src_map->skb = NULL;
5063}
5064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065/* The RX ring scheme is composed of multiple rings which post fresh
5066 * buffers to the chip, and one special ring the chip uses to report
5067 * status back to the host.
5068 *
5069 * The special ring reports the status of received packets to the
5070 * host. The chip does not write into the original descriptor the
5071 * RX buffer was obtained from. The chip simply takes the original
5072 * descriptor as provided by the host, updates the status and length
5073 * field, then writes this into the next status ring entry.
5074 *
5075 * Each ring the host uses to post buffers to the chip is described
5076 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5077 * it is first placed into the on-chip ram. When the packet's length
5078 * is known, it walks down the TG3_BDINFO entries to select the ring.
5079 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5080 * which is within the range of the new packet's length is chosen.
5081 *
5082 * The "separate ring for rx status" scheme may sound queer, but it makes
5083 * sense from a cache coherency perspective. If only the host writes
5084 * to the buffer post rings, and only the chip writes to the rx status
5085 * rings, then cache lines never move beyond shared-modified state.
5086 * If both the host and chip were to write into the same ring, cache line
5087 * eviction could occur since both entities want it in an exclusive state.
5088 */
Matt Carlson17375d22009-08-28 14:02:18 +00005089static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090{
Matt Carlson17375d22009-08-28 14:02:18 +00005091 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005092 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005093 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005094 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005095 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005097 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005099 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 /*
5101 * We need to order the read of hw_idx and the read of
5102 * the opaque cookie.
5103 */
5104 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 work_mask = 0;
5106 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005107 std_prod_idx = tpr->rx_std_prod_idx;
5108 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005110 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005111 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 unsigned int len;
5113 struct sk_buff *skb;
5114 dma_addr_t dma_addr;
5115 u32 opaque_key, desc_idx, *post_ptr;
5116
5117 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5118 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5119 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005120 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005121 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00005122 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00005123 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005124 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005126 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005127 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00005128 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00005129 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005130 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
5133 work_mask |= opaque_key;
5134
5135 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5136 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5137 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005138 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 desc_idx, *post_ptr);
5140 drop_it_no_recycle:
5141 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005142 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 goto next_pkt;
5144 }
5145
Matt Carlsonad829262008-11-21 17:16:16 -08005146 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5147 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
Matt Carlsond2757fc2010-04-12 06:58:27 +00005149 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 int skb_size;
5151
Matt Carlson86b21e52009-11-13 13:03:45 +00005152 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005153 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 if (skb_size < 0)
5155 goto drop_it;
5156
Matt Carlson287be122009-08-28 13:58:46 +00005157 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 PCI_DMA_FROMDEVICE);
5159
Matt Carlson61e800c2010-02-17 15:16:54 +00005160 /* Ensure that the update to the skb happens
5161 * after the usage of the old DMA mapping.
5162 */
5163 smp_wmb();
5164
5165 ri->skb = NULL;
5166
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 skb_put(skb, len);
5168 } else {
5169 struct sk_buff *copy_skb;
5170
Matt Carlsona3896162009-11-13 13:03:44 +00005171 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 desc_idx, *post_ptr);
5173
Matt Carlsonbf933c82011-01-25 15:58:49 +00005174 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00005175 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 if (copy_skb == NULL)
5177 goto drop_it_no_recycle;
5178
Matt Carlsonbf933c82011-01-25 15:58:49 +00005179 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 skb_put(copy_skb, len);
5181 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03005182 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
5184
5185 /* We'll reuse the original ring buffer. */
5186 skb = copy_skb;
5187 }
5188
Michał Mirosławdc668912011-04-07 03:35:07 +00005189 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5191 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5192 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5193 skb->ip_summed = CHECKSUM_UNNECESSARY;
5194 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005195 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196
5197 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005198
5199 if (len > (tp->dev->mtu + ETH_HLEN) &&
5200 skb->protocol != htons(ETH_P_8021Q)) {
5201 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005202 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005203 }
5204
Matt Carlson9dc7a112010-04-12 06:58:28 +00005205 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005206 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5207 __vlan_hwaccel_put_tag(skb,
5208 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005209
Matt Carlsonbf933c82011-01-25 15:58:49 +00005210 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 received++;
5213 budget--;
5214
5215next_pkt:
5216 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005217
5218 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005219 tpr->rx_std_prod_idx = std_prod_idx &
5220 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005221 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5222 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005223 work_mask &= ~RXD_OPAQUE_RING_STD;
5224 rx_std_posted = 0;
5225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005227 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005228 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005229
5230 /* Refresh hw_idx to see if there is new work */
5231 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005232 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005233 rmb();
5234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 }
5236
5237 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005238 tnapi->rx_rcb_ptr = sw_idx;
5239 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240
5241 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005242 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005243 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005244 tpr->rx_std_prod_idx = std_prod_idx &
5245 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005246 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5247 tpr->rx_std_prod_idx);
5248 }
5249 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005250 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5251 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005252 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5253 tpr->rx_jmb_prod_idx);
5254 }
5255 mmiowb();
5256 } else if (work_mask) {
5257 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5258 * updated before the producer indices can be updated.
5259 */
5260 smp_wmb();
5261
Matt Carlson2c49a442010-09-30 10:34:35 +00005262 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5263 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005264
Matt Carlsone4af1af2010-02-12 14:47:05 +00005265 if (tnapi != &tp->napi[1])
5266 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
5269 return received;
5270}
5271
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005272static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005275 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005276 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5277
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 if (sblk->status & SD_STATUS_LINK_CHG) {
5279 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005280 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005281 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005282 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005283 tw32_f(MAC_STATUS,
5284 (MAC_STATUS_SYNC_CHANGED |
5285 MAC_STATUS_CFG_CHANGED |
5286 MAC_STATUS_MI_COMPLETION |
5287 MAC_STATUS_LNKSTATE_CHANGED));
5288 udelay(40);
5289 } else
5290 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005291 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 }
5293 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005294}
5295
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005296static int tg3_rx_prodring_xfer(struct tg3 *tp,
5297 struct tg3_rx_prodring_set *dpr,
5298 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005299{
5300 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005301 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005302
5303 while (1) {
5304 src_prod_idx = spr->rx_std_prod_idx;
5305
5306 /* Make sure updates to the rx_std_buffers[] entries and the
5307 * standard producer index are seen in the correct order.
5308 */
5309 smp_rmb();
5310
5311 if (spr->rx_std_cons_idx == src_prod_idx)
5312 break;
5313
5314 if (spr->rx_std_cons_idx < src_prod_idx)
5315 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5316 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005317 cpycnt = tp->rx_std_ring_mask + 1 -
5318 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005319
Matt Carlson2c49a442010-09-30 10:34:35 +00005320 cpycnt = min(cpycnt,
5321 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005322
5323 si = spr->rx_std_cons_idx;
5324 di = dpr->rx_std_prod_idx;
5325
Matt Carlsone92967b2010-02-12 14:47:06 +00005326 for (i = di; i < di + cpycnt; i++) {
5327 if (dpr->rx_std_buffers[i].skb) {
5328 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005329 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005330 break;
5331 }
5332 }
5333
5334 if (!cpycnt)
5335 break;
5336
5337 /* Ensure that updates to the rx_std_buffers ring and the
5338 * shadowed hardware producer ring from tg3_recycle_skb() are
5339 * ordered correctly WRT the skb check above.
5340 */
5341 smp_rmb();
5342
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005343 memcpy(&dpr->rx_std_buffers[di],
5344 &spr->rx_std_buffers[si],
5345 cpycnt * sizeof(struct ring_info));
5346
5347 for (i = 0; i < cpycnt; i++, di++, si++) {
5348 struct tg3_rx_buffer_desc *sbd, *dbd;
5349 sbd = &spr->rx_std[si];
5350 dbd = &dpr->rx_std[di];
5351 dbd->addr_hi = sbd->addr_hi;
5352 dbd->addr_lo = sbd->addr_lo;
5353 }
5354
Matt Carlson2c49a442010-09-30 10:34:35 +00005355 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5356 tp->rx_std_ring_mask;
5357 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5358 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005359 }
5360
5361 while (1) {
5362 src_prod_idx = spr->rx_jmb_prod_idx;
5363
5364 /* Make sure updates to the rx_jmb_buffers[] entries and
5365 * the jumbo producer index are seen in the correct order.
5366 */
5367 smp_rmb();
5368
5369 if (spr->rx_jmb_cons_idx == src_prod_idx)
5370 break;
5371
5372 if (spr->rx_jmb_cons_idx < src_prod_idx)
5373 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5374 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005375 cpycnt = tp->rx_jmb_ring_mask + 1 -
5376 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005377
5378 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005379 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005380
5381 si = spr->rx_jmb_cons_idx;
5382 di = dpr->rx_jmb_prod_idx;
5383
Matt Carlsone92967b2010-02-12 14:47:06 +00005384 for (i = di; i < di + cpycnt; i++) {
5385 if (dpr->rx_jmb_buffers[i].skb) {
5386 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005387 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005388 break;
5389 }
5390 }
5391
5392 if (!cpycnt)
5393 break;
5394
5395 /* Ensure that updates to the rx_jmb_buffers ring and the
5396 * shadowed hardware producer ring from tg3_recycle_skb() are
5397 * ordered correctly WRT the skb check above.
5398 */
5399 smp_rmb();
5400
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005401 memcpy(&dpr->rx_jmb_buffers[di],
5402 &spr->rx_jmb_buffers[si],
5403 cpycnt * sizeof(struct ring_info));
5404
5405 for (i = 0; i < cpycnt; i++, di++, si++) {
5406 struct tg3_rx_buffer_desc *sbd, *dbd;
5407 sbd = &spr->rx_jmb[si].std;
5408 dbd = &dpr->rx_jmb[di].std;
5409 dbd->addr_hi = sbd->addr_hi;
5410 dbd->addr_lo = sbd->addr_lo;
5411 }
5412
Matt Carlson2c49a442010-09-30 10:34:35 +00005413 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5414 tp->rx_jmb_ring_mask;
5415 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5416 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005417 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005418
5419 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005420}
5421
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005422static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5423{
5424 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
5426 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005427 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005428 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005429 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005430 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 }
5432
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 /* run RX thread, within the bounds set by NAPI.
5434 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005435 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005437 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005438 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439
Joe Perches63c3a662011-04-26 08:12:10 +00005440 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005441 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005442 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005443 u32 std_prod_idx = dpr->rx_std_prod_idx;
5444 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005445
Matt Carlsone4af1af2010-02-12 14:47:05 +00005446 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005447 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005448 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005449
5450 wmb();
5451
Matt Carlsone4af1af2010-02-12 14:47:05 +00005452 if (std_prod_idx != dpr->rx_std_prod_idx)
5453 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5454 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005455
Matt Carlsone4af1af2010-02-12 14:47:05 +00005456 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5457 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5458 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005459
5460 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005461
5462 if (err)
5463 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005464 }
5465
David S. Miller6f535762007-10-11 18:08:29 -07005466 return work_done;
5467}
David S. Millerf7383c22005-05-18 22:50:53 -07005468
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005469static int tg3_poll_msix(struct napi_struct *napi, int budget)
5470{
5471 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5472 struct tg3 *tp = tnapi->tp;
5473 int work_done = 0;
5474 struct tg3_hw_status *sblk = tnapi->hw_status;
5475
5476 while (1) {
5477 work_done = tg3_poll_work(tnapi, work_done, budget);
5478
Joe Perches63c3a662011-04-26 08:12:10 +00005479 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005480 goto tx_recovery;
5481
5482 if (unlikely(work_done >= budget))
5483 break;
5484
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005485 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005486 * to tell the hw how much work has been processed,
5487 * so we must read it before checking for more work.
5488 */
5489 tnapi->last_tag = sblk->status_tag;
5490 tnapi->last_irq_tag = tnapi->last_tag;
5491 rmb();
5492
5493 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005494 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5495 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005496 napi_complete(napi);
5497 /* Reenable interrupts. */
5498 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5499 mmiowb();
5500 break;
5501 }
5502 }
5503
5504 return work_done;
5505
5506tx_recovery:
5507 /* work_done is guaranteed to be less than budget. */
5508 napi_complete(napi);
5509 schedule_work(&tp->reset_task);
5510 return work_done;
5511}
5512
Matt Carlsone64de4e2011-04-13 11:05:05 +00005513static void tg3_process_error(struct tg3 *tp)
5514{
5515 u32 val;
5516 bool real_error = false;
5517
Joe Perches63c3a662011-04-26 08:12:10 +00005518 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005519 return;
5520
5521 /* Check Flow Attention register */
5522 val = tr32(HOSTCC_FLOW_ATTN);
5523 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5524 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5525 real_error = true;
5526 }
5527
5528 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5529 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5530 real_error = true;
5531 }
5532
5533 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5534 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5535 real_error = true;
5536 }
5537
5538 if (!real_error)
5539 return;
5540
5541 tg3_dump_state(tp);
5542
Joe Perches63c3a662011-04-26 08:12:10 +00005543 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005544 schedule_work(&tp->reset_task);
5545}
5546
David S. Miller6f535762007-10-11 18:08:29 -07005547static int tg3_poll(struct napi_struct *napi, int budget)
5548{
Matt Carlson8ef04422009-08-28 14:01:37 +00005549 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5550 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005551 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005552 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005553
5554 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005555 if (sblk->status & SD_STATUS_ERROR)
5556 tg3_process_error(tp);
5557
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005558 tg3_poll_link(tp);
5559
Matt Carlson17375d22009-08-28 14:02:18 +00005560 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005561
Joe Perches63c3a662011-04-26 08:12:10 +00005562 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07005563 goto tx_recovery;
5564
5565 if (unlikely(work_done >= budget))
5566 break;
5567
Joe Perches63c3a662011-04-26 08:12:10 +00005568 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00005569 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005570 * to tell the hw how much work has been processed,
5571 * so we must read it before checking for more work.
5572 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005573 tnapi->last_tag = sblk->status_tag;
5574 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005575 rmb();
5576 } else
5577 sblk->status &= ~SD_STATUS_UPDATED;
5578
Matt Carlson17375d22009-08-28 14:02:18 +00005579 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005580 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005581 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005582 break;
5583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 }
5585
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005586 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005587
5588tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005589 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005590 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005591 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005592 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593}
5594
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005595static void tg3_napi_disable(struct tg3 *tp)
5596{
5597 int i;
5598
5599 for (i = tp->irq_cnt - 1; i >= 0; i--)
5600 napi_disable(&tp->napi[i].napi);
5601}
5602
5603static void tg3_napi_enable(struct tg3 *tp)
5604{
5605 int i;
5606
5607 for (i = 0; i < tp->irq_cnt; i++)
5608 napi_enable(&tp->napi[i].napi);
5609}
5610
5611static void tg3_napi_init(struct tg3 *tp)
5612{
5613 int i;
5614
5615 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5616 for (i = 1; i < tp->irq_cnt; i++)
5617 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5618}
5619
5620static void tg3_napi_fini(struct tg3 *tp)
5621{
5622 int i;
5623
5624 for (i = 0; i < tp->irq_cnt; i++)
5625 netif_napi_del(&tp->napi[i].napi);
5626}
5627
5628static inline void tg3_netif_stop(struct tg3 *tp)
5629{
5630 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5631 tg3_napi_disable(tp);
5632 netif_tx_disable(tp->dev);
5633}
5634
5635static inline void tg3_netif_start(struct tg3 *tp)
5636{
5637 /* NOTE: unconditional netif_tx_wake_all_queues is only
5638 * appropriate so long as all callers are assured to
5639 * have free tx slots (such as after tg3_init_hw)
5640 */
5641 netif_tx_wake_all_queues(tp->dev);
5642
5643 tg3_napi_enable(tp);
5644 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5645 tg3_enable_ints(tp);
5646}
5647
David S. Millerf47c11e2005-06-24 20:18:35 -07005648static void tg3_irq_quiesce(struct tg3 *tp)
5649{
Matt Carlson4f125f42009-09-01 12:55:02 +00005650 int i;
5651
David S. Millerf47c11e2005-06-24 20:18:35 -07005652 BUG_ON(tp->irq_sync);
5653
5654 tp->irq_sync = 1;
5655 smp_mb();
5656
Matt Carlson4f125f42009-09-01 12:55:02 +00005657 for (i = 0; i < tp->irq_cnt; i++)
5658 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005659}
5660
David S. Millerf47c11e2005-06-24 20:18:35 -07005661/* Fully shutdown all tg3 driver activity elsewhere in the system.
5662 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5663 * with as well. Most of the time, this is not necessary except when
5664 * shutting down the device.
5665 */
5666static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5667{
Michael Chan46966542007-07-11 19:47:19 -07005668 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005669 if (irq_sync)
5670 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005671}
5672
5673static inline void tg3_full_unlock(struct tg3 *tp)
5674{
David S. Millerf47c11e2005-06-24 20:18:35 -07005675 spin_unlock_bh(&tp->lock);
5676}
5677
Michael Chanfcfa0a32006-03-20 22:28:41 -08005678/* One-shot MSI handler - Chip automatically disables interrupt
5679 * after sending MSI so driver doesn't have to do it.
5680 */
David Howells7d12e782006-10-05 14:55:46 +01005681static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005682{
Matt Carlson09943a12009-08-28 14:01:57 +00005683 struct tg3_napi *tnapi = dev_id;
5684 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005685
Matt Carlson898a56f2009-08-28 14:02:40 +00005686 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005687 if (tnapi->rx_rcb)
5688 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005689
5690 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005691 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005692
5693 return IRQ_HANDLED;
5694}
5695
Michael Chan88b06bc2005-04-21 17:13:25 -07005696/* MSI ISR - No need to check for interrupt sharing and no need to
5697 * flush status block and interrupt mailbox. PCI ordering rules
5698 * guarantee that MSI will arrive after the status block.
5699 */
David Howells7d12e782006-10-05 14:55:46 +01005700static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07005701{
Matt Carlson09943a12009-08-28 14:01:57 +00005702 struct tg3_napi *tnapi = dev_id;
5703 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07005704
Matt Carlson898a56f2009-08-28 14:02:40 +00005705 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005706 if (tnapi->rx_rcb)
5707 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07005708 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005709 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07005710 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005711 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07005712 * NIC to stop sending us irqs, engaging "in-intr-handler"
5713 * event coalescing.
5714 */
5715 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005716 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005717 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005718
Michael Chan88b06bc2005-04-21 17:13:25 -07005719 return IRQ_RETVAL(1);
5720}
5721
David Howells7d12e782006-10-05 14:55:46 +01005722static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723{
Matt Carlson09943a12009-08-28 14:01:57 +00005724 struct tg3_napi *tnapi = dev_id;
5725 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005726 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 unsigned int handled = 1;
5728
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 /* In INTx mode, it is possible for the interrupt to arrive at
5730 * the CPU before the status block posted prior to the interrupt.
5731 * Reading the PCI State register will confirm whether the
5732 * interrupt is ours and will flush the status block.
5733 */
Michael Chand18edcb2007-03-24 20:57:11 -07005734 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00005735 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005736 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5737 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005738 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005739 }
Michael Chand18edcb2007-03-24 20:57:11 -07005740 }
5741
5742 /*
5743 * Writing any value to intr-mbox-0 clears PCI INTA# and
5744 * chip-internal interrupt pending events.
5745 * Writing non-zero to intr-mbox-0 additional tells the
5746 * NIC to stop sending us irqs, engaging "in-intr-handler"
5747 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005748 *
5749 * Flush the mailbox to de-assert the IRQ immediately to prevent
5750 * spurious interrupts. The flush impacts performance but
5751 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005752 */
Michael Chanc04cb342007-05-07 00:26:15 -07005753 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005754 if (tg3_irq_sync(tp))
5755 goto out;
5756 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005757 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005758 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005759 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005760 } else {
5761 /* No work, shared interrupt perhaps? re-enable
5762 * interrupts, and flush that PCI write
5763 */
5764 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5765 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005766 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005767out:
David S. Millerfac9b832005-05-18 22:46:34 -07005768 return IRQ_RETVAL(handled);
5769}
5770
David Howells7d12e782006-10-05 14:55:46 +01005771static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005772{
Matt Carlson09943a12009-08-28 14:01:57 +00005773 struct tg3_napi *tnapi = dev_id;
5774 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005775 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005776 unsigned int handled = 1;
5777
David S. Millerfac9b832005-05-18 22:46:34 -07005778 /* In INTx mode, it is possible for the interrupt to arrive at
5779 * the CPU before the status block posted prior to the interrupt.
5780 * Reading the PCI State register will confirm whether the
5781 * interrupt is ours and will flush the status block.
5782 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005783 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00005784 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005785 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5786 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005787 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 }
Michael Chand18edcb2007-03-24 20:57:11 -07005789 }
5790
5791 /*
5792 * writing any value to intr-mbox-0 clears PCI INTA# and
5793 * chip-internal interrupt pending events.
5794 * writing non-zero to intr-mbox-0 additional tells the
5795 * NIC to stop sending us irqs, engaging "in-intr-handler"
5796 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005797 *
5798 * Flush the mailbox to de-assert the IRQ immediately to prevent
5799 * spurious interrupts. The flush impacts performance but
5800 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005801 */
Michael Chanc04cb342007-05-07 00:26:15 -07005802 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005803
5804 /*
5805 * In a shared interrupt configuration, sometimes other devices'
5806 * interrupts will scream. We record the current status tag here
5807 * so that the above check can report that the screaming interrupts
5808 * are unhandled. Eventually they will be silenced.
5809 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005810 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005811
Michael Chand18edcb2007-03-24 20:57:11 -07005812 if (tg3_irq_sync(tp))
5813 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005814
Matt Carlson72334482009-08-28 14:03:01 +00005815 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005816
Matt Carlson09943a12009-08-28 14:01:57 +00005817 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005818
David S. Millerf47c11e2005-06-24 20:18:35 -07005819out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 return IRQ_RETVAL(handled);
5821}
5822
Michael Chan79381092005-04-21 17:13:59 -07005823/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005824static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005825{
Matt Carlson09943a12009-08-28 14:01:57 +00005826 struct tg3_napi *tnapi = dev_id;
5827 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005828 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005829
Michael Chanf9804dd2005-09-27 12:13:10 -07005830 if ((sblk->status & SD_STATUS_UPDATED) ||
5831 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005832 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005833 return IRQ_RETVAL(1);
5834 }
5835 return IRQ_RETVAL(0);
5836}
5837
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005838static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005839static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840
Michael Chanb9ec6c12006-07-25 16:37:27 -07005841/* Restart hardware after configuration changes, self-test, etc.
5842 * Invoked with tp->lock held.
5843 */
5844static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005845 __releases(tp->lock)
5846 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005847{
5848 int err;
5849
5850 err = tg3_init_hw(tp, reset_phy);
5851 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005852 netdev_err(tp->dev,
5853 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005854 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5855 tg3_full_unlock(tp);
5856 del_timer_sync(&tp->timer);
5857 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005858 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005859 dev_close(tp->dev);
5860 tg3_full_lock(tp, 0);
5861 }
5862 return err;
5863}
5864
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865#ifdef CONFIG_NET_POLL_CONTROLLER
5866static void tg3_poll_controller(struct net_device *dev)
5867{
Matt Carlson4f125f42009-09-01 12:55:02 +00005868 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07005869 struct tg3 *tp = netdev_priv(dev);
5870
Matt Carlson4f125f42009-09-01 12:55:02 +00005871 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005872 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873}
5874#endif
5875
David Howellsc4028952006-11-22 14:57:56 +00005876static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877{
David Howellsc4028952006-11-22 14:57:56 +00005878 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005879 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 unsigned int restart_timer;
5881
Michael Chan7faa0062006-02-02 17:29:28 -08005882 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005883
5884 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005885 tg3_full_unlock(tp);
5886 return;
5887 }
5888
5889 tg3_full_unlock(tp);
5890
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005891 tg3_phy_stop(tp);
5892
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 tg3_netif_stop(tp);
5894
David S. Millerf47c11e2005-06-24 20:18:35 -07005895 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
Joe Perches63c3a662011-04-26 08:12:10 +00005897 restart_timer = tg3_flag(tp, RESTART_TIMER);
5898 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
Joe Perches63c3a662011-04-26 08:12:10 +00005900 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07005901 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5902 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00005903 tg3_flag_set(tp, MBOX_WRITE_REORDER);
5904 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005905 }
5906
Michael Chan944d9802005-05-29 14:57:48 -07005907 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005908 err = tg3_init_hw(tp, 1);
5909 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005910 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911
5912 tg3_netif_start(tp);
5913
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 if (restart_timer)
5915 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005916
Michael Chanb9ec6c12006-07-25 16:37:27 -07005917out:
Michael Chan7faa0062006-02-02 17:29:28 -08005918 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005919
5920 if (!err)
5921 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922}
5923
5924static void tg3_tx_timeout(struct net_device *dev)
5925{
5926 struct tg3 *tp = netdev_priv(dev);
5927
Michael Chanb0408752007-02-13 12:18:30 -08005928 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005929 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005930 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932
5933 schedule_work(&tp->reset_task);
5934}
5935
Michael Chanc58ec932005-09-17 00:46:27 -07005936/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5937static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5938{
5939 u32 base = (u32) mapping & 0xffffffff;
5940
Eric Dumazet807540b2010-09-23 05:40:09 +00005941 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005942}
5943
Michael Chan72f2afb2006-03-06 19:28:35 -08005944/* Test for DMA addresses > 40-bit */
5945static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5946 int len)
5947{
5948#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00005949 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00005950 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005951 return 0;
5952#else
5953 return 0;
5954#endif
5955}
5956
Matt Carlsond1a3b732011-07-27 14:20:51 +00005957static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00005958 dma_addr_t mapping, u32 len, u32 flags,
5959 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00005960{
Matt Carlson92cd3a12011-07-27 14:20:47 +00005961 txbd->addr_hi = ((u64) mapping >> 32);
5962 txbd->addr_lo = ((u64) mapping & 0xffffffff);
5963 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
5964 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00005965}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966
Matt Carlson84b67b22011-07-27 14:20:52 +00005967static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00005968 dma_addr_t map, u32 len, u32 flags,
5969 u32 mss, u32 vlan)
5970{
5971 struct tg3 *tp = tnapi->tp;
5972 bool hwbug = false;
5973
5974 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
5975 hwbug = 1;
5976
5977 if (tg3_4g_overflow_test(map, len))
5978 hwbug = 1;
5979
5980 if (tg3_40bit_overflow_test(tp, map, len))
5981 hwbug = 1;
5982
Matt Carlsone31aa982011-07-27 14:20:53 +00005983 if (tg3_flag(tp, 4K_FIFO_LIMIT)) {
5984 u32 tmp_flag = flags & ~TXD_FLAG_END;
5985 while (len > TG3_TX_BD_DMA_MAX) {
5986 u32 frag_len = TG3_TX_BD_DMA_MAX;
5987 len -= TG3_TX_BD_DMA_MAX;
5988
5989 if (len) {
5990 tnapi->tx_buffers[*entry].fragmented = true;
5991 /* Avoid the 8byte DMA problem */
5992 if (len <= 8) {
5993 len += TG3_TX_BD_DMA_MAX / 2;
5994 frag_len = TG3_TX_BD_DMA_MAX / 2;
5995 }
5996 } else
5997 tmp_flag = flags;
5998
5999 if (*budget) {
6000 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6001 frag_len, tmp_flag, mss, vlan);
6002 (*budget)--;
6003 *entry = NEXT_TX(*entry);
6004 } else {
6005 hwbug = 1;
6006 break;
6007 }
6008
6009 map += frag_len;
6010 }
6011
6012 if (len) {
6013 if (*budget) {
6014 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6015 len, flags, mss, vlan);
6016 (*budget)--;
6017 *entry = NEXT_TX(*entry);
6018 } else {
6019 hwbug = 1;
6020 }
6021 }
6022 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006023 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6024 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006025 *entry = NEXT_TX(*entry);
6026 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006027
6028 return hwbug;
6029}
6030
Matt Carlson0d681b22011-07-27 14:20:49 +00006031static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006032{
6033 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006034 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006035 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006036
Matt Carlson0d681b22011-07-27 14:20:49 +00006037 skb = txb->skb;
6038 txb->skb = NULL;
6039
Matt Carlson432aa7e2011-05-19 12:12:45 +00006040 pci_unmap_single(tnapi->tp->pdev,
6041 dma_unmap_addr(txb, mapping),
6042 skb_headlen(skb),
6043 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006044
6045 while (txb->fragmented) {
6046 txb->fragmented = false;
6047 entry = NEXT_TX(entry);
6048 txb = &tnapi->tx_buffers[entry];
6049 }
6050
Matt Carlson9a2e0fb2011-06-02 13:01:39 +00006051 for (i = 0; i < last; i++) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00006052 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6053
6054 entry = NEXT_TX(entry);
6055 txb = &tnapi->tx_buffers[entry];
6056
6057 pci_unmap_page(tnapi->tp->pdev,
6058 dma_unmap_addr(txb, mapping),
6059 frag->size, PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006060
6061 while (txb->fragmented) {
6062 txb->fragmented = false;
6063 entry = NEXT_TX(entry);
6064 txb = &tnapi->tx_buffers[entry];
6065 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006066 }
6067}
6068
Michael Chan72f2afb2006-03-06 19:28:35 -08006069/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006070static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
Matt Carlson432aa7e2011-05-19 12:12:45 +00006071 struct sk_buff *skb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006072 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006073 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006075 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07006076 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006077 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006078 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
Matt Carlson41588ba2008-04-19 18:12:33 -07006080 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6081 new_skb = skb_copy(skb, GFP_ATOMIC);
6082 else {
6083 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6084
6085 new_skb = skb_copy_expand(skb,
6086 skb_headroom(skb) + more_headroom,
6087 skb_tailroom(skb), GFP_ATOMIC);
6088 }
6089
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006091 ret = -1;
6092 } else {
6093 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006094 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6095 PCI_DMA_TODEVICE);
6096 /* Make sure the mapping succeeded */
6097 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006098 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006099 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006100 } else {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006101 base_flags |= TXD_FLAG_END;
6102
Matt Carlson84b67b22011-07-27 14:20:52 +00006103 tnapi->tx_buffers[*entry].skb = new_skb;
6104 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006105 mapping, new_addr);
6106
Matt Carlson84b67b22011-07-27 14:20:52 +00006107 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006108 new_skb->len, base_flags,
6109 mss, vlan)) {
Matt Carlson84b67b22011-07-27 14:20:52 +00006110 tg3_tx_skb_unmap(tnapi, *entry, 0);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006111 dev_kfree_skb(new_skb);
6112 ret = -1;
6113 }
Michael Chanc58ec932005-09-17 00:46:27 -07006114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 }
6116
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 dev_kfree_skb(skb);
6118
Michael Chanc58ec932005-09-17 00:46:27 -07006119 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120}
6121
Matt Carlson2ffcc982011-05-19 12:12:44 +00006122static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006123
6124/* Use GSO to workaround a rare TSO bug that may be triggered when the
6125 * TSO header is greater than 80 bytes.
6126 */
6127static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6128{
6129 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006130 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006131
6132 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006133 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006134 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006135
6136 /* netif_tx_stop_queue() must be done before checking
6137 * checking tx index in tg3_tx_avail() below, because in
6138 * tg3_tx(), we update tx index before checking for
6139 * netif_tx_queue_stopped().
6140 */
6141 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006142 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006143 return NETDEV_TX_BUSY;
6144
6145 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006146 }
6147
6148 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006149 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006150 goto tg3_tso_bug_end;
6151
6152 do {
6153 nskb = segs;
6154 segs = segs->next;
6155 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006156 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006157 } while (segs);
6158
6159tg3_tso_bug_end:
6160 dev_kfree_skb(skb);
6161
6162 return NETDEV_TX_OK;
6163}
Michael Chan52c0fd82006-06-29 20:15:54 -07006164
Michael Chan5a6f3072006-03-20 22:28:05 -08006165/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006166 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006167 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006168static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006169{
6170 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006171 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006172 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006173 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006174 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006175 struct tg3_napi *tnapi;
6176 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006177 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006178
Matt Carlson24f4efd2009-11-13 13:03:35 +00006179 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6180 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006181 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006182 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183
Matt Carlson84b67b22011-07-27 14:20:52 +00006184 budget = tg3_tx_avail(tnapi);
6185
Michael Chan00b70502006-06-17 21:58:45 -07006186 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006187 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006188 * interrupt. Furthermore, IRQ processing runs lockless so we have
6189 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006191 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006192 if (!netif_tx_queue_stopped(txq)) {
6193 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006194
6195 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006196 netdev_err(dev,
6197 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 return NETDEV_TX_BUSY;
6200 }
6201
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006202 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006204 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006206
Matt Carlsonbe98da62010-07-11 09:31:46 +00006207 mss = skb_shinfo(skb)->gso_size;
6208 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006209 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006210 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211
6212 if (skb_header_cloned(skb) &&
6213 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
6214 dev_kfree_skb(skb);
6215 goto out_unlock;
6216 }
6217
Matt Carlson34195c32010-07-11 09:31:42 +00006218 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006219 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
Matt Carlson02e96082010-09-15 08:59:59 +00006221 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006222 hdr_len = skb_headlen(skb) - ETH_HLEN;
6223 } else {
6224 u32 ip_tcp_len;
6225
6226 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6227 hdr_len = ip_tcp_len + tcp_opt_len;
6228
6229 iph->check = 0;
6230 iph->tot_len = htons(mss + hdr_len);
6231 }
6232
Michael Chan52c0fd82006-06-29 20:15:54 -07006233 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006234 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006235 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006236
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6238 TXD_FLAG_CPU_POST_DMA);
6239
Joe Perches63c3a662011-04-26 08:12:10 +00006240 if (tg3_flag(tp, HW_TSO_1) ||
6241 tg3_flag(tp, HW_TSO_2) ||
6242 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006243 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006245 } else
6246 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6247 iph->daddr, 0,
6248 IPPROTO_TCP,
6249 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250
Joe Perches63c3a662011-04-26 08:12:10 +00006251 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006252 mss |= (hdr_len & 0xc) << 12;
6253 if (hdr_len & 0x10)
6254 base_flags |= 0x00000010;
6255 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006256 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006257 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006258 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006259 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006260 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 int tsflags;
6262
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006263 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 mss |= (tsflags << 11);
6265 }
6266 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006267 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 int tsflags;
6269
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006270 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 base_flags |= tsflags << 12;
6272 }
6273 }
6274 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006275
Matt Carlson92cd3a12011-07-27 14:20:47 +00006276#ifdef BCM_KERNEL_SUPPORTS_8021Q
6277 if (vlan_tx_tag_present(skb)) {
6278 base_flags |= TXD_FLAG_VLAN;
6279 vlan = vlan_tx_tag_get(skb);
6280 }
6281#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282
Joe Perches63c3a662011-04-26 08:12:10 +00006283 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006284 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006285 base_flags |= TXD_FLAG_JMB_PKT;
6286
Alexander Duyckf4188d82009-12-02 16:48:38 +00006287 len = skb_headlen(skb);
6288
6289 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6290 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006291 dev_kfree_skb(skb);
6292 goto out_unlock;
6293 }
6294
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006295 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006296 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
6298 would_hit_hwbug = 0;
6299
Joe Perches63c3a662011-04-26 08:12:10 +00006300 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006301 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302
Matt Carlson84b67b22011-07-27 14:20:52 +00006303 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006304 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
6305 mss, vlan))
6306 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 /* Now loop through additional data fragments, and queue them. */
6309 if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006310 u32 tmp_mss = mss;
6311
6312 if (!tg3_flag(tp, HW_TSO_1) &&
6313 !tg3_flag(tp, HW_TSO_2) &&
6314 !tg3_flag(tp, HW_TSO_3))
6315 tmp_mss = 0;
6316
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 last = skb_shinfo(skb)->nr_frags - 1;
6318 for (i = 0; i <= last; i++) {
6319 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6320
6321 len = frag->size;
Ian Campbelldc234d02011-08-24 22:28:11 +00006322 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
6323 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006325 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006326 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006327 mapping);
6328 if (pci_dma_mapping_error(tp->pdev, mapping))
6329 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330
Matt Carlson84b67b22011-07-27 14:20:52 +00006331 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
6332 len, base_flags |
6333 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsond1a3b732011-07-27 14:20:51 +00006334 tmp_mss, vlan))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006335 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 }
6337 }
6338
6339 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006340 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341
6342 /* If the workaround fails due to memory/mapping
6343 * failure, silently drop this packet.
6344 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006345 entry = tnapi->tx_prod;
6346 budget = tg3_tx_avail(tnapi);
6347 if (tigon3_dma_hwbug_workaround(tnapi, skb, &entry, &budget,
6348 base_flags, mss, vlan))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 }
6351
Richard Cochrand515b452011-06-19 03:31:41 +00006352 skb_tx_timestamp(skb);
6353
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006355 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006357 tnapi->tx_prod = entry;
6358 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006359 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006360
6361 /* netif_tx_stop_queue() must be done before checking
6362 * checking tx index in tg3_tx_avail() below, because in
6363 * tg3_tx(), we update tx index before checking for
6364 * netif_tx_queue_stopped().
6365 */
6366 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006367 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006368 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370
6371out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006372 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373
6374 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006375
6376dma_error:
Matt Carlson0d681b22011-07-27 14:20:49 +00006377 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Alexander Duyckf4188d82009-12-02 16:48:38 +00006378 dev_kfree_skb(skb);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006379 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006380 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381}
6382
Matt Carlson6e01b202011-08-19 13:58:20 +00006383static void tg3_mac_loopback(struct tg3 *tp, bool enable)
6384{
6385 if (enable) {
6386 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
6387 MAC_MODE_PORT_MODE_MASK);
6388
6389 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6390
6391 if (!tg3_flag(tp, 5705_PLUS))
6392 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
6393
6394 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
6395 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
6396 else
6397 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
6398 } else {
6399 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6400
6401 if (tg3_flag(tp, 5705_PLUS) ||
6402 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
6403 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
6404 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
6405 }
6406
6407 tw32(MAC_MODE, tp->mac_mode);
6408 udelay(40);
6409}
6410
Matt Carlson941ec902011-08-19 13:58:23 +00006411static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006412{
Matt Carlson941ec902011-08-19 13:58:23 +00006413 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006414
6415 tg3_phy_toggle_apd(tp, false);
6416 tg3_phy_toggle_automdix(tp, 0);
6417
Matt Carlson941ec902011-08-19 13:58:23 +00006418 if (extlpbk && tg3_phy_set_extloopbk(tp))
6419 return -EIO;
6420
6421 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006422 switch (speed) {
6423 case SPEED_10:
6424 break;
6425 case SPEED_100:
6426 bmcr |= BMCR_SPEED100;
6427 break;
6428 case SPEED_1000:
6429 default:
6430 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
6431 speed = SPEED_100;
6432 bmcr |= BMCR_SPEED100;
6433 } else {
6434 speed = SPEED_1000;
6435 bmcr |= BMCR_SPEED1000;
6436 }
6437 }
6438
Matt Carlson941ec902011-08-19 13:58:23 +00006439 if (extlpbk) {
6440 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
6441 tg3_readphy(tp, MII_CTRL1000, &val);
6442 val |= CTL1000_AS_MASTER |
6443 CTL1000_ENABLE_MASTER;
6444 tg3_writephy(tp, MII_CTRL1000, val);
6445 } else {
6446 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
6447 MII_TG3_FET_PTEST_TRIM_2;
6448 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
6449 }
6450 } else
6451 bmcr |= BMCR_LOOPBACK;
6452
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006453 tg3_writephy(tp, MII_BMCR, bmcr);
6454
6455 /* The write needs to be flushed for the FETs */
6456 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
6457 tg3_readphy(tp, MII_BMCR, &bmcr);
6458
6459 udelay(40);
6460
6461 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
6462 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00006463 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006464 MII_TG3_FET_PTEST_FRC_TX_LINK |
6465 MII_TG3_FET_PTEST_FRC_TX_LOCK);
6466
6467 /* The write needs to be flushed for the AC131 */
6468 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
6469 }
6470
6471 /* Reset to prevent losing 1st rx packet intermittently */
6472 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
6473 tg3_flag(tp, 5780_CLASS)) {
6474 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
6475 udelay(10);
6476 tw32_f(MAC_RX_MODE, tp->rx_mode);
6477 }
6478
6479 mac_mode = tp->mac_mode &
6480 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
6481 if (speed == SPEED_1000)
6482 mac_mode |= MAC_MODE_PORT_MODE_GMII;
6483 else
6484 mac_mode |= MAC_MODE_PORT_MODE_MII;
6485
6486 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
6487 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
6488
6489 if (masked_phy_id == TG3_PHY_ID_BCM5401)
6490 mac_mode &= ~MAC_MODE_LINK_POLARITY;
6491 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
6492 mac_mode |= MAC_MODE_LINK_POLARITY;
6493
6494 tg3_writephy(tp, MII_TG3_EXT_CTRL,
6495 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
6496 }
6497
6498 tw32(MAC_MODE, mac_mode);
6499 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00006500
6501 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006502}
6503
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006504static void tg3_set_loopback(struct net_device *dev, u32 features)
6505{
6506 struct tg3 *tp = netdev_priv(dev);
6507
6508 if (features & NETIF_F_LOOPBACK) {
6509 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6510 return;
6511
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006512 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006513 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006514 netif_carrier_on(tp->dev);
6515 spin_unlock_bh(&tp->lock);
6516 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6517 } else {
6518 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6519 return;
6520
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006521 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006522 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006523 /* Force link status check */
6524 tg3_setup_phy(tp, 1);
6525 spin_unlock_bh(&tp->lock);
6526 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6527 }
6528}
6529
Michał Mirosławdc668912011-04-07 03:35:07 +00006530static u32 tg3_fix_features(struct net_device *dev, u32 features)
6531{
6532 struct tg3 *tp = netdev_priv(dev);
6533
Joe Perches63c3a662011-04-26 08:12:10 +00006534 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006535 features &= ~NETIF_F_ALL_TSO;
6536
6537 return features;
6538}
6539
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006540static int tg3_set_features(struct net_device *dev, u32 features)
6541{
6542 u32 changed = dev->features ^ features;
6543
6544 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6545 tg3_set_loopback(dev, features);
6546
6547 return 0;
6548}
6549
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6551 int new_mtu)
6552{
6553 dev->mtu = new_mtu;
6554
Michael Chanef7f5ec2005-07-25 12:32:25 -07006555 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006556 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006557 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006558 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006559 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006560 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006561 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006562 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006563 if (tg3_flag(tp, 5780_CLASS)) {
6564 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00006565 netdev_update_features(dev);
6566 }
Joe Perches63c3a662011-04-26 08:12:10 +00006567 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569}
6570
6571static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6572{
6573 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006574 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575
6576 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6577 return -EINVAL;
6578
6579 if (!netif_running(dev)) {
6580 /* We'll just catch it later when the
6581 * device is up'd.
6582 */
6583 tg3_set_mtu(dev, tp, new_mtu);
6584 return 0;
6585 }
6586
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006587 tg3_phy_stop(tp);
6588
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006590
6591 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592
Michael Chan944d9802005-05-29 14:57:48 -07006593 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594
6595 tg3_set_mtu(dev, tp, new_mtu);
6596
Michael Chanb9ec6c12006-07-25 16:37:27 -07006597 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598
Michael Chanb9ec6c12006-07-25 16:37:27 -07006599 if (!err)
6600 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601
David S. Millerf47c11e2005-06-24 20:18:35 -07006602 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006604 if (!err)
6605 tg3_phy_start(tp);
6606
Michael Chanb9ec6c12006-07-25 16:37:27 -07006607 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608}
6609
Matt Carlson21f581a2009-08-28 14:00:25 +00006610static void tg3_rx_prodring_free(struct tg3 *tp,
6611 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 int i;
6614
Matt Carlson8fea32b2010-09-15 08:59:58 +00006615 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006616 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006617 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006618 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6619 tp->rx_pkt_map_sz);
6620
Joe Perches63c3a662011-04-26 08:12:10 +00006621 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006622 for (i = tpr->rx_jmb_cons_idx;
6623 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006624 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006625 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6626 TG3_RX_JMB_MAP_SZ);
6627 }
6628 }
6629
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006630 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632
Matt Carlson2c49a442010-09-30 10:34:35 +00006633 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006634 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6635 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636
Joe Perches63c3a662011-04-26 08:12:10 +00006637 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006638 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006639 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6640 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 }
6642}
6643
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006644/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 *
6646 * The chip has been shut down and the driver detached from
6647 * the networking, so no interrupts or new tx packets will
6648 * end up in the driver. tp->{tx,}lock are held and thus
6649 * we may not sleep.
6650 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006651static int tg3_rx_prodring_alloc(struct tg3 *tp,
6652 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653{
Matt Carlson287be122009-08-28 13:58:46 +00006654 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006656 tpr->rx_std_cons_idx = 0;
6657 tpr->rx_std_prod_idx = 0;
6658 tpr->rx_jmb_cons_idx = 0;
6659 tpr->rx_jmb_prod_idx = 0;
6660
Matt Carlson8fea32b2010-09-15 08:59:58 +00006661 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006662 memset(&tpr->rx_std_buffers[0], 0,
6663 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006664 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006665 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006666 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006667 goto done;
6668 }
6669
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006671 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672
Matt Carlson287be122009-08-28 13:58:46 +00006673 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00006674 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006675 tp->dev->mtu > ETH_DATA_LEN)
6676 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6677 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006678
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 /* Initialize invariants of the rings, we only set this
6680 * stuff once. This works because the card does not
6681 * write into the rx buffer posting rings.
6682 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006683 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 struct tg3_rx_buffer_desc *rxd;
6685
Matt Carlson21f581a2009-08-28 14:00:25 +00006686 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006687 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6689 rxd->opaque = (RXD_OPAQUE_RING_STD |
6690 (i << RXD_OPAQUE_INDEX_SHIFT));
6691 }
6692
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006693 /* Now allocate fresh SKBs for each rx ring. */
6694 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006695 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006696 netdev_warn(tp->dev,
6697 "Using a smaller RX standard ring. Only "
6698 "%d out of %d buffers were allocated "
6699 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006700 if (i == 0)
6701 goto initfail;
6702 tp->rx_pending = i;
6703 break;
6704 }
6705 }
6706
Joe Perches63c3a662011-04-26 08:12:10 +00006707 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006708 goto done;
6709
Matt Carlson2c49a442010-09-30 10:34:35 +00006710 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006711
Joe Perches63c3a662011-04-26 08:12:10 +00006712 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00006713 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714
Matt Carlson2c49a442010-09-30 10:34:35 +00006715 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006716 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717
Matt Carlson0d86df82010-02-17 15:17:00 +00006718 rxd = &tpr->rx_jmb[i].std;
6719 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6720 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6721 RXD_FLAG_JUMBO;
6722 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6723 (i << RXD_OPAQUE_INDEX_SHIFT));
6724 }
6725
6726 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6727 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006728 netdev_warn(tp->dev,
6729 "Using a smaller RX jumbo ring. Only %d "
6730 "out of %d buffers were allocated "
6731 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006732 if (i == 0)
6733 goto initfail;
6734 tp->rx_jumbo_pending = i;
6735 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 }
6737 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006738
6739done:
Michael Chan32d8c572006-07-25 16:38:29 -07006740 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006741
6742initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006743 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006744 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745}
6746
Matt Carlson21f581a2009-08-28 14:00:25 +00006747static void tg3_rx_prodring_fini(struct tg3 *tp,
6748 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749{
Matt Carlson21f581a2009-08-28 14:00:25 +00006750 kfree(tpr->rx_std_buffers);
6751 tpr->rx_std_buffers = NULL;
6752 kfree(tpr->rx_jmb_buffers);
6753 tpr->rx_jmb_buffers = NULL;
6754 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006755 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6756 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006757 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006759 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006760 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6761 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006762 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006764}
6765
Matt Carlson21f581a2009-08-28 14:00:25 +00006766static int tg3_rx_prodring_init(struct tg3 *tp,
6767 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006768{
Matt Carlson2c49a442010-09-30 10:34:35 +00006769 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6770 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006771 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006772 return -ENOMEM;
6773
Matt Carlson4bae65c2010-11-24 08:31:52 +00006774 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6775 TG3_RX_STD_RING_BYTES(tp),
6776 &tpr->rx_std_mapping,
6777 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006778 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006779 goto err_out;
6780
Joe Perches63c3a662011-04-26 08:12:10 +00006781 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006782 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006783 GFP_KERNEL);
6784 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006785 goto err_out;
6786
Matt Carlson4bae65c2010-11-24 08:31:52 +00006787 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6788 TG3_RX_JMB_RING_BYTES(tp),
6789 &tpr->rx_jmb_mapping,
6790 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006791 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006792 goto err_out;
6793 }
6794
6795 return 0;
6796
6797err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006798 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006799 return -ENOMEM;
6800}
6801
6802/* Free up pending packets in all rx/tx rings.
6803 *
6804 * The chip has been shut down and the driver detached from
6805 * the networking, so no interrupts or new tx packets will
6806 * end up in the driver. tp->{tx,}lock is not held and we are not
6807 * in an interrupt context and thus may sleep.
6808 */
6809static void tg3_free_rings(struct tg3 *tp)
6810{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006811 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006812
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006813 for (j = 0; j < tp->irq_cnt; j++) {
6814 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006815
Matt Carlson8fea32b2010-09-15 08:59:58 +00006816 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006817
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006818 if (!tnapi->tx_buffers)
6819 continue;
6820
Matt Carlson0d681b22011-07-27 14:20:49 +00006821 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
6822 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006823
Matt Carlson0d681b22011-07-27 14:20:49 +00006824 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006825 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006826
Matt Carlson0d681b22011-07-27 14:20:49 +00006827 tg3_tx_skb_unmap(tnapi, i, skb_shinfo(skb)->nr_frags);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006828
6829 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006830 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006831 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006832}
6833
6834/* Initialize tx/rx rings for packet processing.
6835 *
6836 * The chip has been shut down and the driver detached from
6837 * the networking, so no interrupts or new tx packets will
6838 * end up in the driver. tp->{tx,}lock are held and thus
6839 * we may not sleep.
6840 */
6841static int tg3_init_rings(struct tg3 *tp)
6842{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006843 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006844
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006845 /* Free up all the SKBs. */
6846 tg3_free_rings(tp);
6847
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006848 for (i = 0; i < tp->irq_cnt; i++) {
6849 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006850
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006851 tnapi->last_tag = 0;
6852 tnapi->last_irq_tag = 0;
6853 tnapi->hw_status->status = 0;
6854 tnapi->hw_status->status_tag = 0;
6855 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6856
6857 tnapi->tx_prod = 0;
6858 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006859 if (tnapi->tx_ring)
6860 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006861
6862 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006863 if (tnapi->rx_rcb)
6864 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006865
Matt Carlson8fea32b2010-09-15 08:59:58 +00006866 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006867 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006868 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006869 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006870 }
Matt Carlson72334482009-08-28 14:03:01 +00006871
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006872 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006873}
6874
6875/*
6876 * Must not be invoked with interrupt sources disabled and
6877 * the hardware shutdown down.
6878 */
6879static void tg3_free_consistent(struct tg3 *tp)
6880{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006881 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006882
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006883 for (i = 0; i < tp->irq_cnt; i++) {
6884 struct tg3_napi *tnapi = &tp->napi[i];
6885
6886 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006887 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006888 tnapi->tx_ring, tnapi->tx_desc_mapping);
6889 tnapi->tx_ring = NULL;
6890 }
6891
6892 kfree(tnapi->tx_buffers);
6893 tnapi->tx_buffers = NULL;
6894
6895 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006896 dma_free_coherent(&tp->pdev->dev,
6897 TG3_RX_RCB_RING_BYTES(tp),
6898 tnapi->rx_rcb,
6899 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006900 tnapi->rx_rcb = NULL;
6901 }
6902
Matt Carlson8fea32b2010-09-15 08:59:58 +00006903 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6904
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006905 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006906 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6907 tnapi->hw_status,
6908 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006909 tnapi->hw_status = NULL;
6910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006912
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006914 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6915 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 tp->hw_stats = NULL;
6917 }
6918}
6919
6920/*
6921 * Must not be invoked with interrupt sources disabled and
6922 * the hardware shutdown down. Can sleep.
6923 */
6924static int tg3_alloc_consistent(struct tg3 *tp)
6925{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006926 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006927
Matt Carlson4bae65c2010-11-24 08:31:52 +00006928 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6929 sizeof(struct tg3_hw_stats),
6930 &tp->stats_mapping,
6931 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932 if (!tp->hw_stats)
6933 goto err_out;
6934
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6936
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006937 for (i = 0; i < tp->irq_cnt; i++) {
6938 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006939 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006940
Matt Carlson4bae65c2010-11-24 08:31:52 +00006941 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6942 TG3_HW_STATUS_SIZE,
6943 &tnapi->status_mapping,
6944 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006945 if (!tnapi->hw_status)
6946 goto err_out;
6947
6948 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006949 sblk = tnapi->hw_status;
6950
Matt Carlson8fea32b2010-09-15 08:59:58 +00006951 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6952 goto err_out;
6953
Matt Carlson19cfaec2009-12-03 08:36:20 +00006954 /* If multivector TSS is enabled, vector 0 does not handle
6955 * tx interrupts. Don't allocate any resources for it.
6956 */
Joe Perches63c3a662011-04-26 08:12:10 +00006957 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
6958 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006959 tnapi->tx_buffers = kzalloc(
6960 sizeof(struct tg3_tx_ring_info) *
6961 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006962 if (!tnapi->tx_buffers)
6963 goto err_out;
6964
Matt Carlson4bae65c2010-11-24 08:31:52 +00006965 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6966 TG3_TX_RING_BYTES,
6967 &tnapi->tx_desc_mapping,
6968 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006969 if (!tnapi->tx_ring)
6970 goto err_out;
6971 }
6972
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006973 /*
6974 * When RSS is enabled, the status block format changes
6975 * slightly. The "rx_jumbo_consumer", "reserved",
6976 * and "rx_mini_consumer" members get mapped to the
6977 * other three rx return ring producer indexes.
6978 */
6979 switch (i) {
6980 default:
6981 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6982 break;
6983 case 2:
6984 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6985 break;
6986 case 3:
6987 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6988 break;
6989 case 4:
6990 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6991 break;
6992 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006993
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006994 /*
6995 * If multivector RSS is enabled, vector 0 does not handle
6996 * rx or tx interrupts. Don't allocate any resources for it.
6997 */
Joe Perches63c3a662011-04-26 08:12:10 +00006998 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006999 continue;
7000
Matt Carlson4bae65c2010-11-24 08:31:52 +00007001 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7002 TG3_RX_RCB_RING_BYTES(tp),
7003 &tnapi->rx_rcb_mapping,
7004 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007005 if (!tnapi->rx_rcb)
7006 goto err_out;
7007
7008 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007009 }
7010
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011 return 0;
7012
7013err_out:
7014 tg3_free_consistent(tp);
7015 return -ENOMEM;
7016}
7017
7018#define MAX_WAIT_CNT 1000
7019
7020/* To stop a block, clear the enable bit and poll till it
7021 * clears. tp->lock is held.
7022 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007023static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024{
7025 unsigned int i;
7026 u32 val;
7027
Joe Perches63c3a662011-04-26 08:12:10 +00007028 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029 switch (ofs) {
7030 case RCVLSC_MODE:
7031 case DMAC_MODE:
7032 case MBFREE_MODE:
7033 case BUFMGR_MODE:
7034 case MEMARB_MODE:
7035 /* We can't enable/disable these bits of the
7036 * 5705/5750, just say success.
7037 */
7038 return 0;
7039
7040 default:
7041 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 }
7044
7045 val = tr32(ofs);
7046 val &= ~enable_bit;
7047 tw32_f(ofs, val);
7048
7049 for (i = 0; i < MAX_WAIT_CNT; i++) {
7050 udelay(100);
7051 val = tr32(ofs);
7052 if ((val & enable_bit) == 0)
7053 break;
7054 }
7055
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007056 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007057 dev_err(&tp->pdev->dev,
7058 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7059 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060 return -ENODEV;
7061 }
7062
7063 return 0;
7064}
7065
7066/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007067static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068{
7069 int i, err;
7070
7071 tg3_disable_ints(tp);
7072
7073 tp->rx_mode &= ~RX_MODE_ENABLE;
7074 tw32_f(MAC_RX_MODE, tp->rx_mode);
7075 udelay(10);
7076
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007077 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7078 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7079 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7080 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7081 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7082 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007084 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7085 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7086 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7087 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7088 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7089 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7090 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091
7092 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7093 tw32_f(MAC_MODE, tp->mac_mode);
7094 udelay(40);
7095
7096 tp->tx_mode &= ~TX_MODE_ENABLE;
7097 tw32_f(MAC_TX_MODE, tp->tx_mode);
7098
7099 for (i = 0; i < MAX_WAIT_CNT; i++) {
7100 udelay(100);
7101 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7102 break;
7103 }
7104 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007105 dev_err(&tp->pdev->dev,
7106 "%s timed out, TX_MODE_ENABLE will not clear "
7107 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007108 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 }
7110
Michael Chane6de8ad2005-05-05 14:42:41 -07007111 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007112 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7113 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114
7115 tw32(FTQ_RESET, 0xffffffff);
7116 tw32(FTQ_RESET, 0x00000000);
7117
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007118 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7119 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007121 for (i = 0; i < tp->irq_cnt; i++) {
7122 struct tg3_napi *tnapi = &tp->napi[i];
7123 if (tnapi->hw_status)
7124 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 if (tp->hw_stats)
7127 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7128
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 return err;
7130}
7131
Matt Carlson0d3031d2007-10-10 18:02:43 -07007132static void tg3_ape_send_event(struct tg3 *tp, u32 event)
7133{
7134 int i;
7135 u32 apedata;
7136
Matt Carlsondc6d0742010-09-15 08:59:55 +00007137 /* NCSI does not support APE events */
Joe Perches63c3a662011-04-26 08:12:10 +00007138 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsondc6d0742010-09-15 08:59:55 +00007139 return;
7140
Matt Carlson0d3031d2007-10-10 18:02:43 -07007141 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
7142 if (apedata != APE_SEG_SIG_MAGIC)
7143 return;
7144
7145 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07007146 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007147 return;
7148
7149 /* Wait for up to 1 millisecond for APE to service previous event. */
7150 for (i = 0; i < 10; i++) {
7151 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
7152 return;
7153
7154 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
7155
7156 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
7157 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
7158 event | APE_EVENT_STATUS_EVENT_PENDING);
7159
7160 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
7161
7162 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
7163 break;
7164
7165 udelay(100);
7166 }
7167
7168 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
7169 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
7170}
7171
7172static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
7173{
7174 u32 event;
7175 u32 apedata;
7176
Joe Perches63c3a662011-04-26 08:12:10 +00007177 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007178 return;
7179
7180 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00007181 case RESET_KIND_INIT:
7182 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
7183 APE_HOST_SEG_SIG_MAGIC);
7184 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
7185 APE_HOST_SEG_LEN_MAGIC);
7186 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
7187 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
7188 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00007189 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00007190 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
7191 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00007192 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
7193 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07007194
Matt Carlson33f401a2010-04-05 10:19:27 +00007195 event = APE_EVENT_STATUS_STATE_START;
7196 break;
7197 case RESET_KIND_SHUTDOWN:
7198 /* With the interface we are currently using,
7199 * APE does not track driver state. Wiping
7200 * out the HOST SEGMENT SIGNATURE forces
7201 * the APE to assume OS absent status.
7202 */
7203 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08007204
Matt Carlsondc6d0742010-09-15 08:59:55 +00007205 if (device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007206 tg3_flag(tp, WOL_ENABLE)) {
Matt Carlsondc6d0742010-09-15 08:59:55 +00007207 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
7208 TG3_APE_HOST_WOL_SPEED_AUTO);
7209 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
7210 } else
7211 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
7212
7213 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
7214
Matt Carlson33f401a2010-04-05 10:19:27 +00007215 event = APE_EVENT_STATUS_STATE_UNLOAD;
7216 break;
7217 case RESET_KIND_SUSPEND:
7218 event = APE_EVENT_STATUS_STATE_SUSPEND;
7219 break;
7220 default:
7221 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007222 }
7223
7224 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
7225
7226 tg3_ape_send_event(tp, event);
7227}
7228
Michael Chane6af3012005-04-21 17:12:05 -07007229/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
7231{
David S. Millerf49639e2006-06-09 11:58:36 -07007232 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
7233 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234
Joe Perches63c3a662011-04-26 08:12:10 +00007235 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 switch (kind) {
7237 case RESET_KIND_INIT:
7238 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7239 DRV_STATE_START);
7240 break;
7241
7242 case RESET_KIND_SHUTDOWN:
7243 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7244 DRV_STATE_UNLOAD);
7245 break;
7246
7247 case RESET_KIND_SUSPEND:
7248 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7249 DRV_STATE_SUSPEND);
7250 break;
7251
7252 default:
7253 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007256
7257 if (kind == RESET_KIND_INIT ||
7258 kind == RESET_KIND_SUSPEND)
7259 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260}
7261
7262/* tp->lock is held. */
7263static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
7264{
Joe Perches63c3a662011-04-26 08:12:10 +00007265 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266 switch (kind) {
7267 case RESET_KIND_INIT:
7268 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7269 DRV_STATE_START_DONE);
7270 break;
7271
7272 case RESET_KIND_SHUTDOWN:
7273 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7274 DRV_STATE_UNLOAD_DONE);
7275 break;
7276
7277 default:
7278 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007281
7282 if (kind == RESET_KIND_SHUTDOWN)
7283 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284}
7285
7286/* tp->lock is held. */
7287static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
7288{
Joe Perches63c3a662011-04-26 08:12:10 +00007289 if (tg3_flag(tp, ENABLE_ASF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290 switch (kind) {
7291 case RESET_KIND_INIT:
7292 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7293 DRV_STATE_START);
7294 break;
7295
7296 case RESET_KIND_SHUTDOWN:
7297 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7298 DRV_STATE_UNLOAD);
7299 break;
7300
7301 case RESET_KIND_SUSPEND:
7302 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7303 DRV_STATE_SUSPEND);
7304 break;
7305
7306 default:
7307 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007309 }
7310}
7311
Michael Chan7a6f4362006-09-27 16:03:31 -07007312static int tg3_poll_fw(struct tg3 *tp)
7313{
7314 int i;
7315 u32 val;
7316
Michael Chanb5d37722006-09-27 16:06:21 -07007317 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08007318 /* Wait up to 20ms for init done. */
7319 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07007320 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
7321 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08007322 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07007323 }
7324 return -ENODEV;
7325 }
7326
Michael Chan7a6f4362006-09-27 16:03:31 -07007327 /* Wait for firmware initialization to complete. */
7328 for (i = 0; i < 100000; i++) {
7329 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
7330 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
7331 break;
7332 udelay(10);
7333 }
7334
7335 /* Chip might not be fitted with firmware. Some Sun onboard
7336 * parts are configured like that. So don't signal the timeout
7337 * of the above loop as an error, but do report the lack of
7338 * running firmware once.
7339 */
Joe Perches63c3a662011-04-26 08:12:10 +00007340 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
7341 tg3_flag_set(tp, NO_FWARE_REPORTED);
Michael Chan7a6f4362006-09-27 16:03:31 -07007342
Joe Perches05dbe002010-02-17 19:44:19 +00007343 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07007344 }
7345
Matt Carlson6b10c162010-02-12 14:47:08 +00007346 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7347 /* The 57765 A0 needs a little more
7348 * time to do some important work.
7349 */
7350 mdelay(10);
7351 }
7352
Michael Chan7a6f4362006-09-27 16:03:31 -07007353 return 0;
7354}
7355
Michael Chanee6a99b2007-07-18 21:49:10 -07007356/* Save PCI command register before chip reset */
7357static void tg3_save_pci_state(struct tg3 *tp)
7358{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007359 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007360}
7361
7362/* Restore PCI state after chip reset */
7363static void tg3_restore_pci_state(struct tg3 *tp)
7364{
7365 u32 val;
7366
7367 /* Re-enable indirect register accesses. */
7368 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7369 tp->misc_host_ctrl);
7370
7371 /* Set MAX PCI retry to zero. */
7372 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7373 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007374 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007375 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007376 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007377 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007378 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00007379 PCISTATE_ALLOW_APE_SHMEM_WR |
7380 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007381 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7382
Matt Carlson8a6eac92007-10-21 16:17:55 -07007383 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007384
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007385 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007386 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007387 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007388 else {
7389 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7390 tp->pci_cacheline_sz);
7391 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7392 tp->pci_lat_timer);
7393 }
Michael Chan114342f2007-10-15 02:12:26 -07007394 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007395
Michael Chanee6a99b2007-07-18 21:49:10 -07007396 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007397 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007398 u16 pcix_cmd;
7399
7400 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7401 &pcix_cmd);
7402 pcix_cmd &= ~PCI_X_CMD_ERO;
7403 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7404 pcix_cmd);
7405 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007406
Joe Perches63c3a662011-04-26 08:12:10 +00007407 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007408
7409 /* Chip reset on 5780 will reset MSI enable bit,
7410 * so need to restore it.
7411 */
Joe Perches63c3a662011-04-26 08:12:10 +00007412 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007413 u16 ctrl;
7414
7415 pci_read_config_word(tp->pdev,
7416 tp->msi_cap + PCI_MSI_FLAGS,
7417 &ctrl);
7418 pci_write_config_word(tp->pdev,
7419 tp->msi_cap + PCI_MSI_FLAGS,
7420 ctrl | PCI_MSI_FLAGS_ENABLE);
7421 val = tr32(MSGINT_MODE);
7422 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7423 }
7424 }
7425}
7426
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427static void tg3_stop_fw(struct tg3 *);
7428
7429/* tp->lock is held. */
7430static int tg3_chip_reset(struct tg3 *tp)
7431{
7432 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007433 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007434 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435
David S. Millerf49639e2006-06-09 11:58:36 -07007436 tg3_nvram_lock(tp);
7437
Matt Carlson77b483f2008-08-15 14:07:24 -07007438 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7439
David S. Millerf49639e2006-06-09 11:58:36 -07007440 /* No matching tg3_nvram_unlock() after this because
7441 * chip reset below will undo the nvram lock.
7442 */
7443 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444
Michael Chanee6a99b2007-07-18 21:49:10 -07007445 /* GRC_MISC_CFG core clock reset will clear the memory
7446 * enable bit in PCI register 4 and the MSI enable bit
7447 * on some chips, so we save relevant registers here.
7448 */
7449 tg3_save_pci_state(tp);
7450
Michael Chand9ab5ad2006-03-20 22:27:35 -08007451 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007452 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007453 tw32(GRC_FASTBOOT_PC, 0);
7454
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 /*
7456 * We must avoid the readl() that normally takes place.
7457 * It locks machines, causes machine checks, and other
7458 * fun things. So, temporarily disable the 5701
7459 * hardware workaround, while we do the reset.
7460 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007461 write_op = tp->write32;
7462 if (write_op == tg3_write_flush_reg32)
7463 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464
Michael Chand18edcb2007-03-24 20:57:11 -07007465 /* Prevent the irq handler from reading or writing PCI registers
7466 * during chip reset when the memory enable bit in the PCI command
7467 * register may be cleared. The chip does not generate interrupt
7468 * at this time, but the irq handler may still be called due to irq
7469 * sharing or irqpoll.
7470 */
Joe Perches63c3a662011-04-26 08:12:10 +00007471 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007472 for (i = 0; i < tp->irq_cnt; i++) {
7473 struct tg3_napi *tnapi = &tp->napi[i];
7474 if (tnapi->hw_status) {
7475 tnapi->hw_status->status = 0;
7476 tnapi->hw_status->status_tag = 0;
7477 }
7478 tnapi->last_tag = 0;
7479 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007480 }
Michael Chand18edcb2007-03-24 20:57:11 -07007481 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007482
7483 for (i = 0; i < tp->irq_cnt; i++)
7484 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007485
Matt Carlson255ca312009-08-25 10:07:27 +00007486 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7487 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7488 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7489 }
7490
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491 /* do the reset */
7492 val = GRC_MISC_CFG_CORECLK_RESET;
7493
Joe Perches63c3a662011-04-26 08:12:10 +00007494 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007495 /* Force PCIe 1.0a mode */
7496 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007497 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007498 tr32(TG3_PCIE_PHY_TSTCTL) ==
7499 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7500 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7501
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7503 tw32(GRC_MISC_CFG, (1 << 29));
7504 val |= (1 << 29);
7505 }
7506 }
7507
Michael Chanb5d37722006-09-27 16:06:21 -07007508 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7509 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7510 tw32(GRC_VCPU_EXT_CTRL,
7511 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7512 }
7513
Matt Carlsonf37500d2010-08-02 11:25:59 +00007514 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007515 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007517
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 tw32(GRC_MISC_CFG, val);
7519
Michael Chan1ee582d2005-08-09 20:16:46 -07007520 /* restore 5701 hardware bug workaround write method */
7521 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522
7523 /* Unfortunately, we have to delay before the PCI read back.
7524 * Some 575X chips even will not respond to a PCI cfg access
7525 * when the reset command is given to the chip.
7526 *
7527 * How do these hardware designers expect things to work
7528 * properly if the PCI write is posted for a long period
7529 * of time? It is always necessary to have some method by
7530 * which a register read back can occur to push the write
7531 * out which does the reset.
7532 *
7533 * For most tg3 variants the trick below was working.
7534 * Ho hum...
7535 */
7536 udelay(120);
7537
7538 /* Flush PCI posted writes. The normal MMIO registers
7539 * are inaccessible at this time so this is the only
7540 * way to make this reliably (actually, this is no longer
7541 * the case, see above). I tried to use indirect
7542 * register read/write but this upset some 5701 variants.
7543 */
7544 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7545
7546 udelay(120);
7547
Jon Mason708ebb32011-06-27 12:56:50 +00007548 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007549 u16 val16;
7550
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7552 int i;
7553 u32 cfg_val;
7554
7555 /* Wait for link training to complete. */
7556 for (i = 0; i < 5000; i++)
7557 udelay(100);
7558
7559 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7560 pci_write_config_dword(tp->pdev, 0xc4,
7561 cfg_val | (1 << 15));
7562 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007563
Matt Carlsone7126992009-08-25 10:08:16 +00007564 /* Clear the "no snoop" and "relaxed ordering" bits. */
7565 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007566 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007567 &val16);
7568 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7569 PCI_EXP_DEVCTL_NOSNOOP_EN);
7570 /*
7571 * Older PCIe devices only support the 128 byte
7572 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007573 */
Joe Perches63c3a662011-04-26 08:12:10 +00007574 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007575 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007576 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007577 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007578 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007579
Matt Carlsoncf790032010-11-24 08:31:48 +00007580 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007581
7582 /* Clear error status */
7583 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007584 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007585 PCI_EXP_DEVSTA_CED |
7586 PCI_EXP_DEVSTA_NFED |
7587 PCI_EXP_DEVSTA_FED |
7588 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589 }
7590
Michael Chanee6a99b2007-07-18 21:49:10 -07007591 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592
Joe Perches63c3a662011-04-26 08:12:10 +00007593 tg3_flag_clear(tp, CHIP_RESETTING);
7594 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007595
Michael Chanee6a99b2007-07-18 21:49:10 -07007596 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007597 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007598 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007599 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007600
7601 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7602 tg3_stop_fw(tp);
7603 tw32(0x5000, 0x400);
7604 }
7605
7606 tw32(GRC_MODE, tp->grc_mode);
7607
7608 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007609 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610
7611 tw32(0xc4, val | (1 << 15));
7612 }
7613
7614 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7615 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7616 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7617 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7618 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7619 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7620 }
7621
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007622 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007623 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007624 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007625 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007626 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007627 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007629 val = 0;
7630
7631 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632 udelay(40);
7633
Matt Carlson77b483f2008-08-15 14:07:24 -07007634 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7635
Michael Chan7a6f4362006-09-27 16:03:31 -07007636 err = tg3_poll_fw(tp);
7637 if (err)
7638 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639
Matt Carlson0a9140c2009-08-28 12:27:50 +00007640 tg3_mdio_start(tp);
7641
Joe Perches63c3a662011-04-26 08:12:10 +00007642 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007643 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7644 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007645 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007646 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007647
7648 tw32(0x7c00, val | (1 << 25));
7649 }
7650
Matt Carlsond78b59f2011-04-05 14:22:46 +00007651 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7652 val = tr32(TG3_CPMU_CLCK_ORIDE);
7653 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7654 }
7655
Linus Torvalds1da177e2005-04-16 15:20:36 -07007656 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007657 tg3_flag_clear(tp, ENABLE_ASF);
7658 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007659 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7660 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7661 u32 nic_cfg;
7662
7663 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7664 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007665 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007666 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007667 if (tg3_flag(tp, 5750_PLUS))
7668 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669 }
7670 }
7671
7672 return 0;
7673}
7674
7675/* tp->lock is held. */
7676static void tg3_stop_fw(struct tg3 *tp)
7677{
Joe Perches63c3a662011-04-26 08:12:10 +00007678 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007679 /* Wait for RX cpu to ACK the previous event. */
7680 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681
7682 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007683
7684 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007685
Matt Carlson7c5026a2008-05-02 16:49:29 -07007686 /* Wait for RX cpu to ACK this event. */
7687 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007688 }
7689}
7690
7691/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007692static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007693{
7694 int err;
7695
7696 tg3_stop_fw(tp);
7697
Michael Chan944d9802005-05-29 14:57:48 -07007698 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007700 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007701 err = tg3_chip_reset(tp);
7702
Matt Carlsondaba2a62009-04-20 06:58:52 +00007703 __tg3_set_mac_addr(tp, 0);
7704
Michael Chan944d9802005-05-29 14:57:48 -07007705 tg3_write_sig_legacy(tp, kind);
7706 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007707
7708 if (err)
7709 return err;
7710
7711 return 0;
7712}
7713
Linus Torvalds1da177e2005-04-16 15:20:36 -07007714#define RX_CPU_SCRATCH_BASE 0x30000
7715#define RX_CPU_SCRATCH_SIZE 0x04000
7716#define TX_CPU_SCRATCH_BASE 0x34000
7717#define TX_CPU_SCRATCH_SIZE 0x04000
7718
7719/* tp->lock is held. */
7720static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7721{
7722 int i;
7723
Joe Perches63c3a662011-04-26 08:12:10 +00007724 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725
Michael Chanb5d37722006-09-27 16:06:21 -07007726 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7727 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7728
7729 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7730 return 0;
7731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007732 if (offset == RX_CPU_BASE) {
7733 for (i = 0; i < 10000; i++) {
7734 tw32(offset + CPU_STATE, 0xffffffff);
7735 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7736 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7737 break;
7738 }
7739
7740 tw32(offset + CPU_STATE, 0xffffffff);
7741 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7742 udelay(10);
7743 } else {
7744 for (i = 0; i < 10000; i++) {
7745 tw32(offset + CPU_STATE, 0xffffffff);
7746 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7747 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7748 break;
7749 }
7750 }
7751
7752 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007753 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7754 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755 return -ENODEV;
7756 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007757
7758 /* Clear firmware's nvram arbitration. */
Joe Perches63c3a662011-04-26 08:12:10 +00007759 if (tg3_flag(tp, NVRAM))
Michael Chanec41c7d2006-01-17 02:40:55 -08007760 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007761 return 0;
7762}
7763
7764struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007765 unsigned int fw_base;
7766 unsigned int fw_len;
7767 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007768};
7769
7770/* tp->lock is held. */
7771static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7772 int cpu_scratch_size, struct fw_info *info)
7773{
Michael Chanec41c7d2006-01-17 02:40:55 -08007774 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007775 void (*write_op)(struct tg3 *, u32, u32);
7776
Joe Perches63c3a662011-04-26 08:12:10 +00007777 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007778 netdev_err(tp->dev,
7779 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007780 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781 return -EINVAL;
7782 }
7783
Joe Perches63c3a662011-04-26 08:12:10 +00007784 if (tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785 write_op = tg3_write_mem;
7786 else
7787 write_op = tg3_write_indirect_reg32;
7788
Michael Chan1b628152005-05-29 14:59:49 -07007789 /* It is possible that bootcode is still loading at this point.
7790 * Get the nvram lock first before halting the cpu.
7791 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007792 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007794 if (!lock_err)
7795 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007796 if (err)
7797 goto out;
7798
7799 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7800 write_op(tp, cpu_scratch_base + i, 0);
7801 tw32(cpu_base + CPU_STATE, 0xffffffff);
7802 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007803 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007805 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007806 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007807 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007808
7809 err = 0;
7810
7811out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812 return err;
7813}
7814
7815/* tp->lock is held. */
7816static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7817{
7818 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007819 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007820 int err, i;
7821
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007822 fw_data = (void *)tp->fw->data;
7823
7824 /* Firmware blob starts with version numbers, followed by
7825 start address and length. We are setting complete length.
7826 length = end_address_of_bss - start_address_of_text.
7827 Remainder is the blob to be loaded contiguously
7828 from start address. */
7829
7830 info.fw_base = be32_to_cpu(fw_data[1]);
7831 info.fw_len = tp->fw->size - 12;
7832 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833
7834 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7835 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7836 &info);
7837 if (err)
7838 return err;
7839
7840 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7841 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7842 &info);
7843 if (err)
7844 return err;
7845
7846 /* Now startup only the RX cpu. */
7847 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007848 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007849
7850 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007851 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007852 break;
7853 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7854 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007855 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856 udelay(1000);
7857 }
7858 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007859 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7860 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007861 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007862 return -ENODEV;
7863 }
7864 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7865 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7866
7867 return 0;
7868}
7869
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870/* tp->lock is held. */
7871static int tg3_load_tso_firmware(struct tg3 *tp)
7872{
7873 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007874 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007875 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7876 int err, i;
7877
Joe Perches63c3a662011-04-26 08:12:10 +00007878 if (tg3_flag(tp, HW_TSO_1) ||
7879 tg3_flag(tp, HW_TSO_2) ||
7880 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007881 return 0;
7882
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007883 fw_data = (void *)tp->fw->data;
7884
7885 /* Firmware blob starts with version numbers, followed by
7886 start address and length. We are setting complete length.
7887 length = end_address_of_bss - start_address_of_text.
7888 Remainder is the blob to be loaded contiguously
7889 from start address. */
7890
7891 info.fw_base = be32_to_cpu(fw_data[1]);
7892 cpu_scratch_size = tp->fw_len;
7893 info.fw_len = tp->fw->size - 12;
7894 info.fw_data = &fw_data[3];
7895
Linus Torvalds1da177e2005-04-16 15:20:36 -07007896 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897 cpu_base = RX_CPU_BASE;
7898 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007899 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007900 cpu_base = TX_CPU_BASE;
7901 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7902 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7903 }
7904
7905 err = tg3_load_firmware_cpu(tp, cpu_base,
7906 cpu_scratch_base, cpu_scratch_size,
7907 &info);
7908 if (err)
7909 return err;
7910
7911 /* Now startup the cpu. */
7912 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007913 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007914
7915 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007916 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917 break;
7918 tw32(cpu_base + CPU_STATE, 0xffffffff);
7919 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007920 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921 udelay(1000);
7922 }
7923 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007924 netdev_err(tp->dev,
7925 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007926 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007927 return -ENODEV;
7928 }
7929 tw32(cpu_base + CPU_STATE, 0xffffffff);
7930 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7931 return 0;
7932}
7933
Linus Torvalds1da177e2005-04-16 15:20:36 -07007934
Linus Torvalds1da177e2005-04-16 15:20:36 -07007935static int tg3_set_mac_addr(struct net_device *dev, void *p)
7936{
7937 struct tg3 *tp = netdev_priv(dev);
7938 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007939 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007940
Michael Chanf9804dd2005-09-27 12:13:10 -07007941 if (!is_valid_ether_addr(addr->sa_data))
7942 return -EINVAL;
7943
Linus Torvalds1da177e2005-04-16 15:20:36 -07007944 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7945
Michael Chane75f7c92006-03-20 21:33:26 -08007946 if (!netif_running(dev))
7947 return 0;
7948
Joe Perches63c3a662011-04-26 08:12:10 +00007949 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007950 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007951
Michael Chan986e0ae2007-05-05 12:10:20 -07007952 addr0_high = tr32(MAC_ADDR_0_HIGH);
7953 addr0_low = tr32(MAC_ADDR_0_LOW);
7954 addr1_high = tr32(MAC_ADDR_1_HIGH);
7955 addr1_low = tr32(MAC_ADDR_1_LOW);
7956
7957 /* Skip MAC addr 1 if ASF is using it. */
7958 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7959 !(addr1_high == 0 && addr1_low == 0))
7960 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007961 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007962 spin_lock_bh(&tp->lock);
7963 __tg3_set_mac_addr(tp, skip_mac_1);
7964 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965
Michael Chanb9ec6c12006-07-25 16:37:27 -07007966 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007967}
7968
7969/* tp->lock is held. */
7970static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7971 dma_addr_t mapping, u32 maxlen_flags,
7972 u32 nic_addr)
7973{
7974 tg3_write_mem(tp,
7975 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7976 ((u64) mapping >> 32));
7977 tg3_write_mem(tp,
7978 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7979 ((u64) mapping & 0xffffffff));
7980 tg3_write_mem(tp,
7981 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7982 maxlen_flags);
7983
Joe Perches63c3a662011-04-26 08:12:10 +00007984 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007985 tg3_write_mem(tp,
7986 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7987 nic_addr);
7988}
7989
7990static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007991static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007992{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007993 int i;
7994
Joe Perches63c3a662011-04-26 08:12:10 +00007995 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007996 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7997 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7998 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007999 } else {
8000 tw32(HOSTCC_TXCOL_TICKS, 0);
8001 tw32(HOSTCC_TXMAX_FRAMES, 0);
8002 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008003 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008004
Joe Perches63c3a662011-04-26 08:12:10 +00008005 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008006 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8007 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8008 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8009 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008010 tw32(HOSTCC_RXCOL_TICKS, 0);
8011 tw32(HOSTCC_RXMAX_FRAMES, 0);
8012 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008013 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008014
Joe Perches63c3a662011-04-26 08:12:10 +00008015 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008016 u32 val = ec->stats_block_coalesce_usecs;
8017
Matt Carlsonb6080e12009-09-01 13:12:00 +00008018 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8019 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8020
David S. Miller15f98502005-05-18 22:49:26 -07008021 if (!netif_carrier_ok(tp->dev))
8022 val = 0;
8023
8024 tw32(HOSTCC_STAT_COAL_TICKS, val);
8025 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008026
8027 for (i = 0; i < tp->irq_cnt - 1; i++) {
8028 u32 reg;
8029
8030 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8031 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008032 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8033 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008034 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8035 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008036
Joe Perches63c3a662011-04-26 08:12:10 +00008037 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008038 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8039 tw32(reg, ec->tx_coalesce_usecs);
8040 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8041 tw32(reg, ec->tx_max_coalesced_frames);
8042 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8043 tw32(reg, ec->tx_max_coalesced_frames_irq);
8044 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008045 }
8046
8047 for (; i < tp->irq_max - 1; i++) {
8048 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008049 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008050 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008051
Joe Perches63c3a662011-04-26 08:12:10 +00008052 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008053 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8054 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8055 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8056 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008057 }
David S. Miller15f98502005-05-18 22:49:26 -07008058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059
8060/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008061static void tg3_rings_reset(struct tg3 *tp)
8062{
8063 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008064 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008065 struct tg3_napi *tnapi = &tp->napi[0];
8066
8067 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008068 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008069 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008070 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008071 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00008072 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8073 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008074 else
8075 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8076
8077 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8078 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8079 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8080 BDINFO_FLAGS_DISABLED);
8081
8082
8083 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008084 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008085 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008086 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008087 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008088 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8089 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00008090 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8091 else
8092 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8093
8094 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8095 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8096 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8097 BDINFO_FLAGS_DISABLED);
8098
8099 /* Disable interrupts */
8100 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008101 tp->napi[0].chk_msi_cnt = 0;
8102 tp->napi[0].last_rx_cons = 0;
8103 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008104
8105 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008106 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008107 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008108 tp->napi[i].tx_prod = 0;
8109 tp->napi[i].tx_cons = 0;
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[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008112 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8113 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008114 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008115 tp->napi[i].last_rx_cons = 0;
8116 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008117 }
Joe Perches63c3a662011-04-26 08:12:10 +00008118 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008119 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008120 } else {
8121 tp->napi[0].tx_prod = 0;
8122 tp->napi[0].tx_cons = 0;
8123 tw32_mailbox(tp->napi[0].prodmbox, 0);
8124 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8125 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008126
8127 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008128 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008129 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8130 for (i = 0; i < 16; i++)
8131 tw32_tx_mbox(mbox + i * 8, 0);
8132 }
8133
8134 txrcb = NIC_SRAM_SEND_RCB;
8135 rxrcb = NIC_SRAM_RCV_RET_RCB;
8136
8137 /* Clear status block in ram. */
8138 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8139
8140 /* Set status block DMA address */
8141 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8142 ((u64) tnapi->status_mapping >> 32));
8143 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8144 ((u64) tnapi->status_mapping & 0xffffffff));
8145
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008146 if (tnapi->tx_ring) {
8147 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8148 (TG3_TX_RING_SIZE <<
8149 BDINFO_FLAGS_MAXLEN_SHIFT),
8150 NIC_SRAM_TX_BUFFER_DESC);
8151 txrcb += TG3_BDINFO_SIZE;
8152 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008153
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008154 if (tnapi->rx_rcb) {
8155 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008156 (tp->rx_ret_ring_mask + 1) <<
8157 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008158 rxrcb += TG3_BDINFO_SIZE;
8159 }
8160
8161 stblk = HOSTCC_STATBLCK_RING1;
8162
8163 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8164 u64 mapping = (u64)tnapi->status_mapping;
8165 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8166 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8167
8168 /* Clear status block in ram. */
8169 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8170
Matt Carlson19cfaec2009-12-03 08:36:20 +00008171 if (tnapi->tx_ring) {
8172 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8173 (TG3_TX_RING_SIZE <<
8174 BDINFO_FLAGS_MAXLEN_SHIFT),
8175 NIC_SRAM_TX_BUFFER_DESC);
8176 txrcb += TG3_BDINFO_SIZE;
8177 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008178
8179 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008180 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008181 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8182
8183 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008184 rxrcb += TG3_BDINFO_SIZE;
8185 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008186}
8187
Matt Carlsoneb07a942011-04-20 07:57:36 +00008188static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8189{
8190 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8191
Joe Perches63c3a662011-04-26 08:12:10 +00008192 if (!tg3_flag(tp, 5750_PLUS) ||
8193 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008194 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
8195 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8196 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8197 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8198 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8199 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8200 else
8201 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8202
8203 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8204 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8205
8206 val = min(nic_rep_thresh, host_rep_thresh);
8207 tw32(RCVBDI_STD_THRESH, val);
8208
Joe Perches63c3a662011-04-26 08:12:10 +00008209 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008210 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8211
Joe Perches63c3a662011-04-26 08:12:10 +00008212 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008213 return;
8214
Joe Perches63c3a662011-04-26 08:12:10 +00008215 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008216 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
8217 else
8218 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
8219
8220 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8221
8222 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8223 tw32(RCVBDI_JUMBO_THRESH, val);
8224
Joe Perches63c3a662011-04-26 08:12:10 +00008225 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008226 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8227}
8228
Matt Carlson2d31eca2009-09-01 12:53:31 +00008229/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008230static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008231{
8232 u32 val, rdmac_mode;
8233 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008234 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008235
8236 tg3_disable_ints(tp);
8237
8238 tg3_stop_fw(tp);
8239
8240 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8241
Joe Perches63c3a662011-04-26 08:12:10 +00008242 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008243 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244
Matt Carlson699c0192010-12-06 08:28:51 +00008245 /* Enable MAC control of LPI */
8246 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8247 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8248 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8249 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8250
8251 tw32_f(TG3_CPMU_EEE_CTRL,
8252 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8253
Matt Carlsona386b902010-12-06 08:28:53 +00008254 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8255 TG3_CPMU_EEEMD_LPI_IN_TX |
8256 TG3_CPMU_EEEMD_LPI_IN_RX |
8257 TG3_CPMU_EEEMD_EEE_ENABLE;
8258
8259 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8260 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8261
Joe Perches63c3a662011-04-26 08:12:10 +00008262 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008263 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8264
8265 tw32_f(TG3_CPMU_EEE_MODE, val);
8266
8267 tw32_f(TG3_CPMU_EEE_DBTMR1,
8268 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8269 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8270
8271 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008272 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008273 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008274 }
8275
Matt Carlson603f1172010-02-12 14:47:10 +00008276 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008277 tg3_phy_reset(tp);
8278
Linus Torvalds1da177e2005-04-16 15:20:36 -07008279 err = tg3_chip_reset(tp);
8280 if (err)
8281 return err;
8282
8283 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8284
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008285 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008286 val = tr32(TG3_CPMU_CTRL);
8287 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8288 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008289
8290 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8291 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8292 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8293 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8294
8295 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8296 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8297 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8298 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8299
8300 val = tr32(TG3_CPMU_HST_ACC);
8301 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8302 val |= CPMU_HST_ACC_MACCLK_6_25;
8303 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008304 }
8305
Matt Carlson33466d92009-04-20 06:57:41 +00008306 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8307 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8308 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8309 PCIE_PWR_MGMT_L1_THRESH_4MS;
8310 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008311
8312 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8313 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8314
8315 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008316
Matt Carlsonf40386c2009-11-02 14:24:02 +00008317 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8318 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008319 }
8320
Joe Perches63c3a662011-04-26 08:12:10 +00008321 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b05902010-01-20 16:58:02 +00008322 u32 grc_mode = tr32(GRC_MODE);
8323
8324 /* Access the lower 1K of PL PCIE block registers. */
8325 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8326 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8327
8328 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8329 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8330 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8331
8332 tw32(GRC_MODE, grc_mode);
8333 }
8334
Matt Carlson5093eed2010-11-24 08:31:45 +00008335 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8336 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8337 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008338
Matt Carlson5093eed2010-11-24 08:31:45 +00008339 /* Access the lower 1K of PL PCIE block registers. */
8340 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8341 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008342
Matt Carlson5093eed2010-11-24 08:31:45 +00008343 val = tr32(TG3_PCIE_TLDLPL_PORT +
8344 TG3_PCIE_PL_LO_PHYCTL5);
8345 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8346 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008347
Matt Carlson5093eed2010-11-24 08:31:45 +00008348 tw32(GRC_MODE, grc_mode);
8349 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008350
Matt Carlson1ff30a52011-05-19 12:12:46 +00008351 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8352 u32 grc_mode = tr32(GRC_MODE);
8353
8354 /* Access the lower 1K of DL PCIE block registers. */
8355 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8356 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8357
8358 val = tr32(TG3_PCIE_TLDLPL_PORT +
8359 TG3_PCIE_DL_LO_FTSMAX);
8360 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8361 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8362 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8363
8364 tw32(GRC_MODE, grc_mode);
8365 }
8366
Matt Carlsona977dbe2010-04-12 06:58:26 +00008367 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8368 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8369 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8370 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008371 }
8372
Linus Torvalds1da177e2005-04-16 15:20:36 -07008373 /* This works around an issue with Athlon chipsets on
8374 * B3 tigon3 silicon. This bit has no effect on any
8375 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008376 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008377 */
Joe Perches63c3a662011-04-26 08:12:10 +00008378 if (!tg3_flag(tp, CPMU_PRESENT)) {
8379 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008380 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8381 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008383
8384 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008385 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008386 val = tr32(TG3PCI_PCISTATE);
8387 val |= PCISTATE_RETRY_SAME_DMA;
8388 tw32(TG3PCI_PCISTATE, val);
8389 }
8390
Joe Perches63c3a662011-04-26 08:12:10 +00008391 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008392 /* Allow reads and writes to the
8393 * APE register and memory space.
8394 */
8395 val = tr32(TG3PCI_PCISTATE);
8396 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00008397 PCISTATE_ALLOW_APE_SHMEM_WR |
8398 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008399 tw32(TG3PCI_PCISTATE, val);
8400 }
8401
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8403 /* Enable some hw fixes. */
8404 val = tr32(TG3PCI_MSI_DATA);
8405 val |= (1 << 26) | (1 << 28) | (1 << 29);
8406 tw32(TG3PCI_MSI_DATA, val);
8407 }
8408
8409 /* Descriptor ring init may make accesses to the
8410 * NIC SRAM area to setup the TX descriptors, so we
8411 * can only do this after the hardware has been
8412 * successfully reset.
8413 */
Michael Chan32d8c572006-07-25 16:38:29 -07008414 err = tg3_init_rings(tp);
8415 if (err)
8416 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008417
Joe Perches63c3a662011-04-26 08:12:10 +00008418 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008419 val = tr32(TG3PCI_DMA_RW_CTRL) &
8420 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008421 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8422 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008423 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8424 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8425 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008426 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8427 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8428 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008429 /* This value is determined during the probe time DMA
8430 * engine test, tg3_test_dma.
8431 */
8432 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008434
8435 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8436 GRC_MODE_4X_NIC_SEND_RINGS |
8437 GRC_MODE_NO_TX_PHDR_CSUM |
8438 GRC_MODE_NO_RX_PHDR_CSUM);
8439 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008440
8441 /* Pseudo-header checksum is done by hardware logic and not
8442 * the offload processers, so make the chip do the pseudo-
8443 * header checksums on receive. For transmit it is more
8444 * convenient to do the pseudo-header checksum in software
8445 * as Linux does that on transmit for us in all cases.
8446 */
8447 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008448
8449 tw32(GRC_MODE,
8450 tp->grc_mode |
8451 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8452
8453 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8454 val = tr32(GRC_MISC_CFG);
8455 val &= ~0xff;
8456 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8457 tw32(GRC_MISC_CFG, val);
8458
8459 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008460 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008461 /* Do nothing. */
8462 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8463 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8464 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8465 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8466 else
8467 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8468 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8469 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008470 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008471 int fw_len;
8472
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008473 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008474 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8475 tw32(BUFMGR_MB_POOL_ADDR,
8476 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8477 tw32(BUFMGR_MB_POOL_SIZE,
8478 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008480
Michael Chan0f893dc2005-07-25 12:30:38 -07008481 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008482 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8483 tp->bufmgr_config.mbuf_read_dma_low_water);
8484 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8485 tp->bufmgr_config.mbuf_mac_rx_low_water);
8486 tw32(BUFMGR_MB_HIGH_WATER,
8487 tp->bufmgr_config.mbuf_high_water);
8488 } else {
8489 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8490 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8491 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8492 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8493 tw32(BUFMGR_MB_HIGH_WATER,
8494 tp->bufmgr_config.mbuf_high_water_jumbo);
8495 }
8496 tw32(BUFMGR_DMA_LOW_WATER,
8497 tp->bufmgr_config.dma_low_water);
8498 tw32(BUFMGR_DMA_HIGH_WATER,
8499 tp->bufmgr_config.dma_high_water);
8500
Matt Carlsond309a462010-09-30 10:34:31 +00008501 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8502 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8503 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008504 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8505 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8506 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8507 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008508 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008509 for (i = 0; i < 2000; i++) {
8510 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8511 break;
8512 udelay(10);
8513 }
8514 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008515 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008516 return -ENODEV;
8517 }
8518
Matt Carlsoneb07a942011-04-20 07:57:36 +00008519 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8520 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008521
Matt Carlsoneb07a942011-04-20 07:57:36 +00008522 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008523
8524 /* Initialize TG3_BDINFO's at:
8525 * RCVDBDI_STD_BD: standard eth size rx ring
8526 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8527 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8528 *
8529 * like so:
8530 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8531 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8532 * ring attribute flags
8533 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8534 *
8535 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8536 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8537 *
8538 * The size of each ring is fixed in the firmware, but the location is
8539 * configurable.
8540 */
8541 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008542 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008543 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008544 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008545 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008546 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8547 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008548
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008549 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008550 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008551 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8552 BDINFO_FLAGS_DISABLED);
8553
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008554 /* Program the jumbo buffer descriptor ring control
8555 * blocks on those devices that have them.
8556 */
Matt Carlsona0512942011-07-27 14:20:54 +00008557 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008558 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008559
Joe Perches63c3a662011-04-26 08:12:10 +00008560 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008562 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008563 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008564 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008565 val = TG3_RX_JMB_RING_SIZE(tp) <<
8566 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008567 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008568 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008569 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008570 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008571 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8572 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008573 } else {
8574 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8575 BDINFO_FLAGS_DISABLED);
8576 }
8577
Joe Perches63c3a662011-04-26 08:12:10 +00008578 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008579 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008580 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008581 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008582 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008583 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8584 val |= (TG3_RX_STD_DMA_SZ << 2);
8585 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008586 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008587 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008588 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008589
8590 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008591
Matt Carlson411da642009-11-13 13:03:46 +00008592 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008593 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008594
Joe Perches63c3a662011-04-26 08:12:10 +00008595 tpr->rx_jmb_prod_idx =
8596 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008597 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008598
Matt Carlson2d31eca2009-09-01 12:53:31 +00008599 tg3_rings_reset(tp);
8600
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008602 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008603
8604 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008605 tw32(MAC_RX_MTU_SIZE,
8606 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008607
8608 /* The slot time is changed by tg3_setup_phy if we
8609 * run at gigabit with half duplex.
8610 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008611 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8612 (6 << TX_LENGTHS_IPG_SHIFT) |
8613 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8614
8615 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8616 val |= tr32(MAC_TX_LENGTHS) &
8617 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8618 TX_LENGTHS_CNT_DWN_VAL_MSK);
8619
8620 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008621
8622 /* Receive rules. */
8623 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8624 tw32(RCVLPC_CONFIG, 0x0181);
8625
8626 /* Calculate RDMAC_MODE setting early, we need it to determine
8627 * the RCVLPC_STATE_ENABLE mask.
8628 */
8629 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8630 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8631 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8632 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8633 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008634
Matt Carlsondeabaac2010-11-24 08:31:50 +00008635 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008636 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8637
Matt Carlson57e69832008-05-25 23:48:31 -07008638 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008639 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8640 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008641 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8642 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8643 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8644
Matt Carlsonc5908932011-03-09 16:58:25 +00008645 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8646 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008647 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008648 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8650 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008651 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008652 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8653 }
8654 }
8655
Joe Perches63c3a662011-04-26 08:12:10 +00008656 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008657 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8658
Joe Perches63c3a662011-04-26 08:12:10 +00008659 if (tg3_flag(tp, HW_TSO_1) ||
8660 tg3_flag(tp, HW_TSO_2) ||
8661 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008662 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8663
Matt Carlson108a6c12011-05-19 12:12:47 +00008664 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008665 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008666 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8667 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008668
Matt Carlsonf2096f92011-04-05 14:22:48 +00008669 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8670 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8671
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008672 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8673 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8674 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8675 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008676 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008677 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008678 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8679 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008680 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8681 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8682 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8683 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8684 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8685 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008686 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008687 tw32(TG3_RDMA_RSRVCTRL_REG,
8688 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8689 }
8690
Matt Carlsond78b59f2011-04-05 14:22:46 +00008691 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8692 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008693 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8694 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8695 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8696 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8697 }
8698
Linus Torvalds1da177e2005-04-16 15:20:36 -07008699 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008700 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008701 val = tr32(RCVLPC_STATS_ENABLE);
8702 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8703 tw32(RCVLPC_STATS_ENABLE, val);
8704 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008705 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008706 val = tr32(RCVLPC_STATS_ENABLE);
8707 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8708 tw32(RCVLPC_STATS_ENABLE, val);
8709 } else {
8710 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8711 }
8712 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8713 tw32(SNDDATAI_STATSENAB, 0xffffff);
8714 tw32(SNDDATAI_STATSCTRL,
8715 (SNDDATAI_SCTRL_ENABLE |
8716 SNDDATAI_SCTRL_FASTUPD));
8717
8718 /* Setup host coalescing engine. */
8719 tw32(HOSTCC_MODE, 0);
8720 for (i = 0; i < 2000; i++) {
8721 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8722 break;
8723 udelay(10);
8724 }
8725
Michael Chand244c892005-07-05 14:42:33 -07008726 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727
Joe Perches63c3a662011-04-26 08:12:10 +00008728 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008729 /* Status/statistics block address. See tg3_timer,
8730 * the tg3_periodic_fetch_stats call there, and
8731 * tg3_get_stats to see how this works for 5705/5750 chips.
8732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008733 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8734 ((u64) tp->stats_mapping >> 32));
8735 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8736 ((u64) tp->stats_mapping & 0xffffffff));
8737 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008738
Linus Torvalds1da177e2005-04-16 15:20:36 -07008739 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008740
8741 /* Clear statistics and status block memory areas */
8742 for (i = NIC_SRAM_STATS_BLK;
8743 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8744 i += sizeof(u32)) {
8745 tg3_write_mem(tp, i, 0);
8746 udelay(40);
8747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008748 }
8749
8750 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8751
8752 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8753 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008754 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008755 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8756
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008757 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8758 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008759 /* reset to prevent losing 1st rx packet intermittently */
8760 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8761 udelay(10);
8762 }
8763
Matt Carlson3bda1252008-08-15 14:08:22 -07008764 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00008765 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8766 MAC_MODE_FHDE_ENABLE;
8767 if (tg3_flag(tp, ENABLE_APE))
8768 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008769 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008770 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008771 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8772 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8774 udelay(40);
8775
Michael Chan314fba32005-04-21 17:07:04 -07008776 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008777 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008778 * register to preserve the GPIO settings for LOMs. The GPIOs,
8779 * whether used as inputs or outputs, are set by boot code after
8780 * reset.
8781 */
Joe Perches63c3a662011-04-26 08:12:10 +00008782 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008783 u32 gpio_mask;
8784
Michael Chan9d26e212006-12-07 00:21:14 -08008785 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8786 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8787 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008788
8789 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8790 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8791 GRC_LCLCTRL_GPIO_OUTPUT3;
8792
Michael Chanaf36e6b2006-03-23 01:28:06 -08008793 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8794 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8795
Gary Zambranoaaf84462007-05-05 11:51:45 -07008796 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008797 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8798
8799 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008800 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008801 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8802 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008804 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8805 udelay(100);
8806
Joe Perches63c3a662011-04-26 08:12:10 +00008807 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008808 val = tr32(MSGINT_MODE);
8809 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8810 tw32(MSGINT_MODE, val);
8811 }
8812
Joe Perches63c3a662011-04-26 08:12:10 +00008813 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008814 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8815 udelay(40);
8816 }
8817
8818 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8819 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8820 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8821 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8822 WDMAC_MODE_LNGREAD_ENAB);
8823
Matt Carlsonc5908932011-03-09 16:58:25 +00008824 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8825 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008826 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008827 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8828 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8829 /* nothing */
8830 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008831 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008832 val |= WDMAC_MODE_RX_ACCEL;
8833 }
8834 }
8835
Michael Chand9ab5ad2006-03-20 22:27:35 -08008836 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008837 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008838 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008839
Matt Carlson788a0352009-11-02 14:26:03 +00008840 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8841 val |= WDMAC_MODE_BURST_ALL_DATA;
8842
Linus Torvalds1da177e2005-04-16 15:20:36 -07008843 tw32_f(WDMAC_MODE, val);
8844 udelay(40);
8845
Joe Perches63c3a662011-04-26 08:12:10 +00008846 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008847 u16 pcix_cmd;
8848
8849 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8850 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008851 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008852 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8853 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008854 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008855 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8856 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008857 }
Matt Carlson9974a352007-10-07 23:27:28 -07008858 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8859 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008860 }
8861
8862 tw32_f(RDMAC_MODE, rdmac_mode);
8863 udelay(40);
8864
8865 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008866 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008867 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008868
8869 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8870 tw32(SNDDATAC_MODE,
8871 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8872 else
8873 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8874
Linus Torvalds1da177e2005-04-16 15:20:36 -07008875 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8876 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008877 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008878 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008879 val |= RCVDBDI_MODE_LRG_RING_SZ;
8880 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008881 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008882 if (tg3_flag(tp, HW_TSO_1) ||
8883 tg3_flag(tp, HW_TSO_2) ||
8884 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008885 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008886 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008887 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008888 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8889 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8891
8892 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8893 err = tg3_load_5701_a0_firmware_fix(tp);
8894 if (err)
8895 return err;
8896 }
8897
Joe Perches63c3a662011-04-26 08:12:10 +00008898 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008899 err = tg3_load_tso_firmware(tp);
8900 if (err)
8901 return err;
8902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008903
8904 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008905
Joe Perches63c3a662011-04-26 08:12:10 +00008906 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008907 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8908 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008909
8910 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8911 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8912 tp->tx_mode &= ~val;
8913 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8914 }
8915
Linus Torvalds1da177e2005-04-16 15:20:36 -07008916 tw32_f(MAC_TX_MODE, tp->tx_mode);
8917 udelay(100);
8918
Joe Perches63c3a662011-04-26 08:12:10 +00008919 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson9d53fa12011-07-20 10:20:54 +00008920 int i = 0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008921 u32 reg = MAC_RSS_INDIR_TBL_0;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008922
Matt Carlson9d53fa12011-07-20 10:20:54 +00008923 if (tp->irq_cnt == 2) {
8924 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i += 8) {
8925 tw32(reg, 0x0);
8926 reg += 4;
8927 }
8928 } else {
8929 u32 val;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008930
Matt Carlson9d53fa12011-07-20 10:20:54 +00008931 while (i < TG3_RSS_INDIR_TBL_SIZE) {
8932 val = i % (tp->irq_cnt - 1);
8933 i++;
8934 for (; i % 8; i++) {
8935 val <<= 4;
8936 val |= (i % (tp->irq_cnt - 1));
8937 }
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008938 tw32(reg, val);
8939 reg += 4;
8940 }
8941 }
8942
8943 /* Setup the "secret" hash key. */
8944 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8945 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8946 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8947 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8948 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8949 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8950 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8951 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8952 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8953 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8954 }
8955
Linus Torvalds1da177e2005-04-16 15:20:36 -07008956 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008957 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008958 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8959
Joe Perches63c3a662011-04-26 08:12:10 +00008960 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008961 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8962 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8963 RX_MODE_RSS_IPV6_HASH_EN |
8964 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8965 RX_MODE_RSS_IPV4_HASH_EN |
8966 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8967
Linus Torvalds1da177e2005-04-16 15:20:36 -07008968 tw32_f(MAC_RX_MODE, tp->rx_mode);
8969 udelay(10);
8970
Linus Torvalds1da177e2005-04-16 15:20:36 -07008971 tw32(MAC_LED_CTRL, tp->led_ctrl);
8972
8973 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008974 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008975 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8976 udelay(10);
8977 }
8978 tw32_f(MAC_RX_MODE, tp->rx_mode);
8979 udelay(10);
8980
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008981 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008982 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008983 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008984 /* Set drive transmission level to 1.2V */
8985 /* only if the signal pre-emphasis bit is not set */
8986 val = tr32(MAC_SERDES_CFG);
8987 val &= 0xfffff000;
8988 val |= 0x880;
8989 tw32(MAC_SERDES_CFG, val);
8990 }
8991 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8992 tw32(MAC_SERDES_CFG, 0x616000);
8993 }
8994
8995 /* Prevent chip from dropping frames when flow control
8996 * is enabled.
8997 */
Matt Carlson666bc832010-01-20 16:58:03 +00008998 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8999 val = 1;
9000 else
9001 val = 2;
9002 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009003
9004 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009005 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009006 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00009007 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009008 }
9009
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009010 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00009011 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08009012 u32 tmp;
9013
9014 tmp = tr32(SERDES_RX_CTRL);
9015 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
9016 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
9017 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
9018 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
9019 }
9020
Joe Perches63c3a662011-04-26 08:12:10 +00009021 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00009022 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
9023 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07009024 tp->link_config.speed = tp->link_config.orig_speed;
9025 tp->link_config.duplex = tp->link_config.orig_duplex;
9026 tp->link_config.autoneg = tp->link_config.orig_autoneg;
9027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009028
Matt Carlsondd477002008-05-25 23:45:58 -07009029 err = tg3_setup_phy(tp, 0);
9030 if (err)
9031 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009032
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009033 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
9034 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07009035 u32 tmp;
9036
9037 /* Clear CRC stats. */
9038 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
9039 tg3_writephy(tp, MII_TG3_TEST1,
9040 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009041 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07009042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009043 }
9044 }
9045
9046 __tg3_set_rx_mode(tp->dev);
9047
9048 /* Initialize receive rules. */
9049 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
9050 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
9051 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
9052 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
9053
Joe Perches63c3a662011-04-26 08:12:10 +00009054 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009055 limit = 8;
9056 else
9057 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00009058 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009059 limit -= 4;
9060 switch (limit) {
9061 case 16:
9062 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
9063 case 15:
9064 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
9065 case 14:
9066 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
9067 case 13:
9068 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
9069 case 12:
9070 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
9071 case 11:
9072 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
9073 case 10:
9074 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
9075 case 9:
9076 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
9077 case 8:
9078 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
9079 case 7:
9080 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
9081 case 6:
9082 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
9083 case 5:
9084 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
9085 case 4:
9086 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
9087 case 3:
9088 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
9089 case 2:
9090 case 1:
9091
9092 default:
9093 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07009094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009095
Joe Perches63c3a662011-04-26 08:12:10 +00009096 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07009097 /* Write our heartbeat update interval to APE. */
9098 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
9099 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07009100
Linus Torvalds1da177e2005-04-16 15:20:36 -07009101 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
9102
Linus Torvalds1da177e2005-04-16 15:20:36 -07009103 return 0;
9104}
9105
9106/* Called at device open time to get the chip ready for
9107 * packet processing. Invoked with tp->lock held.
9108 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009109static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009111 tg3_switch_clocks(tp);
9112
9113 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
9114
Matt Carlson2f751b62008-08-04 23:17:34 -07009115 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009116}
9117
9118#define TG3_STAT_ADD32(PSTAT, REG) \
9119do { u32 __val = tr32(REG); \
9120 (PSTAT)->low += __val; \
9121 if ((PSTAT)->low < __val) \
9122 (PSTAT)->high += 1; \
9123} while (0)
9124
9125static void tg3_periodic_fetch_stats(struct tg3 *tp)
9126{
9127 struct tg3_hw_stats *sp = tp->hw_stats;
9128
9129 if (!netif_carrier_ok(tp->dev))
9130 return;
9131
9132 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
9133 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
9134 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
9135 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
9136 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
9137 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
9138 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
9139 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
9140 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
9141 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
9142 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
9143 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
9144 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
9145
9146 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
9147 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
9148 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
9149 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
9150 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
9151 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
9152 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
9153 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
9154 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
9155 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
9156 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
9157 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
9158 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
9159 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07009160
9161 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00009162 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9163 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
9164 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00009165 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
9166 } else {
9167 u32 val = tr32(HOSTCC_FLOW_ATTN);
9168 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
9169 if (val) {
9170 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
9171 sp->rx_discards.low += val;
9172 if (sp->rx_discards.low < val)
9173 sp->rx_discards.high += 1;
9174 }
9175 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
9176 }
Michael Chan463d3052006-05-22 16:36:27 -07009177 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009178}
9179
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009180static void tg3_chk_missed_msi(struct tg3 *tp)
9181{
9182 u32 i;
9183
9184 for (i = 0; i < tp->irq_cnt; i++) {
9185 struct tg3_napi *tnapi = &tp->napi[i];
9186
9187 if (tg3_has_work(tnapi)) {
9188 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
9189 tnapi->last_tx_cons == tnapi->tx_cons) {
9190 if (tnapi->chk_msi_cnt < 1) {
9191 tnapi->chk_msi_cnt++;
9192 return;
9193 }
Matt Carlson7f230732011-08-31 11:44:48 +00009194 tg3_msi(0, tnapi);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009195 }
9196 }
9197 tnapi->chk_msi_cnt = 0;
9198 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
9199 tnapi->last_tx_cons = tnapi->tx_cons;
9200 }
9201}
9202
Linus Torvalds1da177e2005-04-16 15:20:36 -07009203static void tg3_timer(unsigned long __opaque)
9204{
9205 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009206
Michael Chanf475f162006-03-27 23:20:14 -08009207 if (tp->irq_sync)
9208 goto restart_timer;
9209
David S. Millerf47c11e2005-06-24 20:18:35 -07009210 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009211
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009212 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
9213 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
9214 tg3_chk_missed_msi(tp);
9215
Joe Perches63c3a662011-04-26 08:12:10 +00009216 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07009217 /* All of this garbage is because when using non-tagged
9218 * IRQ status the mailbox/status_block protocol the chip
9219 * uses with the cpu is race prone.
9220 */
Matt Carlson898a56f2009-08-28 14:02:40 +00009221 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07009222 tw32(GRC_LOCAL_CTRL,
9223 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
9224 } else {
9225 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009226 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07009227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009228
David S. Millerfac9b832005-05-18 22:46:34 -07009229 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +00009230 tg3_flag_set(tp, RESTART_TIMER);
David S. Millerf47c11e2005-06-24 20:18:35 -07009231 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07009232 schedule_work(&tp->reset_task);
9233 return;
9234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009235 }
9236
Linus Torvalds1da177e2005-04-16 15:20:36 -07009237 /* This part only runs once per second. */
9238 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009239 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07009240 tg3_periodic_fetch_stats(tp);
9241
Matt Carlsonb0c59432011-05-19 12:12:48 +00009242 if (tp->setlpicnt && !--tp->setlpicnt)
9243 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00009244
Joe Perches63c3a662011-04-26 08:12:10 +00009245 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009246 u32 mac_stat;
9247 int phy_event;
9248
9249 mac_stat = tr32(MAC_STATUS);
9250
9251 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009252 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009253 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
9254 phy_event = 1;
9255 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
9256 phy_event = 1;
9257
9258 if (phy_event)
9259 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00009260 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009261 u32 mac_stat = tr32(MAC_STATUS);
9262 int need_setup = 0;
9263
9264 if (netif_carrier_ok(tp->dev) &&
9265 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
9266 need_setup = 1;
9267 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00009268 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009269 (mac_stat & (MAC_STATUS_PCS_SYNCED |
9270 MAC_STATUS_SIGNAL_DET))) {
9271 need_setup = 1;
9272 }
9273 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07009274 if (!tp->serdes_counter) {
9275 tw32_f(MAC_MODE,
9276 (tp->mac_mode &
9277 ~MAC_MODE_PORT_MODE_MASK));
9278 udelay(40);
9279 tw32_f(MAC_MODE, tp->mac_mode);
9280 udelay(40);
9281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009282 tg3_setup_phy(tp, 0);
9283 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009284 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00009285 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07009286 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00009287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009288
9289 tp->timer_counter = tp->timer_multiplier;
9290 }
9291
Michael Chan130b8e42006-09-27 16:00:40 -07009292 /* Heartbeat is only sent once every 2 seconds.
9293 *
9294 * The heartbeat is to tell the ASF firmware that the host
9295 * driver is still alive. In the event that the OS crashes,
9296 * ASF needs to reset the hardware to free up the FIFO space
9297 * that may be filled with rx packets destined for the host.
9298 * If the FIFO is full, ASF will no longer function properly.
9299 *
9300 * Unintended resets have been reported on real time kernels
9301 * where the timer doesn't run on time. Netpoll will also have
9302 * same problem.
9303 *
9304 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
9305 * to check the ring condition when the heartbeat is expiring
9306 * before doing the reset. This will prevent most unintended
9307 * resets.
9308 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009309 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00009310 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009311 tg3_wait_for_event_ack(tp);
9312
Michael Chanbbadf502006-04-06 21:46:34 -07009313 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009314 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009315 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009316 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9317 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009318
9319 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009320 }
9321 tp->asf_counter = tp->asf_multiplier;
9322 }
9323
David S. Millerf47c11e2005-06-24 20:18:35 -07009324 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009325
Michael Chanf475f162006-03-27 23:20:14 -08009326restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009327 tp->timer.expires = jiffies + tp->timer_offset;
9328 add_timer(&tp->timer);
9329}
9330
Matt Carlson4f125f42009-09-01 12:55:02 +00009331static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009332{
David Howells7d12e782006-10-05 14:55:46 +01009333 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009334 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009335 char *name;
9336 struct tg3_napi *tnapi = &tp->napi[irq_num];
9337
9338 if (tp->irq_cnt == 1)
9339 name = tp->dev->name;
9340 else {
9341 name = &tnapi->irq_lbl[0];
9342 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9343 name[IFNAMSIZ-1] = 0;
9344 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009345
Joe Perches63c3a662011-04-26 08:12:10 +00009346 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009347 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009348 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009349 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009350 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009351 } else {
9352 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009353 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009354 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009355 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009356 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009357
9358 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009359}
9360
Michael Chan79381092005-04-21 17:13:59 -07009361static int tg3_test_interrupt(struct tg3 *tp)
9362{
Matt Carlson09943a12009-08-28 14:01:57 +00009363 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009364 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009365 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009366 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009367
Michael Chand4bc3922005-05-29 14:59:20 -07009368 if (!netif_running(dev))
9369 return -ENODEV;
9370
Michael Chan79381092005-04-21 17:13:59 -07009371 tg3_disable_ints(tp);
9372
Matt Carlson4f125f42009-09-01 12:55:02 +00009373 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009374
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009375 /*
9376 * Turn off MSI one shot mode. Otherwise this test has no
9377 * observable way to know whether the interrupt was delivered.
9378 */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009379 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009380 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9381 tw32(MSGINT_MODE, val);
9382 }
9383
Matt Carlson4f125f42009-09-01 12:55:02 +00009384 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009385 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009386 if (err)
9387 return err;
9388
Matt Carlson898a56f2009-08-28 14:02:40 +00009389 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009390 tg3_enable_ints(tp);
9391
9392 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009393 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009394
9395 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009396 u32 int_mbox, misc_host_ctrl;
9397
Matt Carlson898a56f2009-08-28 14:02:40 +00009398 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009399 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9400
9401 if ((int_mbox != 0) ||
9402 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9403 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009404 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009405 }
9406
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009407 if (tg3_flag(tp, 57765_PLUS) &&
9408 tnapi->hw_status->status_tag != tnapi->last_tag)
9409 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
9410
Michael Chan79381092005-04-21 17:13:59 -07009411 msleep(10);
9412 }
9413
9414 tg3_disable_ints(tp);
9415
Matt Carlson4f125f42009-09-01 12:55:02 +00009416 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009417
Matt Carlson4f125f42009-09-01 12:55:02 +00009418 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009419
9420 if (err)
9421 return err;
9422
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009423 if (intr_ok) {
9424 /* Reenable MSI one shot mode. */
Matt Carlson3aa1cdf2011-07-20 10:20:55 +00009425 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009426 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9427 tw32(MSGINT_MODE, val);
9428 }
Michael Chan79381092005-04-21 17:13:59 -07009429 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009430 }
Michael Chan79381092005-04-21 17:13:59 -07009431
9432 return -EIO;
9433}
9434
9435/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9436 * successfully restored
9437 */
9438static int tg3_test_msi(struct tg3 *tp)
9439{
Michael Chan79381092005-04-21 17:13:59 -07009440 int err;
9441 u16 pci_cmd;
9442
Joe Perches63c3a662011-04-26 08:12:10 +00009443 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009444 return 0;
9445
9446 /* Turn off SERR reporting in case MSI terminates with Master
9447 * Abort.
9448 */
9449 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9450 pci_write_config_word(tp->pdev, PCI_COMMAND,
9451 pci_cmd & ~PCI_COMMAND_SERR);
9452
9453 err = tg3_test_interrupt(tp);
9454
9455 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9456
9457 if (!err)
9458 return 0;
9459
9460 /* other failures */
9461 if (err != -EIO)
9462 return err;
9463
9464 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009465 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9466 "to INTx mode. Please report this failure to the PCI "
9467 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009468
Matt Carlson4f125f42009-09-01 12:55:02 +00009469 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009470
Michael Chan79381092005-04-21 17:13:59 -07009471 pci_disable_msi(tp->pdev);
9472
Joe Perches63c3a662011-04-26 08:12:10 +00009473 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009474 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009475
Matt Carlson4f125f42009-09-01 12:55:02 +00009476 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009477 if (err)
9478 return err;
9479
9480 /* Need to reset the chip because the MSI cycle may have terminated
9481 * with Master Abort.
9482 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009483 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009484
Michael Chan944d9802005-05-29 14:57:48 -07009485 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009486 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009487
David S. Millerf47c11e2005-06-24 20:18:35 -07009488 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009489
9490 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009491 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009492
9493 return err;
9494}
9495
Matt Carlson9e9fd122009-01-19 16:57:45 -08009496static int tg3_request_firmware(struct tg3 *tp)
9497{
9498 const __be32 *fw_data;
9499
9500 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009501 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9502 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009503 return -ENOENT;
9504 }
9505
9506 fw_data = (void *)tp->fw->data;
9507
9508 /* Firmware blob starts with version numbers, followed by
9509 * start address and _full_ length including BSS sections
9510 * (which must be longer than the actual data, of course
9511 */
9512
9513 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9514 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009515 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9516 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009517 release_firmware(tp->fw);
9518 tp->fw = NULL;
9519 return -EINVAL;
9520 }
9521
9522 /* We no longer need firmware; we have it. */
9523 tp->fw_needed = NULL;
9524 return 0;
9525}
9526
Matt Carlson679563f2009-09-01 12:55:46 +00009527static bool tg3_enable_msix(struct tg3 *tp)
9528{
9529 int i, rc, cpus = num_online_cpus();
9530 struct msix_entry msix_ent[tp->irq_max];
9531
9532 if (cpus == 1)
9533 /* Just fallback to the simpler MSI mode. */
9534 return false;
9535
9536 /*
9537 * We want as many rx rings enabled as there are cpus.
9538 * The first MSIX vector only deals with link interrupts, etc,
9539 * so we add one to the number of vectors we are requesting.
9540 */
9541 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9542
9543 for (i = 0; i < tp->irq_max; i++) {
9544 msix_ent[i].entry = i;
9545 msix_ent[i].vector = 0;
9546 }
9547
9548 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009549 if (rc < 0) {
9550 return false;
9551 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009552 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9553 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009554 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9555 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009556 tp->irq_cnt = rc;
9557 }
9558
9559 for (i = 0; i < tp->irq_max; i++)
9560 tp->napi[i].irq_vec = msix_ent[i].vector;
9561
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009562 netif_set_real_num_tx_queues(tp->dev, 1);
9563 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9564 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9565 pci_disable_msix(tp->pdev);
9566 return false;
9567 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009568
9569 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009570 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009571
9572 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9573 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009574 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009575 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9576 }
9577 }
Matt Carlson2430b032010-06-05 17:24:34 +00009578
Matt Carlson679563f2009-09-01 12:55:46 +00009579 return true;
9580}
9581
Matt Carlson07b01732009-08-28 14:01:15 +00009582static void tg3_ints_init(struct tg3 *tp)
9583{
Joe Perches63c3a662011-04-26 08:12:10 +00009584 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9585 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009586 /* All MSI supporting chips should support tagged
9587 * status. Assert that this is the case.
9588 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009589 netdev_warn(tp->dev,
9590 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009591 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009592 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009593
Joe Perches63c3a662011-04-26 08:12:10 +00009594 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9595 tg3_flag_set(tp, USING_MSIX);
9596 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9597 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009598
Joe Perches63c3a662011-04-26 08:12:10 +00009599 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009600 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009601 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009602 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009603 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9604 }
9605defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009606 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009607 tp->irq_cnt = 1;
9608 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009609 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009610 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009611 }
Matt Carlson07b01732009-08-28 14:01:15 +00009612}
9613
9614static void tg3_ints_fini(struct tg3 *tp)
9615{
Joe Perches63c3a662011-04-26 08:12:10 +00009616 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009617 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009618 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009619 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009620 tg3_flag_clear(tp, USING_MSI);
9621 tg3_flag_clear(tp, USING_MSIX);
9622 tg3_flag_clear(tp, ENABLE_RSS);
9623 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009624}
9625
Linus Torvalds1da177e2005-04-16 15:20:36 -07009626static int tg3_open(struct net_device *dev)
9627{
9628 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009629 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009630
Matt Carlson9e9fd122009-01-19 16:57:45 -08009631 if (tp->fw_needed) {
9632 err = tg3_request_firmware(tp);
9633 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9634 if (err)
9635 return err;
9636 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009637 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009638 tg3_flag_clear(tp, TSO_CAPABLE);
9639 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009640 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009641 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009642 }
9643 }
9644
Michael Chanc49a1562006-12-17 17:07:29 -08009645 netif_carrier_off(tp->dev);
9646
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009647 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009648 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009649 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009650
9651 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009652
Linus Torvalds1da177e2005-04-16 15:20:36 -07009653 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009654 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009655
David S. Millerf47c11e2005-06-24 20:18:35 -07009656 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009657
Matt Carlson679563f2009-09-01 12:55:46 +00009658 /*
9659 * Setup interrupts first so we know how
9660 * many NAPI resources to allocate
9661 */
9662 tg3_ints_init(tp);
9663
Linus Torvalds1da177e2005-04-16 15:20:36 -07009664 /* The placement of this call is tied
9665 * to the setup and use of Host TX descriptors.
9666 */
9667 err = tg3_alloc_consistent(tp);
9668 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009669 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009670
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009671 tg3_napi_init(tp);
9672
Matt Carlsonfed97812009-09-01 13:10:19 +00009673 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009674
Matt Carlson4f125f42009-09-01 12:55:02 +00009675 for (i = 0; i < tp->irq_cnt; i++) {
9676 struct tg3_napi *tnapi = &tp->napi[i];
9677 err = tg3_request_irq(tp, i);
9678 if (err) {
9679 for (i--; i >= 0; i--)
9680 free_irq(tnapi->irq_vec, tnapi);
9681 break;
9682 }
9683 }
Matt Carlson07b01732009-08-28 14:01:15 +00009684
9685 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009686 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009687
David S. Millerf47c11e2005-06-24 20:18:35 -07009688 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009689
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009690 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009691 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009692 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009693 tg3_free_rings(tp);
9694 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009695 if (tg3_flag(tp, TAGGED_STATUS) &&
9696 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9697 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765)
David S. Millerfac9b832005-05-18 22:46:34 -07009698 tp->timer_offset = HZ;
9699 else
9700 tp->timer_offset = HZ / 10;
9701
9702 BUG_ON(tp->timer_offset > HZ);
9703 tp->timer_counter = tp->timer_multiplier =
9704 (HZ / tp->timer_offset);
9705 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009706 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009707
9708 init_timer(&tp->timer);
9709 tp->timer.expires = jiffies + tp->timer_offset;
9710 tp->timer.data = (unsigned long) tp;
9711 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009712 }
9713
David S. Millerf47c11e2005-06-24 20:18:35 -07009714 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009715
Matt Carlson07b01732009-08-28 14:01:15 +00009716 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009717 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009718
Joe Perches63c3a662011-04-26 08:12:10 +00009719 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009720 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009721
Michael Chan79381092005-04-21 17:13:59 -07009722 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009723 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009724 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009725 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009726 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009727
Matt Carlson679563f2009-09-01 12:55:46 +00009728 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009729 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009730
Joe Perches63c3a662011-04-26 08:12:10 +00009731 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009732 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009733
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009734 tw32(PCIE_TRANSACTION_CFG,
9735 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009736 }
Michael Chan79381092005-04-21 17:13:59 -07009737 }
9738
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009739 tg3_phy_start(tp);
9740
David S. Millerf47c11e2005-06-24 20:18:35 -07009741 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009742
Michael Chan79381092005-04-21 17:13:59 -07009743 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009744 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009745 tg3_enable_ints(tp);
9746
David S. Millerf47c11e2005-06-24 20:18:35 -07009747 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009748
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009749 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009750
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009751 /*
9752 * Reset loopback feature if it was turned on while the device was down
9753 * make sure that it's installed properly now.
9754 */
9755 if (dev->features & NETIF_F_LOOPBACK)
9756 tg3_set_loopback(dev, dev->features);
9757
Linus Torvalds1da177e2005-04-16 15:20:36 -07009758 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009759
Matt Carlson679563f2009-09-01 12:55:46 +00009760err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009761 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9762 struct tg3_napi *tnapi = &tp->napi[i];
9763 free_irq(tnapi->irq_vec, tnapi);
9764 }
Matt Carlson07b01732009-08-28 14:01:15 +00009765
Matt Carlson679563f2009-09-01 12:55:46 +00009766err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009767 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009768 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009769 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009770
9771err_out1:
9772 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +00009773 tg3_frob_aux_power(tp, false);
9774 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +00009775 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009776}
9777
Eric Dumazet511d2222010-07-07 20:44:24 +00009778static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9779 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009780static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9781
9782static int tg3_close(struct net_device *dev)
9783{
Matt Carlson4f125f42009-09-01 12:55:02 +00009784 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785 struct tg3 *tp = netdev_priv(dev);
9786
Matt Carlsonfed97812009-09-01 13:10:19 +00009787 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009788 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009789
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009790 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009791
9792 del_timer_sync(&tp->timer);
9793
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009794 tg3_phy_stop(tp);
9795
David S. Millerf47c11e2005-06-24 20:18:35 -07009796 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009797
9798 tg3_disable_ints(tp);
9799
Michael Chan944d9802005-05-29 14:57:48 -07009800 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009801 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009802 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803
David S. Millerf47c11e2005-06-24 20:18:35 -07009804 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805
Matt Carlson4f125f42009-09-01 12:55:02 +00009806 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9807 struct tg3_napi *tnapi = &tp->napi[i];
9808 free_irq(tnapi->irq_vec, tnapi);
9809 }
Matt Carlson07b01732009-08-28 14:01:15 +00009810
9811 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009812
Eric Dumazet511d2222010-07-07 20:44:24 +00009813 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9814
Linus Torvalds1da177e2005-04-16 15:20:36 -07009815 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9816 sizeof(tp->estats_prev));
9817
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009818 tg3_napi_fini(tp);
9819
Linus Torvalds1da177e2005-04-16 15:20:36 -07009820 tg3_free_consistent(tp);
9821
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009822 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009823
9824 netif_carrier_off(tp->dev);
9825
Linus Torvalds1da177e2005-04-16 15:20:36 -07009826 return 0;
9827}
9828
Eric Dumazet511d2222010-07-07 20:44:24 +00009829static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009830{
9831 return ((u64)val->high << 32) | ((u64)val->low);
9832}
9833
Eric Dumazet511d2222010-07-07 20:44:24 +00009834static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009835{
9836 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9837
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009838 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009839 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9840 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009841 u32 val;
9842
David S. Millerf47c11e2005-06-24 20:18:35 -07009843 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009844 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9845 tg3_writephy(tp, MII_TG3_TEST1,
9846 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009847 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009848 } else
9849 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009850 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009851
9852 tp->phy_crc_errors += val;
9853
9854 return tp->phy_crc_errors;
9855 }
9856
9857 return get_stat64(&hw_stats->rx_fcs_errors);
9858}
9859
9860#define ESTAT_ADD(member) \
9861 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009862 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009863
9864static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9865{
9866 struct tg3_ethtool_stats *estats = &tp->estats;
9867 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9868 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9869
9870 if (!hw_stats)
9871 return old_estats;
9872
9873 ESTAT_ADD(rx_octets);
9874 ESTAT_ADD(rx_fragments);
9875 ESTAT_ADD(rx_ucast_packets);
9876 ESTAT_ADD(rx_mcast_packets);
9877 ESTAT_ADD(rx_bcast_packets);
9878 ESTAT_ADD(rx_fcs_errors);
9879 ESTAT_ADD(rx_align_errors);
9880 ESTAT_ADD(rx_xon_pause_rcvd);
9881 ESTAT_ADD(rx_xoff_pause_rcvd);
9882 ESTAT_ADD(rx_mac_ctrl_rcvd);
9883 ESTAT_ADD(rx_xoff_entered);
9884 ESTAT_ADD(rx_frame_too_long_errors);
9885 ESTAT_ADD(rx_jabbers);
9886 ESTAT_ADD(rx_undersize_packets);
9887 ESTAT_ADD(rx_in_length_errors);
9888 ESTAT_ADD(rx_out_length_errors);
9889 ESTAT_ADD(rx_64_or_less_octet_packets);
9890 ESTAT_ADD(rx_65_to_127_octet_packets);
9891 ESTAT_ADD(rx_128_to_255_octet_packets);
9892 ESTAT_ADD(rx_256_to_511_octet_packets);
9893 ESTAT_ADD(rx_512_to_1023_octet_packets);
9894 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9895 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9896 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9897 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9898 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9899
9900 ESTAT_ADD(tx_octets);
9901 ESTAT_ADD(tx_collisions);
9902 ESTAT_ADD(tx_xon_sent);
9903 ESTAT_ADD(tx_xoff_sent);
9904 ESTAT_ADD(tx_flow_control);
9905 ESTAT_ADD(tx_mac_errors);
9906 ESTAT_ADD(tx_single_collisions);
9907 ESTAT_ADD(tx_mult_collisions);
9908 ESTAT_ADD(tx_deferred);
9909 ESTAT_ADD(tx_excessive_collisions);
9910 ESTAT_ADD(tx_late_collisions);
9911 ESTAT_ADD(tx_collide_2times);
9912 ESTAT_ADD(tx_collide_3times);
9913 ESTAT_ADD(tx_collide_4times);
9914 ESTAT_ADD(tx_collide_5times);
9915 ESTAT_ADD(tx_collide_6times);
9916 ESTAT_ADD(tx_collide_7times);
9917 ESTAT_ADD(tx_collide_8times);
9918 ESTAT_ADD(tx_collide_9times);
9919 ESTAT_ADD(tx_collide_10times);
9920 ESTAT_ADD(tx_collide_11times);
9921 ESTAT_ADD(tx_collide_12times);
9922 ESTAT_ADD(tx_collide_13times);
9923 ESTAT_ADD(tx_collide_14times);
9924 ESTAT_ADD(tx_collide_15times);
9925 ESTAT_ADD(tx_ucast_packets);
9926 ESTAT_ADD(tx_mcast_packets);
9927 ESTAT_ADD(tx_bcast_packets);
9928 ESTAT_ADD(tx_carrier_sense_errors);
9929 ESTAT_ADD(tx_discards);
9930 ESTAT_ADD(tx_errors);
9931
9932 ESTAT_ADD(dma_writeq_full);
9933 ESTAT_ADD(dma_write_prioq_full);
9934 ESTAT_ADD(rxbds_empty);
9935 ESTAT_ADD(rx_discards);
9936 ESTAT_ADD(rx_errors);
9937 ESTAT_ADD(rx_threshold_hit);
9938
9939 ESTAT_ADD(dma_readq_full);
9940 ESTAT_ADD(dma_read_prioq_full);
9941 ESTAT_ADD(tx_comp_queue_full);
9942
9943 ESTAT_ADD(ring_set_send_prod_index);
9944 ESTAT_ADD(ring_status_update);
9945 ESTAT_ADD(nic_irqs);
9946 ESTAT_ADD(nic_avoided_irqs);
9947 ESTAT_ADD(nic_tx_threshold_hit);
9948
Matt Carlson4452d092011-05-19 12:12:51 +00009949 ESTAT_ADD(mbuf_lwm_thresh_hit);
9950
Linus Torvalds1da177e2005-04-16 15:20:36 -07009951 return estats;
9952}
9953
Eric Dumazet511d2222010-07-07 20:44:24 +00009954static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9955 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009956{
9957 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009958 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009959 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9960
9961 if (!hw_stats)
9962 return old_stats;
9963
9964 stats->rx_packets = old_stats->rx_packets +
9965 get_stat64(&hw_stats->rx_ucast_packets) +
9966 get_stat64(&hw_stats->rx_mcast_packets) +
9967 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009968
Linus Torvalds1da177e2005-04-16 15:20:36 -07009969 stats->tx_packets = old_stats->tx_packets +
9970 get_stat64(&hw_stats->tx_ucast_packets) +
9971 get_stat64(&hw_stats->tx_mcast_packets) +
9972 get_stat64(&hw_stats->tx_bcast_packets);
9973
9974 stats->rx_bytes = old_stats->rx_bytes +
9975 get_stat64(&hw_stats->rx_octets);
9976 stats->tx_bytes = old_stats->tx_bytes +
9977 get_stat64(&hw_stats->tx_octets);
9978
9979 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009980 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009981 stats->tx_errors = old_stats->tx_errors +
9982 get_stat64(&hw_stats->tx_errors) +
9983 get_stat64(&hw_stats->tx_mac_errors) +
9984 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9985 get_stat64(&hw_stats->tx_discards);
9986
9987 stats->multicast = old_stats->multicast +
9988 get_stat64(&hw_stats->rx_mcast_packets);
9989 stats->collisions = old_stats->collisions +
9990 get_stat64(&hw_stats->tx_collisions);
9991
9992 stats->rx_length_errors = old_stats->rx_length_errors +
9993 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9994 get_stat64(&hw_stats->rx_undersize_packets);
9995
9996 stats->rx_over_errors = old_stats->rx_over_errors +
9997 get_stat64(&hw_stats->rxbds_empty);
9998 stats->rx_frame_errors = old_stats->rx_frame_errors +
9999 get_stat64(&hw_stats->rx_align_errors);
10000 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10001 get_stat64(&hw_stats->tx_discards);
10002 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10003 get_stat64(&hw_stats->tx_carrier_sense_errors);
10004
10005 stats->rx_crc_errors = old_stats->rx_crc_errors +
10006 calc_crc_errors(tp);
10007
John W. Linville4f63b872005-09-12 14:43:18 -070010008 stats->rx_missed_errors = old_stats->rx_missed_errors +
10009 get_stat64(&hw_stats->rx_discards);
10010
Eric Dumazetb0057c52010-10-10 19:55:52 +000010011 stats->rx_dropped = tp->rx_dropped;
10012
Linus Torvalds1da177e2005-04-16 15:20:36 -070010013 return stats;
10014}
10015
10016static inline u32 calc_crc(unsigned char *buf, int len)
10017{
10018 u32 reg;
10019 u32 tmp;
10020 int j, k;
10021
10022 reg = 0xffffffff;
10023
10024 for (j = 0; j < len; j++) {
10025 reg ^= buf[j];
10026
10027 for (k = 0; k < 8; k++) {
10028 tmp = reg & 0x01;
10029
10030 reg >>= 1;
10031
Matt Carlson859a5882010-04-05 10:19:28 +000010032 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010033 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010034 }
10035 }
10036
10037 return ~reg;
10038}
10039
10040static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
10041{
10042 /* accept or reject all multicast frames */
10043 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
10044 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
10045 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
10046 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
10047}
10048
10049static void __tg3_set_rx_mode(struct net_device *dev)
10050{
10051 struct tg3 *tp = netdev_priv(dev);
10052 u32 rx_mode;
10053
10054 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
10055 RX_MODE_KEEP_VLAN_TAG);
10056
Matt Carlsonbf933c82011-01-25 15:58:49 +000010057#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010058 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
10059 * flag clear.
10060 */
Joe Perches63c3a662011-04-26 08:12:10 +000010061 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010062 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
10063#endif
10064
10065 if (dev->flags & IFF_PROMISC) {
10066 /* Promiscuous mode. */
10067 rx_mode |= RX_MODE_PROMISC;
10068 } else if (dev->flags & IFF_ALLMULTI) {
10069 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010070 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +000010071 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010072 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010073 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010074 } else {
10075 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +000010076 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010077 u32 mc_filter[4] = { 0, };
10078 u32 regidx;
10079 u32 bit;
10080 u32 crc;
10081
Jiri Pirko22bedad2010-04-01 21:22:57 +000010082 netdev_for_each_mc_addr(ha, dev) {
10083 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084 bit = ~crc & 0x7f;
10085 regidx = (bit & 0x60) >> 5;
10086 bit &= 0x1f;
10087 mc_filter[regidx] |= (1 << bit);
10088 }
10089
10090 tw32(MAC_HASH_REG_0, mc_filter[0]);
10091 tw32(MAC_HASH_REG_1, mc_filter[1]);
10092 tw32(MAC_HASH_REG_2, mc_filter[2]);
10093 tw32(MAC_HASH_REG_3, mc_filter[3]);
10094 }
10095
10096 if (rx_mode != tp->rx_mode) {
10097 tp->rx_mode = rx_mode;
10098 tw32_f(MAC_RX_MODE, rx_mode);
10099 udelay(10);
10100 }
10101}
10102
10103static void tg3_set_rx_mode(struct net_device *dev)
10104{
10105 struct tg3 *tp = netdev_priv(dev);
10106
Michael Chane75f7c92006-03-20 21:33:26 -080010107 if (!netif_running(dev))
10108 return;
10109
David S. Millerf47c11e2005-06-24 20:18:35 -070010110 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010111 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -070010112 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113}
10114
Linus Torvalds1da177e2005-04-16 15:20:36 -070010115static int tg3_get_regs_len(struct net_device *dev)
10116{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010117 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010118}
10119
10120static void tg3_get_regs(struct net_device *dev,
10121 struct ethtool_regs *regs, void *_p)
10122{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010123 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010124
10125 regs->version = 0;
10126
Matt Carlson97bd8e42011-04-13 11:05:04 +000010127 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010128
Matt Carlson80096062010-08-02 11:26:06 +000010129 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010130 return;
10131
David S. Millerf47c11e2005-06-24 20:18:35 -070010132 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133
Matt Carlson97bd8e42011-04-13 11:05:04 +000010134 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010135
David S. Millerf47c11e2005-06-24 20:18:35 -070010136 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010137}
10138
10139static int tg3_get_eeprom_len(struct net_device *dev)
10140{
10141 struct tg3 *tp = netdev_priv(dev);
10142
10143 return tp->nvram_size;
10144}
10145
Linus Torvalds1da177e2005-04-16 15:20:36 -070010146static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10147{
10148 struct tg3 *tp = netdev_priv(dev);
10149 int ret;
10150 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010151 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010152 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010153
Joe Perches63c3a662011-04-26 08:12:10 +000010154 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010155 return -EINVAL;
10156
Matt Carlson80096062010-08-02 11:26:06 +000010157 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010158 return -EAGAIN;
10159
Linus Torvalds1da177e2005-04-16 15:20:36 -070010160 offset = eeprom->offset;
10161 len = eeprom->len;
10162 eeprom->len = 0;
10163
10164 eeprom->magic = TG3_EEPROM_MAGIC;
10165
10166 if (offset & 3) {
10167 /* adjustments to start on required 4 byte boundary */
10168 b_offset = offset & 3;
10169 b_count = 4 - b_offset;
10170 if (b_count > len) {
10171 /* i.e. offset=1 len=2 */
10172 b_count = len;
10173 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010174 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010175 if (ret)
10176 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010177 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010178 len -= b_count;
10179 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010180 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010181 }
10182
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010183 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010184 pd = &data[eeprom->len];
10185 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010186 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010187 if (ret) {
10188 eeprom->len += i;
10189 return ret;
10190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010191 memcpy(pd + i, &val, 4);
10192 }
10193 eeprom->len += i;
10194
10195 if (len & 3) {
10196 /* read last bytes not ending on 4 byte boundary */
10197 pd = &data[eeprom->len];
10198 b_count = len & 3;
10199 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010200 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010201 if (ret)
10202 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010203 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010204 eeprom->len += b_count;
10205 }
10206 return 0;
10207}
10208
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010209static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010210
10211static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10212{
10213 struct tg3 *tp = netdev_priv(dev);
10214 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010215 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010216 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010217 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010218
Matt Carlson80096062010-08-02 11:26:06 +000010219 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010220 return -EAGAIN;
10221
Joe Perches63c3a662011-04-26 08:12:10 +000010222 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010223 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010224 return -EINVAL;
10225
10226 offset = eeprom->offset;
10227 len = eeprom->len;
10228
10229 if ((b_offset = (offset & 3))) {
10230 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010231 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010232 if (ret)
10233 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010234 len += b_offset;
10235 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010236 if (len < 4)
10237 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010238 }
10239
10240 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010241 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010242 /* adjustments to end on required 4 byte boundary */
10243 odd_len = 1;
10244 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010245 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010246 if (ret)
10247 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010248 }
10249
10250 buf = data;
10251 if (b_offset || odd_len) {
10252 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010253 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010254 return -ENOMEM;
10255 if (b_offset)
10256 memcpy(buf, &start, 4);
10257 if (odd_len)
10258 memcpy(buf+len-4, &end, 4);
10259 memcpy(buf + b_offset, data, eeprom->len);
10260 }
10261
10262 ret = tg3_nvram_write_block(tp, offset, len, buf);
10263
10264 if (buf != data)
10265 kfree(buf);
10266
10267 return ret;
10268}
10269
10270static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10271{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010272 struct tg3 *tp = netdev_priv(dev);
10273
Joe Perches63c3a662011-04-26 08:12:10 +000010274 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010275 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010276 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010277 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010278 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10279 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010280 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010281
Linus Torvalds1da177e2005-04-16 15:20:36 -070010282 cmd->supported = (SUPPORTED_Autoneg);
10283
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010284 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010285 cmd->supported |= (SUPPORTED_1000baseT_Half |
10286 SUPPORTED_1000baseT_Full);
10287
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010288 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010289 cmd->supported |= (SUPPORTED_100baseT_Half |
10290 SUPPORTED_100baseT_Full |
10291 SUPPORTED_10baseT_Half |
10292 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010293 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010294 cmd->port = PORT_TP;
10295 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010296 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010297 cmd->port = PORT_FIBRE;
10298 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010299
Linus Torvalds1da177e2005-04-16 15:20:36 -070010300 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010301 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10302 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10303 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10304 cmd->advertising |= ADVERTISED_Pause;
10305 } else {
10306 cmd->advertising |= ADVERTISED_Pause |
10307 ADVERTISED_Asym_Pause;
10308 }
10309 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10310 cmd->advertising |= ADVERTISED_Asym_Pause;
10311 }
10312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010313 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010314 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010315 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +000010316 } else {
David Decotigny70739492011-04-27 18:32:40 +000010317 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010318 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010319 }
Matt Carlson882e9792009-09-01 13:21:36 +000010320 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010321 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010322 cmd->autoneg = tp->link_config.autoneg;
10323 cmd->maxtxpkt = 0;
10324 cmd->maxrxpkt = 0;
10325 return 0;
10326}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010327
Linus Torvalds1da177e2005-04-16 15:20:36 -070010328static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10329{
10330 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010331 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010332
Joe Perches63c3a662011-04-26 08:12:10 +000010333 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010334 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010335 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010336 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010337 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10338 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010339 }
10340
Matt Carlson7e5856b2009-02-25 14:23:01 +000010341 if (cmd->autoneg != AUTONEG_ENABLE &&
10342 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010343 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010344
10345 if (cmd->autoneg == AUTONEG_DISABLE &&
10346 cmd->duplex != DUPLEX_FULL &&
10347 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010348 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010349
Matt Carlson7e5856b2009-02-25 14:23:01 +000010350 if (cmd->autoneg == AUTONEG_ENABLE) {
10351 u32 mask = ADVERTISED_Autoneg |
10352 ADVERTISED_Pause |
10353 ADVERTISED_Asym_Pause;
10354
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010355 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010356 mask |= ADVERTISED_1000baseT_Half |
10357 ADVERTISED_1000baseT_Full;
10358
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010359 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010360 mask |= ADVERTISED_100baseT_Half |
10361 ADVERTISED_100baseT_Full |
10362 ADVERTISED_10baseT_Half |
10363 ADVERTISED_10baseT_Full |
10364 ADVERTISED_TP;
10365 else
10366 mask |= ADVERTISED_FIBRE;
10367
10368 if (cmd->advertising & ~mask)
10369 return -EINVAL;
10370
10371 mask &= (ADVERTISED_1000baseT_Half |
10372 ADVERTISED_1000baseT_Full |
10373 ADVERTISED_100baseT_Half |
10374 ADVERTISED_100baseT_Full |
10375 ADVERTISED_10baseT_Half |
10376 ADVERTISED_10baseT_Full);
10377
10378 cmd->advertising &= mask;
10379 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010380 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010381 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010382 return -EINVAL;
10383
10384 if (cmd->duplex != DUPLEX_FULL)
10385 return -EINVAL;
10386 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010387 if (speed != SPEED_100 &&
10388 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010389 return -EINVAL;
10390 }
10391 }
10392
David S. Millerf47c11e2005-06-24 20:18:35 -070010393 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010394
10395 tp->link_config.autoneg = cmd->autoneg;
10396 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010397 tp->link_config.advertising = (cmd->advertising |
10398 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010399 tp->link_config.speed = SPEED_INVALID;
10400 tp->link_config.duplex = DUPLEX_INVALID;
10401 } else {
10402 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010403 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010404 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010405 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010406
Michael Chan24fcad62006-12-17 17:06:46 -080010407 tp->link_config.orig_speed = tp->link_config.speed;
10408 tp->link_config.orig_duplex = tp->link_config.duplex;
10409 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10410
Linus Torvalds1da177e2005-04-16 15:20:36 -070010411 if (netif_running(dev))
10412 tg3_setup_phy(tp, 1);
10413
David S. Millerf47c11e2005-06-24 20:18:35 -070010414 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010415
Linus Torvalds1da177e2005-04-16 15:20:36 -070010416 return 0;
10417}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010418
Linus Torvalds1da177e2005-04-16 15:20:36 -070010419static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10420{
10421 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010422
Linus Torvalds1da177e2005-04-16 15:20:36 -070010423 strcpy(info->driver, DRV_MODULE_NAME);
10424 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010425 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010426 strcpy(info->bus_info, pci_name(tp->pdev));
10427}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010428
Linus Torvalds1da177e2005-04-16 15:20:36 -070010429static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10430{
10431 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010432
Joe Perches63c3a662011-04-26 08:12:10 +000010433 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010434 wol->supported = WAKE_MAGIC;
10435 else
10436 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010437 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010438 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010439 wol->wolopts = WAKE_MAGIC;
10440 memset(&wol->sopass, 0, sizeof(wol->sopass));
10441}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010442
Linus Torvalds1da177e2005-04-16 15:20:36 -070010443static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10444{
10445 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010446 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010447
Linus Torvalds1da177e2005-04-16 15:20:36 -070010448 if (wol->wolopts & ~WAKE_MAGIC)
10449 return -EINVAL;
10450 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010451 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010452 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010453
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010454 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10455
David S. Millerf47c11e2005-06-24 20:18:35 -070010456 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010457 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010458 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010459 else
Joe Perches63c3a662011-04-26 08:12:10 +000010460 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010461 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010462
Linus Torvalds1da177e2005-04-16 15:20:36 -070010463 return 0;
10464}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010465
Linus Torvalds1da177e2005-04-16 15:20:36 -070010466static u32 tg3_get_msglevel(struct net_device *dev)
10467{
10468 struct tg3 *tp = netdev_priv(dev);
10469 return tp->msg_enable;
10470}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010471
Linus Torvalds1da177e2005-04-16 15:20:36 -070010472static void tg3_set_msglevel(struct net_device *dev, u32 value)
10473{
10474 struct tg3 *tp = netdev_priv(dev);
10475 tp->msg_enable = value;
10476}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010477
Linus Torvalds1da177e2005-04-16 15:20:36 -070010478static int tg3_nway_reset(struct net_device *dev)
10479{
10480 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010481 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010482
Linus Torvalds1da177e2005-04-16 15:20:36 -070010483 if (!netif_running(dev))
10484 return -EAGAIN;
10485
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010486 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010487 return -EINVAL;
10488
Joe Perches63c3a662011-04-26 08:12:10 +000010489 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010490 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010491 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010492 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010493 } else {
10494 u32 bmcr;
10495
10496 spin_lock_bh(&tp->lock);
10497 r = -EINVAL;
10498 tg3_readphy(tp, MII_BMCR, &bmcr);
10499 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10500 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010501 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010502 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10503 BMCR_ANENABLE);
10504 r = 0;
10505 }
10506 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010507 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010508
Linus Torvalds1da177e2005-04-16 15:20:36 -070010509 return r;
10510}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010511
Linus Torvalds1da177e2005-04-16 15:20:36 -070010512static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10513{
10514 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010515
Matt Carlson2c49a442010-09-30 10:34:35 +000010516 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010517 ering->rx_mini_max_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010518 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010519 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010520 else
10521 ering->rx_jumbo_max_pending = 0;
10522
10523 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010524
10525 ering->rx_pending = tp->rx_pending;
10526 ering->rx_mini_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010527 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010528 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10529 else
10530 ering->rx_jumbo_pending = 0;
10531
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010532 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010533}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010534
Linus Torvalds1da177e2005-04-16 15:20:36 -070010535static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10536{
10537 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010538 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010539
Matt Carlson2c49a442010-09-30 10:34:35 +000010540 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10541 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010542 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10543 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010544 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010545 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010546 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010547
Michael Chanbbe832c2005-06-24 20:20:04 -070010548 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010549 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010550 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010551 irq_sync = 1;
10552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010553
Michael Chanbbe832c2005-06-24 20:20:04 -070010554 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010555
Linus Torvalds1da177e2005-04-16 15:20:36 -070010556 tp->rx_pending = ering->rx_pending;
10557
Joe Perches63c3a662011-04-26 08:12:10 +000010558 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010559 tp->rx_pending > 63)
10560 tp->rx_pending = 63;
10561 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010562
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010563 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010564 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010565
10566 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010567 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010568 err = tg3_restart_hw(tp, 1);
10569 if (!err)
10570 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010571 }
10572
David S. Millerf47c11e2005-06-24 20:18:35 -070010573 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010574
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010575 if (irq_sync && !err)
10576 tg3_phy_start(tp);
10577
Michael Chanb9ec6c12006-07-25 16:37:27 -070010578 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010579}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010580
Linus Torvalds1da177e2005-04-16 15:20:36 -070010581static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10582{
10583 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010584
Joe Perches63c3a662011-04-26 08:12:10 +000010585 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010586
Steve Glendinninge18ce342008-12-16 02:00:00 -080010587 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010588 epause->rx_pause = 1;
10589 else
10590 epause->rx_pause = 0;
10591
Steve Glendinninge18ce342008-12-16 02:00:00 -080010592 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010593 epause->tx_pause = 1;
10594 else
10595 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010596}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010597
Linus Torvalds1da177e2005-04-16 15:20:36 -070010598static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10599{
10600 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010601 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010602
Joe Perches63c3a662011-04-26 08:12:10 +000010603 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010604 u32 newadv;
10605 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010606
Matt Carlson27121682010-02-17 15:16:57 +000010607 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010608
Matt Carlson27121682010-02-17 15:16:57 +000010609 if (!(phydev->supported & SUPPORTED_Pause) ||
10610 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010611 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010612 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010613
Matt Carlson27121682010-02-17 15:16:57 +000010614 tp->link_config.flowctrl = 0;
10615 if (epause->rx_pause) {
10616 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010617
Matt Carlson27121682010-02-17 15:16:57 +000010618 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010619 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010620 newadv = ADVERTISED_Pause;
10621 } else
10622 newadv = ADVERTISED_Pause |
10623 ADVERTISED_Asym_Pause;
10624 } else if (epause->tx_pause) {
10625 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10626 newadv = ADVERTISED_Asym_Pause;
10627 } else
10628 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010629
Matt Carlson27121682010-02-17 15:16:57 +000010630 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010631 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010632 else
Joe Perches63c3a662011-04-26 08:12:10 +000010633 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010634
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010635 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010636 u32 oldadv = phydev->advertising &
10637 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10638 if (oldadv != newadv) {
10639 phydev->advertising &=
10640 ~(ADVERTISED_Pause |
10641 ADVERTISED_Asym_Pause);
10642 phydev->advertising |= newadv;
10643 if (phydev->autoneg) {
10644 /*
10645 * Always renegotiate the link to
10646 * inform our link partner of our
10647 * flow control settings, even if the
10648 * flow control is forced. Let
10649 * tg3_adjust_link() do the final
10650 * flow control setup.
10651 */
10652 return phy_start_aneg(phydev);
10653 }
10654 }
10655
10656 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010657 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010658 } else {
10659 tp->link_config.orig_advertising &=
10660 ~(ADVERTISED_Pause |
10661 ADVERTISED_Asym_Pause);
10662 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010663 }
10664 } else {
10665 int irq_sync = 0;
10666
10667 if (netif_running(dev)) {
10668 tg3_netif_stop(tp);
10669 irq_sync = 1;
10670 }
10671
10672 tg3_full_lock(tp, irq_sync);
10673
10674 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010675 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010676 else
Joe Perches63c3a662011-04-26 08:12:10 +000010677 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010678 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010679 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010680 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010681 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010682 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010683 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010684 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010685 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010686
10687 if (netif_running(dev)) {
10688 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10689 err = tg3_restart_hw(tp, 1);
10690 if (!err)
10691 tg3_netif_start(tp);
10692 }
10693
10694 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010696
Michael Chanb9ec6c12006-07-25 16:37:27 -070010697 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010698}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010699
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010700static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010701{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010702 switch (sset) {
10703 case ETH_SS_TEST:
10704 return TG3_NUM_TEST;
10705 case ETH_SS_STATS:
10706 return TG3_NUM_STATS;
10707 default:
10708 return -EOPNOTSUPP;
10709 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010710}
10711
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010712static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010713{
10714 switch (stringset) {
10715 case ETH_SS_STATS:
10716 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10717 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010718 case ETH_SS_TEST:
10719 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10720 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010721 default:
10722 WARN_ON(1); /* we need a WARN() */
10723 break;
10724 }
10725}
10726
stephen hemminger81b87092011-04-04 08:43:50 +000010727static int tg3_set_phys_id(struct net_device *dev,
10728 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010729{
10730 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010731
10732 if (!netif_running(tp->dev))
10733 return -EAGAIN;
10734
stephen hemminger81b87092011-04-04 08:43:50 +000010735 switch (state) {
10736 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010737 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010738
stephen hemminger81b87092011-04-04 08:43:50 +000010739 case ETHTOOL_ID_ON:
10740 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10741 LED_CTRL_1000MBPS_ON |
10742 LED_CTRL_100MBPS_ON |
10743 LED_CTRL_10MBPS_ON |
10744 LED_CTRL_TRAFFIC_OVERRIDE |
10745 LED_CTRL_TRAFFIC_BLINK |
10746 LED_CTRL_TRAFFIC_LED);
10747 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010748
stephen hemminger81b87092011-04-04 08:43:50 +000010749 case ETHTOOL_ID_OFF:
10750 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10751 LED_CTRL_TRAFFIC_OVERRIDE);
10752 break;
Michael Chan4009a932005-09-05 17:52:54 -070010753
stephen hemminger81b87092011-04-04 08:43:50 +000010754 case ETHTOOL_ID_INACTIVE:
10755 tw32(MAC_LED_CTRL, tp->led_ctrl);
10756 break;
Michael Chan4009a932005-09-05 17:52:54 -070010757 }
stephen hemminger81b87092011-04-04 08:43:50 +000010758
Michael Chan4009a932005-09-05 17:52:54 -070010759 return 0;
10760}
10761
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010762static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010763 struct ethtool_stats *estats, u64 *tmp_stats)
10764{
10765 struct tg3 *tp = netdev_priv(dev);
10766 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10767}
10768
Matt Carlson535a4902011-07-20 10:20:56 +000010769static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000010770{
10771 int i;
10772 __be32 *buf;
10773 u32 offset = 0, len = 0;
10774 u32 magic, val;
10775
Joe Perches63c3a662011-04-26 08:12:10 +000010776 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010777 return NULL;
10778
10779 if (magic == TG3_EEPROM_MAGIC) {
10780 for (offset = TG3_NVM_DIR_START;
10781 offset < TG3_NVM_DIR_END;
10782 offset += TG3_NVM_DIRENT_SIZE) {
10783 if (tg3_nvram_read(tp, offset, &val))
10784 return NULL;
10785
10786 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10787 TG3_NVM_DIRTYPE_EXTVPD)
10788 break;
10789 }
10790
10791 if (offset != TG3_NVM_DIR_END) {
10792 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10793 if (tg3_nvram_read(tp, offset + 4, &offset))
10794 return NULL;
10795
10796 offset = tg3_nvram_logical_addr(tp, offset);
10797 }
10798 }
10799
10800 if (!offset || !len) {
10801 offset = TG3_NVM_VPD_OFF;
10802 len = TG3_NVM_VPD_LEN;
10803 }
10804
10805 buf = kmalloc(len, GFP_KERNEL);
10806 if (buf == NULL)
10807 return NULL;
10808
10809 if (magic == TG3_EEPROM_MAGIC) {
10810 for (i = 0; i < len; i += 4) {
10811 /* The data is in little-endian format in NVRAM.
10812 * Use the big-endian read routines to preserve
10813 * the byte order as it exists in NVRAM.
10814 */
10815 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10816 goto error;
10817 }
10818 } else {
10819 u8 *ptr;
10820 ssize_t cnt;
10821 unsigned int pos = 0;
10822
10823 ptr = (u8 *)&buf[0];
10824 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10825 cnt = pci_read_vpd(tp->pdev, pos,
10826 len - pos, ptr);
10827 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10828 cnt = 0;
10829 else if (cnt < 0)
10830 goto error;
10831 }
10832 if (pos != len)
10833 goto error;
10834 }
10835
Matt Carlson535a4902011-07-20 10:20:56 +000010836 *vpdlen = len;
10837
Matt Carlsonc3e94502011-04-13 11:05:08 +000010838 return buf;
10839
10840error:
10841 kfree(buf);
10842 return NULL;
10843}
10844
Michael Chan566f86a2005-05-29 14:56:58 -070010845#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010846#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10847#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10848#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010849#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10850#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000010851#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070010852#define NVRAM_SELFBOOT_HW_SIZE 0x20
10853#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010854
10855static int tg3_test_nvram(struct tg3 *tp)
10856{
Matt Carlson535a4902011-07-20 10:20:56 +000010857 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010858 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010859 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010860
Joe Perches63c3a662011-04-26 08:12:10 +000010861 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010862 return 0;
10863
Matt Carlsone4f34112009-02-25 14:25:00 +000010864 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010865 return -EIO;
10866
Michael Chan1b277772006-03-20 22:27:48 -080010867 if (magic == TG3_EEPROM_MAGIC)
10868 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010869 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010870 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10871 TG3_EEPROM_SB_FORMAT_1) {
10872 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10873 case TG3_EEPROM_SB_REVISION_0:
10874 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10875 break;
10876 case TG3_EEPROM_SB_REVISION_2:
10877 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10878 break;
10879 case TG3_EEPROM_SB_REVISION_3:
10880 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10881 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010882 case TG3_EEPROM_SB_REVISION_4:
10883 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10884 break;
10885 case TG3_EEPROM_SB_REVISION_5:
10886 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10887 break;
10888 case TG3_EEPROM_SB_REVISION_6:
10889 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10890 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010891 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010892 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010893 }
10894 } else
Michael Chan1b277772006-03-20 22:27:48 -080010895 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010896 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10897 size = NVRAM_SELFBOOT_HW_SIZE;
10898 else
Michael Chan1b277772006-03-20 22:27:48 -080010899 return -EIO;
10900
10901 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010902 if (buf == NULL)
10903 return -ENOMEM;
10904
Michael Chan1b277772006-03-20 22:27:48 -080010905 err = -EIO;
10906 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010907 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10908 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010909 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010910 }
Michael Chan1b277772006-03-20 22:27:48 -080010911 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010912 goto out;
10913
Michael Chan1b277772006-03-20 22:27:48 -080010914 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010915 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010916 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010917 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010918 u8 *buf8 = (u8 *) buf, csum8 = 0;
10919
Al Virob9fc7dc2007-12-17 22:59:57 -080010920 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010921 TG3_EEPROM_SB_REVISION_2) {
10922 /* For rev 2, the csum doesn't include the MBA. */
10923 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10924 csum8 += buf8[i];
10925 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10926 csum8 += buf8[i];
10927 } else {
10928 for (i = 0; i < size; i++)
10929 csum8 += buf8[i];
10930 }
Michael Chan1b277772006-03-20 22:27:48 -080010931
Adrian Bunkad96b482006-04-05 22:21:04 -070010932 if (csum8 == 0) {
10933 err = 0;
10934 goto out;
10935 }
10936
10937 err = -EIO;
10938 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010939 }
Michael Chan566f86a2005-05-29 14:56:58 -070010940
Al Virob9fc7dc2007-12-17 22:59:57 -080010941 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010942 TG3_EEPROM_MAGIC_HW) {
10943 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010944 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010945 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010946
10947 /* Separate the parity bits and the data bytes. */
10948 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10949 if ((i == 0) || (i == 8)) {
10950 int l;
10951 u8 msk;
10952
10953 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10954 parity[k++] = buf8[i] & msk;
10955 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010956 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010957 int l;
10958 u8 msk;
10959
10960 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10961 parity[k++] = buf8[i] & msk;
10962 i++;
10963
10964 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10965 parity[k++] = buf8[i] & msk;
10966 i++;
10967 }
10968 data[j++] = buf8[i];
10969 }
10970
10971 err = -EIO;
10972 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10973 u8 hw8 = hweight8(data[i]);
10974
10975 if ((hw8 & 0x1) && parity[i])
10976 goto out;
10977 else if (!(hw8 & 0x1) && !parity[i])
10978 goto out;
10979 }
10980 err = 0;
10981 goto out;
10982 }
10983
Matt Carlson01c3a392011-03-09 16:58:20 +000010984 err = -EIO;
10985
Michael Chan566f86a2005-05-29 14:56:58 -070010986 /* Bootstrap checksum at offset 0x10 */
10987 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010988 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010989 goto out;
10990
10991 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10992 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010993 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010994 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010995
Matt Carlsonc3e94502011-04-13 11:05:08 +000010996 kfree(buf);
10997
Matt Carlson535a4902011-07-20 10:20:56 +000010998 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000010999 if (!buf)
11000 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011001
Matt Carlson535a4902011-07-20 10:20:56 +000011002 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011003 if (i > 0) {
11004 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11005 if (j < 0)
11006 goto out;
11007
Matt Carlson535a4902011-07-20 10:20:56 +000011008 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011009 goto out;
11010
11011 i += PCI_VPD_LRDT_TAG_SIZE;
11012 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11013 PCI_VPD_RO_KEYWORD_CHKSUM);
11014 if (j > 0) {
11015 u8 csum8 = 0;
11016
11017 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11018
11019 for (i = 0; i <= j; i++)
11020 csum8 += ((u8 *)buf)[i];
11021
11022 if (csum8)
11023 goto out;
11024 }
11025 }
11026
Michael Chan566f86a2005-05-29 14:56:58 -070011027 err = 0;
11028
11029out:
11030 kfree(buf);
11031 return err;
11032}
11033
Michael Chanca430072005-05-29 14:57:23 -070011034#define TG3_SERDES_TIMEOUT_SEC 2
11035#define TG3_COPPER_TIMEOUT_SEC 6
11036
11037static int tg3_test_link(struct tg3 *tp)
11038{
11039 int i, max;
11040
11041 if (!netif_running(tp->dev))
11042 return -ENODEV;
11043
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011044 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011045 max = TG3_SERDES_TIMEOUT_SEC;
11046 else
11047 max = TG3_COPPER_TIMEOUT_SEC;
11048
11049 for (i = 0; i < max; i++) {
11050 if (netif_carrier_ok(tp->dev))
11051 return 0;
11052
11053 if (msleep_interruptible(1000))
11054 break;
11055 }
11056
11057 return -EIO;
11058}
11059
Michael Chana71116d2005-05-29 14:58:11 -070011060/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011061static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011062{
Michael Chanb16250e2006-09-27 16:10:14 -070011063 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011064 u32 offset, read_mask, write_mask, val, save_val, read_val;
11065 static struct {
11066 u16 offset;
11067 u16 flags;
11068#define TG3_FL_5705 0x1
11069#define TG3_FL_NOT_5705 0x2
11070#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011071#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011072 u32 read_mask;
11073 u32 write_mask;
11074 } reg_tbl[] = {
11075 /* MAC Control Registers */
11076 { MAC_MODE, TG3_FL_NOT_5705,
11077 0x00000000, 0x00ef6f8c },
11078 { MAC_MODE, TG3_FL_5705,
11079 0x00000000, 0x01ef6b8c },
11080 { MAC_STATUS, TG3_FL_NOT_5705,
11081 0x03800107, 0x00000000 },
11082 { MAC_STATUS, TG3_FL_5705,
11083 0x03800100, 0x00000000 },
11084 { MAC_ADDR_0_HIGH, 0x0000,
11085 0x00000000, 0x0000ffff },
11086 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011087 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011088 { MAC_RX_MTU_SIZE, 0x0000,
11089 0x00000000, 0x0000ffff },
11090 { MAC_TX_MODE, 0x0000,
11091 0x00000000, 0x00000070 },
11092 { MAC_TX_LENGTHS, 0x0000,
11093 0x00000000, 0x00003fff },
11094 { MAC_RX_MODE, TG3_FL_NOT_5705,
11095 0x00000000, 0x000007fc },
11096 { MAC_RX_MODE, TG3_FL_5705,
11097 0x00000000, 0x000007dc },
11098 { MAC_HASH_REG_0, 0x0000,
11099 0x00000000, 0xffffffff },
11100 { MAC_HASH_REG_1, 0x0000,
11101 0x00000000, 0xffffffff },
11102 { MAC_HASH_REG_2, 0x0000,
11103 0x00000000, 0xffffffff },
11104 { MAC_HASH_REG_3, 0x0000,
11105 0x00000000, 0xffffffff },
11106
11107 /* Receive Data and Receive BD Initiator Control Registers. */
11108 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11109 0x00000000, 0xffffffff },
11110 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11111 0x00000000, 0xffffffff },
11112 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11113 0x00000000, 0x00000003 },
11114 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11115 0x00000000, 0xffffffff },
11116 { RCVDBDI_STD_BD+0, 0x0000,
11117 0x00000000, 0xffffffff },
11118 { RCVDBDI_STD_BD+4, 0x0000,
11119 0x00000000, 0xffffffff },
11120 { RCVDBDI_STD_BD+8, 0x0000,
11121 0x00000000, 0xffff0002 },
11122 { RCVDBDI_STD_BD+0xc, 0x0000,
11123 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011124
Michael Chana71116d2005-05-29 14:58:11 -070011125 /* Receive BD Initiator Control Registers. */
11126 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11127 0x00000000, 0xffffffff },
11128 { RCVBDI_STD_THRESH, TG3_FL_5705,
11129 0x00000000, 0x000003ff },
11130 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11131 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011132
Michael Chana71116d2005-05-29 14:58:11 -070011133 /* Host Coalescing Control Registers. */
11134 { HOSTCC_MODE, TG3_FL_NOT_5705,
11135 0x00000000, 0x00000004 },
11136 { HOSTCC_MODE, TG3_FL_5705,
11137 0x00000000, 0x000000f6 },
11138 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11139 0x00000000, 0xffffffff },
11140 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11141 0x00000000, 0x000003ff },
11142 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11143 0x00000000, 0xffffffff },
11144 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11145 0x00000000, 0x000003ff },
11146 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11147 0x00000000, 0xffffffff },
11148 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11149 0x00000000, 0x000000ff },
11150 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11151 0x00000000, 0xffffffff },
11152 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11153 0x00000000, 0x000000ff },
11154 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11155 0x00000000, 0xffffffff },
11156 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11157 0x00000000, 0xffffffff },
11158 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11159 0x00000000, 0xffffffff },
11160 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11161 0x00000000, 0x000000ff },
11162 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11163 0x00000000, 0xffffffff },
11164 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11165 0x00000000, 0x000000ff },
11166 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11167 0x00000000, 0xffffffff },
11168 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11169 0x00000000, 0xffffffff },
11170 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11171 0x00000000, 0xffffffff },
11172 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11173 0x00000000, 0xffffffff },
11174 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11175 0x00000000, 0xffffffff },
11176 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11177 0xffffffff, 0x00000000 },
11178 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11179 0xffffffff, 0x00000000 },
11180
11181 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011182 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011183 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011184 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011185 0x00000000, 0x007fffff },
11186 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11187 0x00000000, 0x0000003f },
11188 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11189 0x00000000, 0x000001ff },
11190 { BUFMGR_MB_HIGH_WATER, 0x0000,
11191 0x00000000, 0x000001ff },
11192 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11193 0xffffffff, 0x00000000 },
11194 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11195 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011196
Michael Chana71116d2005-05-29 14:58:11 -070011197 /* Mailbox Registers */
11198 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11199 0x00000000, 0x000001ff },
11200 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11201 0x00000000, 0x000001ff },
11202 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11203 0x00000000, 0x000007ff },
11204 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11205 0x00000000, 0x000001ff },
11206
11207 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11208 };
11209
Michael Chanb16250e2006-09-27 16:10:14 -070011210 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011211 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011212 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011213 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011214 is_5750 = 1;
11215 }
Michael Chana71116d2005-05-29 14:58:11 -070011216
11217 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11218 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11219 continue;
11220
11221 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11222 continue;
11223
Joe Perches63c3a662011-04-26 08:12:10 +000011224 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011225 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11226 continue;
11227
Michael Chanb16250e2006-09-27 16:10:14 -070011228 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11229 continue;
11230
Michael Chana71116d2005-05-29 14:58:11 -070011231 offset = (u32) reg_tbl[i].offset;
11232 read_mask = reg_tbl[i].read_mask;
11233 write_mask = reg_tbl[i].write_mask;
11234
11235 /* Save the original register content */
11236 save_val = tr32(offset);
11237
11238 /* Determine the read-only value. */
11239 read_val = save_val & read_mask;
11240
11241 /* Write zero to the register, then make sure the read-only bits
11242 * are not changed and the read/write bits are all zeros.
11243 */
11244 tw32(offset, 0);
11245
11246 val = tr32(offset);
11247
11248 /* Test the read-only and read/write bits. */
11249 if (((val & read_mask) != read_val) || (val & write_mask))
11250 goto out;
11251
11252 /* Write ones to all the bits defined by RdMask and WrMask, then
11253 * make sure the read-only bits are not changed and the
11254 * read/write bits are all ones.
11255 */
11256 tw32(offset, read_mask | write_mask);
11257
11258 val = tr32(offset);
11259
11260 /* Test the read-only bits. */
11261 if ((val & read_mask) != read_val)
11262 goto out;
11263
11264 /* Test the read/write bits. */
11265 if ((val & write_mask) != write_mask)
11266 goto out;
11267
11268 tw32(offset, save_val);
11269 }
11270
11271 return 0;
11272
11273out:
Michael Chan9f88f292006-12-07 00:22:54 -080011274 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011275 netdev_err(tp->dev,
11276 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011277 tw32(offset, save_val);
11278 return -EIO;
11279}
11280
Michael Chan7942e1d2005-05-29 14:58:36 -070011281static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11282{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011283 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011284 int i;
11285 u32 j;
11286
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011287 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011288 for (j = 0; j < len; j += 4) {
11289 u32 val;
11290
11291 tg3_write_mem(tp, offset + j, test_pattern[i]);
11292 tg3_read_mem(tp, offset + j, &val);
11293 if (val != test_pattern[i])
11294 return -EIO;
11295 }
11296 }
11297 return 0;
11298}
11299
11300static int tg3_test_memory(struct tg3 *tp)
11301{
11302 static struct mem_entry {
11303 u32 offset;
11304 u32 len;
11305 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011306 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011307 { 0x00002000, 0x1c000},
11308 { 0xffffffff, 0x00000}
11309 }, mem_tbl_5705[] = {
11310 { 0x00000100, 0x0000c},
11311 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011312 { 0x00004000, 0x00800},
11313 { 0x00006000, 0x01000},
11314 { 0x00008000, 0x02000},
11315 { 0x00010000, 0x0e000},
11316 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011317 }, mem_tbl_5755[] = {
11318 { 0x00000200, 0x00008},
11319 { 0x00004000, 0x00800},
11320 { 0x00006000, 0x00800},
11321 { 0x00008000, 0x02000},
11322 { 0x00010000, 0x0c000},
11323 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011324 }, mem_tbl_5906[] = {
11325 { 0x00000200, 0x00008},
11326 { 0x00004000, 0x00400},
11327 { 0x00006000, 0x00400},
11328 { 0x00008000, 0x01000},
11329 { 0x00010000, 0x01000},
11330 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011331 }, mem_tbl_5717[] = {
11332 { 0x00000200, 0x00008},
11333 { 0x00010000, 0x0a000},
11334 { 0x00020000, 0x13c00},
11335 { 0xffffffff, 0x00000}
11336 }, mem_tbl_57765[] = {
11337 { 0x00000200, 0x00008},
11338 { 0x00004000, 0x00800},
11339 { 0x00006000, 0x09800},
11340 { 0x00010000, 0x0a000},
11341 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011342 };
11343 struct mem_entry *mem_tbl;
11344 int err = 0;
11345 int i;
11346
Joe Perches63c3a662011-04-26 08:12:10 +000011347 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011348 mem_tbl = mem_tbl_5717;
11349 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11350 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011351 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011352 mem_tbl = mem_tbl_5755;
11353 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11354 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011355 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011356 mem_tbl = mem_tbl_5705;
11357 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011358 mem_tbl = mem_tbl_570x;
11359
11360 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011361 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11362 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011363 break;
11364 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011365
Michael Chan7942e1d2005-05-29 14:58:36 -070011366 return err;
11367}
11368
Matt Carlsonbb158d62011-04-25 12:42:47 +000011369#define TG3_TSO_MSS 500
11370
11371#define TG3_TSO_IP_HDR_LEN 20
11372#define TG3_TSO_TCP_HDR_LEN 20
11373#define TG3_TSO_TCP_OPT_LEN 12
11374
11375static const u8 tg3_tso_header[] = {
113760x08, 0x00,
113770x45, 0x00, 0x00, 0x00,
113780x00, 0x00, 0x40, 0x00,
113790x40, 0x06, 0x00, 0x00,
113800x0a, 0x00, 0x00, 0x01,
113810x0a, 0x00, 0x00, 0x02,
113820x0d, 0x00, 0xe0, 0x00,
113830x00, 0x00, 0x01, 0x00,
113840x00, 0x00, 0x02, 0x00,
113850x80, 0x10, 0x10, 0x00,
113860x14, 0x09, 0x00, 0x00,
113870x01, 0x01, 0x08, 0x0a,
113880x11, 0x11, 0x11, 0x11,
113890x11, 0x11, 0x11, 0x11,
11390};
Michael Chan9f40dea2005-09-05 17:53:06 -070011391
Matt Carlson28a45952011-08-19 13:58:22 +000011392static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011393{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011394 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011395 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011396 u32 budget;
Michael Chanc76949a2005-05-29 14:58:59 -070011397 struct sk_buff *skb, *rx_skb;
11398 u8 *tx_data;
11399 dma_addr_t map;
11400 int num_pkts, tx_len, rx_len, i, err;
11401 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011402 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011403 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011404
Matt Carlsonc8873402010-02-12 14:47:11 +000011405 tnapi = &tp->napi[0];
11406 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011407 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011408 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011409 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011410 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011411 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011412 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011413 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011414
Michael Chanc76949a2005-05-29 14:58:59 -070011415 err = -EIO;
11416
Matt Carlson4852a862011-04-13 11:05:07 +000011417 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011418 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011419 if (!skb)
11420 return -ENOMEM;
11421
Michael Chanc76949a2005-05-29 14:58:59 -070011422 tx_data = skb_put(skb, tx_len);
11423 memcpy(tx_data, tp->dev->dev_addr, 6);
11424 memset(tx_data + 6, 0x0, 8);
11425
Matt Carlson4852a862011-04-13 11:05:07 +000011426 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011427
Matt Carlson28a45952011-08-19 13:58:22 +000011428 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011429 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11430
11431 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11432 TG3_TSO_TCP_OPT_LEN;
11433
11434 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11435 sizeof(tg3_tso_header));
11436 mss = TG3_TSO_MSS;
11437
11438 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11439 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11440
11441 /* Set the total length field in the IP header */
11442 iph->tot_len = htons((u16)(mss + hdr_len));
11443
11444 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11445 TXD_FLAG_CPU_POST_DMA);
11446
Joe Perches63c3a662011-04-26 08:12:10 +000011447 if (tg3_flag(tp, HW_TSO_1) ||
11448 tg3_flag(tp, HW_TSO_2) ||
11449 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011450 struct tcphdr *th;
11451 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11452 th = (struct tcphdr *)&tx_data[val];
11453 th->check = 0;
11454 } else
11455 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11456
Joe Perches63c3a662011-04-26 08:12:10 +000011457 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011458 mss |= (hdr_len & 0xc) << 12;
11459 if (hdr_len & 0x10)
11460 base_flags |= 0x00000010;
11461 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011462 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011463 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011464 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011465 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11466 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11467 } else {
11468 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11469 }
11470
11471 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11472 } else {
11473 num_pkts = 1;
11474 data_off = ETH_HLEN;
11475 }
11476
11477 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011478 tx_data[i] = (u8) (i & 0xff);
11479
Alexander Duyckf4188d82009-12-02 16:48:38 +000011480 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11481 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011482 dev_kfree_skb(skb);
11483 return -EIO;
11484 }
Michael Chanc76949a2005-05-29 14:58:59 -070011485
Matt Carlson0d681b22011-07-27 14:20:49 +000011486 val = tnapi->tx_prod;
11487 tnapi->tx_buffers[val].skb = skb;
11488 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11489
Michael Chanc76949a2005-05-29 14:58:59 -070011490 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011491 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011492
11493 udelay(10);
11494
Matt Carlson898a56f2009-08-28 14:02:40 +000011495 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011496
Matt Carlson84b67b22011-07-27 14:20:52 +000011497 budget = tg3_tx_avail(tnapi);
11498 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011499 base_flags | TXD_FLAG_END, mss, 0)) {
11500 tnapi->tx_buffers[val].skb = NULL;
11501 dev_kfree_skb(skb);
11502 return -EIO;
11503 }
Michael Chanc76949a2005-05-29 14:58:59 -070011504
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011505 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011506
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011507 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11508 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011509
11510 udelay(10);
11511
Matt Carlson303fc922009-11-02 14:27:34 +000011512 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11513 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011514 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011515 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011516
11517 udelay(10);
11518
Matt Carlson898a56f2009-08-28 14:02:40 +000011519 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11520 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011521 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011522 (rx_idx == (rx_start_idx + num_pkts)))
11523 break;
11524 }
11525
Matt Carlson0d681b22011-07-27 14:20:49 +000011526 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, 0);
Michael Chanc76949a2005-05-29 14:58:59 -070011527 dev_kfree_skb(skb);
11528
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011529 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011530 goto out;
11531
11532 if (rx_idx != rx_start_idx + num_pkts)
11533 goto out;
11534
Matt Carlsonbb158d62011-04-25 12:42:47 +000011535 val = data_off;
11536 while (rx_idx != rx_start_idx) {
11537 desc = &rnapi->rx_rcb[rx_start_idx++];
11538 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11539 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011540
Matt Carlsonbb158d62011-04-25 12:42:47 +000011541 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11542 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011543 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011544
Matt Carlsonbb158d62011-04-25 12:42:47 +000011545 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11546 - ETH_FCS_LEN;
11547
Matt Carlson28a45952011-08-19 13:58:22 +000011548 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011549 if (rx_len != tx_len)
11550 goto out;
11551
11552 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11553 if (opaque_key != RXD_OPAQUE_RING_STD)
11554 goto out;
11555 } else {
11556 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11557 goto out;
11558 }
11559 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11560 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011561 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011562 goto out;
11563 }
11564
11565 if (opaque_key == RXD_OPAQUE_RING_STD) {
11566 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11567 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11568 mapping);
11569 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
11570 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11571 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11572 mapping);
11573 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011574 goto out;
11575
Matt Carlsonbb158d62011-04-25 12:42:47 +000011576 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11577 PCI_DMA_FROMDEVICE);
11578
11579 for (i = data_off; i < rx_len; i++, val++) {
11580 if (*(rx_skb->data + i) != (u8) (val & 0xff))
11581 goto out;
11582 }
Matt Carlson4852a862011-04-13 11:05:07 +000011583 }
11584
Michael Chanc76949a2005-05-29 14:58:59 -070011585 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011586
Michael Chanc76949a2005-05-29 14:58:59 -070011587 /* tg3_free_rings will unmap and free the rx_skb */
11588out:
11589 return err;
11590}
11591
Matt Carlson00c266b2011-04-25 12:42:46 +000011592#define TG3_STD_LOOPBACK_FAILED 1
11593#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011594#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011595#define TG3_LOOPBACK_FAILED \
11596 (TG3_STD_LOOPBACK_FAILED | \
11597 TG3_JMB_LOOPBACK_FAILED | \
11598 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011599
Matt Carlson941ec902011-08-19 13:58:23 +000011600static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011601{
Matt Carlson28a45952011-08-19 13:58:22 +000011602 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011603 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011604
Matt Carlsonab789042011-01-25 15:58:54 +000011605 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11606 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11607
Matt Carlson28a45952011-08-19 13:58:22 +000011608 if (!netif_running(tp->dev)) {
11609 data[0] = TG3_LOOPBACK_FAILED;
11610 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011611 if (do_extlpbk)
11612 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011613 goto done;
11614 }
11615
Michael Chanb9ec6c12006-07-25 16:37:27 -070011616 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011617 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011618 data[0] = TG3_LOOPBACK_FAILED;
11619 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011620 if (do_extlpbk)
11621 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011622 goto done;
11623 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011624
Joe Perches63c3a662011-04-26 08:12:10 +000011625 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011626 int i;
11627
11628 /* Reroute all rx packets to the 1st queue */
11629 for (i = MAC_RSS_INDIR_TBL_0;
11630 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11631 tw32(i, 0x0);
11632 }
11633
Matt Carlson6e01b202011-08-19 13:58:20 +000011634 /* HW errata - mac loopback fails in some cases on 5780.
11635 * Normal traffic and PHY loopback are not affected by
11636 * errata. Also, the MAC loopback test is deprecated for
11637 * all newer ASIC revisions.
11638 */
11639 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11640 !tg3_flag(tp, CPMU_PRESENT)) {
11641 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011642
Matt Carlson28a45952011-08-19 13:58:22 +000011643 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11644 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011645
11646 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011647 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11648 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011649
11650 tg3_mac_loopback(tp, false);
11651 }
Matt Carlson4852a862011-04-13 11:05:07 +000011652
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011653 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011654 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011655 int i;
11656
Matt Carlson941ec902011-08-19 13:58:23 +000011657 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011658
11659 /* Wait for link */
11660 for (i = 0; i < 100; i++) {
11661 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11662 break;
11663 mdelay(1);
11664 }
11665
Matt Carlson28a45952011-08-19 13:58:22 +000011666 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11667 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011668 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011669 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11670 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011671 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011672 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11673 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011674
Matt Carlson941ec902011-08-19 13:58:23 +000011675 if (do_extlpbk) {
11676 tg3_phy_lpbk_set(tp, 0, true);
11677
11678 /* All link indications report up, but the hardware
11679 * isn't really ready for about 20 msec. Double it
11680 * to be sure.
11681 */
11682 mdelay(40);
11683
11684 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11685 data[2] |= TG3_STD_LOOPBACK_FAILED;
11686 if (tg3_flag(tp, TSO_CAPABLE) &&
11687 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11688 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11689 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11690 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11691 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11692 }
11693
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011694 /* Re-enable gphy autopowerdown. */
11695 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11696 tg3_phy_toggle_apd(tp, true);
11697 }
Matt Carlson6833c042008-11-21 17:18:59 -080011698
Matt Carlson941ec902011-08-19 13:58:23 +000011699 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011700
Matt Carlsonab789042011-01-25 15:58:54 +000011701done:
11702 tp->phy_flags |= eee_cap;
11703
Michael Chan9f40dea2005-09-05 17:53:06 -070011704 return err;
11705}
11706
Michael Chan4cafd3f2005-05-29 14:56:34 -070011707static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11708 u64 *data)
11709{
Michael Chan566f86a2005-05-29 14:56:58 -070011710 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011711 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011712
Matt Carlsonbed98292011-07-13 09:27:29 +000011713 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11714 tg3_power_up(tp)) {
11715 etest->flags |= ETH_TEST_FL_FAILED;
11716 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11717 return;
11718 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011719
Michael Chan566f86a2005-05-29 14:56:58 -070011720 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11721
11722 if (tg3_test_nvram(tp) != 0) {
11723 etest->flags |= ETH_TEST_FL_FAILED;
11724 data[0] = 1;
11725 }
Matt Carlson941ec902011-08-19 13:58:23 +000011726 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011727 etest->flags |= ETH_TEST_FL_FAILED;
11728 data[1] = 1;
11729 }
Michael Chana71116d2005-05-29 14:58:11 -070011730 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011731 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011732
Michael Chanbbe832c2005-06-24 20:20:04 -070011733 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011734 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011735 tg3_netif_stop(tp);
11736 irq_sync = 1;
11737 }
11738
11739 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011740
11741 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011742 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011743 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011744 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011745 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011746 if (!err)
11747 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011748
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011749 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011750 tg3_phy_reset(tp);
11751
Michael Chana71116d2005-05-29 14:58:11 -070011752 if (tg3_test_registers(tp) != 0) {
11753 etest->flags |= ETH_TEST_FL_FAILED;
11754 data[2] = 1;
11755 }
Matt Carlson28a45952011-08-19 13:58:22 +000011756
Michael Chan7942e1d2005-05-29 14:58:36 -070011757 if (tg3_test_memory(tp) != 0) {
11758 etest->flags |= ETH_TEST_FL_FAILED;
11759 data[3] = 1;
11760 }
Matt Carlson28a45952011-08-19 13:58:22 +000011761
Matt Carlson941ec902011-08-19 13:58:23 +000011762 if (doextlpbk)
11763 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
11764
11765 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070011766 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011767
David S. Millerf47c11e2005-06-24 20:18:35 -070011768 tg3_full_unlock(tp);
11769
Michael Chand4bc3922005-05-29 14:59:20 -070011770 if (tg3_test_interrupt(tp) != 0) {
11771 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011772 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070011773 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011774
11775 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011776
Michael Chana71116d2005-05-29 14:58:11 -070011777 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11778 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011779 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011780 err2 = tg3_restart_hw(tp, 1);
11781 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011782 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011783 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011784
11785 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011786
11787 if (irq_sync && !err2)
11788 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011789 }
Matt Carlson80096062010-08-02 11:26:06 +000011790 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011791 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011792
Michael Chan4cafd3f2005-05-29 14:56:34 -070011793}
11794
Linus Torvalds1da177e2005-04-16 15:20:36 -070011795static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11796{
11797 struct mii_ioctl_data *data = if_mii(ifr);
11798 struct tg3 *tp = netdev_priv(dev);
11799 int err;
11800
Joe Perches63c3a662011-04-26 08:12:10 +000011801 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011802 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011803 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011804 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011805 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011806 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011807 }
11808
Matt Carlson33f401a2010-04-05 10:19:27 +000011809 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011810 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011811 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011812
11813 /* fallthru */
11814 case SIOCGMIIREG: {
11815 u32 mii_regval;
11816
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011817 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011818 break; /* We have no PHY */
11819
Matt Carlson34eea5a2011-04-20 07:57:38 +000011820 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011821 return -EAGAIN;
11822
David S. Millerf47c11e2005-06-24 20:18:35 -070011823 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011824 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011825 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011826
11827 data->val_out = mii_regval;
11828
11829 return err;
11830 }
11831
11832 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011833 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011834 break; /* We have no PHY */
11835
Matt Carlson34eea5a2011-04-20 07:57:38 +000011836 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011837 return -EAGAIN;
11838
David S. Millerf47c11e2005-06-24 20:18:35 -070011839 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011840 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011841 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011842
11843 return err;
11844
11845 default:
11846 /* do nothing */
11847 break;
11848 }
11849 return -EOPNOTSUPP;
11850}
11851
David S. Miller15f98502005-05-18 22:49:26 -070011852static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11853{
11854 struct tg3 *tp = netdev_priv(dev);
11855
11856 memcpy(ec, &tp->coal, sizeof(*ec));
11857 return 0;
11858}
11859
Michael Chand244c892005-07-05 14:42:33 -070011860static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11861{
11862 struct tg3 *tp = netdev_priv(dev);
11863 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11864 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11865
Joe Perches63c3a662011-04-26 08:12:10 +000011866 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011867 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11868 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11869 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11870 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11871 }
11872
11873 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11874 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11875 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11876 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11877 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11878 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11879 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11880 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11881 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11882 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11883 return -EINVAL;
11884
11885 /* No rx interrupts will be generated if both are zero */
11886 if ((ec->rx_coalesce_usecs == 0) &&
11887 (ec->rx_max_coalesced_frames == 0))
11888 return -EINVAL;
11889
11890 /* No tx interrupts will be generated if both are zero */
11891 if ((ec->tx_coalesce_usecs == 0) &&
11892 (ec->tx_max_coalesced_frames == 0))
11893 return -EINVAL;
11894
11895 /* Only copy relevant parameters, ignore all others. */
11896 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11897 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11898 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11899 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11900 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11901 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11902 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11903 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11904 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11905
11906 if (netif_running(dev)) {
11907 tg3_full_lock(tp, 0);
11908 __tg3_set_coalesce(tp, &tp->coal);
11909 tg3_full_unlock(tp);
11910 }
11911 return 0;
11912}
11913
Jeff Garzik7282d492006-09-13 14:30:00 -040011914static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011915 .get_settings = tg3_get_settings,
11916 .set_settings = tg3_set_settings,
11917 .get_drvinfo = tg3_get_drvinfo,
11918 .get_regs_len = tg3_get_regs_len,
11919 .get_regs = tg3_get_regs,
11920 .get_wol = tg3_get_wol,
11921 .set_wol = tg3_set_wol,
11922 .get_msglevel = tg3_get_msglevel,
11923 .set_msglevel = tg3_set_msglevel,
11924 .nway_reset = tg3_nway_reset,
11925 .get_link = ethtool_op_get_link,
11926 .get_eeprom_len = tg3_get_eeprom_len,
11927 .get_eeprom = tg3_get_eeprom,
11928 .set_eeprom = tg3_set_eeprom,
11929 .get_ringparam = tg3_get_ringparam,
11930 .set_ringparam = tg3_set_ringparam,
11931 .get_pauseparam = tg3_get_pauseparam,
11932 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011933 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011934 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011935 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011936 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011937 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011938 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011939 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011940};
11941
11942static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11943{
Michael Chan1b277772006-03-20 22:27:48 -080011944 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011945
11946 tp->nvram_size = EEPROM_CHIP_SIZE;
11947
Matt Carlsone4f34112009-02-25 14:25:00 +000011948 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011949 return;
11950
Michael Chanb16250e2006-09-27 16:10:14 -070011951 if ((magic != TG3_EEPROM_MAGIC) &&
11952 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11953 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011954 return;
11955
11956 /*
11957 * Size the chip by reading offsets at increasing powers of two.
11958 * When we encounter our validation signature, we know the addressing
11959 * has wrapped around, and thus have our chip size.
11960 */
Michael Chan1b277772006-03-20 22:27:48 -080011961 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011962
11963 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011964 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011965 return;
11966
Michael Chan18201802006-03-20 22:29:15 -080011967 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011968 break;
11969
11970 cursize <<= 1;
11971 }
11972
11973 tp->nvram_size = cursize;
11974}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011975
Linus Torvalds1da177e2005-04-16 15:20:36 -070011976static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11977{
11978 u32 val;
11979
Joe Perches63c3a662011-04-26 08:12:10 +000011980 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011981 return;
11982
11983 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011984 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011985 tg3_get_eeprom_size(tp);
11986 return;
11987 }
11988
Matt Carlson6d348f22009-02-25 14:25:52 +000011989 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011990 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011991 /* This is confusing. We want to operate on the
11992 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11993 * call will read from NVRAM and byteswap the data
11994 * according to the byteswapping settings for all
11995 * other register accesses. This ensures the data we
11996 * want will always reside in the lower 16-bits.
11997 * However, the data in NVRAM is in LE format, which
11998 * means the data from the NVRAM read will always be
11999 * opposite the endianness of the CPU. The 16-bit
12000 * byteswap then brings the data to CPU endianness.
12001 */
12002 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012003 return;
12004 }
12005 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012006 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012007}
12008
12009static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12010{
12011 u32 nvcfg1;
12012
12013 nvcfg1 = tr32(NVRAM_CFG1);
12014 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012015 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012016 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012017 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12018 tw32(NVRAM_CFG1, nvcfg1);
12019 }
12020
Matt Carlson6ff6f812011-05-19 12:12:54 +000012021 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012022 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012023 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012024 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12025 tp->nvram_jedecnum = JEDEC_ATMEL;
12026 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012027 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012028 break;
12029 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12030 tp->nvram_jedecnum = JEDEC_ATMEL;
12031 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12032 break;
12033 case FLASH_VENDOR_ATMEL_EEPROM:
12034 tp->nvram_jedecnum = JEDEC_ATMEL;
12035 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012036 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012037 break;
12038 case FLASH_VENDOR_ST:
12039 tp->nvram_jedecnum = JEDEC_ST;
12040 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012041 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012042 break;
12043 case FLASH_VENDOR_SAIFUN:
12044 tp->nvram_jedecnum = JEDEC_SAIFUN;
12045 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12046 break;
12047 case FLASH_VENDOR_SST_SMALL:
12048 case FLASH_VENDOR_SST_LARGE:
12049 tp->nvram_jedecnum = JEDEC_SST;
12050 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12051 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012052 }
Matt Carlson8590a602009-08-28 12:29:16 +000012053 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012054 tp->nvram_jedecnum = JEDEC_ATMEL;
12055 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012056 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012057 }
12058}
12059
Matt Carlsona1b950d2009-09-01 13:20:17 +000012060static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12061{
12062 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12063 case FLASH_5752PAGE_SIZE_256:
12064 tp->nvram_pagesize = 256;
12065 break;
12066 case FLASH_5752PAGE_SIZE_512:
12067 tp->nvram_pagesize = 512;
12068 break;
12069 case FLASH_5752PAGE_SIZE_1K:
12070 tp->nvram_pagesize = 1024;
12071 break;
12072 case FLASH_5752PAGE_SIZE_2K:
12073 tp->nvram_pagesize = 2048;
12074 break;
12075 case FLASH_5752PAGE_SIZE_4K:
12076 tp->nvram_pagesize = 4096;
12077 break;
12078 case FLASH_5752PAGE_SIZE_264:
12079 tp->nvram_pagesize = 264;
12080 break;
12081 case FLASH_5752PAGE_SIZE_528:
12082 tp->nvram_pagesize = 528;
12083 break;
12084 }
12085}
12086
Michael Chan361b4ac2005-04-21 17:11:21 -070012087static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12088{
12089 u32 nvcfg1;
12090
12091 nvcfg1 = tr32(NVRAM_CFG1);
12092
Michael Chane6af3012005-04-21 17:12:05 -070012093 /* NVRAM protection for TPM */
12094 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012095 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012096
Michael Chan361b4ac2005-04-21 17:11:21 -070012097 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012098 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12099 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12100 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012101 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012102 break;
12103 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12104 tp->nvram_jedecnum = JEDEC_ATMEL;
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;
12108 case FLASH_5752VENDOR_ST_M45PE10:
12109 case FLASH_5752VENDOR_ST_M45PE20:
12110 case FLASH_5752VENDOR_ST_M45PE40:
12111 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012112 tg3_flag_set(tp, NVRAM_BUFFERED);
12113 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012114 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012115 }
12116
Joe Perches63c3a662011-04-26 08:12:10 +000012117 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012118 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012119 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012120 /* For eeprom, set pagesize to maximum eeprom size */
12121 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12122
12123 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12124 tw32(NVRAM_CFG1, nvcfg1);
12125 }
12126}
12127
Michael Chand3c7b882006-03-23 01:28:25 -080012128static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12129{
Matt Carlson989a9d22007-05-05 11:51:05 -070012130 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012131
12132 nvcfg1 = tr32(NVRAM_CFG1);
12133
12134 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012135 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012136 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012137 protect = 1;
12138 }
Michael Chand3c7b882006-03-23 01:28:25 -080012139
Matt Carlson989a9d22007-05-05 11:51:05 -070012140 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12141 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012142 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12143 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12144 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12145 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12146 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012147 tg3_flag_set(tp, NVRAM_BUFFERED);
12148 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012149 tp->nvram_pagesize = 264;
12150 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12151 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12152 tp->nvram_size = (protect ? 0x3e200 :
12153 TG3_NVRAM_SIZE_512KB);
12154 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12155 tp->nvram_size = (protect ? 0x1f200 :
12156 TG3_NVRAM_SIZE_256KB);
12157 else
12158 tp->nvram_size = (protect ? 0x1f200 :
12159 TG3_NVRAM_SIZE_128KB);
12160 break;
12161 case FLASH_5752VENDOR_ST_M45PE10:
12162 case FLASH_5752VENDOR_ST_M45PE20:
12163 case FLASH_5752VENDOR_ST_M45PE40:
12164 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012165 tg3_flag_set(tp, NVRAM_BUFFERED);
12166 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012167 tp->nvram_pagesize = 256;
12168 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12169 tp->nvram_size = (protect ?
12170 TG3_NVRAM_SIZE_64KB :
12171 TG3_NVRAM_SIZE_128KB);
12172 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12173 tp->nvram_size = (protect ?
12174 TG3_NVRAM_SIZE_64KB :
12175 TG3_NVRAM_SIZE_256KB);
12176 else
12177 tp->nvram_size = (protect ?
12178 TG3_NVRAM_SIZE_128KB :
12179 TG3_NVRAM_SIZE_512KB);
12180 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012181 }
12182}
12183
Michael Chan1b277772006-03-20 22:27:48 -080012184static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12185{
12186 u32 nvcfg1;
12187
12188 nvcfg1 = tr32(NVRAM_CFG1);
12189
12190 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012191 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12192 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12193 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12194 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12195 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012196 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012197 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012198
Matt Carlson8590a602009-08-28 12:29:16 +000012199 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12200 tw32(NVRAM_CFG1, nvcfg1);
12201 break;
12202 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12203 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12204 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12205 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12206 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012207 tg3_flag_set(tp, NVRAM_BUFFERED);
12208 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012209 tp->nvram_pagesize = 264;
12210 break;
12211 case FLASH_5752VENDOR_ST_M45PE10:
12212 case FLASH_5752VENDOR_ST_M45PE20:
12213 case FLASH_5752VENDOR_ST_M45PE40:
12214 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012215 tg3_flag_set(tp, NVRAM_BUFFERED);
12216 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012217 tp->nvram_pagesize = 256;
12218 break;
Michael Chan1b277772006-03-20 22:27:48 -080012219 }
12220}
12221
Matt Carlson6b91fa02007-10-10 18:01:09 -070012222static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12223{
12224 u32 nvcfg1, protect = 0;
12225
12226 nvcfg1 = tr32(NVRAM_CFG1);
12227
12228 /* NVRAM protection for TPM */
12229 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012230 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012231 protect = 1;
12232 }
12233
12234 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12235 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012236 case FLASH_5761VENDOR_ATMEL_ADB021D:
12237 case FLASH_5761VENDOR_ATMEL_ADB041D:
12238 case FLASH_5761VENDOR_ATMEL_ADB081D:
12239 case FLASH_5761VENDOR_ATMEL_ADB161D:
12240 case FLASH_5761VENDOR_ATMEL_MDB021D:
12241 case FLASH_5761VENDOR_ATMEL_MDB041D:
12242 case FLASH_5761VENDOR_ATMEL_MDB081D:
12243 case FLASH_5761VENDOR_ATMEL_MDB161D:
12244 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012245 tg3_flag_set(tp, NVRAM_BUFFERED);
12246 tg3_flag_set(tp, FLASH);
12247 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012248 tp->nvram_pagesize = 256;
12249 break;
12250 case FLASH_5761VENDOR_ST_A_M45PE20:
12251 case FLASH_5761VENDOR_ST_A_M45PE40:
12252 case FLASH_5761VENDOR_ST_A_M45PE80:
12253 case FLASH_5761VENDOR_ST_A_M45PE16:
12254 case FLASH_5761VENDOR_ST_M_M45PE20:
12255 case FLASH_5761VENDOR_ST_M_M45PE40:
12256 case FLASH_5761VENDOR_ST_M_M45PE80:
12257 case FLASH_5761VENDOR_ST_M_M45PE16:
12258 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012259 tg3_flag_set(tp, NVRAM_BUFFERED);
12260 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012261 tp->nvram_pagesize = 256;
12262 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012263 }
12264
12265 if (protect) {
12266 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12267 } else {
12268 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012269 case FLASH_5761VENDOR_ATMEL_ADB161D:
12270 case FLASH_5761VENDOR_ATMEL_MDB161D:
12271 case FLASH_5761VENDOR_ST_A_M45PE16:
12272 case FLASH_5761VENDOR_ST_M_M45PE16:
12273 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12274 break;
12275 case FLASH_5761VENDOR_ATMEL_ADB081D:
12276 case FLASH_5761VENDOR_ATMEL_MDB081D:
12277 case FLASH_5761VENDOR_ST_A_M45PE80:
12278 case FLASH_5761VENDOR_ST_M_M45PE80:
12279 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12280 break;
12281 case FLASH_5761VENDOR_ATMEL_ADB041D:
12282 case FLASH_5761VENDOR_ATMEL_MDB041D:
12283 case FLASH_5761VENDOR_ST_A_M45PE40:
12284 case FLASH_5761VENDOR_ST_M_M45PE40:
12285 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12286 break;
12287 case FLASH_5761VENDOR_ATMEL_ADB021D:
12288 case FLASH_5761VENDOR_ATMEL_MDB021D:
12289 case FLASH_5761VENDOR_ST_A_M45PE20:
12290 case FLASH_5761VENDOR_ST_M_M45PE20:
12291 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12292 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012293 }
12294 }
12295}
12296
Michael Chanb5d37722006-09-27 16:06:21 -070012297static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12298{
12299 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012300 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012301 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12302}
12303
Matt Carlson321d32a2008-11-21 17:22:19 -080012304static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12305{
12306 u32 nvcfg1;
12307
12308 nvcfg1 = tr32(NVRAM_CFG1);
12309
12310 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12311 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12312 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12313 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012314 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012315 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12316
12317 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12318 tw32(NVRAM_CFG1, nvcfg1);
12319 return;
12320 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12321 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12322 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12323 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12324 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12325 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12326 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12327 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012328 tg3_flag_set(tp, NVRAM_BUFFERED);
12329 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012330
12331 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12332 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12333 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12334 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12335 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12336 break;
12337 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12338 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12339 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12340 break;
12341 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12342 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12343 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12344 break;
12345 }
12346 break;
12347 case FLASH_5752VENDOR_ST_M45PE10:
12348 case FLASH_5752VENDOR_ST_M45PE20:
12349 case FLASH_5752VENDOR_ST_M45PE40:
12350 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012351 tg3_flag_set(tp, NVRAM_BUFFERED);
12352 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012353
12354 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12355 case FLASH_5752VENDOR_ST_M45PE10:
12356 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12357 break;
12358 case FLASH_5752VENDOR_ST_M45PE20:
12359 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12360 break;
12361 case FLASH_5752VENDOR_ST_M45PE40:
12362 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12363 break;
12364 }
12365 break;
12366 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012367 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012368 return;
12369 }
12370
Matt Carlsona1b950d2009-09-01 13:20:17 +000012371 tg3_nvram_get_pagesize(tp, nvcfg1);
12372 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012373 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012374}
12375
12376
12377static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12378{
12379 u32 nvcfg1;
12380
12381 nvcfg1 = tr32(NVRAM_CFG1);
12382
12383 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12384 case FLASH_5717VENDOR_ATMEL_EEPROM:
12385 case FLASH_5717VENDOR_MICRO_EEPROM:
12386 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012387 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012388 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12389
12390 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12391 tw32(NVRAM_CFG1, nvcfg1);
12392 return;
12393 case FLASH_5717VENDOR_ATMEL_MDB011D:
12394 case FLASH_5717VENDOR_ATMEL_ADB011B:
12395 case FLASH_5717VENDOR_ATMEL_ADB011D:
12396 case FLASH_5717VENDOR_ATMEL_MDB021D:
12397 case FLASH_5717VENDOR_ATMEL_ADB021B:
12398 case FLASH_5717VENDOR_ATMEL_ADB021D:
12399 case FLASH_5717VENDOR_ATMEL_45USPT:
12400 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012401 tg3_flag_set(tp, NVRAM_BUFFERED);
12402 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012403
12404 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12405 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012406 /* Detect size with tg3_nvram_get_size() */
12407 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012408 case FLASH_5717VENDOR_ATMEL_ADB021B:
12409 case FLASH_5717VENDOR_ATMEL_ADB021D:
12410 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12411 break;
12412 default:
12413 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12414 break;
12415 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012416 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012417 case FLASH_5717VENDOR_ST_M_M25PE10:
12418 case FLASH_5717VENDOR_ST_A_M25PE10:
12419 case FLASH_5717VENDOR_ST_M_M45PE10:
12420 case FLASH_5717VENDOR_ST_A_M45PE10:
12421 case FLASH_5717VENDOR_ST_M_M25PE20:
12422 case FLASH_5717VENDOR_ST_A_M25PE20:
12423 case FLASH_5717VENDOR_ST_M_M45PE20:
12424 case FLASH_5717VENDOR_ST_A_M45PE20:
12425 case FLASH_5717VENDOR_ST_25USPT:
12426 case FLASH_5717VENDOR_ST_45USPT:
12427 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012428 tg3_flag_set(tp, NVRAM_BUFFERED);
12429 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012430
12431 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12432 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012433 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012434 /* Detect size with tg3_nvram_get_size() */
12435 break;
12436 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012437 case FLASH_5717VENDOR_ST_A_M45PE20:
12438 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12439 break;
12440 default:
12441 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12442 break;
12443 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012444 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012445 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012446 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012447 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012448 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012449
12450 tg3_nvram_get_pagesize(tp, nvcfg1);
12451 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012452 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012453}
12454
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012455static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12456{
12457 u32 nvcfg1, nvmpinstrp;
12458
12459 nvcfg1 = tr32(NVRAM_CFG1);
12460 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12461
12462 switch (nvmpinstrp) {
12463 case FLASH_5720_EEPROM_HD:
12464 case FLASH_5720_EEPROM_LD:
12465 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012466 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012467
12468 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12469 tw32(NVRAM_CFG1, nvcfg1);
12470 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12471 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12472 else
12473 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12474 return;
12475 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12476 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12477 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12478 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12479 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12480 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12481 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12482 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12483 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12484 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12485 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12486 case FLASH_5720VENDOR_ATMEL_45USPT:
12487 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012488 tg3_flag_set(tp, NVRAM_BUFFERED);
12489 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012490
12491 switch (nvmpinstrp) {
12492 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12493 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12494 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12495 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12496 break;
12497 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12498 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12499 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12500 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12501 break;
12502 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12503 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12504 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12505 break;
12506 default:
12507 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12508 break;
12509 }
12510 break;
12511 case FLASH_5720VENDOR_M_ST_M25PE10:
12512 case FLASH_5720VENDOR_M_ST_M45PE10:
12513 case FLASH_5720VENDOR_A_ST_M25PE10:
12514 case FLASH_5720VENDOR_A_ST_M45PE10:
12515 case FLASH_5720VENDOR_M_ST_M25PE20:
12516 case FLASH_5720VENDOR_M_ST_M45PE20:
12517 case FLASH_5720VENDOR_A_ST_M25PE20:
12518 case FLASH_5720VENDOR_A_ST_M45PE20:
12519 case FLASH_5720VENDOR_M_ST_M25PE40:
12520 case FLASH_5720VENDOR_M_ST_M45PE40:
12521 case FLASH_5720VENDOR_A_ST_M25PE40:
12522 case FLASH_5720VENDOR_A_ST_M45PE40:
12523 case FLASH_5720VENDOR_M_ST_M25PE80:
12524 case FLASH_5720VENDOR_M_ST_M45PE80:
12525 case FLASH_5720VENDOR_A_ST_M25PE80:
12526 case FLASH_5720VENDOR_A_ST_M45PE80:
12527 case FLASH_5720VENDOR_ST_25USPT:
12528 case FLASH_5720VENDOR_ST_45USPT:
12529 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012530 tg3_flag_set(tp, NVRAM_BUFFERED);
12531 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012532
12533 switch (nvmpinstrp) {
12534 case FLASH_5720VENDOR_M_ST_M25PE20:
12535 case FLASH_5720VENDOR_M_ST_M45PE20:
12536 case FLASH_5720VENDOR_A_ST_M25PE20:
12537 case FLASH_5720VENDOR_A_ST_M45PE20:
12538 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12539 break;
12540 case FLASH_5720VENDOR_M_ST_M25PE40:
12541 case FLASH_5720VENDOR_M_ST_M45PE40:
12542 case FLASH_5720VENDOR_A_ST_M25PE40:
12543 case FLASH_5720VENDOR_A_ST_M45PE40:
12544 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12545 break;
12546 case FLASH_5720VENDOR_M_ST_M25PE80:
12547 case FLASH_5720VENDOR_M_ST_M45PE80:
12548 case FLASH_5720VENDOR_A_ST_M25PE80:
12549 case FLASH_5720VENDOR_A_ST_M45PE80:
12550 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12551 break;
12552 default:
12553 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12554 break;
12555 }
12556 break;
12557 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012558 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012559 return;
12560 }
12561
12562 tg3_nvram_get_pagesize(tp, nvcfg1);
12563 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012564 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012565}
12566
Linus Torvalds1da177e2005-04-16 15:20:36 -070012567/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12568static void __devinit tg3_nvram_init(struct tg3 *tp)
12569{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012570 tw32_f(GRC_EEPROM_ADDR,
12571 (EEPROM_ADDR_FSM_RESET |
12572 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12573 EEPROM_ADDR_CLKPERD_SHIFT)));
12574
Michael Chan9d57f012006-12-07 00:23:25 -080012575 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012576
12577 /* Enable seeprom accesses. */
12578 tw32_f(GRC_LOCAL_CTRL,
12579 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12580 udelay(100);
12581
12582 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12583 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012584 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012585
Michael Chanec41c7d2006-01-17 02:40:55 -080012586 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012587 netdev_warn(tp->dev,
12588 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012589 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012590 return;
12591 }
Michael Chane6af3012005-04-21 17:12:05 -070012592 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012593
Matt Carlson989a9d22007-05-05 11:51:05 -070012594 tp->nvram_size = 0;
12595
Michael Chan361b4ac2005-04-21 17:11:21 -070012596 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12597 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012598 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12599 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012600 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012601 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12602 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012603 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012604 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12605 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012606 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12607 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012608 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12609 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012610 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012611 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12612 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012613 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012614 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12615 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012616 else
12617 tg3_get_nvram_info(tp);
12618
Matt Carlson989a9d22007-05-05 11:51:05 -070012619 if (tp->nvram_size == 0)
12620 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012621
Michael Chane6af3012005-04-21 17:12:05 -070012622 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012623 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012624
12625 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012626 tg3_flag_clear(tp, NVRAM);
12627 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012628
12629 tg3_get_eeprom_size(tp);
12630 }
12631}
12632
Linus Torvalds1da177e2005-04-16 15:20:36 -070012633static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12634 u32 offset, u32 len, u8 *buf)
12635{
12636 int i, j, rc = 0;
12637 u32 val;
12638
12639 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012640 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012641 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012642
12643 addr = offset + i;
12644
12645 memcpy(&data, buf + i, 4);
12646
Matt Carlson62cedd12009-04-20 14:52:29 -070012647 /*
12648 * The SEEPROM interface expects the data to always be opposite
12649 * the native endian format. We accomplish this by reversing
12650 * all the operations that would have been performed on the
12651 * data from a call to tg3_nvram_read_be32().
12652 */
12653 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012654
12655 val = tr32(GRC_EEPROM_ADDR);
12656 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12657
12658 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12659 EEPROM_ADDR_READ);
12660 tw32(GRC_EEPROM_ADDR, val |
12661 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12662 (addr & EEPROM_ADDR_ADDR_MASK) |
12663 EEPROM_ADDR_START |
12664 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012665
Michael Chan9d57f012006-12-07 00:23:25 -080012666 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012667 val = tr32(GRC_EEPROM_ADDR);
12668
12669 if (val & EEPROM_ADDR_COMPLETE)
12670 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012671 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012672 }
12673 if (!(val & EEPROM_ADDR_COMPLETE)) {
12674 rc = -EBUSY;
12675 break;
12676 }
12677 }
12678
12679 return rc;
12680}
12681
12682/* offset and length are dword aligned */
12683static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12684 u8 *buf)
12685{
12686 int ret = 0;
12687 u32 pagesize = tp->nvram_pagesize;
12688 u32 pagemask = pagesize - 1;
12689 u32 nvram_cmd;
12690 u8 *tmp;
12691
12692 tmp = kmalloc(pagesize, GFP_KERNEL);
12693 if (tmp == NULL)
12694 return -ENOMEM;
12695
12696 while (len) {
12697 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012698 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012699
12700 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012701
Linus Torvalds1da177e2005-04-16 15:20:36 -070012702 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012703 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12704 (__be32 *) (tmp + j));
12705 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012706 break;
12707 }
12708 if (ret)
12709 break;
12710
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012711 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012712 size = pagesize;
12713 if (len < size)
12714 size = len;
12715
12716 len -= size;
12717
12718 memcpy(tmp + page_off, buf, size);
12719
12720 offset = offset + (pagesize - page_off);
12721
Michael Chane6af3012005-04-21 17:12:05 -070012722 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012723
12724 /*
12725 * Before we can erase the flash page, we need
12726 * to issue a special "write enable" command.
12727 */
12728 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12729
12730 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12731 break;
12732
12733 /* Erase the target page */
12734 tw32(NVRAM_ADDR, phy_addr);
12735
12736 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12737 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12738
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012739 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012740 break;
12741
12742 /* Issue another write enable to start the write. */
12743 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12744
12745 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12746 break;
12747
12748 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012749 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012750
Al Virob9fc7dc2007-12-17 22:59:57 -080012751 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012752
Al Virob9fc7dc2007-12-17 22:59:57 -080012753 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012754
12755 tw32(NVRAM_ADDR, phy_addr + j);
12756
12757 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12758 NVRAM_CMD_WR;
12759
12760 if (j == 0)
12761 nvram_cmd |= NVRAM_CMD_FIRST;
12762 else if (j == (pagesize - 4))
12763 nvram_cmd |= NVRAM_CMD_LAST;
12764
12765 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12766 break;
12767 }
12768 if (ret)
12769 break;
12770 }
12771
12772 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12773 tg3_nvram_exec_cmd(tp, nvram_cmd);
12774
12775 kfree(tmp);
12776
12777 return ret;
12778}
12779
12780/* offset and length are dword aligned */
12781static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12782 u8 *buf)
12783{
12784 int i, ret = 0;
12785
12786 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012787 u32 page_off, phy_addr, nvram_cmd;
12788 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012789
12790 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012791 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012792
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012793 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012794
Michael Chan18201802006-03-20 22:29:15 -080012795 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012796
12797 tw32(NVRAM_ADDR, phy_addr);
12798
12799 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12800
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012801 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012802 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012803 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012804 nvram_cmd |= NVRAM_CMD_LAST;
12805
12806 if (i == (len - 4))
12807 nvram_cmd |= NVRAM_CMD_LAST;
12808
Matt Carlson321d32a2008-11-21 17:22:19 -080012809 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012810 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012811 (tp->nvram_jedecnum == JEDEC_ST) &&
12812 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012813
12814 if ((ret = tg3_nvram_exec_cmd(tp,
12815 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12816 NVRAM_CMD_DONE)))
12817
12818 break;
12819 }
Joe Perches63c3a662011-04-26 08:12:10 +000012820 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012821 /* We always do complete word writes to eeprom. */
12822 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12823 }
12824
12825 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12826 break;
12827 }
12828 return ret;
12829}
12830
12831/* offset and length are dword aligned */
12832static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12833{
12834 int ret;
12835
Joe Perches63c3a662011-04-26 08:12:10 +000012836 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012837 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12838 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012839 udelay(40);
12840 }
12841
Joe Perches63c3a662011-04-26 08:12:10 +000012842 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012843 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012844 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012845 u32 grc_mode;
12846
Michael Chanec41c7d2006-01-17 02:40:55 -080012847 ret = tg3_nvram_lock(tp);
12848 if (ret)
12849 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012850
Michael Chane6af3012005-04-21 17:12:05 -070012851 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012852 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012853 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012854
12855 grc_mode = tr32(GRC_MODE);
12856 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12857
Joe Perches63c3a662011-04-26 08:12:10 +000012858 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012859 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12860 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012861 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012862 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12863 buf);
12864 }
12865
12866 grc_mode = tr32(GRC_MODE);
12867 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12868
Michael Chane6af3012005-04-21 17:12:05 -070012869 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012870 tg3_nvram_unlock(tp);
12871 }
12872
Joe Perches63c3a662011-04-26 08:12:10 +000012873 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012874 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012875 udelay(40);
12876 }
12877
12878 return ret;
12879}
12880
12881struct subsys_tbl_ent {
12882 u16 subsys_vendor, subsys_devid;
12883 u32 phy_id;
12884};
12885
Matt Carlson24daf2b2010-02-17 15:17:02 +000012886static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012887 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012888 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012889 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012890 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012891 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012892 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012893 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012894 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12895 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12896 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012897 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012898 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012899 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012900 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12901 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12902 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012903 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012904 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012905 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012906 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012907 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012908 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012909 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012910
12911 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012912 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012913 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012914 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012915 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012916 { TG3PCI_SUBVENDOR_ID_3COM,
12917 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12918 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012919 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012920 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012921 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012922
12923 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012924 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012925 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012926 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012927 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012928 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012929 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012930 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012931 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012932
12933 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012934 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012935 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012936 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012937 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012938 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12939 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12940 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012941 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012942 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012943 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012944
12945 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012946 { TG3PCI_SUBVENDOR_ID_IBM,
12947 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012948};
12949
Matt Carlson24daf2b2010-02-17 15:17:02 +000012950static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012951{
12952 int i;
12953
12954 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12955 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12956 tp->pdev->subsystem_vendor) &&
12957 (subsys_id_to_phy_id[i].subsys_devid ==
12958 tp->pdev->subsystem_device))
12959 return &subsys_id_to_phy_id[i];
12960 }
12961 return NULL;
12962}
12963
Michael Chan7d0c41e2005-04-21 17:06:20 -070012964static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012965{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012966 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070012967
Matt Carlson79eb6902010-02-17 15:17:03 +000012968 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012969 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12970
Gary Zambranoa85feb82007-05-05 11:52:19 -070012971 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012972 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12973 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012974
Michael Chanb5d37722006-09-27 16:06:21 -070012975 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012976 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012977 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12978 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012979 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012980 val = tr32(VCPU_CFGSHDW);
12981 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012982 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012983 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012984 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012985 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012986 device_set_wakeup_enable(&tp->pdev->dev, true);
12987 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012988 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012989 }
12990
Linus Torvalds1da177e2005-04-16 15:20:36 -070012991 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12992 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12993 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012994 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012995 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012996
12997 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12998 tp->nic_sram_data_cfg = nic_cfg;
12999
13000 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13001 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013002 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13003 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13004 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013005 (ver > 0) && (ver < 0x100))
13006 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13007
Matt Carlsona9daf362008-05-25 23:49:44 -070013008 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13009 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13010
Linus Torvalds1da177e2005-04-16 15:20:36 -070013011 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13012 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13013 eeprom_phy_serdes = 1;
13014
13015 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13016 if (nic_phy_id != 0) {
13017 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13018 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13019
13020 eeprom_phy_id = (id1 >> 16) << 10;
13021 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13022 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13023 } else
13024 eeprom_phy_id = 0;
13025
Michael Chan7d0c41e2005-04-21 17:06:20 -070013026 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013027 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013028 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013029 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013030 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013031 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013032 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013033
Joe Perches63c3a662011-04-26 08:12:10 +000013034 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013035 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13036 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013037 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013038 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13039
13040 switch (led_cfg) {
13041 default:
13042 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13043 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13044 break;
13045
13046 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13047 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13048 break;
13049
13050 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13051 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013052
13053 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13054 * read on some older 5700/5701 bootcode.
13055 */
13056 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13057 ASIC_REV_5700 ||
13058 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13059 ASIC_REV_5701)
13060 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13061
Linus Torvalds1da177e2005-04-16 15:20:36 -070013062 break;
13063
13064 case SHASTA_EXT_LED_SHARED:
13065 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13066 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13067 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13068 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13069 LED_CTRL_MODE_PHY_2);
13070 break;
13071
13072 case SHASTA_EXT_LED_MAC:
13073 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13074 break;
13075
13076 case SHASTA_EXT_LED_COMBO:
13077 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13078 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13079 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13080 LED_CTRL_MODE_PHY_2);
13081 break;
13082
Stephen Hemminger855e1112008-04-16 16:37:28 -070013083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013084
13085 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13086 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13087 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13088 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13089
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013090 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13091 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013092
Michael Chan9d26e212006-12-07 00:21:14 -080013093 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013094 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013095 if ((tp->pdev->subsystem_vendor ==
13096 PCI_VENDOR_ID_ARIMA) &&
13097 (tp->pdev->subsystem_device == 0x205a ||
13098 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013099 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013100 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013101 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13102 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013104
13105 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013106 tg3_flag_set(tp, ENABLE_ASF);
13107 if (tg3_flag(tp, 5750_PLUS))
13108 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013109 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013110
13111 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013112 tg3_flag(tp, 5750_PLUS))
13113 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013114
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013115 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013116 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013117 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013118
Joe Perches63c3a662011-04-26 08:12:10 +000013119 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013120 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013121 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013122 device_set_wakeup_enable(&tp->pdev->dev, true);
13123 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013124
Linus Torvalds1da177e2005-04-16 15:20:36 -070013125 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013126 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013127
13128 /* serdes signal pre-emphasis in register 0x590 set by */
13129 /* bootcode if bit 18 is set */
13130 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013131 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013132
Joe Perches63c3a662011-04-26 08:12:10 +000013133 if ((tg3_flag(tp, 57765_PLUS) ||
13134 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13135 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013136 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013137 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013138
Joe Perches63c3a662011-04-26 08:12:10 +000013139 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013140 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013141 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013142 u32 cfg3;
13143
13144 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13145 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013146 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013147 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013148
Matt Carlson14417062010-02-17 15:16:59 +000013149 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013150 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013151 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013152 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013153 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013154 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013155 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013156done:
Joe Perches63c3a662011-04-26 08:12:10 +000013157 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013158 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013159 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013160 else
13161 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013162}
13163
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013164static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13165{
13166 int i;
13167 u32 val;
13168
13169 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13170 tw32(OTP_CTRL, cmd);
13171
13172 /* Wait for up to 1 ms for command to execute. */
13173 for (i = 0; i < 100; i++) {
13174 val = tr32(OTP_STATUS);
13175 if (val & OTP_STATUS_CMD_DONE)
13176 break;
13177 udelay(10);
13178 }
13179
13180 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13181}
13182
13183/* Read the gphy configuration from the OTP region of the chip. The gphy
13184 * configuration is a 32-bit value that straddles the alignment boundary.
13185 * We do two 32-bit reads and then shift and merge the results.
13186 */
13187static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13188{
13189 u32 bhalf_otp, thalf_otp;
13190
13191 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13192
13193 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13194 return 0;
13195
13196 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13197
13198 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13199 return 0;
13200
13201 thalf_otp = tr32(OTP_READ_DATA);
13202
13203 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13204
13205 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13206 return 0;
13207
13208 bhalf_otp = tr32(OTP_READ_DATA);
13209
13210 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13211}
13212
Matt Carlsone256f8a2011-03-09 16:58:24 +000013213static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13214{
13215 u32 adv = ADVERTISED_Autoneg |
13216 ADVERTISED_Pause;
13217
13218 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13219 adv |= ADVERTISED_1000baseT_Half |
13220 ADVERTISED_1000baseT_Full;
13221
13222 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13223 adv |= ADVERTISED_100baseT_Half |
13224 ADVERTISED_100baseT_Full |
13225 ADVERTISED_10baseT_Half |
13226 ADVERTISED_10baseT_Full |
13227 ADVERTISED_TP;
13228 else
13229 adv |= ADVERTISED_FIBRE;
13230
13231 tp->link_config.advertising = adv;
13232 tp->link_config.speed = SPEED_INVALID;
13233 tp->link_config.duplex = DUPLEX_INVALID;
13234 tp->link_config.autoneg = AUTONEG_ENABLE;
13235 tp->link_config.active_speed = SPEED_INVALID;
13236 tp->link_config.active_duplex = DUPLEX_INVALID;
13237 tp->link_config.orig_speed = SPEED_INVALID;
13238 tp->link_config.orig_duplex = DUPLEX_INVALID;
13239 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13240}
13241
Michael Chan7d0c41e2005-04-21 17:06:20 -070013242static int __devinit tg3_phy_probe(struct tg3 *tp)
13243{
13244 u32 hw_phy_id_1, hw_phy_id_2;
13245 u32 hw_phy_id, hw_phy_id_masked;
13246 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013247
Matt Carlsone256f8a2011-03-09 16:58:24 +000013248 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013249 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013250 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13251
Joe Perches63c3a662011-04-26 08:12:10 +000013252 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013253 return tg3_phy_init(tp);
13254
Linus Torvalds1da177e2005-04-16 15:20:36 -070013255 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013256 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013257 */
13258 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013259 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013260 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013261 } else {
13262 /* Now read the physical PHY_ID from the chip and verify
13263 * that it is sane. If it doesn't look good, we fall back
13264 * to either the hard-coded table based PHY_ID and failing
13265 * that the value found in the eeprom area.
13266 */
13267 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13268 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13269
13270 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13271 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13272 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13273
Matt Carlson79eb6902010-02-17 15:17:03 +000013274 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013275 }
13276
Matt Carlson79eb6902010-02-17 15:17:03 +000013277 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013278 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013279 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013280 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013281 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013282 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013283 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013284 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013285 /* Do nothing, phy ID already set up in
13286 * tg3_get_eeprom_hw_cfg().
13287 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013288 } else {
13289 struct subsys_tbl_ent *p;
13290
13291 /* No eeprom signature? Try the hardcoded
13292 * subsys device table.
13293 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013294 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013295 if (!p)
13296 return -ENODEV;
13297
13298 tp->phy_id = p->phy_id;
13299 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013300 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013301 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013302 }
13303 }
13304
Matt Carlsona6b68da2010-12-06 08:28:52 +000013305 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013306 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13307 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13308 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013309 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13310 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13311 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013312 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13313
Matt Carlsone256f8a2011-03-09 16:58:24 +000013314 tg3_phy_init_link_config(tp);
13315
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013316 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013317 !tg3_flag(tp, ENABLE_APE) &&
13318 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013319 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013320
13321 tg3_readphy(tp, MII_BMSR, &bmsr);
13322 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13323 (bmsr & BMSR_LSTATUS))
13324 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013325
Linus Torvalds1da177e2005-04-16 15:20:36 -070013326 err = tg3_phy_reset(tp);
13327 if (err)
13328 return err;
13329
Matt Carlson42b64a42011-05-19 12:12:49 +000013330 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013331
Michael Chan3600d912006-12-07 00:21:48 -080013332 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13333 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13334 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13335 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013336 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13337 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013338
13339 tg3_writephy(tp, MII_BMCR,
13340 BMCR_ANENABLE | BMCR_ANRESTART);
13341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013342 }
13343
13344skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013345 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013346 err = tg3_init_5401phy_dsp(tp);
13347 if (err)
13348 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013349
Linus Torvalds1da177e2005-04-16 15:20:36 -070013350 err = tg3_init_5401phy_dsp(tp);
13351 }
13352
Linus Torvalds1da177e2005-04-16 15:20:36 -070013353 return err;
13354}
13355
Matt Carlson184b8902010-04-05 10:19:25 +000013356static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013357{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013358 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013359 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013360 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013361 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013362
Matt Carlson535a4902011-07-20 10:20:56 +000013363 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013364 if (!vpd_data)
13365 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013366
Matt Carlson535a4902011-07-20 10:20:56 +000013367 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013368 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013369 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013370
13371 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13372 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13373 i += PCI_VPD_LRDT_TAG_SIZE;
13374
Matt Carlson535a4902011-07-20 10:20:56 +000013375 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013376 goto out_not_found;
13377
Matt Carlson184b8902010-04-05 10:19:25 +000013378 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13379 PCI_VPD_RO_KEYWORD_MFR_ID);
13380 if (j > 0) {
13381 len = pci_vpd_info_field_size(&vpd_data[j]);
13382
13383 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13384 if (j + len > block_end || len != 4 ||
13385 memcmp(&vpd_data[j], "1028", 4))
13386 goto partno;
13387
13388 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13389 PCI_VPD_RO_KEYWORD_VENDOR0);
13390 if (j < 0)
13391 goto partno;
13392
13393 len = pci_vpd_info_field_size(&vpd_data[j]);
13394
13395 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13396 if (j + len > block_end)
13397 goto partno;
13398
13399 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013400 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013401 }
13402
13403partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013404 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13405 PCI_VPD_RO_KEYWORD_PARTNO);
13406 if (i < 0)
13407 goto out_not_found;
13408
13409 len = pci_vpd_info_field_size(&vpd_data[i]);
13410
13411 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13412 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013413 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013414 goto out_not_found;
13415
13416 memcpy(tp->board_part_number, &vpd_data[i], len);
13417
Linus Torvalds1da177e2005-04-16 15:20:36 -070013418out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013419 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013420 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013421 return;
13422
13423out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013424 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13425 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13426 strcpy(tp->board_part_number, "BCM5717");
13427 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13428 strcpy(tp->board_part_number, "BCM5718");
13429 else
13430 goto nomatch;
13431 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13432 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13433 strcpy(tp->board_part_number, "BCM57780");
13434 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13435 strcpy(tp->board_part_number, "BCM57760");
13436 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13437 strcpy(tp->board_part_number, "BCM57790");
13438 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13439 strcpy(tp->board_part_number, "BCM57788");
13440 else
13441 goto nomatch;
13442 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13443 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13444 strcpy(tp->board_part_number, "BCM57761");
13445 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13446 strcpy(tp->board_part_number, "BCM57765");
13447 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13448 strcpy(tp->board_part_number, "BCM57781");
13449 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13450 strcpy(tp->board_part_number, "BCM57785");
13451 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13452 strcpy(tp->board_part_number, "BCM57791");
13453 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13454 strcpy(tp->board_part_number, "BCM57795");
13455 else
13456 goto nomatch;
13457 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013458 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013459 } else {
13460nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013461 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013463}
13464
Matt Carlson9c8a6202007-10-21 16:16:08 -070013465static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13466{
13467 u32 val;
13468
Matt Carlsone4f34112009-02-25 14:25:00 +000013469 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013470 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013471 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013472 val != 0)
13473 return 0;
13474
13475 return 1;
13476}
13477
Matt Carlsonacd9c112009-02-25 14:26:33 +000013478static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13479{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013480 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013481 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013482 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013483
13484 if (tg3_nvram_read(tp, 0xc, &offset) ||
13485 tg3_nvram_read(tp, 0x4, &start))
13486 return;
13487
13488 offset = tg3_nvram_logical_addr(tp, offset);
13489
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013490 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013491 return;
13492
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013493 if ((val & 0xfc000000) == 0x0c000000) {
13494 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013495 return;
13496
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013497 if (val == 0)
13498 newver = true;
13499 }
13500
Matt Carlson75f99362010-04-05 10:19:24 +000013501 dst_off = strlen(tp->fw_ver);
13502
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013503 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013504 if (TG3_VER_SIZE - dst_off < 16 ||
13505 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013506 return;
13507
13508 offset = offset + ver_offset - start;
13509 for (i = 0; i < 16; i += 4) {
13510 __be32 v;
13511 if (tg3_nvram_read_be32(tp, offset + i, &v))
13512 return;
13513
Matt Carlson75f99362010-04-05 10:19:24 +000013514 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013515 }
13516 } else {
13517 u32 major, minor;
13518
13519 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13520 return;
13521
13522 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13523 TG3_NVM_BCVER_MAJSFT;
13524 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013525 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13526 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013527 }
13528}
13529
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013530static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13531{
13532 u32 val, major, minor;
13533
13534 /* Use native endian representation */
13535 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13536 return;
13537
13538 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13539 TG3_NVM_HWSB_CFG1_MAJSFT;
13540 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13541 TG3_NVM_HWSB_CFG1_MINSFT;
13542
13543 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13544}
13545
Matt Carlsondfe00d72008-11-21 17:19:41 -080013546static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13547{
13548 u32 offset, major, minor, build;
13549
Matt Carlson75f99362010-04-05 10:19:24 +000013550 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013551
13552 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13553 return;
13554
13555 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13556 case TG3_EEPROM_SB_REVISION_0:
13557 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13558 break;
13559 case TG3_EEPROM_SB_REVISION_2:
13560 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13561 break;
13562 case TG3_EEPROM_SB_REVISION_3:
13563 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13564 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013565 case TG3_EEPROM_SB_REVISION_4:
13566 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13567 break;
13568 case TG3_EEPROM_SB_REVISION_5:
13569 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13570 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013571 case TG3_EEPROM_SB_REVISION_6:
13572 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13573 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013574 default:
13575 return;
13576 }
13577
Matt Carlsone4f34112009-02-25 14:25:00 +000013578 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013579 return;
13580
13581 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13582 TG3_EEPROM_SB_EDH_BLD_SHFT;
13583 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13584 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13585 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13586
13587 if (minor > 99 || build > 26)
13588 return;
13589
Matt Carlson75f99362010-04-05 10:19:24 +000013590 offset = strlen(tp->fw_ver);
13591 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13592 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013593
13594 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013595 offset = strlen(tp->fw_ver);
13596 if (offset < TG3_VER_SIZE - 1)
13597 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013598 }
13599}
13600
Matt Carlsonacd9c112009-02-25 14:26:33 +000013601static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013602{
13603 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013604 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013605
13606 for (offset = TG3_NVM_DIR_START;
13607 offset < TG3_NVM_DIR_END;
13608 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013609 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013610 return;
13611
13612 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13613 break;
13614 }
13615
13616 if (offset == TG3_NVM_DIR_END)
13617 return;
13618
Joe Perches63c3a662011-04-26 08:12:10 +000013619 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013620 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013621 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013622 return;
13623
Matt Carlsone4f34112009-02-25 14:25:00 +000013624 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013625 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013626 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013627 return;
13628
13629 offset += val - start;
13630
Matt Carlsonacd9c112009-02-25 14:26:33 +000013631 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013632
Matt Carlsonacd9c112009-02-25 14:26:33 +000013633 tp->fw_ver[vlen++] = ',';
13634 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013635
13636 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013637 __be32 v;
13638 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013639 return;
13640
Al Virob9fc7dc2007-12-17 22:59:57 -080013641 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013642
Matt Carlsonacd9c112009-02-25 14:26:33 +000013643 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13644 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013645 break;
13646 }
13647
Matt Carlsonacd9c112009-02-25 14:26:33 +000013648 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13649 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013650 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013651}
13652
Matt Carlson7fd76442009-02-25 14:27:20 +000013653static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13654{
13655 int vlen;
13656 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013657 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013658
Joe Perches63c3a662011-04-26 08:12:10 +000013659 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013660 return;
13661
13662 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13663 if (apedata != APE_SEG_SIG_MAGIC)
13664 return;
13665
13666 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13667 if (!(apedata & APE_FW_STATUS_READY))
13668 return;
13669
13670 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13671
Matt Carlsondc6d0742010-09-15 08:59:55 +000013672 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013673 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013674 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013675 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013676 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013677 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013678
Matt Carlson7fd76442009-02-25 14:27:20 +000013679 vlen = strlen(tp->fw_ver);
13680
Matt Carlsonecc79642010-08-02 11:26:01 +000013681 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13682 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013683 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13684 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13685 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13686 (apedata & APE_FW_VERSION_BLDMSK));
13687}
13688
Matt Carlsonacd9c112009-02-25 14:26:33 +000013689static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13690{
13691 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013692 bool vpd_vers = false;
13693
13694 if (tp->fw_ver[0] != 0)
13695 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013696
Joe Perches63c3a662011-04-26 08:12:10 +000013697 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013698 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013699 return;
13700 }
13701
Matt Carlsonacd9c112009-02-25 14:26:33 +000013702 if (tg3_nvram_read(tp, 0, &val))
13703 return;
13704
13705 if (val == TG3_EEPROM_MAGIC)
13706 tg3_read_bc_ver(tp);
13707 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13708 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013709 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13710 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013711 else
13712 return;
13713
Matt Carlsonc9cab242011-07-13 09:27:27 +000013714 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013715 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013716
Matt Carlsonc9cab242011-07-13 09:27:27 +000013717 if (tg3_flag(tp, ENABLE_APE)) {
13718 if (tg3_flag(tp, ENABLE_ASF))
13719 tg3_read_dash_ver(tp);
13720 } else if (tg3_flag(tp, ENABLE_ASF)) {
13721 tg3_read_mgmtfw_ver(tp);
13722 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013723
Matt Carlson75f99362010-04-05 10:19:24 +000013724done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013725 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013726}
13727
Michael Chan7544b092007-05-05 13:08:32 -070013728static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13729
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013730static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13731{
Joe Perches63c3a662011-04-26 08:12:10 +000013732 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013733 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013734 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013735 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013736 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013737 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013738}
13739
Matt Carlson41434702011-03-09 16:58:22 +000013740static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013741 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13742 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13743 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13744 { },
13745};
13746
Linus Torvalds1da177e2005-04-16 15:20:36 -070013747static int __devinit tg3_get_invariants(struct tg3 *tp)
13748{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013749 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013750 u32 pci_state_reg, grc_misc_cfg;
13751 u32 val;
13752 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013753 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013754
Linus Torvalds1da177e2005-04-16 15:20:36 -070013755 /* Force memory write invalidate off. If we leave it on,
13756 * then on 5700_BX chips we have to enable a workaround.
13757 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13758 * to match the cacheline size. The Broadcom driver have this
13759 * workaround but turns MWI off all the times so never uses
13760 * it. This seems to suggest that the workaround is insufficient.
13761 */
13762 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13763 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13764 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13765
Matt Carlson16821282011-07-13 09:27:28 +000013766 /* Important! -- Make sure register accesses are byteswapped
13767 * correctly. Also, for those chips that require it, make
13768 * sure that indirect register accesses are enabled before
13769 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013770 */
13771 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13772 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013773 tp->misc_host_ctrl |= (misc_ctrl_reg &
13774 MISC_HOST_CTRL_CHIPREV);
13775 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13776 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013777
13778 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13779 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013780 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13781 u32 prod_id_asic_rev;
13782
Matt Carlson5001e2f2009-11-13 13:03:51 +000013783 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13784 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013785 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13786 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013787 pci_read_config_dword(tp->pdev,
13788 TG3PCI_GEN2_PRODID_ASICREV,
13789 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013790 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13791 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13792 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13793 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13794 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13795 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13796 pci_read_config_dword(tp->pdev,
13797 TG3PCI_GEN15_PRODID_ASICREV,
13798 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013799 else
13800 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13801 &prod_id_asic_rev);
13802
Matt Carlson321d32a2008-11-21 17:22:19 -080013803 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013805
Michael Chanff645be2005-04-21 17:09:53 -070013806 /* Wrong chip ID in 5752 A0. This code can be removed later
13807 * as A0 is not in production.
13808 */
13809 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13810 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13811
Michael Chan68929142005-08-09 20:17:14 -070013812 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13813 * we need to disable memory and use config. cycles
13814 * only to access all registers. The 5702/03 chips
13815 * can mistakenly decode the special cycles from the
13816 * ICH chipsets as memory write cycles, causing corruption
13817 * of register and memory space. Only certain ICH bridges
13818 * will drive special cycles with non-zero data during the
13819 * address phase which can fall within the 5703's address
13820 * range. This is not an ICH bug as the PCI spec allows
13821 * non-zero address during special cycles. However, only
13822 * these ICH bridges are known to drive non-zero addresses
13823 * during special cycles.
13824 *
13825 * Since special cycles do not cross PCI bridges, we only
13826 * enable this workaround if the 5703 is on the secondary
13827 * bus of these ICH bridges.
13828 */
13829 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13830 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13831 static struct tg3_dev_id {
13832 u32 vendor;
13833 u32 device;
13834 u32 rev;
13835 } ich_chipsets[] = {
13836 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13837 PCI_ANY_ID },
13838 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13839 PCI_ANY_ID },
13840 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13841 0xa },
13842 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13843 PCI_ANY_ID },
13844 { },
13845 };
13846 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13847 struct pci_dev *bridge = NULL;
13848
13849 while (pci_id->vendor != 0) {
13850 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13851 bridge);
13852 if (!bridge) {
13853 pci_id++;
13854 continue;
13855 }
13856 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013857 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013858 continue;
13859 }
13860 if (bridge->subordinate &&
13861 (bridge->subordinate->number ==
13862 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013863 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013864 pci_dev_put(bridge);
13865 break;
13866 }
13867 }
13868 }
13869
Matt Carlson6ff6f812011-05-19 12:12:54 +000013870 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013871 static struct tg3_dev_id {
13872 u32 vendor;
13873 u32 device;
13874 } bridge_chipsets[] = {
13875 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13876 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13877 { },
13878 };
13879 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13880 struct pci_dev *bridge = NULL;
13881
13882 while (pci_id->vendor != 0) {
13883 bridge = pci_get_device(pci_id->vendor,
13884 pci_id->device,
13885 bridge);
13886 if (!bridge) {
13887 pci_id++;
13888 continue;
13889 }
13890 if (bridge->subordinate &&
13891 (bridge->subordinate->number <=
13892 tp->pdev->bus->number) &&
13893 (bridge->subordinate->subordinate >=
13894 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013895 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013896 pci_dev_put(bridge);
13897 break;
13898 }
13899 }
13900 }
13901
Michael Chan4a29cc22006-03-19 13:21:12 -080013902 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13903 * DMA addresses > 40-bit. This bridge may have other additional
13904 * 57xx devices behind it in some 4-port NIC designs for example.
13905 * Any tg3 device found behind the bridge will also need the 40-bit
13906 * DMA workaround.
13907 */
Michael Chana4e2b342005-10-26 15:46:52 -070013908 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13909 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013910 tg3_flag_set(tp, 5780_CLASS);
13911 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013912 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013913 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013914 struct pci_dev *bridge = NULL;
13915
13916 do {
13917 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13918 PCI_DEVICE_ID_SERVERWORKS_EPB,
13919 bridge);
13920 if (bridge && bridge->subordinate &&
13921 (bridge->subordinate->number <=
13922 tp->pdev->bus->number) &&
13923 (bridge->subordinate->subordinate >=
13924 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013925 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013926 pci_dev_put(bridge);
13927 break;
13928 }
13929 } while (bridge);
13930 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013931
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013932 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000013933 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070013934 tp->pdev_peer = tg3_find_peer(tp);
13935
Matt Carlsonc885e822010-08-02 11:25:57 +000013936 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013937 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13938 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013939 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013940
13941 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013942 tg3_flag(tp, 5717_PLUS))
13943 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013944
Matt Carlson321d32a2008-11-21 17:22:19 -080013945 /* Intentionally exclude ASIC_REV_5906 */
13946 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013947 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013948 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013949 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013950 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013951 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013952 tg3_flag(tp, 57765_PLUS))
13953 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013954
13955 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13956 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013957 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013958 tg3_flag(tp, 5755_PLUS) ||
13959 tg3_flag(tp, 5780_CLASS))
13960 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013961
Matt Carlson6ff6f812011-05-19 12:12:54 +000013962 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013963 tg3_flag(tp, 5750_PLUS))
13964 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013965
Matt Carlson507399f2009-11-13 13:03:37 +000013966 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000013967 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000013968 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013969 else if (tg3_flag(tp, 57765_PLUS))
13970 tg3_flag_set(tp, HW_TSO_3);
13971 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013972 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013973 tg3_flag_set(tp, HW_TSO_2);
13974 else if (tg3_flag(tp, 5750_PLUS)) {
13975 tg3_flag_set(tp, HW_TSO_1);
13976 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_5750 &&
13978 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013979 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013980 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13981 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13982 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013983 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013984 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13985 tp->fw_needed = FIRMWARE_TG3TSO5;
13986 else
13987 tp->fw_needed = FIRMWARE_TG3TSO;
13988 }
13989
Matt Carlsondabc5c62011-05-19 12:12:52 +000013990 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013991 if (tg3_flag(tp, HW_TSO_1) ||
13992 tg3_flag(tp, HW_TSO_2) ||
13993 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsondabc5c62011-05-19 12:12:52 +000013994 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13995 tg3_flag_set(tp, TSO_CAPABLE);
13996 else {
13997 tg3_flag_clear(tp, TSO_CAPABLE);
13998 tg3_flag_clear(tp, TSO_BUG);
13999 tp->fw_needed = NULL;
14000 }
14001
14002 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14003 tp->fw_needed = FIRMWARE_TG3;
14004
Matt Carlson507399f2009-11-13 13:03:37 +000014005 tp->irq_max = 1;
14006
Joe Perches63c3a662011-04-26 08:12:10 +000014007 if (tg3_flag(tp, 5750_PLUS)) {
14008 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014009 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14010 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14011 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14012 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14013 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014014 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014015
Joe Perches63c3a662011-04-26 08:12:10 +000014016 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014017 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014018 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014020
Joe Perches63c3a662011-04-26 08:12:10 +000014021 if (tg3_flag(tp, 57765_PLUS)) {
14022 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014023 tp->irq_max = TG3_IRQ_MAX_VECS;
14024 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014025 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014026
Matt Carlson2ffcc982011-05-19 12:12:44 +000014027 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014028 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014029
Matt Carlsone31aa982011-07-27 14:20:53 +000014030 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
14031 tg3_flag_set(tp, 4K_FIFO_LIMIT);
14032
Joe Perches63c3a662011-04-26 08:12:10 +000014033 if (tg3_flag(tp, 5717_PLUS))
14034 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014035
Joe Perches63c3a662011-04-26 08:12:10 +000014036 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014037 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014038 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014039
Joe Perches63c3a662011-04-26 08:12:10 +000014040 if (!tg3_flag(tp, 5705_PLUS) ||
14041 tg3_flag(tp, 5780_CLASS) ||
14042 tg3_flag(tp, USE_JUMBO_BDFLAG))
14043 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014044
Matt Carlson52f44902008-11-21 17:17:04 -080014045 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14046 &pci_state_reg);
14047
Jon Mason708ebb32011-06-27 12:56:50 +000014048 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014049 u16 lnkctl;
14050
Joe Perches63c3a662011-04-26 08:12:10 +000014051 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014052
Matt Carlsoncf790032010-11-24 08:31:48 +000014053 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000014054 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14055 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000014056 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000014057
14058 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014059
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014060 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +000014061 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014062 &lnkctl);
14063 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014064 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14065 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014066 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014067 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014068 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014069 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014070 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014071 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14072 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014073 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b05902010-01-20 16:58:02 +000014074 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014075 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014076 }
Matt Carlson52f44902008-11-21 17:17:04 -080014077 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000014078 /* BCM5785 devices are effectively PCIe devices, and should
14079 * follow PCIe codepaths, but do not have a PCIe capabilities
14080 * section.
14081 */
Joe Perches63c3a662011-04-26 08:12:10 +000014082 tg3_flag_set(tp, PCI_EXPRESS);
14083 } else if (!tg3_flag(tp, 5705_PLUS) ||
14084 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014085 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14086 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014087 dev_err(&tp->pdev->dev,
14088 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014089 return -EIO;
14090 }
14091
14092 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014093 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014095
Michael Chan399de502005-10-03 14:02:39 -070014096 /* If we have an AMD 762 or VIA K8T800 chipset, write
14097 * reordering to the mailbox registers done by the host
14098 * controller can cause major troubles. We read back from
14099 * every mailbox register write to force the writes to be
14100 * posted to the chip in order.
14101 */
Matt Carlson41434702011-03-09 16:58:22 +000014102 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014103 !tg3_flag(tp, PCI_EXPRESS))
14104 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014105
Matt Carlson69fc4052008-12-21 20:19:57 -080014106 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14107 &tp->pci_cacheline_sz);
14108 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14109 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014110 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14111 tp->pci_lat_timer < 64) {
14112 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014113 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14114 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014115 }
14116
Matt Carlson16821282011-07-13 09:27:28 +000014117 /* Important! -- It is critical that the PCI-X hw workaround
14118 * situation is decided before the first MMIO register access.
14119 */
Matt Carlson52f44902008-11-21 17:17:04 -080014120 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14121 /* 5700 BX chips need to have their TX producer index
14122 * mailboxes written twice to workaround a bug.
14123 */
Joe Perches63c3a662011-04-26 08:12:10 +000014124 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014125
Matt Carlson52f44902008-11-21 17:17:04 -080014126 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014127 *
14128 * The workaround is to use indirect register accesses
14129 * for all chip writes not to mailbox registers.
14130 */
Joe Perches63c3a662011-04-26 08:12:10 +000014131 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014132 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014133
Joe Perches63c3a662011-04-26 08:12:10 +000014134 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014135
14136 /* The chip can have it's power management PCI config
14137 * space registers clobbered due to this bug.
14138 * So explicitly force the chip into D0 here.
14139 */
Matt Carlson9974a352007-10-07 23:27:28 -070014140 pci_read_config_dword(tp->pdev,
14141 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014142 &pm_reg);
14143 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14144 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014145 pci_write_config_dword(tp->pdev,
14146 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014147 pm_reg);
14148
14149 /* Also, force SERR#/PERR# in PCI command. */
14150 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14151 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14152 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14153 }
14154 }
14155
Linus Torvalds1da177e2005-04-16 15:20:36 -070014156 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014157 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014158 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014159 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014160
14161 /* Chip-specific fixup from Broadcom driver */
14162 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14163 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14164 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14165 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14166 }
14167
Michael Chan1ee582d2005-08-09 20:16:46 -070014168 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014169 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014170 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014171 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014172 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014173 tp->write32_tx_mbox = tg3_write32;
14174 tp->write32_rx_mbox = tg3_write32;
14175
14176 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014177 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014178 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014179 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014180 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014181 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14182 /*
14183 * Back to back register writes can cause problems on these
14184 * chips, the workaround is to read back all reg writes
14185 * except those to mailbox regs.
14186 *
14187 * See tg3_write_indirect_reg32().
14188 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014189 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014190 }
14191
Joe Perches63c3a662011-04-26 08:12:10 +000014192 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014193 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014194 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014195 tp->write32_rx_mbox = tg3_write_flush_reg32;
14196 }
Michael Chan20094932005-08-09 20:16:32 -070014197
Joe Perches63c3a662011-04-26 08:12:10 +000014198 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014199 tp->read32 = tg3_read_indirect_reg32;
14200 tp->write32 = tg3_write_indirect_reg32;
14201 tp->read32_mbox = tg3_read_indirect_mbox;
14202 tp->write32_mbox = tg3_write_indirect_mbox;
14203 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14204 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14205
14206 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014207 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014208
14209 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14210 pci_cmd &= ~PCI_COMMAND_MEMORY;
14211 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14212 }
Michael Chanb5d37722006-09-27 16:06:21 -070014213 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14214 tp->read32_mbox = tg3_read32_mbox_5906;
14215 tp->write32_mbox = tg3_write32_mbox_5906;
14216 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14217 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14218 }
Michael Chan68929142005-08-09 20:17:14 -070014219
Michael Chanbbadf502006-04-06 21:46:34 -070014220 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014221 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014222 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014223 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014224 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014225
Matt Carlson16821282011-07-13 09:27:28 +000014226 /* The memory arbiter has to be enabled in order for SRAM accesses
14227 * to succeed. Normally on powerup the tg3 chip firmware will make
14228 * sure it is enabled, but other entities such as system netboot
14229 * code might disable it.
14230 */
14231 val = tr32(MEMARB_MODE);
14232 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14233
Matt Carlson69f11c92011-07-13 09:27:30 +000014234 if (tg3_flag(tp, PCIX_MODE)) {
14235 pci_read_config_dword(tp->pdev,
14236 tp->pcix_cap + PCI_X_STATUS, &val);
14237 tp->pci_fn = val & 0x7;
14238 } else {
14239 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14240 }
14241
Michael Chan7d0c41e2005-04-21 17:06:20 -070014242 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014243 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014244 * determined before calling tg3_set_power_state() so that
14245 * we know whether or not to switch out of Vaux power.
14246 * When the flag is set, it means that GPIO1 is used for eeprom
14247 * write protect and also implies that it is a LOM where GPIOs
14248 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014249 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014250 tg3_get_eeprom_hw_cfg(tp);
14251
Joe Perches63c3a662011-04-26 08:12:10 +000014252 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014253 /* Allow reads and writes to the
14254 * APE register and memory space.
14255 */
14256 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +000014257 PCISTATE_ALLOW_APE_SHMEM_WR |
14258 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014259 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14260 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014261
14262 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014263 }
14264
Matt Carlson9936bcf2007-10-10 18:03:07 -070014265 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014266 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014267 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014268 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014269 tg3_flag(tp, 57765_PLUS))
14270 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014271
Matt Carlson16821282011-07-13 09:27:28 +000014272 /* Set up tp->grc_local_ctrl before calling
14273 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14274 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014275 * It is also used as eeprom write protect on LOMs.
14276 */
14277 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014278 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014279 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014280 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14281 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014282 /* Unused GPIO3 must be driven as output on 5752 because there
14283 * are no pull-up resistors on unused GPIO pins.
14284 */
14285 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14286 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014287
Matt Carlson321d32a2008-11-21 17:22:19 -080014288 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014289 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14290 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080014291 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14292
Matt Carlson8d519ab2009-04-20 06:58:01 +000014293 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14294 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014295 /* Turn off the debug UART. */
14296 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014297 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014298 /* Keep VMain power. */
14299 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14300 GRC_LCLCTRL_GPIO_OUTPUT0;
14301 }
14302
Matt Carlson16821282011-07-13 09:27:28 +000014303 /* Switch out of Vaux if it is a NIC */
14304 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014305
Linus Torvalds1da177e2005-04-16 15:20:36 -070014306 /* Derive initial jumbo mode from MTU assigned in
14307 * ether_setup() via the alloc_etherdev() call
14308 */
Joe Perches63c3a662011-04-26 08:12:10 +000014309 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14310 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014311
14312 /* Determine WakeOnLan speed to use. */
14313 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14314 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14315 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14316 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014317 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014318 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014319 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014320 }
14321
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014322 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014323 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014324
Linus Torvalds1da177e2005-04-16 15:20:36 -070014325 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014326 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14327 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014328 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014329 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014330 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14331 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14332 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014333
14334 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14335 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014336 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014337 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014338 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014339
Joe Perches63c3a662011-04-26 08:12:10 +000014340 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014341 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014342 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014343 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014344 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014345 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014346 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014347 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14348 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014349 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14350 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014351 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014352 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014353 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014354 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014355 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014357
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014358 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14359 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14360 tp->phy_otp = tg3_read_otp_phycfg(tp);
14361 if (tp->phy_otp == 0)
14362 tp->phy_otp = TG3_OTP_DEFAULT;
14363 }
14364
Joe Perches63c3a662011-04-26 08:12:10 +000014365 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014366 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14367 else
14368 tp->mi_mode = MAC_MI_MODE_BASE;
14369
Linus Torvalds1da177e2005-04-16 15:20:36 -070014370 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014371 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14372 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14373 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14374
Matt Carlson4d958472011-04-20 07:57:35 +000014375 /* Set these bits to enable statistics workaround. */
14376 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14377 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14378 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14379 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14380 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14381 }
14382
Matt Carlson321d32a2008-11-21 17:22:19 -080014383 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14384 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014385 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014386
Matt Carlson158d7ab2008-05-29 01:37:54 -070014387 err = tg3_mdio_init(tp);
14388 if (err)
14389 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014390
14391 /* Initialize data/descriptor byte/word swapping. */
14392 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014393 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14394 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14395 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14396 GRC_MODE_B2HRX_ENABLE |
14397 GRC_MODE_HTX2B_ENABLE |
14398 GRC_MODE_HOST_STACKUP);
14399 else
14400 val &= GRC_MODE_HOST_STACKUP;
14401
Linus Torvalds1da177e2005-04-16 15:20:36 -070014402 tw32(GRC_MODE, val | tp->grc_mode);
14403
14404 tg3_switch_clocks(tp);
14405
14406 /* Clear this out for sanity. */
14407 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14408
14409 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14410 &pci_state_reg);
14411 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014412 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014413 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14414
14415 if (chiprevid == CHIPREV_ID_5701_A0 ||
14416 chiprevid == CHIPREV_ID_5701_B0 ||
14417 chiprevid == CHIPREV_ID_5701_B2 ||
14418 chiprevid == CHIPREV_ID_5701_B5) {
14419 void __iomem *sram_base;
14420
14421 /* Write some dummy words into the SRAM status block
14422 * area, see if it reads back correctly. If the return
14423 * value is bad, force enable the PCIX workaround.
14424 */
14425 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14426
14427 writel(0x00000000, sram_base);
14428 writel(0x00000000, sram_base + 4);
14429 writel(0xffffffff, sram_base + 4);
14430 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014431 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014432 }
14433 }
14434
14435 udelay(50);
14436 tg3_nvram_init(tp);
14437
14438 grc_misc_cfg = tr32(GRC_MISC_CFG);
14439 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14440
Linus Torvalds1da177e2005-04-16 15:20:36 -070014441 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14442 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14443 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014444 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014445
Joe Perches63c3a662011-04-26 08:12:10 +000014446 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014447 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014448 tg3_flag_set(tp, TAGGED_STATUS);
14449 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014450 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14451 HOSTCC_MODE_CLRTICK_TXBD);
14452
14453 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14454 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14455 tp->misc_host_ctrl);
14456 }
14457
Matt Carlson3bda1252008-08-15 14:08:22 -070014458 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014459 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014460 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014461 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014462 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014463
Linus Torvalds1da177e2005-04-16 15:20:36 -070014464 /* these are limited to 10/100 only */
14465 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14466 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14467 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14468 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14469 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14470 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14471 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14472 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14473 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014474 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14475 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014476 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014477 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14478 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014479 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14480 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014481
14482 err = tg3_phy_probe(tp);
14483 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014484 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014485 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014486 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014487 }
14488
Matt Carlson184b8902010-04-05 10:19:25 +000014489 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014490 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014491
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014492 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14493 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014494 } else {
14495 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014496 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014497 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014498 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014499 }
14500
14501 /* 5700 {AX,BX} chips have a broken status block link
14502 * change bit implementation, so we must use the
14503 * status register in those cases.
14504 */
14505 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014506 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014507 else
Joe Perches63c3a662011-04-26 08:12:10 +000014508 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014509
14510 /* The led_ctrl is set during tg3_phy_probe, here we might
14511 * have to force the link status polling mechanism based
14512 * upon subsystem IDs.
14513 */
14514 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014515 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014516 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14517 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014518 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014519 }
14520
14521 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014522 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014523 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014524 else
Joe Perches63c3a662011-04-26 08:12:10 +000014525 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014526
Matt Carlsonbf933c82011-01-25 15:58:49 +000014527 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014528 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014529 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014530 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014531 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014532#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014533 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014534#endif
14535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014536
Matt Carlson2c49a442010-09-30 10:34:35 +000014537 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14538 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014539 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14540
Matt Carlson2c49a442010-09-30 10:34:35 +000014541 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014542
14543 /* Increment the rx prod index on the rx std ring by at most
14544 * 8 for these chips to workaround hw errata.
14545 */
14546 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14547 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14548 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14549 tp->rx_std_max_post = 8;
14550
Joe Perches63c3a662011-04-26 08:12:10 +000014551 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014552 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14553 PCIE_PWR_MGMT_L1_THRESH_MSK;
14554
Linus Torvalds1da177e2005-04-16 15:20:36 -070014555 return err;
14556}
14557
David S. Miller49b6e95f2007-03-29 01:38:42 -070014558#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014559static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14560{
14561 struct net_device *dev = tp->dev;
14562 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014563 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014564 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014565 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014566
David S. Miller49b6e95f2007-03-29 01:38:42 -070014567 addr = of_get_property(dp, "local-mac-address", &len);
14568 if (addr && len == 6) {
14569 memcpy(dev->dev_addr, addr, 6);
14570 memcpy(dev->perm_addr, dev->dev_addr, 6);
14571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014572 }
14573 return -ENODEV;
14574}
14575
14576static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14577{
14578 struct net_device *dev = tp->dev;
14579
14580 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014581 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014582 return 0;
14583}
14584#endif
14585
14586static int __devinit tg3_get_device_address(struct tg3 *tp)
14587{
14588 struct net_device *dev = tp->dev;
14589 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014590 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014591
David S. Miller49b6e95f2007-03-29 01:38:42 -070014592#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014593 if (!tg3_get_macaddr_sparc(tp))
14594 return 0;
14595#endif
14596
14597 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014598 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014599 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014600 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14601 mac_offset = 0xcc;
14602 if (tg3_nvram_lock(tp))
14603 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14604 else
14605 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014606 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014607 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014608 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014609 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014610 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014611 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014612 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014613
14614 /* First try to get it from MAC address mailbox. */
14615 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14616 if ((hi >> 16) == 0x484b) {
14617 dev->dev_addr[0] = (hi >> 8) & 0xff;
14618 dev->dev_addr[1] = (hi >> 0) & 0xff;
14619
14620 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14621 dev->dev_addr[2] = (lo >> 24) & 0xff;
14622 dev->dev_addr[3] = (lo >> 16) & 0xff;
14623 dev->dev_addr[4] = (lo >> 8) & 0xff;
14624 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014625
Michael Chan008652b2006-03-27 23:14:53 -080014626 /* Some old bootcode may report a 0 MAC address in SRAM */
14627 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14628 }
14629 if (!addr_ok) {
14630 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014631 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014632 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014633 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014634 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14635 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014636 }
14637 /* Finally just fetch it out of the MAC control regs. */
14638 else {
14639 hi = tr32(MAC_ADDR_0_HIGH);
14640 lo = tr32(MAC_ADDR_0_LOW);
14641
14642 dev->dev_addr[5] = lo & 0xff;
14643 dev->dev_addr[4] = (lo >> 8) & 0xff;
14644 dev->dev_addr[3] = (lo >> 16) & 0xff;
14645 dev->dev_addr[2] = (lo >> 24) & 0xff;
14646 dev->dev_addr[1] = hi & 0xff;
14647 dev->dev_addr[0] = (hi >> 8) & 0xff;
14648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014649 }
14650
14651 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014652#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014653 if (!tg3_get_default_macaddr_sparc(tp))
14654 return 0;
14655#endif
14656 return -EINVAL;
14657 }
John W. Linville2ff43692005-09-12 14:44:20 -070014658 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014659 return 0;
14660}
14661
David S. Miller59e6b432005-05-18 22:50:10 -070014662#define BOUNDARY_SINGLE_CACHELINE 1
14663#define BOUNDARY_MULTI_CACHELINE 2
14664
14665static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14666{
14667 int cacheline_size;
14668 u8 byte;
14669 int goal;
14670
14671 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14672 if (byte == 0)
14673 cacheline_size = 1024;
14674 else
14675 cacheline_size = (int) byte * 4;
14676
14677 /* On 5703 and later chips, the boundary bits have no
14678 * effect.
14679 */
14680 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14681 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014682 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014683 goto out;
14684
14685#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14686 goal = BOUNDARY_MULTI_CACHELINE;
14687#else
14688#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14689 goal = BOUNDARY_SINGLE_CACHELINE;
14690#else
14691 goal = 0;
14692#endif
14693#endif
14694
Joe Perches63c3a662011-04-26 08:12:10 +000014695 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014696 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14697 goto out;
14698 }
14699
David S. Miller59e6b432005-05-18 22:50:10 -070014700 if (!goal)
14701 goto out;
14702
14703 /* PCI controllers on most RISC systems tend to disconnect
14704 * when a device tries to burst across a cache-line boundary.
14705 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14706 *
14707 * Unfortunately, for PCI-E there are only limited
14708 * write-side controls for this, and thus for reads
14709 * we will still get the disconnects. We'll also waste
14710 * these PCI cycles for both read and write for chips
14711 * other than 5700 and 5701 which do not implement the
14712 * boundary bits.
14713 */
Joe Perches63c3a662011-04-26 08:12:10 +000014714 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014715 switch (cacheline_size) {
14716 case 16:
14717 case 32:
14718 case 64:
14719 case 128:
14720 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14721 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14722 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14723 } else {
14724 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14725 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14726 }
14727 break;
14728
14729 case 256:
14730 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14731 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14732 break;
14733
14734 default:
14735 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14736 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14737 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014738 }
Joe Perches63c3a662011-04-26 08:12:10 +000014739 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014740 switch (cacheline_size) {
14741 case 16:
14742 case 32:
14743 case 64:
14744 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14745 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14746 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14747 break;
14748 }
14749 /* fallthrough */
14750 case 128:
14751 default:
14752 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14753 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14754 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014755 }
David S. Miller59e6b432005-05-18 22:50:10 -070014756 } else {
14757 switch (cacheline_size) {
14758 case 16:
14759 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14760 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14761 DMA_RWCTRL_WRITE_BNDRY_16);
14762 break;
14763 }
14764 /* fallthrough */
14765 case 32:
14766 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14767 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14768 DMA_RWCTRL_WRITE_BNDRY_32);
14769 break;
14770 }
14771 /* fallthrough */
14772 case 64:
14773 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14774 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14775 DMA_RWCTRL_WRITE_BNDRY_64);
14776 break;
14777 }
14778 /* fallthrough */
14779 case 128:
14780 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14781 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14782 DMA_RWCTRL_WRITE_BNDRY_128);
14783 break;
14784 }
14785 /* fallthrough */
14786 case 256:
14787 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14788 DMA_RWCTRL_WRITE_BNDRY_256);
14789 break;
14790 case 512:
14791 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14792 DMA_RWCTRL_WRITE_BNDRY_512);
14793 break;
14794 case 1024:
14795 default:
14796 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14797 DMA_RWCTRL_WRITE_BNDRY_1024);
14798 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014799 }
David S. Miller59e6b432005-05-18 22:50:10 -070014800 }
14801
14802out:
14803 return val;
14804}
14805
Linus Torvalds1da177e2005-04-16 15:20:36 -070014806static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14807{
14808 struct tg3_internal_buffer_desc test_desc;
14809 u32 sram_dma_descs;
14810 int i, ret;
14811
14812 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14813
14814 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14815 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14816 tw32(RDMAC_STATUS, 0);
14817 tw32(WDMAC_STATUS, 0);
14818
14819 tw32(BUFMGR_MODE, 0);
14820 tw32(FTQ_RESET, 0);
14821
14822 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14823 test_desc.addr_lo = buf_dma & 0xffffffff;
14824 test_desc.nic_mbuf = 0x00002100;
14825 test_desc.len = size;
14826
14827 /*
14828 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14829 * the *second* time the tg3 driver was getting loaded after an
14830 * initial scan.
14831 *
14832 * Broadcom tells me:
14833 * ...the DMA engine is connected to the GRC block and a DMA
14834 * reset may affect the GRC block in some unpredictable way...
14835 * The behavior of resets to individual blocks has not been tested.
14836 *
14837 * Broadcom noted the GRC reset will also reset all sub-components.
14838 */
14839 if (to_device) {
14840 test_desc.cqid_sqid = (13 << 8) | 2;
14841
14842 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14843 udelay(40);
14844 } else {
14845 test_desc.cqid_sqid = (16 << 8) | 7;
14846
14847 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14848 udelay(40);
14849 }
14850 test_desc.flags = 0x00000005;
14851
14852 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14853 u32 val;
14854
14855 val = *(((u32 *)&test_desc) + i);
14856 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14857 sram_dma_descs + (i * sizeof(u32)));
14858 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14859 }
14860 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14861
Matt Carlson859a5882010-04-05 10:19:28 +000014862 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014863 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014864 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014865 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014866
14867 ret = -ENODEV;
14868 for (i = 0; i < 40; i++) {
14869 u32 val;
14870
14871 if (to_device)
14872 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14873 else
14874 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14875 if ((val & 0xffff) == sram_dma_descs) {
14876 ret = 0;
14877 break;
14878 }
14879
14880 udelay(100);
14881 }
14882
14883 return ret;
14884}
14885
David S. Millerded73402005-05-23 13:59:47 -070014886#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014887
Matt Carlson41434702011-03-09 16:58:22 +000014888static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014889 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14890 { },
14891};
14892
Linus Torvalds1da177e2005-04-16 15:20:36 -070014893static int __devinit tg3_test_dma(struct tg3 *tp)
14894{
14895 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014896 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014897 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014898
Matt Carlson4bae65c2010-11-24 08:31:52 +000014899 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14900 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014901 if (!buf) {
14902 ret = -ENOMEM;
14903 goto out_nofree;
14904 }
14905
14906 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14907 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14908
David S. Miller59e6b432005-05-18 22:50:10 -070014909 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014910
Joe Perches63c3a662011-04-26 08:12:10 +000014911 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014912 goto out;
14913
Joe Perches63c3a662011-04-26 08:12:10 +000014914 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014915 /* DMA read watermark not used on PCIE */
14916 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014917 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014918 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14919 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014920 tp->dma_rwctrl |= 0x003f0000;
14921 else
14922 tp->dma_rwctrl |= 0x003f000f;
14923 } else {
14924 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14925 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14926 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014927 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014928
Michael Chan4a29cc22006-03-19 13:21:12 -080014929 /* If the 5704 is behind the EPB bridge, we can
14930 * do the less restrictive ONE_DMA workaround for
14931 * better performance.
14932 */
Joe Perches63c3a662011-04-26 08:12:10 +000014933 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014934 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14935 tp->dma_rwctrl |= 0x8000;
14936 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014937 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14938
Michael Chan49afdeb2007-02-13 12:17:03 -080014939 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14940 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014941 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014942 tp->dma_rwctrl |=
14943 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14944 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14945 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014946 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14947 /* 5780 always in PCIX mode */
14948 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014949 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14950 /* 5714 always in PCIX mode */
14951 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014952 } else {
14953 tp->dma_rwctrl |= 0x001b000f;
14954 }
14955 }
14956
14957 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14958 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14959 tp->dma_rwctrl &= 0xfffffff0;
14960
14961 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14962 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14963 /* Remove this if it causes problems for some boards. */
14964 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14965
14966 /* On 5700/5701 chips, we need to set this bit.
14967 * Otherwise the chip will issue cacheline transactions
14968 * to streamable DMA memory with not all the byte
14969 * enables turned on. This is an error on several
14970 * RISC PCI controllers, in particular sparc64.
14971 *
14972 * On 5703/5704 chips, this bit has been reassigned
14973 * a different meaning. In particular, it is used
14974 * on those chips to enable a PCI-X workaround.
14975 */
14976 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14977 }
14978
14979 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14980
14981#if 0
14982 /* Unneeded, already done by tg3_get_invariants. */
14983 tg3_switch_clocks(tp);
14984#endif
14985
Linus Torvalds1da177e2005-04-16 15:20:36 -070014986 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14987 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14988 goto out;
14989
David S. Miller59e6b432005-05-18 22:50:10 -070014990 /* It is best to perform DMA test with maximum write burst size
14991 * to expose the 5700/5701 write DMA bug.
14992 */
14993 saved_dma_rwctrl = tp->dma_rwctrl;
14994 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14995 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14996
Linus Torvalds1da177e2005-04-16 15:20:36 -070014997 while (1) {
14998 u32 *p = buf, i;
14999
15000 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15001 p[i] = i;
15002
15003 /* Send the buffer to the chip. */
15004 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15005 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015006 dev_err(&tp->pdev->dev,
15007 "%s: Buffer write failed. err = %d\n",
15008 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015009 break;
15010 }
15011
15012#if 0
15013 /* validate data reached card RAM correctly. */
15014 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15015 u32 val;
15016 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15017 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015018 dev_err(&tp->pdev->dev,
15019 "%s: Buffer corrupted on device! "
15020 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015021 /* ret = -ENODEV here? */
15022 }
15023 p[i] = 0;
15024 }
15025#endif
15026 /* Now read it back. */
15027 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15028 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015029 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15030 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015031 break;
15032 }
15033
15034 /* Verify it. */
15035 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15036 if (p[i] == i)
15037 continue;
15038
David S. Miller59e6b432005-05-18 22:50:10 -070015039 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15040 DMA_RWCTRL_WRITE_BNDRY_16) {
15041 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015042 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15043 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15044 break;
15045 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015046 dev_err(&tp->pdev->dev,
15047 "%s: Buffer corrupted on read back! "
15048 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015049 ret = -ENODEV;
15050 goto out;
15051 }
15052 }
15053
15054 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15055 /* Success. */
15056 ret = 0;
15057 break;
15058 }
15059 }
David S. Miller59e6b432005-05-18 22:50:10 -070015060 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15061 DMA_RWCTRL_WRITE_BNDRY_16) {
15062 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015063 * now look for chipsets that are known to expose the
15064 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015065 */
Matt Carlson41434702011-03-09 16:58:22 +000015066 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015067 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15068 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000015069 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015070 /* Safe to use the calculated DMA boundary. */
15071 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000015072 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015073
David S. Miller59e6b432005-05-18 22:50:10 -070015074 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015076
15077out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015078 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015079out_nofree:
15080 return ret;
15081}
15082
Linus Torvalds1da177e2005-04-16 15:20:36 -070015083static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15084{
Joe Perches63c3a662011-04-26 08:12:10 +000015085 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015086 tp->bufmgr_config.mbuf_read_dma_low_water =
15087 DEFAULT_MB_RDMA_LOW_WATER_5705;
15088 tp->bufmgr_config.mbuf_mac_rx_low_water =
15089 DEFAULT_MB_MACRX_LOW_WATER_57765;
15090 tp->bufmgr_config.mbuf_high_water =
15091 DEFAULT_MB_HIGH_WATER_57765;
15092
15093 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15094 DEFAULT_MB_RDMA_LOW_WATER_5705;
15095 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15096 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15097 tp->bufmgr_config.mbuf_high_water_jumbo =
15098 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015099 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec172005-07-25 12:31:48 -070015100 tp->bufmgr_config.mbuf_read_dma_low_water =
15101 DEFAULT_MB_RDMA_LOW_WATER_5705;
15102 tp->bufmgr_config.mbuf_mac_rx_low_water =
15103 DEFAULT_MB_MACRX_LOW_WATER_5705;
15104 tp->bufmgr_config.mbuf_high_water =
15105 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015106 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15107 tp->bufmgr_config.mbuf_mac_rx_low_water =
15108 DEFAULT_MB_MACRX_LOW_WATER_5906;
15109 tp->bufmgr_config.mbuf_high_water =
15110 DEFAULT_MB_HIGH_WATER_5906;
15111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015112
Michael Chanfdfec172005-07-25 12:31:48 -070015113 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15114 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15115 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15116 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15117 tp->bufmgr_config.mbuf_high_water_jumbo =
15118 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15119 } else {
15120 tp->bufmgr_config.mbuf_read_dma_low_water =
15121 DEFAULT_MB_RDMA_LOW_WATER;
15122 tp->bufmgr_config.mbuf_mac_rx_low_water =
15123 DEFAULT_MB_MACRX_LOW_WATER;
15124 tp->bufmgr_config.mbuf_high_water =
15125 DEFAULT_MB_HIGH_WATER;
15126
15127 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15128 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15129 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15130 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15131 tp->bufmgr_config.mbuf_high_water_jumbo =
15132 DEFAULT_MB_HIGH_WATER_JUMBO;
15133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015134
15135 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15136 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15137}
15138
15139static char * __devinit tg3_phy_string(struct tg3 *tp)
15140{
Matt Carlson79eb6902010-02-17 15:17:03 +000015141 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15142 case TG3_PHY_ID_BCM5400: return "5400";
15143 case TG3_PHY_ID_BCM5401: return "5401";
15144 case TG3_PHY_ID_BCM5411: return "5411";
15145 case TG3_PHY_ID_BCM5701: return "5701";
15146 case TG3_PHY_ID_BCM5703: return "5703";
15147 case TG3_PHY_ID_BCM5704: return "5704";
15148 case TG3_PHY_ID_BCM5705: return "5705";
15149 case TG3_PHY_ID_BCM5750: return "5750";
15150 case TG3_PHY_ID_BCM5752: return "5752";
15151 case TG3_PHY_ID_BCM5714: return "5714";
15152 case TG3_PHY_ID_BCM5780: return "5780";
15153 case TG3_PHY_ID_BCM5755: return "5755";
15154 case TG3_PHY_ID_BCM5787: return "5787";
15155 case TG3_PHY_ID_BCM5784: return "5784";
15156 case TG3_PHY_ID_BCM5756: return "5722/5756";
15157 case TG3_PHY_ID_BCM5906: return "5906";
15158 case TG3_PHY_ID_BCM5761: return "5761";
15159 case TG3_PHY_ID_BCM5718C: return "5718C";
15160 case TG3_PHY_ID_BCM5718S: return "5718S";
15161 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015162 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015163 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015164 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015165 case 0: return "serdes";
15166 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015168}
15169
Michael Chanf9804dd2005-09-27 12:13:10 -070015170static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15171{
Joe Perches63c3a662011-04-26 08:12:10 +000015172 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015173 strcpy(str, "PCI Express");
15174 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015175 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015176 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15177
15178 strcpy(str, "PCIX:");
15179
15180 if ((clock_ctrl == 7) ||
15181 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15182 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15183 strcat(str, "133MHz");
15184 else if (clock_ctrl == 0)
15185 strcat(str, "33MHz");
15186 else if (clock_ctrl == 2)
15187 strcat(str, "50MHz");
15188 else if (clock_ctrl == 4)
15189 strcat(str, "66MHz");
15190 else if (clock_ctrl == 6)
15191 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015192 } else {
15193 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015194 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015195 strcat(str, "66MHz");
15196 else
15197 strcat(str, "33MHz");
15198 }
Joe Perches63c3a662011-04-26 08:12:10 +000015199 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015200 strcat(str, ":32-bit");
15201 else
15202 strcat(str, ":64-bit");
15203 return str;
15204}
15205
Michael Chan8c2dc7e2005-12-19 16:26:02 -080015206static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015207{
15208 struct pci_dev *peer;
15209 unsigned int func, devnr = tp->pdev->devfn & ~7;
15210
15211 for (func = 0; func < 8; func++) {
15212 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15213 if (peer && peer != tp->pdev)
15214 break;
15215 pci_dev_put(peer);
15216 }
Michael Chan16fe9d72005-12-13 21:09:54 -080015217 /* 5704 can be configured in single-port mode, set peer to
15218 * tp->pdev in that case.
15219 */
15220 if (!peer) {
15221 peer = tp->pdev;
15222 return peer;
15223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015224
15225 /*
15226 * We don't need to keep the refcount elevated; there's no way
15227 * to remove one half of this device without removing the other
15228 */
15229 pci_dev_put(peer);
15230
15231 return peer;
15232}
15233
David S. Miller15f98502005-05-18 22:49:26 -070015234static void __devinit tg3_init_coal(struct tg3 *tp)
15235{
15236 struct ethtool_coalesce *ec = &tp->coal;
15237
15238 memset(ec, 0, sizeof(*ec));
15239 ec->cmd = ETHTOOL_GCOALESCE;
15240 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15241 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15242 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15243 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15244 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15245 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15246 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15247 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15248 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15249
15250 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15251 HOSTCC_MODE_CLRTICK_TXBD)) {
15252 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15253 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15254 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15255 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15256 }
Michael Chand244c892005-07-05 14:42:33 -070015257
Joe Perches63c3a662011-04-26 08:12:10 +000015258 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015259 ec->rx_coalesce_usecs_irq = 0;
15260 ec->tx_coalesce_usecs_irq = 0;
15261 ec->stats_block_coalesce_usecs = 0;
15262 }
David S. Miller15f98502005-05-18 22:49:26 -070015263}
15264
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015265static const struct net_device_ops tg3_netdev_ops = {
15266 .ndo_open = tg3_open,
15267 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080015268 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000015269 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080015270 .ndo_validate_addr = eth_validate_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +000015271 .ndo_set_rx_mode = tg3_set_rx_mode,
Stephen Hemminger00829822008-11-20 20:14:53 -080015272 .ndo_set_mac_address = tg3_set_mac_addr,
15273 .ndo_do_ioctl = tg3_ioctl,
15274 .ndo_tx_timeout = tg3_tx_timeout,
15275 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000015276 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015277 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080015278#ifdef CONFIG_NET_POLL_CONTROLLER
15279 .ndo_poll_controller = tg3_poll_controller,
15280#endif
15281};
15282
Linus Torvalds1da177e2005-04-16 15:20:36 -070015283static int __devinit tg3_init_one(struct pci_dev *pdev,
15284 const struct pci_device_id *ent)
15285{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015286 struct net_device *dev;
15287 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015288 int i, err, pm_cap;
15289 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015290 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015291 u64 dma_mask, persist_dma_mask;
Matt Carlson0da06062011-05-19 12:12:53 +000015292 u32 features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015293
Joe Perches05dbe002010-02-17 19:44:19 +000015294 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015295
15296 err = pci_enable_device(pdev);
15297 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015298 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015299 return err;
15300 }
15301
Linus Torvalds1da177e2005-04-16 15:20:36 -070015302 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15303 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015304 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015305 goto err_out_disable_pdev;
15306 }
15307
15308 pci_set_master(pdev);
15309
15310 /* Find power-management capability. */
15311 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15312 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015313 dev_err(&pdev->dev,
15314 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015315 err = -EIO;
15316 goto err_out_free_res;
15317 }
15318
Matt Carlson16821282011-07-13 09:27:28 +000015319 err = pci_set_power_state(pdev, PCI_D0);
15320 if (err) {
15321 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15322 goto err_out_free_res;
15323 }
15324
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015325 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015326 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015327 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015328 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015329 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015330 }
15331
Linus Torvalds1da177e2005-04-16 15:20:36 -070015332 SET_NETDEV_DEV(dev, &pdev->dev);
15333
Linus Torvalds1da177e2005-04-16 15:20:36 -070015334 tp = netdev_priv(dev);
15335 tp->pdev = pdev;
15336 tp->dev = dev;
15337 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015338 tp->rx_mode = TG3_DEF_RX_MODE;
15339 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015340
Linus Torvalds1da177e2005-04-16 15:20:36 -070015341 if (tg3_debug > 0)
15342 tp->msg_enable = tg3_debug;
15343 else
15344 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15345
15346 /* The word/byte swap controls here control register access byte
15347 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15348 * setting below.
15349 */
15350 tp->misc_host_ctrl =
15351 MISC_HOST_CTRL_MASK_PCI_INT |
15352 MISC_HOST_CTRL_WORD_SWAP |
15353 MISC_HOST_CTRL_INDIR_ACCESS |
15354 MISC_HOST_CTRL_PCISTATE_RW;
15355
15356 /* The NONFRM (non-frame) byte/word swap controls take effect
15357 * on descriptor entries, anything which isn't packet data.
15358 *
15359 * The StrongARM chips on the board (one for tx, one for rx)
15360 * are running in big-endian mode.
15361 */
15362 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15363 GRC_MODE_WSWAP_NONFRM_DATA);
15364#ifdef __BIG_ENDIAN
15365 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15366#endif
15367 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015368 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015369 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015370
Matt Carlsond5fe4882008-11-21 17:20:32 -080015371 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015372 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015373 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015374 err = -ENOMEM;
15375 goto err_out_free_dev;
15376 }
15377
Matt Carlsonc9cab242011-07-13 09:27:27 +000015378 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15379 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15380 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15381 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15382 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15383 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15384 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15385 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15386 tg3_flag_set(tp, ENABLE_APE);
15387 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15388 if (!tp->aperegs) {
15389 dev_err(&pdev->dev,
15390 "Cannot map APE registers, aborting\n");
15391 err = -ENOMEM;
15392 goto err_out_iounmap;
15393 }
15394 }
15395
Linus Torvalds1da177e2005-04-16 15:20:36 -070015396 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15397 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015398
Linus Torvalds1da177e2005-04-16 15:20:36 -070015399 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015400 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015401 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015402 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015403
15404 err = tg3_get_invariants(tp);
15405 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015406 dev_err(&pdev->dev,
15407 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015408 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015409 }
15410
Michael Chan4a29cc22006-03-19 13:21:12 -080015411 /* The EPB bridge inside 5714, 5715, and 5780 and any
15412 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015413 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15414 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15415 * do DMA address check in tg3_start_xmit().
15416 */
Joe Perches63c3a662011-04-26 08:12:10 +000015417 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015418 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015419 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015420 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015421#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015422 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015423#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015424 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015425 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015426
15427 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015428 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015429 err = pci_set_dma_mask(pdev, dma_mask);
15430 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015431 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015432 err = pci_set_consistent_dma_mask(pdev,
15433 persist_dma_mask);
15434 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015435 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15436 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015437 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015438 }
15439 }
15440 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015441 if (err || dma_mask == DMA_BIT_MASK(32)) {
15442 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015443 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015444 dev_err(&pdev->dev,
15445 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015446 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015447 }
15448 }
15449
Michael Chanfdfec172005-07-25 12:31:48 -070015450 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015451
Matt Carlson0da06062011-05-19 12:12:53 +000015452 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15453
15454 /* 5700 B0 chips do not support checksumming correctly due
15455 * to hardware bugs.
15456 */
15457 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15458 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15459
15460 if (tg3_flag(tp, 5755_PLUS))
15461 features |= NETIF_F_IPV6_CSUM;
15462 }
15463
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015464 /* TSO is on by default on chips that support hardware TSO.
15465 * Firmware TSO on older chips gives lower performance, so it
15466 * is off by default, but can be enabled using ethtool.
15467 */
Joe Perches63c3a662011-04-26 08:12:10 +000015468 if ((tg3_flag(tp, HW_TSO_1) ||
15469 tg3_flag(tp, HW_TSO_2) ||
15470 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015471 (features & NETIF_F_IP_CSUM))
15472 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015473 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015474 if (features & NETIF_F_IPV6_CSUM)
15475 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015476 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015477 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015478 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15479 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015480 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015481 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015482 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015484
Matt Carlsond542fe22011-05-19 16:02:43 +000015485 dev->features |= features;
15486 dev->vlan_features |= features;
15487
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015488 /*
15489 * Add loopback capability only for a subset of devices that support
15490 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15491 * loopback for the remaining devices.
15492 */
15493 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15494 !tg3_flag(tp, CPMU_PRESENT))
15495 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015496 features |= NETIF_F_LOOPBACK;
15497
Matt Carlson0da06062011-05-19 12:12:53 +000015498 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015499
Linus Torvalds1da177e2005-04-16 15:20:36 -070015500 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015501 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015502 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015503 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015504 tp->rx_pending = 63;
15505 }
15506
Linus Torvalds1da177e2005-04-16 15:20:36 -070015507 err = tg3_get_device_address(tp);
15508 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015509 dev_err(&pdev->dev,
15510 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015511 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015512 }
15513
Matt Carlsonc88864d2007-11-12 21:07:01 -080015514 /*
15515 * Reset chip in case UNDI or EFI driver did not shutdown
15516 * DMA self test will enable WDMAC and we'll see (spurious)
15517 * pending DMA on the PCI bus at that point.
15518 */
15519 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15520 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15521 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15522 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15523 }
15524
15525 err = tg3_test_dma(tp);
15526 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015527 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015528 goto err_out_apeunmap;
15529 }
15530
Matt Carlson78f90dc2009-11-13 13:03:42 +000015531 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15532 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15533 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015534 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015535 struct tg3_napi *tnapi = &tp->napi[i];
15536
15537 tnapi->tp = tp;
15538 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15539
15540 tnapi->int_mbox = intmbx;
15541 if (i < 4)
15542 intmbx += 0x8;
15543 else
15544 intmbx += 0x4;
15545
15546 tnapi->consmbox = rcvmbx;
15547 tnapi->prodmbox = sndmbx;
15548
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015549 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015550 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015551 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015552 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015553
Joe Perches63c3a662011-04-26 08:12:10 +000015554 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015555 break;
15556
15557 /*
15558 * If we support MSIX, we'll be using RSS. If we're using
15559 * RSS, the first vector only handles link interrupts and the
15560 * remaining vectors handle rx and tx interrupts. Reuse the
15561 * mailbox values for the next iteration. The values we setup
15562 * above are still useful for the single vectored mode.
15563 */
15564 if (!i)
15565 continue;
15566
15567 rcvmbx += 0x8;
15568
15569 if (sndmbx & 0x4)
15570 sndmbx -= 0x4;
15571 else
15572 sndmbx += 0xc;
15573 }
15574
Matt Carlsonc88864d2007-11-12 21:07:01 -080015575 tg3_init_coal(tp);
15576
Michael Chanc49a1562006-12-17 17:07:29 -080015577 pci_set_drvdata(pdev, dev);
15578
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015579 if (tg3_flag(tp, 5717_PLUS)) {
15580 /* Resume a low-power mode */
15581 tg3_frob_aux_power(tp, false);
15582 }
15583
Linus Torvalds1da177e2005-04-16 15:20:36 -070015584 err = register_netdev(dev);
15585 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015586 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015587 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015588 }
15589
Joe Perches05dbe002010-02-17 19:44:19 +000015590 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15591 tp->board_part_number,
15592 tp->pci_chip_rev_id,
15593 tg3_bus_string(tp, str),
15594 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015595
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015596 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015597 struct phy_device *phydev;
15598 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015599 netdev_info(dev,
15600 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015601 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015602 } else {
15603 char *ethtype;
15604
15605 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15606 ethtype = "10/100Base-TX";
15607 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15608 ethtype = "1000Base-SX";
15609 else
15610 ethtype = "10/100/1000Base-T";
15611
Matt Carlson5129c3a2010-04-05 10:19:23 +000015612 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015613 "(WireSpeed[%d], EEE[%d])\n",
15614 tg3_phy_string(tp), ethtype,
15615 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15616 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015617 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015618
Joe Perches05dbe002010-02-17 19:44:19 +000015619 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015620 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015621 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015622 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015623 tg3_flag(tp, ENABLE_ASF) != 0,
15624 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015625 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15626 tp->dma_rwctrl,
15627 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15628 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015629
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015630 pci_save_state(pdev);
15631
Linus Torvalds1da177e2005-04-16 15:20:36 -070015632 return 0;
15633
Matt Carlson0d3031d2007-10-10 18:02:43 -070015634err_out_apeunmap:
15635 if (tp->aperegs) {
15636 iounmap(tp->aperegs);
15637 tp->aperegs = NULL;
15638 }
15639
Linus Torvalds1da177e2005-04-16 15:20:36 -070015640err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015641 if (tp->regs) {
15642 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015643 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015645
15646err_out_free_dev:
15647 free_netdev(dev);
15648
Matt Carlson16821282011-07-13 09:27:28 +000015649err_out_power_down:
15650 pci_set_power_state(pdev, PCI_D3hot);
15651
Linus Torvalds1da177e2005-04-16 15:20:36 -070015652err_out_free_res:
15653 pci_release_regions(pdev);
15654
15655err_out_disable_pdev:
15656 pci_disable_device(pdev);
15657 pci_set_drvdata(pdev, NULL);
15658 return err;
15659}
15660
15661static void __devexit tg3_remove_one(struct pci_dev *pdev)
15662{
15663 struct net_device *dev = pci_get_drvdata(pdev);
15664
15665 if (dev) {
15666 struct tg3 *tp = netdev_priv(dev);
15667
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015668 if (tp->fw)
15669 release_firmware(tp->fw);
15670
Tejun Heo23f333a2010-12-12 16:45:14 +010015671 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015672
Joe Perches63c3a662011-04-26 08:12:10 +000015673 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015674 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015675 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015676 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015677
Linus Torvalds1da177e2005-04-16 15:20:36 -070015678 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015679 if (tp->aperegs) {
15680 iounmap(tp->aperegs);
15681 tp->aperegs = NULL;
15682 }
Michael Chan68929142005-08-09 20:17:14 -070015683 if (tp->regs) {
15684 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015685 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015687 free_netdev(dev);
15688 pci_release_regions(pdev);
15689 pci_disable_device(pdev);
15690 pci_set_drvdata(pdev, NULL);
15691 }
15692}
15693
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015694#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015695static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015696{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015697 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015698 struct net_device *dev = pci_get_drvdata(pdev);
15699 struct tg3 *tp = netdev_priv(dev);
15700 int err;
15701
15702 if (!netif_running(dev))
15703 return 0;
15704
Tejun Heo23f333a2010-12-12 16:45:14 +010015705 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015706 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015707 tg3_netif_stop(tp);
15708
15709 del_timer_sync(&tp->timer);
15710
David S. Millerf47c11e2005-06-24 20:18:35 -070015711 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015712 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015713 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015714
15715 netif_device_detach(dev);
15716
David S. Millerf47c11e2005-06-24 20:18:35 -070015717 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015718 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015719 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015720 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015721
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015722 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015723 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015724 int err2;
15725
David S. Millerf47c11e2005-06-24 20:18:35 -070015726 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015727
Joe Perches63c3a662011-04-26 08:12:10 +000015728 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015729 err2 = tg3_restart_hw(tp, 1);
15730 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015731 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015732
15733 tp->timer.expires = jiffies + tp->timer_offset;
15734 add_timer(&tp->timer);
15735
15736 netif_device_attach(dev);
15737 tg3_netif_start(tp);
15738
Michael Chanb9ec6c12006-07-25 16:37:27 -070015739out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015740 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015741
15742 if (!err2)
15743 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015744 }
15745
15746 return err;
15747}
15748
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015749static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015750{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015751 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015752 struct net_device *dev = pci_get_drvdata(pdev);
15753 struct tg3 *tp = netdev_priv(dev);
15754 int err;
15755
15756 if (!netif_running(dev))
15757 return 0;
15758
Linus Torvalds1da177e2005-04-16 15:20:36 -070015759 netif_device_attach(dev);
15760
David S. Millerf47c11e2005-06-24 20:18:35 -070015761 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015762
Joe Perches63c3a662011-04-26 08:12:10 +000015763 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015764 err = tg3_restart_hw(tp, 1);
15765 if (err)
15766 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015767
15768 tp->timer.expires = jiffies + tp->timer_offset;
15769 add_timer(&tp->timer);
15770
Linus Torvalds1da177e2005-04-16 15:20:36 -070015771 tg3_netif_start(tp);
15772
Michael Chanb9ec6c12006-07-25 16:37:27 -070015773out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015774 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015775
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015776 if (!err)
15777 tg3_phy_start(tp);
15778
Michael Chanb9ec6c12006-07-25 16:37:27 -070015779 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015780}
15781
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015782static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015783#define TG3_PM_OPS (&tg3_pm_ops)
15784
15785#else
15786
15787#define TG3_PM_OPS NULL
15788
15789#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015790
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015791/**
15792 * tg3_io_error_detected - called when PCI error is detected
15793 * @pdev: Pointer to PCI device
15794 * @state: The current pci connection state
15795 *
15796 * This function is called after a PCI bus error affecting
15797 * this device has been detected.
15798 */
15799static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15800 pci_channel_state_t state)
15801{
15802 struct net_device *netdev = pci_get_drvdata(pdev);
15803 struct tg3 *tp = netdev_priv(netdev);
15804 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15805
15806 netdev_info(netdev, "PCI I/O error detected\n");
15807
15808 rtnl_lock();
15809
15810 if (!netif_running(netdev))
15811 goto done;
15812
15813 tg3_phy_stop(tp);
15814
15815 tg3_netif_stop(tp);
15816
15817 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015818 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015819
15820 /* Want to make sure that the reset task doesn't run */
15821 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015822 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15823 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015824
15825 netif_device_detach(netdev);
15826
15827 /* Clean up software state, even if MMIO is blocked */
15828 tg3_full_lock(tp, 0);
15829 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15830 tg3_full_unlock(tp);
15831
15832done:
15833 if (state == pci_channel_io_perm_failure)
15834 err = PCI_ERS_RESULT_DISCONNECT;
15835 else
15836 pci_disable_device(pdev);
15837
15838 rtnl_unlock();
15839
15840 return err;
15841}
15842
15843/**
15844 * tg3_io_slot_reset - called after the pci bus has been reset.
15845 * @pdev: Pointer to PCI device
15846 *
15847 * Restart the card from scratch, as if from a cold-boot.
15848 * At this point, the card has exprienced a hard reset,
15849 * followed by fixups by BIOS, and has its config space
15850 * set up identically to what it was at cold boot.
15851 */
15852static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15853{
15854 struct net_device *netdev = pci_get_drvdata(pdev);
15855 struct tg3 *tp = netdev_priv(netdev);
15856 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15857 int err;
15858
15859 rtnl_lock();
15860
15861 if (pci_enable_device(pdev)) {
15862 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15863 goto done;
15864 }
15865
15866 pci_set_master(pdev);
15867 pci_restore_state(pdev);
15868 pci_save_state(pdev);
15869
15870 if (!netif_running(netdev)) {
15871 rc = PCI_ERS_RESULT_RECOVERED;
15872 goto done;
15873 }
15874
15875 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000015876 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015877 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015878
15879 rc = PCI_ERS_RESULT_RECOVERED;
15880
15881done:
15882 rtnl_unlock();
15883
15884 return rc;
15885}
15886
15887/**
15888 * tg3_io_resume - called when traffic can start flowing again.
15889 * @pdev: Pointer to PCI device
15890 *
15891 * This callback is called when the error recovery driver tells
15892 * us that its OK to resume normal operation.
15893 */
15894static void tg3_io_resume(struct pci_dev *pdev)
15895{
15896 struct net_device *netdev = pci_get_drvdata(pdev);
15897 struct tg3 *tp = netdev_priv(netdev);
15898 int err;
15899
15900 rtnl_lock();
15901
15902 if (!netif_running(netdev))
15903 goto done;
15904
15905 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015906 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015907 err = tg3_restart_hw(tp, 1);
15908 tg3_full_unlock(tp);
15909 if (err) {
15910 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15911 goto done;
15912 }
15913
15914 netif_device_attach(netdev);
15915
15916 tp->timer.expires = jiffies + tp->timer_offset;
15917 add_timer(&tp->timer);
15918
15919 tg3_netif_start(tp);
15920
15921 tg3_phy_start(tp);
15922
15923done:
15924 rtnl_unlock();
15925}
15926
15927static struct pci_error_handlers tg3_err_handler = {
15928 .error_detected = tg3_io_error_detected,
15929 .slot_reset = tg3_io_slot_reset,
15930 .resume = tg3_io_resume
15931};
15932
Linus Torvalds1da177e2005-04-16 15:20:36 -070015933static struct pci_driver tg3_driver = {
15934 .name = DRV_MODULE_NAME,
15935 .id_table = tg3_pci_tbl,
15936 .probe = tg3_init_one,
15937 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015938 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015939 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015940};
15941
15942static int __init tg3_init(void)
15943{
Jeff Garzik29917622006-08-19 17:48:59 -040015944 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015945}
15946
15947static void __exit tg3_cleanup(void)
15948{
15949 pci_unregister_driver(&tg3_driver);
15950}
15951
15952module_init(tg3_init);
15953module_exit(tg3_cleanup);