blob: d1a71c8892a88bc626edfae44c564840059212d3 [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
Matt Carlson8d5a89b2011-08-31 11:44:51 +00001399/* tp->lock is held. */
1400static void tg3_stop_fw(struct tg3 *tp)
1401{
1402 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
1403 /* Wait for RX cpu to ACK the previous event. */
1404 tg3_wait_for_event_ack(tp);
1405
1406 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
1407
1408 tg3_generate_fw_event(tp);
1409
1410 /* Wait for RX cpu to ACK this event. */
1411 tg3_wait_for_event_ack(tp);
1412 }
1413}
1414
Matt Carlson95e28692008-05-25 23:44:14 -07001415static void tg3_link_report(struct tg3 *tp)
1416{
1417 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001418 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001419 tg3_ump_link_report(tp);
1420 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001421 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1422 (tp->link_config.active_speed == SPEED_1000 ?
1423 1000 :
1424 (tp->link_config.active_speed == SPEED_100 ?
1425 100 : 10)),
1426 (tp->link_config.active_duplex == DUPLEX_FULL ?
1427 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001428
Joe Perches05dbe002010-02-17 19:44:19 +00001429 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1430 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1431 "on" : "off",
1432 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1433 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001434
1435 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1436 netdev_info(tp->dev, "EEE is %s\n",
1437 tp->setlpicnt ? "enabled" : "disabled");
1438
Matt Carlson95e28692008-05-25 23:44:14 -07001439 tg3_ump_link_report(tp);
1440 }
1441}
1442
1443static u16 tg3_advert_flowctrl_1000T(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_PAUSE_CAP;
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_PAUSE_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_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1453 else
1454 miireg = 0;
1455
1456 return miireg;
1457}
1458
1459static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1460{
1461 u16 miireg;
1462
Steve Glendinninge18ce342008-12-16 02:00:00 -08001463 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001464 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001465 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001466 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001467 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001468 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1469 else
1470 miireg = 0;
1471
1472 return miireg;
1473}
1474
Matt Carlson95e28692008-05-25 23:44:14 -07001475static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1476{
1477 u8 cap = 0;
1478
1479 if (lcladv & ADVERTISE_1000XPAUSE) {
1480 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1481 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001482 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001483 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001484 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001485 } else {
1486 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001487 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001488 }
1489 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1490 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001491 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001492 }
1493
1494 return cap;
1495}
1496
Matt Carlsonf51f3562008-05-25 23:45:08 -07001497static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001498{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001499 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001500 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001501 u32 old_rx_mode = tp->rx_mode;
1502 u32 old_tx_mode = tp->tx_mode;
1503
Joe Perches63c3a662011-04-26 08:12:10 +00001504 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001505 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001506 else
1507 autoneg = tp->link_config.autoneg;
1508
Joe Perches63c3a662011-04-26 08:12:10 +00001509 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001510 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001511 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001512 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001513 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001514 } else
1515 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001516
Matt Carlsonf51f3562008-05-25 23:45:08 -07001517 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001518
Steve Glendinninge18ce342008-12-16 02:00:00 -08001519 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001520 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1521 else
1522 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1523
Matt Carlsonf51f3562008-05-25 23:45:08 -07001524 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001525 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001526
Steve Glendinninge18ce342008-12-16 02:00:00 -08001527 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001528 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1529 else
1530 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1531
Matt Carlsonf51f3562008-05-25 23:45:08 -07001532 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001533 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001534}
1535
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001536static void tg3_adjust_link(struct net_device *dev)
1537{
1538 u8 oldflowctrl, linkmesg = 0;
1539 u32 mac_mode, lcl_adv, rmt_adv;
1540 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001541 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001542
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001543 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001544
1545 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1546 MAC_MODE_HALF_DUPLEX);
1547
1548 oldflowctrl = tp->link_config.active_flowctrl;
1549
1550 if (phydev->link) {
1551 lcl_adv = 0;
1552 rmt_adv = 0;
1553
1554 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1555 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001556 else if (phydev->speed == SPEED_1000 ||
1557 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001558 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001559 else
1560 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001561
1562 if (phydev->duplex == DUPLEX_HALF)
1563 mac_mode |= MAC_MODE_HALF_DUPLEX;
1564 else {
1565 lcl_adv = tg3_advert_flowctrl_1000T(
1566 tp->link_config.flowctrl);
1567
1568 if (phydev->pause)
1569 rmt_adv = LPA_PAUSE_CAP;
1570 if (phydev->asym_pause)
1571 rmt_adv |= LPA_PAUSE_ASYM;
1572 }
1573
1574 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1575 } else
1576 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1577
1578 if (mac_mode != tp->mac_mode) {
1579 tp->mac_mode = mac_mode;
1580 tw32_f(MAC_MODE, tp->mac_mode);
1581 udelay(40);
1582 }
1583
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001584 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1585 if (phydev->speed == SPEED_10)
1586 tw32(MAC_MI_STAT,
1587 MAC_MI_STAT_10MBPS_MODE |
1588 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1589 else
1590 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1591 }
1592
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001593 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1594 tw32(MAC_TX_LENGTHS,
1595 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1596 (6 << TX_LENGTHS_IPG_SHIFT) |
1597 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1598 else
1599 tw32(MAC_TX_LENGTHS,
1600 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1601 (6 << TX_LENGTHS_IPG_SHIFT) |
1602 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1603
1604 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1605 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1606 phydev->speed != tp->link_config.active_speed ||
1607 phydev->duplex != tp->link_config.active_duplex ||
1608 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001609 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001610
1611 tp->link_config.active_speed = phydev->speed;
1612 tp->link_config.active_duplex = phydev->duplex;
1613
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001614 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001615
1616 if (linkmesg)
1617 tg3_link_report(tp);
1618}
1619
1620static int tg3_phy_init(struct tg3 *tp)
1621{
1622 struct phy_device *phydev;
1623
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001624 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001625 return 0;
1626
1627 /* Bring the PHY back to a known state. */
1628 tg3_bmcr_reset(tp);
1629
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001630 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001631
1632 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001633 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001634 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001635 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001636 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001637 return PTR_ERR(phydev);
1638 }
1639
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001640 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001641 switch (phydev->interface) {
1642 case PHY_INTERFACE_MODE_GMII:
1643 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001644 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001645 phydev->supported &= (PHY_GBIT_FEATURES |
1646 SUPPORTED_Pause |
1647 SUPPORTED_Asym_Pause);
1648 break;
1649 }
1650 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001651 case PHY_INTERFACE_MODE_MII:
1652 phydev->supported &= (PHY_BASIC_FEATURES |
1653 SUPPORTED_Pause |
1654 SUPPORTED_Asym_Pause);
1655 break;
1656 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001657 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001658 return -EINVAL;
1659 }
1660
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001661 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001662
1663 phydev->advertising = phydev->supported;
1664
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001665 return 0;
1666}
1667
1668static void tg3_phy_start(struct tg3 *tp)
1669{
1670 struct phy_device *phydev;
1671
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001672 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001673 return;
1674
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001675 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001676
Matt Carlson80096062010-08-02 11:26:06 +00001677 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1678 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001679 phydev->speed = tp->link_config.orig_speed;
1680 phydev->duplex = tp->link_config.orig_duplex;
1681 phydev->autoneg = tp->link_config.orig_autoneg;
1682 phydev->advertising = tp->link_config.orig_advertising;
1683 }
1684
1685 phy_start(phydev);
1686
1687 phy_start_aneg(phydev);
1688}
1689
1690static void tg3_phy_stop(struct tg3 *tp)
1691{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001692 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001693 return;
1694
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001695 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001696}
1697
1698static void tg3_phy_fini(struct tg3 *tp)
1699{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001700 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001701 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001702 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001703 }
1704}
1705
Matt Carlson941ec902011-08-19 13:58:23 +00001706static int tg3_phy_set_extloopbk(struct tg3 *tp)
1707{
1708 int err;
1709 u32 val;
1710
1711 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
1712 return 0;
1713
1714 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
1715 /* Cannot do read-modify-write on 5401 */
1716 err = tg3_phy_auxctl_write(tp,
1717 MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
1718 MII_TG3_AUXCTL_ACTL_EXTLOOPBK |
1719 0x4c20);
1720 goto done;
1721 }
1722
1723 err = tg3_phy_auxctl_read(tp,
1724 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
1725 if (err)
1726 return err;
1727
1728 val |= MII_TG3_AUXCTL_ACTL_EXTLOOPBK;
1729 err = tg3_phy_auxctl_write(tp,
1730 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, val);
1731
1732done:
1733 return err;
1734}
1735
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001736static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1737{
1738 u32 phytest;
1739
1740 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1741 u32 phy;
1742
1743 tg3_writephy(tp, MII_TG3_FET_TEST,
1744 phytest | MII_TG3_FET_SHADOW_EN);
1745 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1746 if (enable)
1747 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1748 else
1749 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1750 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1751 }
1752 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1753 }
1754}
1755
Matt Carlson6833c042008-11-21 17:18:59 -08001756static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1757{
1758 u32 reg;
1759
Joe Perches63c3a662011-04-26 08:12:10 +00001760 if (!tg3_flag(tp, 5705_PLUS) ||
1761 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001762 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001763 return;
1764
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001765 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001766 tg3_phy_fet_toggle_apd(tp, enable);
1767 return;
1768 }
1769
Matt Carlson6833c042008-11-21 17:18:59 -08001770 reg = MII_TG3_MISC_SHDW_WREN |
1771 MII_TG3_MISC_SHDW_SCR5_SEL |
1772 MII_TG3_MISC_SHDW_SCR5_LPED |
1773 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1774 MII_TG3_MISC_SHDW_SCR5_SDTL |
1775 MII_TG3_MISC_SHDW_SCR5_C125OE;
1776 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1777 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1778
1779 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1780
1781
1782 reg = MII_TG3_MISC_SHDW_WREN |
1783 MII_TG3_MISC_SHDW_APD_SEL |
1784 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1785 if (enable)
1786 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1787
1788 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1789}
1790
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001791static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1792{
1793 u32 phy;
1794
Joe Perches63c3a662011-04-26 08:12:10 +00001795 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001796 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001797 return;
1798
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001799 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001800 u32 ephy;
1801
Matt Carlson535ef6e2009-08-25 10:09:36 +00001802 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1803 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1804
1805 tg3_writephy(tp, MII_TG3_FET_TEST,
1806 ephy | MII_TG3_FET_SHADOW_EN);
1807 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001808 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001809 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001810 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001811 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1812 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001813 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001814 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001815 }
1816 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00001817 int ret;
1818
1819 ret = tg3_phy_auxctl_read(tp,
1820 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
1821 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001822 if (enable)
1823 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1824 else
1825 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001826 tg3_phy_auxctl_write(tp,
1827 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001828 }
1829 }
1830}
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832static void tg3_phy_set_wirespeed(struct tg3 *tp)
1833{
Matt Carlson15ee95c2011-04-20 07:57:40 +00001834 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 u32 val;
1836
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001837 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return;
1839
Matt Carlson15ee95c2011-04-20 07:57:40 +00001840 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
1841 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001842 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
1843 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001846static void tg3_phy_apply_otp(struct tg3 *tp)
1847{
1848 u32 otp, phy;
1849
1850 if (!tp->phy_otp)
1851 return;
1852
1853 otp = tp->phy_otp;
1854
Matt Carlson1d36ba42011-04-20 07:57:42 +00001855 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
1856 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001857
1858 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1859 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1860 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1861
1862 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1863 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1864 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1865
1866 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1867 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1868 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1869
1870 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1871 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1872
1873 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1874 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1875
1876 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1877 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1878 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1879
Matt Carlson1d36ba42011-04-20 07:57:42 +00001880 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001881}
1882
Matt Carlson52b02d02010-10-14 10:37:41 +00001883static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1884{
1885 u32 val;
1886
1887 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1888 return;
1889
1890 tp->setlpicnt = 0;
1891
1892 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1893 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001894 tp->link_config.active_duplex == DUPLEX_FULL &&
1895 (tp->link_config.active_speed == SPEED_100 ||
1896 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001897 u32 eeectl;
1898
1899 if (tp->link_config.active_speed == SPEED_1000)
1900 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1901 else
1902 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1903
1904 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1905
Matt Carlson3110f5f52010-12-06 08:28:50 +00001906 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1907 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001908
Matt Carlsonb0c59432011-05-19 12:12:48 +00001909 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
1910 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00001911 tp->setlpicnt = 2;
1912 }
1913
1914 if (!tp->setlpicnt) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00001915 if (current_link_up == 1 &&
1916 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
1917 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
1918 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1919 }
1920
Matt Carlson52b02d02010-10-14 10:37:41 +00001921 val = tr32(TG3_CPMU_EEE_MODE);
1922 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1923 }
1924}
1925
Matt Carlsonb0c59432011-05-19 12:12:48 +00001926static void tg3_phy_eee_enable(struct tg3 *tp)
1927{
1928 u32 val;
1929
1930 if (tp->link_config.active_speed == SPEED_1000 &&
1931 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1932 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
1933 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
1934 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlsonb715ce92011-07-20 10:20:52 +00001935 val = MII_TG3_DSP_TAP26_ALNOKO |
1936 MII_TG3_DSP_TAP26_RMRXSTO;
1937 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
Matt Carlsonb0c59432011-05-19 12:12:48 +00001938 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1939 }
1940
1941 val = tr32(TG3_CPMU_EEE_MODE);
1942 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
1943}
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945static int tg3_wait_macro_done(struct tg3 *tp)
1946{
1947 int limit = 100;
1948
1949 while (limit--) {
1950 u32 tmp32;
1951
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001952 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if ((tmp32 & 0x1000) == 0)
1954 break;
1955 }
1956 }
Roel Kluind4675b52009-02-12 16:33:27 -08001957 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 return -EBUSY;
1959
1960 return 0;
1961}
1962
1963static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1964{
1965 static const u32 test_pat[4][6] = {
1966 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1967 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1968 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1969 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1970 };
1971 int chan;
1972
1973 for (chan = 0; chan < 4; chan++) {
1974 int i;
1975
1976 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1977 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001978 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 for (i = 0; i < 6; i++)
1981 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1982 test_pat[chan][i]);
1983
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001984 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (tg3_wait_macro_done(tp)) {
1986 *resetp = 1;
1987 return -EBUSY;
1988 }
1989
1990 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1991 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001992 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 if (tg3_wait_macro_done(tp)) {
1994 *resetp = 1;
1995 return -EBUSY;
1996 }
1997
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001998 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (tg3_wait_macro_done(tp)) {
2000 *resetp = 1;
2001 return -EBUSY;
2002 }
2003
2004 for (i = 0; i < 6; i += 2) {
2005 u32 low, high;
2006
2007 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
2008 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
2009 tg3_wait_macro_done(tp)) {
2010 *resetp = 1;
2011 return -EBUSY;
2012 }
2013 low &= 0x7fff;
2014 high &= 0x000f;
2015 if (low != test_pat[chan][i] ||
2016 high != test_pat[chan][i+1]) {
2017 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
2018 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
2019 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
2020
2021 return -EBUSY;
2022 }
2023 }
2024 }
2025
2026 return 0;
2027}
2028
2029static int tg3_phy_reset_chanpat(struct tg3 *tp)
2030{
2031 int chan;
2032
2033 for (chan = 0; chan < 4; chan++) {
2034 int i;
2035
2036 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
2037 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002038 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 for (i = 0; i < 6; i++)
2040 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002041 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (tg3_wait_macro_done(tp))
2043 return -EBUSY;
2044 }
2045
2046 return 0;
2047}
2048
2049static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
2050{
2051 u32 reg32, phy9_orig;
2052 int retries, do_phy_reset, err;
2053
2054 retries = 10;
2055 do_phy_reset = 1;
2056 do {
2057 if (do_phy_reset) {
2058 err = tg3_bmcr_reset(tp);
2059 if (err)
2060 return err;
2061 do_phy_reset = 0;
2062 }
2063
2064 /* Disable transmitter and interrupt. */
2065 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
2066 continue;
2067
2068 reg32 |= 0x3000;
2069 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2070
2071 /* Set full-duplex, 1000 mbps. */
2072 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00002073 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00002076 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 continue;
2078
Matt Carlson221c5632011-06-13 13:39:01 +00002079 tg3_writephy(tp, MII_CTRL1000,
2080 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Matt Carlson1d36ba42011-04-20 07:57:42 +00002082 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
2083 if (err)
2084 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002087 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2090 if (!err)
2091 break;
2092 } while (--retries);
2093
2094 err = tg3_phy_reset_chanpat(tp);
2095 if (err)
2096 return err;
2097
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002098 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
2100 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002101 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Matt Carlson1d36ba42011-04-20 07:57:42 +00002103 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Matt Carlson221c5632011-06-13 13:39:01 +00002105 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2108 reg32 &= ~0x3000;
2109 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2110 } else if (!err)
2111 err = -EBUSY;
2112
2113 return err;
2114}
2115
2116/* This will reset the tigon3 PHY if there is no valid
2117 * link unless the FORCE argument is non-zero.
2118 */
2119static int tg3_phy_reset(struct tg3 *tp)
2120{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002121 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 int err;
2123
Michael Chan60189dd2006-12-17 17:08:07 -08002124 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002125 val = tr32(GRC_MISC_CFG);
2126 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2127 udelay(40);
2128 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002129 err = tg3_readphy(tp, MII_BMSR, &val);
2130 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (err != 0)
2132 return -EBUSY;
2133
Michael Chanc8e1e822006-04-29 18:55:17 -07002134 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2135 netif_carrier_off(tp->dev);
2136 tg3_link_report(tp);
2137 }
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2140 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2141 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2142 err = tg3_phy_reset_5703_4_5(tp);
2143 if (err)
2144 return err;
2145 goto out;
2146 }
2147
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002148 cpmuctrl = 0;
2149 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2150 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2151 cpmuctrl = tr32(TG3_CPMU_CTRL);
2152 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2153 tw32(TG3_CPMU_CTRL,
2154 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2155 }
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 err = tg3_bmcr_reset(tp);
2158 if (err)
2159 return err;
2160
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002161 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002162 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2163 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002164
2165 tw32(TG3_CPMU_CTRL, cpmuctrl);
2166 }
2167
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002168 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2169 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002170 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2171 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2172 CPMU_LSPD_1000MB_MACCLK_12_5) {
2173 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2174 udelay(40);
2175 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2176 }
2177 }
2178
Joe Perches63c3a662011-04-26 08:12:10 +00002179 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002180 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002181 return 0;
2182
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002183 tg3_phy_apply_otp(tp);
2184
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002185 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002186 tg3_phy_toggle_apd(tp, true);
2187 else
2188 tg3_phy_toggle_apd(tp, false);
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002191 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2192 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002193 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2194 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002195 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002197
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002198 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002199 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2200 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002202
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002203 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002204 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2205 tg3_phydsp_write(tp, 0x000a, 0x310b);
2206 tg3_phydsp_write(tp, 0x201f, 0x9506);
2207 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2208 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2209 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002210 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002211 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2212 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2213 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2214 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2215 tg3_writephy(tp, MII_TG3_TEST1,
2216 MII_TG3_TEST1_TRIM_EN | 0x4);
2217 } else
2218 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2219
2220 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2221 }
Michael Chanc424cb22006-04-29 18:56:34 -07002222 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 /* Set Extended packet length bit (bit 14) on all chips that */
2225 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002226 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002228 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002229 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002231 err = tg3_phy_auxctl_read(tp,
2232 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2233 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002234 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2235 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237
2238 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2239 * jumbo frames transmission.
2240 */
Joe Perches63c3a662011-04-26 08:12:10 +00002241 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002242 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002243 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002244 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
2246
Michael Chan715116a2006-09-27 16:09:25 -07002247 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002248 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002249 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002250 }
2251
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002252 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 tg3_phy_set_wirespeed(tp);
2254 return 0;
2255}
2256
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002257#define TG3_GPIO_MSG_DRVR_PRES 0x00000001
2258#define TG3_GPIO_MSG_NEED_VAUX 0x00000002
2259#define TG3_GPIO_MSG_MASK (TG3_GPIO_MSG_DRVR_PRES | \
2260 TG3_GPIO_MSG_NEED_VAUX)
2261#define TG3_GPIO_MSG_ALL_DRVR_PRES_MASK \
2262 ((TG3_GPIO_MSG_DRVR_PRES << 0) | \
2263 (TG3_GPIO_MSG_DRVR_PRES << 4) | \
2264 (TG3_GPIO_MSG_DRVR_PRES << 8) | \
2265 (TG3_GPIO_MSG_DRVR_PRES << 12))
2266
2267#define TG3_GPIO_MSG_ALL_NEED_VAUX_MASK \
2268 ((TG3_GPIO_MSG_NEED_VAUX << 0) | \
2269 (TG3_GPIO_MSG_NEED_VAUX << 4) | \
2270 (TG3_GPIO_MSG_NEED_VAUX << 8) | \
2271 (TG3_GPIO_MSG_NEED_VAUX << 12))
2272
2273static inline u32 tg3_set_function_status(struct tg3 *tp, u32 newstat)
2274{
2275 u32 status, shift;
2276
2277 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2278 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2279 status = tg3_ape_read32(tp, TG3_APE_GPIO_MSG);
2280 else
2281 status = tr32(TG3_CPMU_DRV_STATUS);
2282
2283 shift = TG3_APE_GPIO_MSG_SHIFT + 4 * tp->pci_fn;
2284 status &= ~(TG3_GPIO_MSG_MASK << shift);
2285 status |= (newstat << shift);
2286
2287 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2288 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
2289 tg3_ape_write32(tp, TG3_APE_GPIO_MSG, status);
2290 else
2291 tw32(TG3_CPMU_DRV_STATUS, status);
2292
2293 return status >> TG3_APE_GPIO_MSG_SHIFT;
2294}
2295
Matt Carlson520b2752011-06-13 13:39:02 +00002296static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2297{
2298 if (!tg3_flag(tp, IS_NIC))
2299 return 0;
2300
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002301 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2302 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2303 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
2304 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2305 return -EIO;
Matt Carlson520b2752011-06-13 13:39:02 +00002306
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002307 tg3_set_function_status(tp, TG3_GPIO_MSG_DRVR_PRES);
2308
2309 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2310 TG3_GRC_LCLCTL_PWRSW_DELAY);
2311
2312 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
2313 } else {
2314 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2315 TG3_GRC_LCLCTL_PWRSW_DELAY);
2316 }
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002317
Matt Carlson520b2752011-06-13 13:39:02 +00002318 return 0;
2319}
2320
2321static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2322{
2323 u32 grc_local_ctrl;
2324
2325 if (!tg3_flag(tp, IS_NIC) ||
2326 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2327 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2328 return;
2329
2330 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2331
2332 tw32_wait_f(GRC_LOCAL_CTRL,
2333 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2334 TG3_GRC_LCLCTL_PWRSW_DELAY);
2335
2336 tw32_wait_f(GRC_LOCAL_CTRL,
2337 grc_local_ctrl,
2338 TG3_GRC_LCLCTL_PWRSW_DELAY);
2339
2340 tw32_wait_f(GRC_LOCAL_CTRL,
2341 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2342 TG3_GRC_LCLCTL_PWRSW_DELAY);
2343}
2344
2345static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2346{
2347 if (!tg3_flag(tp, IS_NIC))
2348 return;
2349
2350 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2351 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2352 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2353 (GRC_LCLCTRL_GPIO_OE0 |
2354 GRC_LCLCTRL_GPIO_OE1 |
2355 GRC_LCLCTRL_GPIO_OE2 |
2356 GRC_LCLCTRL_GPIO_OUTPUT0 |
2357 GRC_LCLCTRL_GPIO_OUTPUT1),
2358 TG3_GRC_LCLCTL_PWRSW_DELAY);
2359 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2360 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2361 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2362 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2363 GRC_LCLCTRL_GPIO_OE1 |
2364 GRC_LCLCTRL_GPIO_OE2 |
2365 GRC_LCLCTRL_GPIO_OUTPUT0 |
2366 GRC_LCLCTRL_GPIO_OUTPUT1 |
2367 tp->grc_local_ctrl;
2368 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2369 TG3_GRC_LCLCTL_PWRSW_DELAY);
2370
2371 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2372 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2373 TG3_GRC_LCLCTL_PWRSW_DELAY);
2374
2375 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2376 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2377 TG3_GRC_LCLCTL_PWRSW_DELAY);
2378 } else {
2379 u32 no_gpio2;
2380 u32 grc_local_ctrl = 0;
2381
2382 /* Workaround to prevent overdrawing Amps. */
2383 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2384 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2385 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2386 grc_local_ctrl,
2387 TG3_GRC_LCLCTL_PWRSW_DELAY);
2388 }
2389
2390 /* On 5753 and variants, GPIO2 cannot be used. */
2391 no_gpio2 = tp->nic_sram_data_cfg &
2392 NIC_SRAM_DATA_CFG_NO_GPIO2;
2393
2394 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2395 GRC_LCLCTRL_GPIO_OE1 |
2396 GRC_LCLCTRL_GPIO_OE2 |
2397 GRC_LCLCTRL_GPIO_OUTPUT1 |
2398 GRC_LCLCTRL_GPIO_OUTPUT2;
2399 if (no_gpio2) {
2400 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2401 GRC_LCLCTRL_GPIO_OUTPUT2);
2402 }
2403 tw32_wait_f(GRC_LOCAL_CTRL,
2404 tp->grc_local_ctrl | grc_local_ctrl,
2405 TG3_GRC_LCLCTL_PWRSW_DELAY);
2406
2407 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2408
2409 tw32_wait_f(GRC_LOCAL_CTRL,
2410 tp->grc_local_ctrl | grc_local_ctrl,
2411 TG3_GRC_LCLCTL_PWRSW_DELAY);
2412
2413 if (!no_gpio2) {
2414 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2415 tw32_wait_f(GRC_LOCAL_CTRL,
2416 tp->grc_local_ctrl | grc_local_ctrl,
2417 TG3_GRC_LCLCTL_PWRSW_DELAY);
2418 }
2419 }
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002420}
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002421
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002422static void tg3_frob_aux_power_5717(struct tg3 *tp, bool wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002423{
2424 u32 msg = 0;
2425
2426 /* Serialize power state transitions */
2427 if (tg3_ape_lock(tp, TG3_APE_LOCK_GPIO))
2428 return;
2429
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002430 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || wol_enable)
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002431 msg = TG3_GPIO_MSG_NEED_VAUX;
2432
2433 msg = tg3_set_function_status(tp, msg);
2434
2435 if (msg & TG3_GPIO_MSG_ALL_DRVR_PRES_MASK)
2436 goto done;
2437
2438 if (msg & TG3_GPIO_MSG_ALL_NEED_VAUX_MASK)
2439 tg3_pwrsrc_switch_to_vaux(tp);
2440 else
2441 tg3_pwrsrc_die_with_vmain(tp);
2442
2443done:
Matt Carlson6f5c8f832011-07-13 09:27:31 +00002444 tg3_ape_unlock(tp, TG3_APE_LOCK_GPIO);
Matt Carlson520b2752011-06-13 13:39:02 +00002445}
2446
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002447static void tg3_frob_aux_power(struct tg3 *tp, bool include_wol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
Matt Carlson683644b2011-03-09 16:58:23 +00002449 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Matt Carlson334355a2010-01-20 16:58:10 +00002451 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002452 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlson334355a2010-01-20 16:58:10 +00002453 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return;
2455
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002456 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2457 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
2458 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002459 tg3_frob_aux_power_5717(tp, include_wol ?
2460 tg3_flag(tp, WOL_ENABLE) != 0 : 0);
Matt Carlson3a1e19d2011-07-13 09:27:32 +00002461 return;
2462 }
2463
2464 if (tp->pdev_peer && tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002465 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002467 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002468
Michael Chanbc1c7562006-03-20 17:48:03 -08002469 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002470 if (dev_peer) {
2471 struct tg3 *tp_peer = netdev_priv(dev_peer);
2472
Joe Perches63c3a662011-04-26 08:12:10 +00002473 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002474 return;
2475
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002476 if ((include_wol && tg3_flag(tp_peer, WOL_ENABLE)) ||
Joe Perches63c3a662011-04-26 08:12:10 +00002477 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002478 need_vaux = true;
2479 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Matt Carlsoncd0d7222011-07-13 09:27:33 +00002482 if ((include_wol && tg3_flag(tp, WOL_ENABLE)) ||
2483 tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002484 need_vaux = true;
2485
Matt Carlson520b2752011-06-13 13:39:02 +00002486 if (need_vaux)
2487 tg3_pwrsrc_switch_to_vaux(tp);
2488 else
2489 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
2491
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002492static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2493{
2494 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2495 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002496 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002497 if (speed != SPEED_10)
2498 return 1;
2499 } else if (speed == SPEED_10)
2500 return 1;
2501
2502 return 0;
2503}
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505static int tg3_setup_phy(struct tg3 *, int);
2506
2507#define RESET_KIND_SHUTDOWN 0
2508#define RESET_KIND_INIT 1
2509#define RESET_KIND_SUSPEND 2
2510
2511static void tg3_write_sig_post_reset(struct tg3 *, int);
2512static int tg3_halt_cpu(struct tg3 *, u32);
2513
Matt Carlson0a459aa2008-11-03 16:54:15 -08002514static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002515{
Matt Carlsonce057f02007-11-12 21:08:03 -08002516 u32 val;
2517
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002518 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002519 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2520 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2521 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2522
2523 sg_dig_ctrl |=
2524 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2525 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2526 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2527 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002528 return;
Michael Chan51297242007-02-13 12:17:57 -08002529 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002530
Michael Chan60189dd2006-12-17 17:08:07 -08002531 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002532 tg3_bmcr_reset(tp);
2533 val = tr32(GRC_MISC_CFG);
2534 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2535 udelay(40);
2536 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002537 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002538 u32 phytest;
2539 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2540 u32 phy;
2541
2542 tg3_writephy(tp, MII_ADVERTISE, 0);
2543 tg3_writephy(tp, MII_BMCR,
2544 BMCR_ANENABLE | BMCR_ANRESTART);
2545
2546 tg3_writephy(tp, MII_TG3_FET_TEST,
2547 phytest | MII_TG3_FET_SHADOW_EN);
2548 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2549 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2550 tg3_writephy(tp,
2551 MII_TG3_FET_SHDW_AUXMODE4,
2552 phy);
2553 }
2554 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2555 }
2556 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002557 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002558 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2559 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002560
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002561 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2562 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2563 MII_TG3_AUXCTL_PCTL_VREG_11V;
2564 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002565 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002566
Michael Chan15c3b692006-03-22 01:06:52 -08002567 /* The PHY should not be powered down on some chips because
2568 * of bugs.
2569 */
2570 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2571 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2572 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002573 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002574 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002575
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002576 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2577 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002578 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2579 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2580 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2581 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2582 }
2583
Michael Chan15c3b692006-03-22 01:06:52 -08002584 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2585}
2586
Matt Carlson3f007892008-11-03 16:51:36 -08002587/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002588static int tg3_nvram_lock(struct tg3 *tp)
2589{
Joe Perches63c3a662011-04-26 08:12:10 +00002590 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002591 int i;
2592
2593 if (tp->nvram_lock_cnt == 0) {
2594 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2595 for (i = 0; i < 8000; i++) {
2596 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2597 break;
2598 udelay(20);
2599 }
2600 if (i == 8000) {
2601 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2602 return -ENODEV;
2603 }
2604 }
2605 tp->nvram_lock_cnt++;
2606 }
2607 return 0;
2608}
2609
2610/* tp->lock is held. */
2611static void tg3_nvram_unlock(struct tg3 *tp)
2612{
Joe Perches63c3a662011-04-26 08:12:10 +00002613 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002614 if (tp->nvram_lock_cnt > 0)
2615 tp->nvram_lock_cnt--;
2616 if (tp->nvram_lock_cnt == 0)
2617 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2618 }
2619}
2620
2621/* tp->lock is held. */
2622static void tg3_enable_nvram_access(struct tg3 *tp)
2623{
Joe Perches63c3a662011-04-26 08:12:10 +00002624 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002625 u32 nvaccess = tr32(NVRAM_ACCESS);
2626
2627 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2628 }
2629}
2630
2631/* tp->lock is held. */
2632static void tg3_disable_nvram_access(struct tg3 *tp)
2633{
Joe Perches63c3a662011-04-26 08:12:10 +00002634 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002635 u32 nvaccess = tr32(NVRAM_ACCESS);
2636
2637 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2638 }
2639}
2640
2641static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2642 u32 offset, u32 *val)
2643{
2644 u32 tmp;
2645 int i;
2646
2647 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2648 return -EINVAL;
2649
2650 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2651 EEPROM_ADDR_DEVID_MASK |
2652 EEPROM_ADDR_READ);
2653 tw32(GRC_EEPROM_ADDR,
2654 tmp |
2655 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2656 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2657 EEPROM_ADDR_ADDR_MASK) |
2658 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2659
2660 for (i = 0; i < 1000; i++) {
2661 tmp = tr32(GRC_EEPROM_ADDR);
2662
2663 if (tmp & EEPROM_ADDR_COMPLETE)
2664 break;
2665 msleep(1);
2666 }
2667 if (!(tmp & EEPROM_ADDR_COMPLETE))
2668 return -EBUSY;
2669
Matt Carlson62cedd12009-04-20 14:52:29 -07002670 tmp = tr32(GRC_EEPROM_DATA);
2671
2672 /*
2673 * The data will always be opposite the native endian
2674 * format. Perform a blind byteswap to compensate.
2675 */
2676 *val = swab32(tmp);
2677
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002678 return 0;
2679}
2680
2681#define NVRAM_CMD_TIMEOUT 10000
2682
2683static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2684{
2685 int i;
2686
2687 tw32(NVRAM_CMD, nvram_cmd);
2688 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2689 udelay(10);
2690 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2691 udelay(10);
2692 break;
2693 }
2694 }
2695
2696 if (i == NVRAM_CMD_TIMEOUT)
2697 return -EBUSY;
2698
2699 return 0;
2700}
2701
2702static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2703{
Joe Perches63c3a662011-04-26 08:12:10 +00002704 if (tg3_flag(tp, NVRAM) &&
2705 tg3_flag(tp, NVRAM_BUFFERED) &&
2706 tg3_flag(tp, FLASH) &&
2707 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002708 (tp->nvram_jedecnum == JEDEC_ATMEL))
2709
2710 addr = ((addr / tp->nvram_pagesize) <<
2711 ATMEL_AT45DB0X1B_PAGE_POS) +
2712 (addr % tp->nvram_pagesize);
2713
2714 return addr;
2715}
2716
2717static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2718{
Joe Perches63c3a662011-04-26 08:12:10 +00002719 if (tg3_flag(tp, NVRAM) &&
2720 tg3_flag(tp, NVRAM_BUFFERED) &&
2721 tg3_flag(tp, FLASH) &&
2722 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002723 (tp->nvram_jedecnum == JEDEC_ATMEL))
2724
2725 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2726 tp->nvram_pagesize) +
2727 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2728
2729 return addr;
2730}
2731
Matt Carlsone4f34112009-02-25 14:25:00 +00002732/* NOTE: Data read in from NVRAM is byteswapped according to
2733 * the byteswapping settings for all other register accesses.
2734 * tg3 devices are BE devices, so on a BE machine, the data
2735 * returned will be exactly as it is seen in NVRAM. On a LE
2736 * machine, the 32-bit value will be byteswapped.
2737 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002738static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2739{
2740 int ret;
2741
Joe Perches63c3a662011-04-26 08:12:10 +00002742 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002743 return tg3_nvram_read_using_eeprom(tp, offset, val);
2744
2745 offset = tg3_nvram_phys_addr(tp, offset);
2746
2747 if (offset > NVRAM_ADDR_MSK)
2748 return -EINVAL;
2749
2750 ret = tg3_nvram_lock(tp);
2751 if (ret)
2752 return ret;
2753
2754 tg3_enable_nvram_access(tp);
2755
2756 tw32(NVRAM_ADDR, offset);
2757 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2758 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2759
2760 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002761 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002762
2763 tg3_disable_nvram_access(tp);
2764
2765 tg3_nvram_unlock(tp);
2766
2767 return ret;
2768}
2769
Matt Carlsona9dc5292009-02-25 14:25:30 +00002770/* Ensures NVRAM data is in bytestream format. */
2771static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002772{
2773 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002774 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002775 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002776 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002777 return res;
2778}
2779
2780/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002781static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2782{
2783 u32 addr_high, addr_low;
2784 int i;
2785
2786 addr_high = ((tp->dev->dev_addr[0] << 8) |
2787 tp->dev->dev_addr[1]);
2788 addr_low = ((tp->dev->dev_addr[2] << 24) |
2789 (tp->dev->dev_addr[3] << 16) |
2790 (tp->dev->dev_addr[4] << 8) |
2791 (tp->dev->dev_addr[5] << 0));
2792 for (i = 0; i < 4; i++) {
2793 if (i == 1 && skip_mac_1)
2794 continue;
2795 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2796 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2797 }
2798
2799 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2800 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2801 for (i = 0; i < 12; i++) {
2802 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2803 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2804 }
2805 }
2806
2807 addr_high = (tp->dev->dev_addr[0] +
2808 tp->dev->dev_addr[1] +
2809 tp->dev->dev_addr[2] +
2810 tp->dev->dev_addr[3] +
2811 tp->dev->dev_addr[4] +
2812 tp->dev->dev_addr[5]) &
2813 TX_BACKOFF_SEED_MASK;
2814 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2815}
2816
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002817static void tg3_enable_register_access(struct tg3 *tp)
2818{
2819 /*
2820 * Make sure register accesses (indirect or otherwise) will function
2821 * correctly.
2822 */
2823 pci_write_config_dword(tp->pdev,
2824 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2825}
2826
2827static int tg3_power_up(struct tg3 *tp)
2828{
Matt Carlsonbed98292011-07-13 09:27:29 +00002829 int err;
2830
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002831 tg3_enable_register_access(tp);
2832
Matt Carlsonbed98292011-07-13 09:27:29 +00002833 err = pci_set_power_state(tp->pdev, PCI_D0);
2834 if (!err) {
2835 /* Switch out of Vaux if it is a NIC */
2836 tg3_pwrsrc_switch_to_vmain(tp);
2837 } else {
2838 netdev_err(tp->dev, "Transition to D0 failed\n");
2839 }
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002840
Matt Carlsonbed98292011-07-13 09:27:29 +00002841 return err;
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002842}
2843
2844static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
2846 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002847 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002849 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002850
2851 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00002852 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002853 u16 lnkctl;
2854
2855 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00002856 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002857 &lnkctl);
2858 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2859 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00002860 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002861 lnkctl);
2862 }
2863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2865 tw32(TG3PCI_MISC_HOST_CTRL,
2866 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2867
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002868 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00002869 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002870
Joe Perches63c3a662011-04-26 08:12:10 +00002871 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002872 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002873 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002874 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002875 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002876 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002877
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002878 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002879
Matt Carlson80096062010-08-02 11:26:06 +00002880 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002881
2882 tp->link_config.orig_speed = phydev->speed;
2883 tp->link_config.orig_duplex = phydev->duplex;
2884 tp->link_config.orig_autoneg = phydev->autoneg;
2885 tp->link_config.orig_advertising = phydev->advertising;
2886
2887 advertising = ADVERTISED_TP |
2888 ADVERTISED_Pause |
2889 ADVERTISED_Autoneg |
2890 ADVERTISED_10baseT_Half;
2891
Joe Perches63c3a662011-04-26 08:12:10 +00002892 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
2893 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002894 advertising |=
2895 ADVERTISED_100baseT_Half |
2896 ADVERTISED_100baseT_Full |
2897 ADVERTISED_10baseT_Full;
2898 else
2899 advertising |= ADVERTISED_10baseT_Full;
2900 }
2901
2902 phydev->advertising = advertising;
2903
2904 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002905
2906 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002907 if (phyid != PHY_ID_BCMAC131) {
2908 phyid &= PHY_BCM_OUI_MASK;
2909 if (phyid == PHY_BCM_OUI_1 ||
2910 phyid == PHY_BCM_OUI_2 ||
2911 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002912 do_low_power = true;
2913 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002914 }
Matt Carlsondd477002008-05-25 23:45:58 -07002915 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002916 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002917
Matt Carlson80096062010-08-02 11:26:06 +00002918 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2919 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002920 tp->link_config.orig_speed = tp->link_config.speed;
2921 tp->link_config.orig_duplex = tp->link_config.duplex;
2922 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002925 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002926 tp->link_config.speed = SPEED_10;
2927 tp->link_config.duplex = DUPLEX_HALF;
2928 tp->link_config.autoneg = AUTONEG_ENABLE;
2929 tg3_setup_phy(tp, 0);
2930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 }
2932
Michael Chanb5d37722006-09-27 16:06:21 -07002933 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2934 u32 val;
2935
2936 val = tr32(GRC_VCPU_EXT_CTRL);
2937 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00002938 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002939 int i;
2940 u32 val;
2941
2942 for (i = 0; i < 200; i++) {
2943 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2944 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2945 break;
2946 msleep(1);
2947 }
2948 }
Joe Perches63c3a662011-04-26 08:12:10 +00002949 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07002950 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2951 WOL_DRV_STATE_SHUTDOWN |
2952 WOL_DRV_WOL |
2953 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002954
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002955 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 u32 mac_mode;
2957
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002958 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002959 if (do_low_power &&
2960 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
2961 tg3_phy_auxctl_write(tp,
2962 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
2963 MII_TG3_AUXCTL_PCTL_WOL_EN |
2964 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2965 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07002966 udelay(40);
2967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002969 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002970 mac_mode = MAC_MODE_PORT_MODE_GMII;
2971 else
2972 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002974 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2975 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2976 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00002977 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002978 SPEED_100 : SPEED_10;
2979 if (tg3_5700_link_polarity(tp, speed))
2980 mac_mode |= MAC_MODE_LINK_POLARITY;
2981 else
2982 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 } else {
2985 mac_mode = MAC_MODE_PORT_MODE_TBI;
2986 }
2987
Joe Perches63c3a662011-04-26 08:12:10 +00002988 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 tw32(MAC_LED_CTRL, tp->led_ctrl);
2990
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002991 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00002992 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
2993 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002994 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Joe Perches63c3a662011-04-26 08:12:10 +00002996 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002997 mac_mode |= MAC_MODE_APE_TX_EN |
2998 MAC_MODE_APE_RX_EN |
2999 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07003000
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 tw32_f(MAC_MODE, mac_mode);
3002 udelay(100);
3003
3004 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
3005 udelay(10);
3006 }
3007
Joe Perches63c3a662011-04-26 08:12:10 +00003008 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3010 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
3011 u32 base_val;
3012
3013 base_val = tp->pci_clock_ctrl;
3014 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
3015 CLOCK_CTRL_TXCLK_DISABLE);
3016
Michael Chanb401e9e2005-12-19 16:27:04 -08003017 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
3018 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00003019 } else if (tg3_flag(tp, 5780_CLASS) ||
3020 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00003021 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07003022 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00003023 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 u32 newbits1, newbits2;
3025
3026 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3027 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3028 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
3029 CLOCK_CTRL_TXCLK_DISABLE |
3030 CLOCK_CTRL_ALTCLK);
3031 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00003032 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 newbits1 = CLOCK_CTRL_625_CORE;
3034 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
3035 } else {
3036 newbits1 = CLOCK_CTRL_ALTCLK;
3037 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
3038 }
3039
Michael Chanb401e9e2005-12-19 16:27:04 -08003040 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
3041 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Michael Chanb401e9e2005-12-19 16:27:04 -08003043 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
3044 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Joe Perches63c3a662011-04-26 08:12:10 +00003046 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 u32 newbits3;
3048
3049 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3050 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3051 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
3052 CLOCK_CTRL_TXCLK_DISABLE |
3053 CLOCK_CTRL_44MHZ_CORE);
3054 } else {
3055 newbits3 = CLOCK_CTRL_44MHZ_CORE;
3056 }
3057
Michael Chanb401e9e2005-12-19 16:27:04 -08003058 tw32_wait_f(TG3PCI_CLOCK_CTRL,
3059 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 }
3061 }
3062
Joe Perches63c3a662011-04-26 08:12:10 +00003063 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08003064 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08003065
Matt Carlsoncd0d7222011-07-13 09:27:33 +00003066 tg3_frob_aux_power(tp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068 /* Workaround for unstable PLL clock */
3069 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
3070 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
3071 u32 val = tr32(0x7d00);
3072
3073 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
3074 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00003075 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08003076 int err;
3077
3078 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08003080 if (!err)
3081 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08003082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
3084
Michael Chanbbadf502006-04-06 21:46:34 -07003085 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
3086
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 return 0;
3088}
3089
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003090static void tg3_power_down(struct tg3 *tp)
3091{
3092 tg3_power_down_prepare(tp);
3093
Joe Perches63c3a662011-04-26 08:12:10 +00003094 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00003095 pci_set_power_state(tp->pdev, PCI_D3hot);
3096}
3097
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
3099{
3100 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
3101 case MII_TG3_AUX_STAT_10HALF:
3102 *speed = SPEED_10;
3103 *duplex = DUPLEX_HALF;
3104 break;
3105
3106 case MII_TG3_AUX_STAT_10FULL:
3107 *speed = SPEED_10;
3108 *duplex = DUPLEX_FULL;
3109 break;
3110
3111 case MII_TG3_AUX_STAT_100HALF:
3112 *speed = SPEED_100;
3113 *duplex = DUPLEX_HALF;
3114 break;
3115
3116 case MII_TG3_AUX_STAT_100FULL:
3117 *speed = SPEED_100;
3118 *duplex = DUPLEX_FULL;
3119 break;
3120
3121 case MII_TG3_AUX_STAT_1000HALF:
3122 *speed = SPEED_1000;
3123 *duplex = DUPLEX_HALF;
3124 break;
3125
3126 case MII_TG3_AUX_STAT_1000FULL:
3127 *speed = SPEED_1000;
3128 *duplex = DUPLEX_FULL;
3129 break;
3130
3131 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003132 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07003133 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
3134 SPEED_10;
3135 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
3136 DUPLEX_HALF;
3137 break;
3138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 *speed = SPEED_INVALID;
3140 *duplex = DUPLEX_INVALID;
3141 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143}
3144
Matt Carlson42b64a42011-05-19 12:12:49 +00003145static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
Matt Carlson42b64a42011-05-19 12:12:49 +00003147 int err = 0;
3148 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Matt Carlson42b64a42011-05-19 12:12:49 +00003150 new_adv = ADVERTISE_CSMA;
3151 if (advertise & ADVERTISED_10baseT_Half)
3152 new_adv |= ADVERTISE_10HALF;
3153 if (advertise & ADVERTISED_10baseT_Full)
3154 new_adv |= ADVERTISE_10FULL;
3155 if (advertise & ADVERTISED_100baseT_Half)
3156 new_adv |= ADVERTISE_100HALF;
3157 if (advertise & ADVERTISED_100baseT_Full)
3158 new_adv |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Matt Carlson42b64a42011-05-19 12:12:49 +00003160 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Matt Carlson42b64a42011-05-19 12:12:49 +00003162 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
3163 if (err)
3164 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Matt Carlson42b64a42011-05-19 12:12:49 +00003166 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3167 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003168
Matt Carlson42b64a42011-05-19 12:12:49 +00003169 new_adv = 0;
3170 if (advertise & ADVERTISED_1000baseT_Half)
Matt Carlson221c5632011-06-13 13:39:01 +00003171 new_adv |= ADVERTISE_1000HALF;
Matt Carlson42b64a42011-05-19 12:12:49 +00003172 if (advertise & ADVERTISED_1000baseT_Full)
Matt Carlson221c5632011-06-13 13:39:01 +00003173 new_adv |= ADVERTISE_1000FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003174
Matt Carlson42b64a42011-05-19 12:12:49 +00003175 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3176 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
Matt Carlson221c5632011-06-13 13:39:01 +00003177 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Matt Carlson221c5632011-06-13 13:39:01 +00003179 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
Matt Carlson42b64a42011-05-19 12:12:49 +00003180 if (err)
3181 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003182
Matt Carlson42b64a42011-05-19 12:12:49 +00003183 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3184 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Matt Carlson42b64a42011-05-19 12:12:49 +00003186 tw32(TG3_CPMU_EEE_MODE,
3187 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003188
Matt Carlson42b64a42011-05-19 12:12:49 +00003189 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3190 if (!err) {
3191 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003192
Matt Carlsona6b68da2010-12-06 08:28:52 +00003193 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003194 /* Advertise 100-BaseTX EEE ability */
3195 if (advertise & ADVERTISED_100baseT_Full)
3196 val |= MDIO_AN_EEE_ADV_100TX;
3197 /* Advertise 1000-BaseT EEE ability */
3198 if (advertise & ADVERTISED_1000baseT_Full)
3199 val |= MDIO_AN_EEE_ADV_1000T;
3200 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlsonb715ce92011-07-20 10:20:52 +00003201 if (err)
3202 val = 0;
3203
3204 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3205 case ASIC_REV_5717:
3206 case ASIC_REV_57765:
3207 case ASIC_REV_5719:
3208 /* If we advertised any eee advertisements above... */
3209 if (val)
3210 val = MII_TG3_DSP_TAP26_ALNOKO |
3211 MII_TG3_DSP_TAP26_RMRXSTO |
3212 MII_TG3_DSP_TAP26_OPCSINPT;
3213 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3214 /* Fall through */
3215 case ASIC_REV_5720:
3216 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3217 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3218 MII_TG3_DSP_CH34TP2_HIBW01);
3219 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003220
Matt Carlson42b64a42011-05-19 12:12:49 +00003221 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3222 if (!err)
3223 err = err2;
3224 }
3225
3226done:
3227 return err;
3228}
3229
3230static void tg3_phy_copper_begin(struct tg3 *tp)
3231{
3232 u32 new_adv;
3233 int i;
3234
3235 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3236 new_adv = ADVERTISED_10baseT_Half |
3237 ADVERTISED_10baseT_Full;
3238 if (tg3_flag(tp, WOL_SPEED_100MB))
3239 new_adv |= ADVERTISED_100baseT_Half |
3240 ADVERTISED_100baseT_Full;
3241
3242 tg3_phy_autoneg_cfg(tp, new_adv,
3243 FLOW_CTRL_TX | FLOW_CTRL_RX);
3244 } else if (tp->link_config.speed == SPEED_INVALID) {
3245 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3246 tp->link_config.advertising &=
3247 ~(ADVERTISED_1000baseT_Half |
3248 ADVERTISED_1000baseT_Full);
3249
3250 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3251 tp->link_config.flowctrl);
3252 } else {
3253 /* Asking for a specific link mode. */
3254 if (tp->link_config.speed == SPEED_1000) {
3255 if (tp->link_config.duplex == DUPLEX_FULL)
3256 new_adv = ADVERTISED_1000baseT_Full;
3257 else
3258 new_adv = ADVERTISED_1000baseT_Half;
3259 } else if (tp->link_config.speed == SPEED_100) {
3260 if (tp->link_config.duplex == DUPLEX_FULL)
3261 new_adv = ADVERTISED_100baseT_Full;
3262 else
3263 new_adv = ADVERTISED_100baseT_Half;
3264 } else {
3265 if (tp->link_config.duplex == DUPLEX_FULL)
3266 new_adv = ADVERTISED_10baseT_Full;
3267 else
3268 new_adv = ADVERTISED_10baseT_Half;
3269 }
3270
3271 tg3_phy_autoneg_cfg(tp, new_adv,
3272 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003273 }
3274
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3276 tp->link_config.speed != SPEED_INVALID) {
3277 u32 bmcr, orig_bmcr;
3278
3279 tp->link_config.active_speed = tp->link_config.speed;
3280 tp->link_config.active_duplex = tp->link_config.duplex;
3281
3282 bmcr = 0;
3283 switch (tp->link_config.speed) {
3284 default:
3285 case SPEED_10:
3286 break;
3287
3288 case SPEED_100:
3289 bmcr |= BMCR_SPEED100;
3290 break;
3291
3292 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003293 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
3297 if (tp->link_config.duplex == DUPLEX_FULL)
3298 bmcr |= BMCR_FULLDPLX;
3299
3300 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3301 (bmcr != orig_bmcr)) {
3302 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3303 for (i = 0; i < 1500; i++) {
3304 u32 tmp;
3305
3306 udelay(10);
3307 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3308 tg3_readphy(tp, MII_BMSR, &tmp))
3309 continue;
3310 if (!(tmp & BMSR_LSTATUS)) {
3311 udelay(40);
3312 break;
3313 }
3314 }
3315 tg3_writephy(tp, MII_BMCR, bmcr);
3316 udelay(40);
3317 }
3318 } else {
3319 tg3_writephy(tp, MII_BMCR,
3320 BMCR_ANENABLE | BMCR_ANRESTART);
3321 }
3322}
3323
3324static int tg3_init_5401phy_dsp(struct tg3 *tp)
3325{
3326 int err;
3327
3328 /* Turn off tap power management. */
3329 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003330 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003332 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3333 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3334 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3335 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3336 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
3338 udelay(40);
3339
3340 return err;
3341}
3342
Michael Chan3600d912006-12-07 00:21:48 -08003343static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
Michael Chan3600d912006-12-07 00:21:48 -08003345 u32 adv_reg, all_mask = 0;
3346
3347 if (mask & ADVERTISED_10baseT_Half)
3348 all_mask |= ADVERTISE_10HALF;
3349 if (mask & ADVERTISED_10baseT_Full)
3350 all_mask |= ADVERTISE_10FULL;
3351 if (mask & ADVERTISED_100baseT_Half)
3352 all_mask |= ADVERTISE_100HALF;
3353 if (mask & ADVERTISED_100baseT_Full)
3354 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
3356 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3357 return 0;
3358
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003359 if ((adv_reg & ADVERTISE_ALL) != all_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 return 0;
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003361
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003362 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 u32 tg3_ctrl;
3364
Michael Chan3600d912006-12-07 00:21:48 -08003365 all_mask = 0;
3366 if (mask & ADVERTISED_1000baseT_Half)
3367 all_mask |= ADVERTISE_1000HALF;
3368 if (mask & ADVERTISED_1000baseT_Full)
3369 all_mask |= ADVERTISE_1000FULL;
3370
Matt Carlson221c5632011-06-13 13:39:01 +00003371 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 return 0;
3373
Matt Carlsonb99d2a52011-08-31 11:44:47 +00003374 tg3_ctrl &= (ADVERTISE_1000HALF | ADVERTISE_1000FULL);
3375 if (tg3_ctrl != all_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 return 0;
3377 }
3378 return 1;
3379}
3380
Matt Carlsonef167e22007-12-20 20:10:01 -08003381static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3382{
3383 u32 curadv, reqadv;
3384
3385 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3386 return 1;
3387
3388 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3389 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3390
3391 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3392 if (curadv != reqadv)
3393 return 0;
3394
Joe Perches63c3a662011-04-26 08:12:10 +00003395 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003396 tg3_readphy(tp, MII_LPA, rmtadv);
3397 } else {
3398 /* Reprogram the advertisement register, even if it
3399 * does not affect the current link. If the link
3400 * gets renegotiated in the future, we can save an
3401 * additional renegotiation cycle by advertising
3402 * it correctly in the first place.
3403 */
3404 if (curadv != reqadv) {
3405 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3406 ADVERTISE_PAUSE_ASYM);
3407 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3408 }
3409 }
3410
3411 return 1;
3412}
3413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3415{
3416 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003417 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003418 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 u16 current_speed;
3420 u8 current_duplex;
3421 int i, err;
3422
3423 tw32(MAC_EVENT, 0);
3424
3425 tw32_f(MAC_STATUS,
3426 (MAC_STATUS_SYNC_CHANGED |
3427 MAC_STATUS_CFG_CHANGED |
3428 MAC_STATUS_MI_COMPLETION |
3429 MAC_STATUS_LNKSTATE_CHANGED));
3430 udelay(40);
3431
Matt Carlson8ef21422008-05-02 16:47:53 -07003432 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3433 tw32_f(MAC_MI_MODE,
3434 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3435 udelay(80);
3436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003438 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
3440 /* Some third-party PHYs need to be reset on link going
3441 * down.
3442 */
3443 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3444 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3445 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3446 netif_carrier_ok(tp->dev)) {
3447 tg3_readphy(tp, MII_BMSR, &bmsr);
3448 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3449 !(bmsr & BMSR_LSTATUS))
3450 force_reset = 1;
3451 }
3452 if (force_reset)
3453 tg3_phy_reset(tp);
3454
Matt Carlson79eb6902010-02-17 15:17:03 +00003455 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 tg3_readphy(tp, MII_BMSR, &bmsr);
3457 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003458 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 bmsr = 0;
3460
3461 if (!(bmsr & BMSR_LSTATUS)) {
3462 err = tg3_init_5401phy_dsp(tp);
3463 if (err)
3464 return err;
3465
3466 tg3_readphy(tp, MII_BMSR, &bmsr);
3467 for (i = 0; i < 1000; i++) {
3468 udelay(10);
3469 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3470 (bmsr & BMSR_LSTATUS)) {
3471 udelay(40);
3472 break;
3473 }
3474 }
3475
Matt Carlson79eb6902010-02-17 15:17:03 +00003476 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3477 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 !(bmsr & BMSR_LSTATUS) &&
3479 tp->link_config.active_speed == SPEED_1000) {
3480 err = tg3_phy_reset(tp);
3481 if (!err)
3482 err = tg3_init_5401phy_dsp(tp);
3483 if (err)
3484 return err;
3485 }
3486 }
3487 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3488 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3489 /* 5701 {A0,B0} CRC bug workaround */
3490 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003491 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3492 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3493 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 }
3495
3496 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003497 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3498 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003500 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003502 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3504
3505 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3506 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3507 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3508 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3509 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3510 else
3511 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3512 }
3513
3514 current_link_up = 0;
3515 current_speed = SPEED_INVALID;
3516 current_duplex = DUPLEX_INVALID;
3517
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003518 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003519 err = tg3_phy_auxctl_read(tp,
3520 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3521 &val);
3522 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003523 tg3_phy_auxctl_write(tp,
3524 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3525 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 goto relink;
3527 }
3528 }
3529
3530 bmsr = 0;
3531 for (i = 0; i < 100; i++) {
3532 tg3_readphy(tp, MII_BMSR, &bmsr);
3533 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3534 (bmsr & BMSR_LSTATUS))
3535 break;
3536 udelay(40);
3537 }
3538
3539 if (bmsr & BMSR_LSTATUS) {
3540 u32 aux_stat, bmcr;
3541
3542 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3543 for (i = 0; i < 2000; i++) {
3544 udelay(10);
3545 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3546 aux_stat)
3547 break;
3548 }
3549
3550 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3551 &current_speed,
3552 &current_duplex);
3553
3554 bmcr = 0;
3555 for (i = 0; i < 200; i++) {
3556 tg3_readphy(tp, MII_BMCR, &bmcr);
3557 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3558 continue;
3559 if (bmcr && bmcr != 0x7fff)
3560 break;
3561 udelay(10);
3562 }
3563
Matt Carlsonef167e22007-12-20 20:10:01 -08003564 lcl_adv = 0;
3565 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Matt Carlsonef167e22007-12-20 20:10:01 -08003567 tp->link_config.active_speed = current_speed;
3568 tp->link_config.active_duplex = current_duplex;
3569
3570 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3571 if ((bmcr & BMCR_ANENABLE) &&
3572 tg3_copper_is_advertising_all(tp,
3573 tp->link_config.advertising)) {
3574 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3575 &rmt_adv))
3576 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 }
3578 } else {
3579 if (!(bmcr & BMCR_ANENABLE) &&
3580 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003581 tp->link_config.duplex == current_duplex &&
3582 tp->link_config.flowctrl ==
3583 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 }
3586 }
3587
Matt Carlsonef167e22007-12-20 20:10:01 -08003588 if (current_link_up == 1 &&
3589 tp->link_config.active_duplex == DUPLEX_FULL)
3590 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 }
3592
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593relink:
Matt Carlson80096062010-08-02 11:26:06 +00003594 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 tg3_phy_copper_begin(tp);
3596
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003597 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00003598 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
3599 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 current_link_up = 1;
3601 }
3602
3603 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3604 if (current_link_up == 1) {
3605 if (tp->link_config.active_speed == SPEED_100 ||
3606 tp->link_config.active_speed == SPEED_10)
3607 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3608 else
3609 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003610 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003611 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3612 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3614
3615 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3616 if (tp->link_config.active_duplex == DUPLEX_HALF)
3617 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3618
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003620 if (current_link_up == 1 &&
3621 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003623 else
3624 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 }
3626
3627 /* ??? Without this setting Netgear GA302T PHY does not
3628 * ??? send/receive packets...
3629 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003630 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3632 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3633 tw32_f(MAC_MI_MODE, tp->mi_mode);
3634 udelay(80);
3635 }
3636
3637 tw32_f(MAC_MODE, tp->mac_mode);
3638 udelay(40);
3639
Matt Carlson52b02d02010-10-14 10:37:41 +00003640 tg3_phy_eee_adjust(tp, current_link_up);
3641
Joe Perches63c3a662011-04-26 08:12:10 +00003642 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 /* Polled via timer. */
3644 tw32_f(MAC_EVENT, 0);
3645 } else {
3646 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3647 }
3648 udelay(40);
3649
3650 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3651 current_link_up == 1 &&
3652 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00003653 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 udelay(120);
3655 tw32_f(MAC_STATUS,
3656 (MAC_STATUS_SYNC_CHANGED |
3657 MAC_STATUS_CFG_CHANGED));
3658 udelay(40);
3659 tg3_write_mem(tp,
3660 NIC_SRAM_FIRMWARE_MBOX,
3661 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3662 }
3663
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003664 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00003665 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003666 u16 oldlnkctl, newlnkctl;
3667
3668 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003669 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003670 &oldlnkctl);
3671 if (tp->link_config.active_speed == SPEED_100 ||
3672 tp->link_config.active_speed == SPEED_10)
3673 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3674 else
3675 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3676 if (newlnkctl != oldlnkctl)
3677 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00003678 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003679 newlnkctl);
3680 }
3681
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 if (current_link_up != netif_carrier_ok(tp->dev)) {
3683 if (current_link_up)
3684 netif_carrier_on(tp->dev);
3685 else
3686 netif_carrier_off(tp->dev);
3687 tg3_link_report(tp);
3688 }
3689
3690 return 0;
3691}
3692
3693struct tg3_fiber_aneginfo {
3694 int state;
3695#define ANEG_STATE_UNKNOWN 0
3696#define ANEG_STATE_AN_ENABLE 1
3697#define ANEG_STATE_RESTART_INIT 2
3698#define ANEG_STATE_RESTART 3
3699#define ANEG_STATE_DISABLE_LINK_OK 4
3700#define ANEG_STATE_ABILITY_DETECT_INIT 5
3701#define ANEG_STATE_ABILITY_DETECT 6
3702#define ANEG_STATE_ACK_DETECT_INIT 7
3703#define ANEG_STATE_ACK_DETECT 8
3704#define ANEG_STATE_COMPLETE_ACK_INIT 9
3705#define ANEG_STATE_COMPLETE_ACK 10
3706#define ANEG_STATE_IDLE_DETECT_INIT 11
3707#define ANEG_STATE_IDLE_DETECT 12
3708#define ANEG_STATE_LINK_OK 13
3709#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3710#define ANEG_STATE_NEXT_PAGE_WAIT 15
3711
3712 u32 flags;
3713#define MR_AN_ENABLE 0x00000001
3714#define MR_RESTART_AN 0x00000002
3715#define MR_AN_COMPLETE 0x00000004
3716#define MR_PAGE_RX 0x00000008
3717#define MR_NP_LOADED 0x00000010
3718#define MR_TOGGLE_TX 0x00000020
3719#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3720#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3721#define MR_LP_ADV_SYM_PAUSE 0x00000100
3722#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3723#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3724#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3725#define MR_LP_ADV_NEXT_PAGE 0x00001000
3726#define MR_TOGGLE_RX 0x00002000
3727#define MR_NP_RX 0x00004000
3728
3729#define MR_LINK_OK 0x80000000
3730
3731 unsigned long link_time, cur_time;
3732
3733 u32 ability_match_cfg;
3734 int ability_match_count;
3735
3736 char ability_match, idle_match, ack_match;
3737
3738 u32 txconfig, rxconfig;
3739#define ANEG_CFG_NP 0x00000080
3740#define ANEG_CFG_ACK 0x00000040
3741#define ANEG_CFG_RF2 0x00000020
3742#define ANEG_CFG_RF1 0x00000010
3743#define ANEG_CFG_PS2 0x00000001
3744#define ANEG_CFG_PS1 0x00008000
3745#define ANEG_CFG_HD 0x00004000
3746#define ANEG_CFG_FD 0x00002000
3747#define ANEG_CFG_INVAL 0x00001f06
3748
3749};
3750#define ANEG_OK 0
3751#define ANEG_DONE 1
3752#define ANEG_TIMER_ENAB 2
3753#define ANEG_FAILED -1
3754
3755#define ANEG_STATE_SETTLE_TIME 10000
3756
3757static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3758 struct tg3_fiber_aneginfo *ap)
3759{
Matt Carlson5be73b42007-12-20 20:09:29 -08003760 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 unsigned long delta;
3762 u32 rx_cfg_reg;
3763 int ret;
3764
3765 if (ap->state == ANEG_STATE_UNKNOWN) {
3766 ap->rxconfig = 0;
3767 ap->link_time = 0;
3768 ap->cur_time = 0;
3769 ap->ability_match_cfg = 0;
3770 ap->ability_match_count = 0;
3771 ap->ability_match = 0;
3772 ap->idle_match = 0;
3773 ap->ack_match = 0;
3774 }
3775 ap->cur_time++;
3776
3777 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3778 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3779
3780 if (rx_cfg_reg != ap->ability_match_cfg) {
3781 ap->ability_match_cfg = rx_cfg_reg;
3782 ap->ability_match = 0;
3783 ap->ability_match_count = 0;
3784 } else {
3785 if (++ap->ability_match_count > 1) {
3786 ap->ability_match = 1;
3787 ap->ability_match_cfg = rx_cfg_reg;
3788 }
3789 }
3790 if (rx_cfg_reg & ANEG_CFG_ACK)
3791 ap->ack_match = 1;
3792 else
3793 ap->ack_match = 0;
3794
3795 ap->idle_match = 0;
3796 } else {
3797 ap->idle_match = 1;
3798 ap->ability_match_cfg = 0;
3799 ap->ability_match_count = 0;
3800 ap->ability_match = 0;
3801 ap->ack_match = 0;
3802
3803 rx_cfg_reg = 0;
3804 }
3805
3806 ap->rxconfig = rx_cfg_reg;
3807 ret = ANEG_OK;
3808
Matt Carlson33f401a2010-04-05 10:19:27 +00003809 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 case ANEG_STATE_UNKNOWN:
3811 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3812 ap->state = ANEG_STATE_AN_ENABLE;
3813
3814 /* fallthru */
3815 case ANEG_STATE_AN_ENABLE:
3816 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3817 if (ap->flags & MR_AN_ENABLE) {
3818 ap->link_time = 0;
3819 ap->cur_time = 0;
3820 ap->ability_match_cfg = 0;
3821 ap->ability_match_count = 0;
3822 ap->ability_match = 0;
3823 ap->idle_match = 0;
3824 ap->ack_match = 0;
3825
3826 ap->state = ANEG_STATE_RESTART_INIT;
3827 } else {
3828 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3829 }
3830 break;
3831
3832 case ANEG_STATE_RESTART_INIT:
3833 ap->link_time = ap->cur_time;
3834 ap->flags &= ~(MR_NP_LOADED);
3835 ap->txconfig = 0;
3836 tw32(MAC_TX_AUTO_NEG, 0);
3837 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3838 tw32_f(MAC_MODE, tp->mac_mode);
3839 udelay(40);
3840
3841 ret = ANEG_TIMER_ENAB;
3842 ap->state = ANEG_STATE_RESTART;
3843
3844 /* fallthru */
3845 case ANEG_STATE_RESTART:
3846 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00003847 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00003849 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 break;
3852
3853 case ANEG_STATE_DISABLE_LINK_OK:
3854 ret = ANEG_DONE;
3855 break;
3856
3857 case ANEG_STATE_ABILITY_DETECT_INIT:
3858 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003859 ap->txconfig = ANEG_CFG_FD;
3860 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3861 if (flowctrl & ADVERTISE_1000XPAUSE)
3862 ap->txconfig |= ANEG_CFG_PS1;
3863 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3864 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3866 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3867 tw32_f(MAC_MODE, tp->mac_mode);
3868 udelay(40);
3869
3870 ap->state = ANEG_STATE_ABILITY_DETECT;
3871 break;
3872
3873 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00003874 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 break;
3877
3878 case ANEG_STATE_ACK_DETECT_INIT:
3879 ap->txconfig |= ANEG_CFG_ACK;
3880 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3881 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3882 tw32_f(MAC_MODE, tp->mac_mode);
3883 udelay(40);
3884
3885 ap->state = ANEG_STATE_ACK_DETECT;
3886
3887 /* fallthru */
3888 case ANEG_STATE_ACK_DETECT:
3889 if (ap->ack_match != 0) {
3890 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3891 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3892 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3893 } else {
3894 ap->state = ANEG_STATE_AN_ENABLE;
3895 }
3896 } else if (ap->ability_match != 0 &&
3897 ap->rxconfig == 0) {
3898 ap->state = ANEG_STATE_AN_ENABLE;
3899 }
3900 break;
3901
3902 case ANEG_STATE_COMPLETE_ACK_INIT:
3903 if (ap->rxconfig & ANEG_CFG_INVAL) {
3904 ret = ANEG_FAILED;
3905 break;
3906 }
3907 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3908 MR_LP_ADV_HALF_DUPLEX |
3909 MR_LP_ADV_SYM_PAUSE |
3910 MR_LP_ADV_ASYM_PAUSE |
3911 MR_LP_ADV_REMOTE_FAULT1 |
3912 MR_LP_ADV_REMOTE_FAULT2 |
3913 MR_LP_ADV_NEXT_PAGE |
3914 MR_TOGGLE_RX |
3915 MR_NP_RX);
3916 if (ap->rxconfig & ANEG_CFG_FD)
3917 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3918 if (ap->rxconfig & ANEG_CFG_HD)
3919 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3920 if (ap->rxconfig & ANEG_CFG_PS1)
3921 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3922 if (ap->rxconfig & ANEG_CFG_PS2)
3923 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3924 if (ap->rxconfig & ANEG_CFG_RF1)
3925 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3926 if (ap->rxconfig & ANEG_CFG_RF2)
3927 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3928 if (ap->rxconfig & ANEG_CFG_NP)
3929 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3930
3931 ap->link_time = ap->cur_time;
3932
3933 ap->flags ^= (MR_TOGGLE_TX);
3934 if (ap->rxconfig & 0x0008)
3935 ap->flags |= MR_TOGGLE_RX;
3936 if (ap->rxconfig & ANEG_CFG_NP)
3937 ap->flags |= MR_NP_RX;
3938 ap->flags |= MR_PAGE_RX;
3939
3940 ap->state = ANEG_STATE_COMPLETE_ACK;
3941 ret = ANEG_TIMER_ENAB;
3942 break;
3943
3944 case ANEG_STATE_COMPLETE_ACK:
3945 if (ap->ability_match != 0 &&
3946 ap->rxconfig == 0) {
3947 ap->state = ANEG_STATE_AN_ENABLE;
3948 break;
3949 }
3950 delta = ap->cur_time - ap->link_time;
3951 if (delta > ANEG_STATE_SETTLE_TIME) {
3952 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3953 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3954 } else {
3955 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3956 !(ap->flags & MR_NP_RX)) {
3957 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3958 } else {
3959 ret = ANEG_FAILED;
3960 }
3961 }
3962 }
3963 break;
3964
3965 case ANEG_STATE_IDLE_DETECT_INIT:
3966 ap->link_time = ap->cur_time;
3967 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3968 tw32_f(MAC_MODE, tp->mac_mode);
3969 udelay(40);
3970
3971 ap->state = ANEG_STATE_IDLE_DETECT;
3972 ret = ANEG_TIMER_ENAB;
3973 break;
3974
3975 case ANEG_STATE_IDLE_DETECT:
3976 if (ap->ability_match != 0 &&
3977 ap->rxconfig == 0) {
3978 ap->state = ANEG_STATE_AN_ENABLE;
3979 break;
3980 }
3981 delta = ap->cur_time - ap->link_time;
3982 if (delta > ANEG_STATE_SETTLE_TIME) {
3983 /* XXX another gem from the Broadcom driver :( */
3984 ap->state = ANEG_STATE_LINK_OK;
3985 }
3986 break;
3987
3988 case ANEG_STATE_LINK_OK:
3989 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3990 ret = ANEG_DONE;
3991 break;
3992
3993 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3994 /* ??? unimplemented */
3995 break;
3996
3997 case ANEG_STATE_NEXT_PAGE_WAIT:
3998 /* ??? unimplemented */
3999 break;
4000
4001 default:
4002 ret = ANEG_FAILED;
4003 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
4006 return ret;
4007}
4008
Matt Carlson5be73b42007-12-20 20:09:29 -08004009static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
4011 int res = 0;
4012 struct tg3_fiber_aneginfo aninfo;
4013 int status = ANEG_FAILED;
4014 unsigned int tick;
4015 u32 tmp;
4016
4017 tw32_f(MAC_TX_AUTO_NEG, 0);
4018
4019 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
4020 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
4021 udelay(40);
4022
4023 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
4024 udelay(40);
4025
4026 memset(&aninfo, 0, sizeof(aninfo));
4027 aninfo.flags |= MR_AN_ENABLE;
4028 aninfo.state = ANEG_STATE_UNKNOWN;
4029 aninfo.cur_time = 0;
4030 tick = 0;
4031 while (++tick < 195000) {
4032 status = tg3_fiber_aneg_smachine(tp, &aninfo);
4033 if (status == ANEG_DONE || status == ANEG_FAILED)
4034 break;
4035
4036 udelay(1);
4037 }
4038
4039 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
4040 tw32_f(MAC_MODE, tp->mac_mode);
4041 udelay(40);
4042
Matt Carlson5be73b42007-12-20 20:09:29 -08004043 *txflags = aninfo.txconfig;
4044 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045
4046 if (status == ANEG_DONE &&
4047 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
4048 MR_LP_ADV_FULL_DUPLEX)))
4049 res = 1;
4050
4051 return res;
4052}
4053
4054static void tg3_init_bcm8002(struct tg3 *tp)
4055{
4056 u32 mac_status = tr32(MAC_STATUS);
4057 int i;
4058
4059 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00004060 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 !(mac_status & MAC_STATUS_PCS_SYNCED))
4062 return;
4063
4064 /* Set PLL lock range. */
4065 tg3_writephy(tp, 0x16, 0x8007);
4066
4067 /* SW reset */
4068 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
4069
4070 /* Wait for reset to complete. */
4071 /* XXX schedule_timeout() ... */
4072 for (i = 0; i < 500; i++)
4073 udelay(10);
4074
4075 /* Config mode; select PMA/Ch 1 regs. */
4076 tg3_writephy(tp, 0x10, 0x8411);
4077
4078 /* Enable auto-lock and comdet, select txclk for tx. */
4079 tg3_writephy(tp, 0x11, 0x0a10);
4080
4081 tg3_writephy(tp, 0x18, 0x00a0);
4082 tg3_writephy(tp, 0x16, 0x41ff);
4083
4084 /* Assert and deassert POR. */
4085 tg3_writephy(tp, 0x13, 0x0400);
4086 udelay(40);
4087 tg3_writephy(tp, 0x13, 0x0000);
4088
4089 tg3_writephy(tp, 0x11, 0x0a50);
4090 udelay(40);
4091 tg3_writephy(tp, 0x11, 0x0a10);
4092
4093 /* Wait for signal to stabilize */
4094 /* XXX schedule_timeout() ... */
4095 for (i = 0; i < 15000; i++)
4096 udelay(10);
4097
4098 /* Deselect the channel register so we can read the PHYID
4099 * later.
4100 */
4101 tg3_writephy(tp, 0x10, 0x8011);
4102}
4103
4104static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
4105{
Matt Carlson82cd3d12007-12-20 20:09:00 -08004106 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 u32 sg_dig_ctrl, sg_dig_status;
4108 u32 serdes_cfg, expected_sg_dig_ctrl;
4109 int workaround, port_a;
4110 int current_link_up;
4111
4112 serdes_cfg = 0;
4113 expected_sg_dig_ctrl = 0;
4114 workaround = 0;
4115 port_a = 1;
4116 current_link_up = 0;
4117
4118 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
4119 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
4120 workaround = 1;
4121 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
4122 port_a = 0;
4123
4124 /* preserve bits 0-11,13,14 for signal pre-emphasis */
4125 /* preserve bits 20-23 for voltage regulator */
4126 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
4127 }
4128
4129 sg_dig_ctrl = tr32(SG_DIG_CTRL);
4130
4131 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004132 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 if (workaround) {
4134 u32 val = serdes_cfg;
4135
4136 if (port_a)
4137 val |= 0xc010000;
4138 else
4139 val |= 0x4010000;
4140 tw32_f(MAC_SERDES_CFG, val);
4141 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004142
4143 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 }
4145 if (mac_status & MAC_STATUS_PCS_SYNCED) {
4146 tg3_setup_flow_control(tp, 0, 0);
4147 current_link_up = 1;
4148 }
4149 goto out;
4150 }
4151
4152 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004153 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Matt Carlson82cd3d12007-12-20 20:09:00 -08004155 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
4156 if (flowctrl & ADVERTISE_1000XPAUSE)
4157 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
4158 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
4159 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
4161 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004162 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07004163 tp->serdes_counter &&
4164 ((mac_status & (MAC_STATUS_PCS_SYNCED |
4165 MAC_STATUS_RCVD_CFG)) ==
4166 MAC_STATUS_PCS_SYNCED)) {
4167 tp->serdes_counter--;
4168 current_link_up = 1;
4169 goto out;
4170 }
4171restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 if (workaround)
4173 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004174 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 udelay(5);
4176 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
4177
Michael Chan3d3ebe72006-09-27 15:59:15 -07004178 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004179 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4181 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004182 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 mac_status = tr32(MAC_STATUS);
4184
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004185 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004187 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
Matt Carlson82cd3d12007-12-20 20:09:00 -08004189 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4190 local_adv |= ADVERTISE_1000XPAUSE;
4191 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4192 local_adv |= ADVERTISE_1000XPSE_ASYM;
4193
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004194 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004195 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004196 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004197 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
4199 tg3_setup_flow_control(tp, local_adv, remote_adv);
4200 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004201 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004202 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004203 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004204 if (tp->serdes_counter)
4205 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 else {
4207 if (workaround) {
4208 u32 val = serdes_cfg;
4209
4210 if (port_a)
4211 val |= 0xc010000;
4212 else
4213 val |= 0x4010000;
4214
4215 tw32_f(MAC_SERDES_CFG, val);
4216 }
4217
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004218 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 udelay(40);
4220
4221 /* Link parallel detection - link is up */
4222 /* only if we have PCS_SYNC and not */
4223 /* receiving config code words */
4224 mac_status = tr32(MAC_STATUS);
4225 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4226 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4227 tg3_setup_flow_control(tp, 0, 0);
4228 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004229 tp->phy_flags |=
4230 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004231 tp->serdes_counter =
4232 SERDES_PARALLEL_DET_TIMEOUT;
4233 } else
4234 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 }
4236 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004237 } else {
4238 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004239 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 }
4241
4242out:
4243 return current_link_up;
4244}
4245
4246static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4247{
4248 int current_link_up = 0;
4249
Michael Chan5cf64b82007-05-05 12:11:21 -07004250 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
4253 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004254 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004256
Matt Carlson5be73b42007-12-20 20:09:29 -08004257 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4258 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Matt Carlson5be73b42007-12-20 20:09:29 -08004260 if (txflags & ANEG_CFG_PS1)
4261 local_adv |= ADVERTISE_1000XPAUSE;
4262 if (txflags & ANEG_CFG_PS2)
4263 local_adv |= ADVERTISE_1000XPSE_ASYM;
4264
4265 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4266 remote_adv |= LPA_1000XPAUSE;
4267 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4268 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
4270 tg3_setup_flow_control(tp, local_adv, remote_adv);
4271
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 current_link_up = 1;
4273 }
4274 for (i = 0; i < 30; i++) {
4275 udelay(20);
4276 tw32_f(MAC_STATUS,
4277 (MAC_STATUS_SYNC_CHANGED |
4278 MAC_STATUS_CFG_CHANGED));
4279 udelay(40);
4280 if ((tr32(MAC_STATUS) &
4281 (MAC_STATUS_SYNC_CHANGED |
4282 MAC_STATUS_CFG_CHANGED)) == 0)
4283 break;
4284 }
4285
4286 mac_status = tr32(MAC_STATUS);
4287 if (current_link_up == 0 &&
4288 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4289 !(mac_status & MAC_STATUS_RCVD_CFG))
4290 current_link_up = 1;
4291 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004292 tg3_setup_flow_control(tp, 0, 0);
4293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 /* Forcing 1000FD link up. */
4295 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
4297 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4298 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004299
4300 tw32_f(MAC_MODE, tp->mac_mode);
4301 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 }
4303
4304out:
4305 return current_link_up;
4306}
4307
4308static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4309{
4310 u32 orig_pause_cfg;
4311 u16 orig_active_speed;
4312 u8 orig_active_duplex;
4313 u32 mac_status;
4314 int current_link_up;
4315 int i;
4316
Matt Carlson8d018622007-12-20 20:05:44 -08004317 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 orig_active_speed = tp->link_config.active_speed;
4319 orig_active_duplex = tp->link_config.active_duplex;
4320
Joe Perches63c3a662011-04-26 08:12:10 +00004321 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004323 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 mac_status = tr32(MAC_STATUS);
4325 mac_status &= (MAC_STATUS_PCS_SYNCED |
4326 MAC_STATUS_SIGNAL_DET |
4327 MAC_STATUS_CFG_CHANGED |
4328 MAC_STATUS_RCVD_CFG);
4329 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4330 MAC_STATUS_SIGNAL_DET)) {
4331 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4332 MAC_STATUS_CFG_CHANGED));
4333 return 0;
4334 }
4335 }
4336
4337 tw32_f(MAC_TX_AUTO_NEG, 0);
4338
4339 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4340 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4341 tw32_f(MAC_MODE, tp->mac_mode);
4342 udelay(40);
4343
Matt Carlson79eb6902010-02-17 15:17:03 +00004344 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 tg3_init_bcm8002(tp);
4346
4347 /* Enable link change event even when serdes polling. */
4348 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4349 udelay(40);
4350
4351 current_link_up = 0;
4352 mac_status = tr32(MAC_STATUS);
4353
Joe Perches63c3a662011-04-26 08:12:10 +00004354 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4356 else
4357 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4358
Matt Carlson898a56f2009-08-28 14:02:40 +00004359 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004361 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
4363 for (i = 0; i < 100; i++) {
4364 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4365 MAC_STATUS_CFG_CHANGED));
4366 udelay(5);
4367 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004368 MAC_STATUS_CFG_CHANGED |
4369 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 break;
4371 }
4372
4373 mac_status = tr32(MAC_STATUS);
4374 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4375 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004376 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4377 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 tw32_f(MAC_MODE, (tp->mac_mode |
4379 MAC_MODE_SEND_CONFIGS));
4380 udelay(1);
4381 tw32_f(MAC_MODE, tp->mac_mode);
4382 }
4383 }
4384
4385 if (current_link_up == 1) {
4386 tp->link_config.active_speed = SPEED_1000;
4387 tp->link_config.active_duplex = DUPLEX_FULL;
4388 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4389 LED_CTRL_LNKLED_OVERRIDE |
4390 LED_CTRL_1000MBPS_ON));
4391 } else {
4392 tp->link_config.active_speed = SPEED_INVALID;
4393 tp->link_config.active_duplex = DUPLEX_INVALID;
4394 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4395 LED_CTRL_LNKLED_OVERRIDE |
4396 LED_CTRL_TRAFFIC_OVERRIDE));
4397 }
4398
4399 if (current_link_up != netif_carrier_ok(tp->dev)) {
4400 if (current_link_up)
4401 netif_carrier_on(tp->dev);
4402 else
4403 netif_carrier_off(tp->dev);
4404 tg3_link_report(tp);
4405 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004406 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 if (orig_pause_cfg != now_pause_cfg ||
4408 orig_active_speed != tp->link_config.active_speed ||
4409 orig_active_duplex != tp->link_config.active_duplex)
4410 tg3_link_report(tp);
4411 }
4412
4413 return 0;
4414}
4415
Michael Chan747e8f82005-07-25 12:33:22 -07004416static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4417{
4418 int current_link_up, err = 0;
4419 u32 bmsr, bmcr;
4420 u16 current_speed;
4421 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004422 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004423
4424 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4425 tw32_f(MAC_MODE, tp->mac_mode);
4426 udelay(40);
4427
4428 tw32(MAC_EVENT, 0);
4429
4430 tw32_f(MAC_STATUS,
4431 (MAC_STATUS_SYNC_CHANGED |
4432 MAC_STATUS_CFG_CHANGED |
4433 MAC_STATUS_MI_COMPLETION |
4434 MAC_STATUS_LNKSTATE_CHANGED));
4435 udelay(40);
4436
4437 if (force_reset)
4438 tg3_phy_reset(tp);
4439
4440 current_link_up = 0;
4441 current_speed = SPEED_INVALID;
4442 current_duplex = DUPLEX_INVALID;
4443
4444 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4445 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004446 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4447 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4448 bmsr |= BMSR_LSTATUS;
4449 else
4450 bmsr &= ~BMSR_LSTATUS;
4451 }
Michael Chan747e8f82005-07-25 12:33:22 -07004452
4453 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4454
4455 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004456 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004457 /* do nothing, just check for link up at the end */
4458 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4459 u32 adv, new_adv;
4460
4461 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4462 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4463 ADVERTISE_1000XPAUSE |
4464 ADVERTISE_1000XPSE_ASYM |
4465 ADVERTISE_SLCT);
4466
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004467 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004468
4469 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4470 new_adv |= ADVERTISE_1000XHALF;
4471 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4472 new_adv |= ADVERTISE_1000XFULL;
4473
4474 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4475 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4476 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4477 tg3_writephy(tp, MII_BMCR, bmcr);
4478
4479 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004480 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004481 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004482
4483 return err;
4484 }
4485 } else {
4486 u32 new_bmcr;
4487
4488 bmcr &= ~BMCR_SPEED1000;
4489 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4490
4491 if (tp->link_config.duplex == DUPLEX_FULL)
4492 new_bmcr |= BMCR_FULLDPLX;
4493
4494 if (new_bmcr != bmcr) {
4495 /* BMCR_SPEED1000 is a reserved bit that needs
4496 * to be set on write.
4497 */
4498 new_bmcr |= BMCR_SPEED1000;
4499
4500 /* Force a linkdown */
4501 if (netif_carrier_ok(tp->dev)) {
4502 u32 adv;
4503
4504 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4505 adv &= ~(ADVERTISE_1000XFULL |
4506 ADVERTISE_1000XHALF |
4507 ADVERTISE_SLCT);
4508 tg3_writephy(tp, MII_ADVERTISE, adv);
4509 tg3_writephy(tp, MII_BMCR, bmcr |
4510 BMCR_ANRESTART |
4511 BMCR_ANENABLE);
4512 udelay(10);
4513 netif_carrier_off(tp->dev);
4514 }
4515 tg3_writephy(tp, MII_BMCR, new_bmcr);
4516 bmcr = new_bmcr;
4517 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4518 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004519 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4520 ASIC_REV_5714) {
4521 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4522 bmsr |= BMSR_LSTATUS;
4523 else
4524 bmsr &= ~BMSR_LSTATUS;
4525 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004526 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004527 }
4528 }
4529
4530 if (bmsr & BMSR_LSTATUS) {
4531 current_speed = SPEED_1000;
4532 current_link_up = 1;
4533 if (bmcr & BMCR_FULLDPLX)
4534 current_duplex = DUPLEX_FULL;
4535 else
4536 current_duplex = DUPLEX_HALF;
4537
Matt Carlsonef167e22007-12-20 20:10:01 -08004538 local_adv = 0;
4539 remote_adv = 0;
4540
Michael Chan747e8f82005-07-25 12:33:22 -07004541 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004542 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004543
4544 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4545 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4546 common = local_adv & remote_adv;
4547 if (common & (ADVERTISE_1000XHALF |
4548 ADVERTISE_1000XFULL)) {
4549 if (common & ADVERTISE_1000XFULL)
4550 current_duplex = DUPLEX_FULL;
4551 else
4552 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004553 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004554 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004555 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004556 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004557 }
Michael Chan747e8f82005-07-25 12:33:22 -07004558 }
4559 }
4560
Matt Carlsonef167e22007-12-20 20:10:01 -08004561 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4562 tg3_setup_flow_control(tp, local_adv, remote_adv);
4563
Michael Chan747e8f82005-07-25 12:33:22 -07004564 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4565 if (tp->link_config.active_duplex == DUPLEX_HALF)
4566 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4567
4568 tw32_f(MAC_MODE, tp->mac_mode);
4569 udelay(40);
4570
4571 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4572
4573 tp->link_config.active_speed = current_speed;
4574 tp->link_config.active_duplex = current_duplex;
4575
4576 if (current_link_up != netif_carrier_ok(tp->dev)) {
4577 if (current_link_up)
4578 netif_carrier_on(tp->dev);
4579 else {
4580 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004581 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004582 }
4583 tg3_link_report(tp);
4584 }
4585 return err;
4586}
4587
4588static void tg3_serdes_parallel_detect(struct tg3 *tp)
4589{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004590 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004591 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004592 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004593 return;
4594 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004595
Michael Chan747e8f82005-07-25 12:33:22 -07004596 if (!netif_carrier_ok(tp->dev) &&
4597 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4598 u32 bmcr;
4599
4600 tg3_readphy(tp, MII_BMCR, &bmcr);
4601 if (bmcr & BMCR_ANENABLE) {
4602 u32 phy1, phy2;
4603
4604 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004605 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4606 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004607
4608 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004609 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4610 MII_TG3_DSP_EXP1_INT_STAT);
4611 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4612 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004613
4614 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4615 /* We have signal detect and not receiving
4616 * config code words, link is up by parallel
4617 * detection.
4618 */
4619
4620 bmcr &= ~BMCR_ANENABLE;
4621 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4622 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004623 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004624 }
4625 }
Matt Carlson859a5882010-04-05 10:19:28 +00004626 } else if (netif_carrier_ok(tp->dev) &&
4627 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004628 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004629 u32 phy2;
4630
4631 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004632 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4633 MII_TG3_DSP_EXP1_INT_STAT);
4634 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004635 if (phy2 & 0x20) {
4636 u32 bmcr;
4637
4638 /* Config code words received, turn on autoneg. */
4639 tg3_readphy(tp, MII_BMCR, &bmcr);
4640 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4641
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004642 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004643
4644 }
4645 }
4646}
4647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4649{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004650 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 int err;
4652
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004653 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004655 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004656 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00004657 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004660 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004661 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004662
4663 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4664 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4665 scale = 65;
4666 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4667 scale = 6;
4668 else
4669 scale = 12;
4670
4671 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4672 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4673 tw32(GRC_MISC_CFG, val);
4674 }
4675
Matt Carlsonf2096f92011-04-05 14:22:48 +00004676 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4677 (6 << TX_LENGTHS_IPG_SHIFT);
4678 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4679 val |= tr32(MAC_TX_LENGTHS) &
4680 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4681 TX_LENGTHS_CNT_DWN_VAL_MSK);
4682
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 if (tp->link_config.active_speed == SPEED_1000 &&
4684 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004685 tw32(MAC_TX_LENGTHS, val |
4686 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004688 tw32(MAC_TX_LENGTHS, val |
4689 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Joe Perches63c3a662011-04-26 08:12:10 +00004691 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 if (netif_carrier_ok(tp->dev)) {
4693 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004694 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 } else {
4696 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4697 }
4698 }
4699
Joe Perches63c3a662011-04-26 08:12:10 +00004700 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004701 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004702 if (!netif_carrier_ok(tp->dev))
4703 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4704 tp->pwrmgmt_thresh;
4705 else
4706 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4707 tw32(PCIE_PWR_MGMT_THRESH, val);
4708 }
4709
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 return err;
4711}
4712
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004713static inline int tg3_irq_sync(struct tg3 *tp)
4714{
4715 return tp->irq_sync;
4716}
4717
Matt Carlson97bd8e42011-04-13 11:05:04 +00004718static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4719{
4720 int i;
4721
4722 dst = (u32 *)((u8 *)dst + off);
4723 for (i = 0; i < len; i += sizeof(u32))
4724 *dst++ = tr32(off + i);
4725}
4726
4727static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4728{
4729 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4730 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4731 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4732 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4733 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4734 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4735 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4736 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4737 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4738 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4739 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4740 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4741 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4742 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4743 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4744 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4745 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4746 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4747 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4748
Joe Perches63c3a662011-04-26 08:12:10 +00004749 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004750 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4751
4752 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4753 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4754 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4755 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4756 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4757 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4758 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4759 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4760
Joe Perches63c3a662011-04-26 08:12:10 +00004761 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004762 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4763 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4764 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4765 }
4766
4767 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4768 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4769 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4770 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4771 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4772
Joe Perches63c3a662011-04-26 08:12:10 +00004773 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004774 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4775}
4776
4777static void tg3_dump_state(struct tg3 *tp)
4778{
4779 int i;
4780 u32 *regs;
4781
4782 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4783 if (!regs) {
4784 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4785 return;
4786 }
4787
Joe Perches63c3a662011-04-26 08:12:10 +00004788 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004789 /* Read up to but not including private PCI registers */
4790 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4791 regs[i / sizeof(u32)] = tr32(i);
4792 } else
4793 tg3_dump_legacy_regs(tp, regs);
4794
4795 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4796 if (!regs[i + 0] && !regs[i + 1] &&
4797 !regs[i + 2] && !regs[i + 3])
4798 continue;
4799
4800 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4801 i * 4,
4802 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4803 }
4804
4805 kfree(regs);
4806
4807 for (i = 0; i < tp->irq_cnt; i++) {
4808 struct tg3_napi *tnapi = &tp->napi[i];
4809
4810 /* SW status block */
4811 netdev_err(tp->dev,
4812 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4813 i,
4814 tnapi->hw_status->status,
4815 tnapi->hw_status->status_tag,
4816 tnapi->hw_status->rx_jumbo_consumer,
4817 tnapi->hw_status->rx_consumer,
4818 tnapi->hw_status->rx_mini_consumer,
4819 tnapi->hw_status->idx[0].rx_producer,
4820 tnapi->hw_status->idx[0].tx_consumer);
4821
4822 netdev_err(tp->dev,
4823 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4824 i,
4825 tnapi->last_tag, tnapi->last_irq_tag,
4826 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4827 tnapi->rx_rcb_ptr,
4828 tnapi->prodring.rx_std_prod_idx,
4829 tnapi->prodring.rx_std_cons_idx,
4830 tnapi->prodring.rx_jmb_prod_idx,
4831 tnapi->prodring.rx_jmb_cons_idx);
4832 }
4833}
4834
Michael Chandf3e6542006-05-26 17:48:07 -07004835/* This is called whenever we suspect that the system chipset is re-
4836 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4837 * is bogus tx completions. We try to recover by setting the
4838 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4839 * in the workqueue.
4840 */
4841static void tg3_tx_recover(struct tg3 *tp)
4842{
Joe Perches63c3a662011-04-26 08:12:10 +00004843 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07004844 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4845
Matt Carlson5129c3a2010-04-05 10:19:23 +00004846 netdev_warn(tp->dev,
4847 "The system may be re-ordering memory-mapped I/O "
4848 "cycles to the network device, attempting to recover. "
4849 "Please report the problem to the driver maintainer "
4850 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004851
4852 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00004853 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07004854 spin_unlock(&tp->lock);
4855}
4856
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004857static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004858{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004859 /* Tell compiler to fetch tx indices from memory. */
4860 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004861 return tnapi->tx_pending -
4862 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004863}
4864
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865/* Tigon3 never reports partial packet sends. So we do not
4866 * need special logic to handle SKBs that have not had all
4867 * of their frags sent yet, like SunGEM does.
4868 */
Matt Carlson17375d22009-08-28 14:02:18 +00004869static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870{
Matt Carlson17375d22009-08-28 14:02:18 +00004871 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004872 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004873 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004874 struct netdev_queue *txq;
4875 int index = tnapi - tp->napi;
4876
Joe Perches63c3a662011-04-26 08:12:10 +00004877 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004878 index--;
4879
4880 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
4882 while (sw_idx != hw_idx) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00004883 struct tg3_tx_ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004885 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
Michael Chandf3e6542006-05-26 17:48:07 -07004887 if (unlikely(skb == NULL)) {
4888 tg3_tx_recover(tp);
4889 return;
4890 }
4891
Alexander Duyckf4188d82009-12-02 16:48:38 +00004892 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004893 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004894 skb_headlen(skb),
4895 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896
4897 ri->skb = NULL;
4898
Matt Carlsone01ee142011-07-27 14:20:50 +00004899 while (ri->fragmented) {
4900 ri->fragmented = false;
4901 sw_idx = NEXT_TX(sw_idx);
4902 ri = &tnapi->tx_buffers[sw_idx];
4903 }
4904
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 sw_idx = NEXT_TX(sw_idx);
4906
4907 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004908 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004909 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4910 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004911
4912 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004913 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004914 skb_shinfo(skb)->frags[i].size,
4915 PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00004916
4917 while (ri->fragmented) {
4918 ri->fragmented = false;
4919 sw_idx = NEXT_TX(sw_idx);
4920 ri = &tnapi->tx_buffers[sw_idx];
4921 }
4922
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 sw_idx = NEXT_TX(sw_idx);
4924 }
4925
David S. Millerf47c11e2005-06-24 20:18:35 -07004926 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004927
4928 if (unlikely(tx_bug)) {
4929 tg3_tx_recover(tp);
4930 return;
4931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 }
4933
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004934 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935
Michael Chan1b2a7202006-08-07 21:46:02 -07004936 /* Need to make the tx_cons update visible to tg3_start_xmit()
4937 * before checking for netif_queue_stopped(). Without the
4938 * memory barrier, there is a small possibility that tg3_start_xmit()
4939 * will miss it and cause the queue to be stopped forever.
4940 */
4941 smp_mb();
4942
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004943 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004944 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004945 __netif_tx_lock(txq, smp_processor_id());
4946 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004947 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004948 netif_tx_wake_queue(txq);
4949 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951}
4952
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004953static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4954{
4955 if (!ri->skb)
4956 return;
4957
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004958 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004959 map_sz, PCI_DMA_FROMDEVICE);
4960 dev_kfree_skb_any(ri->skb);
4961 ri->skb = NULL;
4962}
4963
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964/* Returns size of skb allocated or < 0 on error.
4965 *
4966 * We only need to fill in the address because the other members
4967 * of the RX descriptor are invariant, see tg3_init_rings.
4968 *
4969 * Note the purposeful assymetry of cpu vs. chip accesses. For
4970 * posting buffers we only dirty the first cache line of the RX
4971 * descriptor (containing the address). Whereas for the RX status
4972 * buffers the cpu only reads the last cacheline of the RX descriptor
4973 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4974 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004975static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004976 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977{
4978 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004979 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 struct sk_buff *skb;
4981 dma_addr_t mapping;
4982 int skb_size, dest_idx;
4983
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 switch (opaque_key) {
4985 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004986 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004987 desc = &tpr->rx_std[dest_idx];
4988 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004989 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 break;
4991
4992 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004993 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004994 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004995 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004996 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 break;
4998
4999 default:
5000 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
5003 /* Do not overwrite any of the map or rp information
5004 * until we are sure we can commit to a new buffer.
5005 *
5006 * Callers depend upon this behavior and assume that
5007 * we leave everything unchanged if we fail.
5008 */
Matt Carlson81389f52011-08-31 11:44:49 +00005009 skb = netdev_alloc_skb(tp->dev, skb_size + TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 if (skb == NULL)
5011 return -ENOMEM;
5012
Matt Carlson81389f52011-08-31 11:44:49 +00005013 skb_reserve(skb, TG3_RX_OFFSET(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014
Matt Carlson287be122009-08-28 13:58:46 +00005015 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00005017 if (pci_dma_mapping_error(tp->pdev, mapping)) {
5018 dev_kfree_skb(skb);
5019 return -EIO;
5020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
5022 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005023 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 desc->addr_hi = ((u64)mapping >> 32);
5026 desc->addr_lo = ((u64)mapping & 0xffffffff);
5027
5028 return skb_size;
5029}
5030
5031/* We only need to move over in the address because the other
5032 * members of the RX descriptor are invariant. See notes above
5033 * tg3_alloc_rx_skb for full details.
5034 */
Matt Carlsona3896162009-11-13 13:03:44 +00005035static void tg3_recycle_rx(struct tg3_napi *tnapi,
5036 struct tg3_rx_prodring_set *dpr,
5037 u32 opaque_key, int src_idx,
5038 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039{
Matt Carlson17375d22009-08-28 14:02:18 +00005040 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
5042 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005043 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005044 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
5046 switch (opaque_key) {
5047 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00005048 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005049 dest_desc = &dpr->rx_std[dest_idx];
5050 dest_map = &dpr->rx_std_buffers[dest_idx];
5051 src_desc = &spr->rx_std[src_idx];
5052 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 break;
5054
5055 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00005056 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00005057 dest_desc = &dpr->rx_jmb[dest_idx].std;
5058 dest_map = &dpr->rx_jmb_buffers[dest_idx];
5059 src_desc = &spr->rx_jmb[src_idx].std;
5060 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 break;
5062
5063 default:
5064 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07005065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
5067 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005068 dma_unmap_addr_set(dest_map, mapping,
5069 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 dest_desc->addr_hi = src_desc->addr_hi;
5071 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00005072
5073 /* Ensure that the update to the skb happens after the physical
5074 * addresses have been transferred to the new BD location.
5075 */
5076 smp_wmb();
5077
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 src_map->skb = NULL;
5079}
5080
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081/* The RX ring scheme is composed of multiple rings which post fresh
5082 * buffers to the chip, and one special ring the chip uses to report
5083 * status back to the host.
5084 *
5085 * The special ring reports the status of received packets to the
5086 * host. The chip does not write into the original descriptor the
5087 * RX buffer was obtained from. The chip simply takes the original
5088 * descriptor as provided by the host, updates the status and length
5089 * field, then writes this into the next status ring entry.
5090 *
5091 * Each ring the host uses to post buffers to the chip is described
5092 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
5093 * it is first placed into the on-chip ram. When the packet's length
5094 * is known, it walks down the TG3_BDINFO entries to select the ring.
5095 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
5096 * which is within the range of the new packet's length is chosen.
5097 *
5098 * The "separate ring for rx status" scheme may sound queer, but it makes
5099 * sense from a cache coherency perspective. If only the host writes
5100 * to the buffer post rings, and only the chip writes to the rx status
5101 * rings, then cache lines never move beyond shared-modified state.
5102 * If both the host and chip were to write into the same ring, cache line
5103 * eviction could occur since both entities want it in an exclusive state.
5104 */
Matt Carlson17375d22009-08-28 14:02:18 +00005105static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106{
Matt Carlson17375d22009-08-28 14:02:18 +00005107 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07005108 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005109 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00005110 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07005111 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00005113 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005115 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 /*
5117 * We need to order the read of hw_idx and the read of
5118 * the opaque cookie.
5119 */
5120 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 work_mask = 0;
5122 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00005123 std_prod_idx = tpr->rx_std_prod_idx;
5124 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00005126 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00005127 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 unsigned int len;
5129 struct sk_buff *skb;
5130 dma_addr_t dma_addr;
5131 u32 opaque_key, desc_idx, *post_ptr;
5132
5133 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
5134 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
5135 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005136 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005137 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00005138 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00005139 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07005140 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005142 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005143 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00005144 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00005145 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00005146 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
5149 work_mask |= opaque_key;
5150
5151 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
5152 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
5153 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00005154 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 desc_idx, *post_ptr);
5156 drop_it_no_recycle:
5157 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00005158 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 goto next_pkt;
5160 }
5161
Matt Carlsonad829262008-11-21 17:16:16 -08005162 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
5163 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
Matt Carlsond2757fc2010-04-12 06:58:27 +00005165 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 int skb_size;
5167
Matt Carlson86b21e52009-11-13 13:03:45 +00005168 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00005169 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 if (skb_size < 0)
5171 goto drop_it;
5172
Matt Carlson287be122009-08-28 13:58:46 +00005173 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 PCI_DMA_FROMDEVICE);
5175
Matt Carlson61e800c2010-02-17 15:16:54 +00005176 /* Ensure that the update to the skb happens
5177 * after the usage of the old DMA mapping.
5178 */
5179 smp_wmb();
5180
5181 ri->skb = NULL;
5182
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 skb_put(skb, len);
5184 } else {
5185 struct sk_buff *copy_skb;
5186
Matt Carlsona3896162009-11-13 13:03:44 +00005187 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 desc_idx, *post_ptr);
5189
Matt Carlsonbf933c82011-01-25 15:58:49 +00005190 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00005191 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 if (copy_skb == NULL)
5193 goto drop_it_no_recycle;
5194
Matt Carlsonbf933c82011-01-25 15:58:49 +00005195 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 skb_put(copy_skb, len);
5197 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03005198 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
5200
5201 /* We'll reuse the original ring buffer. */
5202 skb = copy_skb;
5203 }
5204
Michał Mirosławdc668912011-04-07 03:35:07 +00005205 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5207 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5208 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5209 skb->ip_summed = CHECKSUM_UNNECESSARY;
5210 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005211 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212
5213 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005214
5215 if (len > (tp->dev->mtu + ETH_HLEN) &&
5216 skb->protocol != htons(ETH_P_8021Q)) {
5217 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005218 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005219 }
5220
Matt Carlson9dc7a112010-04-12 06:58:28 +00005221 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005222 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5223 __vlan_hwaccel_put_tag(skb,
5224 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005225
Matt Carlsonbf933c82011-01-25 15:58:49 +00005226 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 received++;
5229 budget--;
5230
5231next_pkt:
5232 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005233
5234 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005235 tpr->rx_std_prod_idx = std_prod_idx &
5236 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005237 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5238 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005239 work_mask &= ~RXD_OPAQUE_RING_STD;
5240 rx_std_posted = 0;
5241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005243 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005244 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005245
5246 /* Refresh hw_idx to see if there is new work */
5247 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005248 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005249 rmb();
5250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 }
5252
5253 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005254 tnapi->rx_rcb_ptr = sw_idx;
5255 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
5257 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005258 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005259 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005260 tpr->rx_std_prod_idx = std_prod_idx &
5261 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005262 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5263 tpr->rx_std_prod_idx);
5264 }
5265 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005266 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5267 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005268 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5269 tpr->rx_jmb_prod_idx);
5270 }
5271 mmiowb();
5272 } else if (work_mask) {
5273 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5274 * updated before the producer indices can be updated.
5275 */
5276 smp_wmb();
5277
Matt Carlson2c49a442010-09-30 10:34:35 +00005278 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5279 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005280
Matt Carlsone4af1af2010-02-12 14:47:05 +00005281 if (tnapi != &tp->napi[1])
5282 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284
5285 return received;
5286}
5287
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005288static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005291 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005292 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5293
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 if (sblk->status & SD_STATUS_LINK_CHG) {
5295 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005296 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005297 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005298 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005299 tw32_f(MAC_STATUS,
5300 (MAC_STATUS_SYNC_CHANGED |
5301 MAC_STATUS_CFG_CHANGED |
5302 MAC_STATUS_MI_COMPLETION |
5303 MAC_STATUS_LNKSTATE_CHANGED));
5304 udelay(40);
5305 } else
5306 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005307 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 }
5309 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005310}
5311
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005312static int tg3_rx_prodring_xfer(struct tg3 *tp,
5313 struct tg3_rx_prodring_set *dpr,
5314 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005315{
5316 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005317 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005318
5319 while (1) {
5320 src_prod_idx = spr->rx_std_prod_idx;
5321
5322 /* Make sure updates to the rx_std_buffers[] entries and the
5323 * standard producer index are seen in the correct order.
5324 */
5325 smp_rmb();
5326
5327 if (spr->rx_std_cons_idx == src_prod_idx)
5328 break;
5329
5330 if (spr->rx_std_cons_idx < src_prod_idx)
5331 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5332 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005333 cpycnt = tp->rx_std_ring_mask + 1 -
5334 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005335
Matt Carlson2c49a442010-09-30 10:34:35 +00005336 cpycnt = min(cpycnt,
5337 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005338
5339 si = spr->rx_std_cons_idx;
5340 di = dpr->rx_std_prod_idx;
5341
Matt Carlsone92967b2010-02-12 14:47:06 +00005342 for (i = di; i < di + cpycnt; i++) {
5343 if (dpr->rx_std_buffers[i].skb) {
5344 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005345 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005346 break;
5347 }
5348 }
5349
5350 if (!cpycnt)
5351 break;
5352
5353 /* Ensure that updates to the rx_std_buffers ring and the
5354 * shadowed hardware producer ring from tg3_recycle_skb() are
5355 * ordered correctly WRT the skb check above.
5356 */
5357 smp_rmb();
5358
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005359 memcpy(&dpr->rx_std_buffers[di],
5360 &spr->rx_std_buffers[si],
5361 cpycnt * sizeof(struct ring_info));
5362
5363 for (i = 0; i < cpycnt; i++, di++, si++) {
5364 struct tg3_rx_buffer_desc *sbd, *dbd;
5365 sbd = &spr->rx_std[si];
5366 dbd = &dpr->rx_std[di];
5367 dbd->addr_hi = sbd->addr_hi;
5368 dbd->addr_lo = sbd->addr_lo;
5369 }
5370
Matt Carlson2c49a442010-09-30 10:34:35 +00005371 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5372 tp->rx_std_ring_mask;
5373 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5374 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005375 }
5376
5377 while (1) {
5378 src_prod_idx = spr->rx_jmb_prod_idx;
5379
5380 /* Make sure updates to the rx_jmb_buffers[] entries and
5381 * the jumbo producer index are seen in the correct order.
5382 */
5383 smp_rmb();
5384
5385 if (spr->rx_jmb_cons_idx == src_prod_idx)
5386 break;
5387
5388 if (spr->rx_jmb_cons_idx < src_prod_idx)
5389 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5390 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005391 cpycnt = tp->rx_jmb_ring_mask + 1 -
5392 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005393
5394 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005395 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005396
5397 si = spr->rx_jmb_cons_idx;
5398 di = dpr->rx_jmb_prod_idx;
5399
Matt Carlsone92967b2010-02-12 14:47:06 +00005400 for (i = di; i < di + cpycnt; i++) {
5401 if (dpr->rx_jmb_buffers[i].skb) {
5402 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005403 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005404 break;
5405 }
5406 }
5407
5408 if (!cpycnt)
5409 break;
5410
5411 /* Ensure that updates to the rx_jmb_buffers ring and the
5412 * shadowed hardware producer ring from tg3_recycle_skb() are
5413 * ordered correctly WRT the skb check above.
5414 */
5415 smp_rmb();
5416
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005417 memcpy(&dpr->rx_jmb_buffers[di],
5418 &spr->rx_jmb_buffers[si],
5419 cpycnt * sizeof(struct ring_info));
5420
5421 for (i = 0; i < cpycnt; i++, di++, si++) {
5422 struct tg3_rx_buffer_desc *sbd, *dbd;
5423 sbd = &spr->rx_jmb[si].std;
5424 dbd = &dpr->rx_jmb[di].std;
5425 dbd->addr_hi = sbd->addr_hi;
5426 dbd->addr_lo = sbd->addr_lo;
5427 }
5428
Matt Carlson2c49a442010-09-30 10:34:35 +00005429 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5430 tp->rx_jmb_ring_mask;
5431 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5432 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005433 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005434
5435 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005436}
5437
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005438static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5439{
5440 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441
5442 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005443 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005444 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005445 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005446 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 }
5448
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 /* run RX thread, within the bounds set by NAPI.
5450 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005451 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005453 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005454 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
Joe Perches63c3a662011-04-26 08:12:10 +00005456 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005457 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005458 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005459 u32 std_prod_idx = dpr->rx_std_prod_idx;
5460 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005461
Matt Carlsone4af1af2010-02-12 14:47:05 +00005462 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005463 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005464 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005465
5466 wmb();
5467
Matt Carlsone4af1af2010-02-12 14:47:05 +00005468 if (std_prod_idx != dpr->rx_std_prod_idx)
5469 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5470 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005471
Matt Carlsone4af1af2010-02-12 14:47:05 +00005472 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5473 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5474 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005475
5476 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005477
5478 if (err)
5479 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005480 }
5481
David S. Miller6f535762007-10-11 18:08:29 -07005482 return work_done;
5483}
David S. Millerf7383c22005-05-18 22:50:53 -07005484
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005485static int tg3_poll_msix(struct napi_struct *napi, int budget)
5486{
5487 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5488 struct tg3 *tp = tnapi->tp;
5489 int work_done = 0;
5490 struct tg3_hw_status *sblk = tnapi->hw_status;
5491
5492 while (1) {
5493 work_done = tg3_poll_work(tnapi, work_done, budget);
5494
Joe Perches63c3a662011-04-26 08:12:10 +00005495 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005496 goto tx_recovery;
5497
5498 if (unlikely(work_done >= budget))
5499 break;
5500
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005501 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005502 * to tell the hw how much work has been processed,
5503 * so we must read it before checking for more work.
5504 */
5505 tnapi->last_tag = sblk->status_tag;
5506 tnapi->last_irq_tag = tnapi->last_tag;
5507 rmb();
5508
5509 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005510 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5511 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005512 napi_complete(napi);
5513 /* Reenable interrupts. */
5514 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5515 mmiowb();
5516 break;
5517 }
5518 }
5519
5520 return work_done;
5521
5522tx_recovery:
5523 /* work_done is guaranteed to be less than budget. */
5524 napi_complete(napi);
5525 schedule_work(&tp->reset_task);
5526 return work_done;
5527}
5528
Matt Carlsone64de4e2011-04-13 11:05:05 +00005529static void tg3_process_error(struct tg3 *tp)
5530{
5531 u32 val;
5532 bool real_error = false;
5533
Joe Perches63c3a662011-04-26 08:12:10 +00005534 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005535 return;
5536
5537 /* Check Flow Attention register */
5538 val = tr32(HOSTCC_FLOW_ATTN);
5539 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5540 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5541 real_error = true;
5542 }
5543
5544 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5545 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5546 real_error = true;
5547 }
5548
5549 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5550 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5551 real_error = true;
5552 }
5553
5554 if (!real_error)
5555 return;
5556
5557 tg3_dump_state(tp);
5558
Joe Perches63c3a662011-04-26 08:12:10 +00005559 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005560 schedule_work(&tp->reset_task);
5561}
5562
David S. Miller6f535762007-10-11 18:08:29 -07005563static int tg3_poll(struct napi_struct *napi, int budget)
5564{
Matt Carlson8ef04422009-08-28 14:01:37 +00005565 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5566 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005567 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005568 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005569
5570 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005571 if (sblk->status & SD_STATUS_ERROR)
5572 tg3_process_error(tp);
5573
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005574 tg3_poll_link(tp);
5575
Matt Carlson17375d22009-08-28 14:02:18 +00005576 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005577
Joe Perches63c3a662011-04-26 08:12:10 +00005578 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07005579 goto tx_recovery;
5580
5581 if (unlikely(work_done >= budget))
5582 break;
5583
Joe Perches63c3a662011-04-26 08:12:10 +00005584 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00005585 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005586 * to tell the hw how much work has been processed,
5587 * so we must read it before checking for more work.
5588 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005589 tnapi->last_tag = sblk->status_tag;
5590 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005591 rmb();
5592 } else
5593 sblk->status &= ~SD_STATUS_UPDATED;
5594
Matt Carlson17375d22009-08-28 14:02:18 +00005595 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005596 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005597 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005598 break;
5599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 }
5601
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005602 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005603
5604tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005605 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005606 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005607 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005608 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609}
5610
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005611static void tg3_napi_disable(struct tg3 *tp)
5612{
5613 int i;
5614
5615 for (i = tp->irq_cnt - 1; i >= 0; i--)
5616 napi_disable(&tp->napi[i].napi);
5617}
5618
5619static void tg3_napi_enable(struct tg3 *tp)
5620{
5621 int i;
5622
5623 for (i = 0; i < tp->irq_cnt; i++)
5624 napi_enable(&tp->napi[i].napi);
5625}
5626
5627static void tg3_napi_init(struct tg3 *tp)
5628{
5629 int i;
5630
5631 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5632 for (i = 1; i < tp->irq_cnt; i++)
5633 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5634}
5635
5636static void tg3_napi_fini(struct tg3 *tp)
5637{
5638 int i;
5639
5640 for (i = 0; i < tp->irq_cnt; i++)
5641 netif_napi_del(&tp->napi[i].napi);
5642}
5643
5644static inline void tg3_netif_stop(struct tg3 *tp)
5645{
5646 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5647 tg3_napi_disable(tp);
5648 netif_tx_disable(tp->dev);
5649}
5650
5651static inline void tg3_netif_start(struct tg3 *tp)
5652{
5653 /* NOTE: unconditional netif_tx_wake_all_queues is only
5654 * appropriate so long as all callers are assured to
5655 * have free tx slots (such as after tg3_init_hw)
5656 */
5657 netif_tx_wake_all_queues(tp->dev);
5658
5659 tg3_napi_enable(tp);
5660 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5661 tg3_enable_ints(tp);
5662}
5663
David S. Millerf47c11e2005-06-24 20:18:35 -07005664static void tg3_irq_quiesce(struct tg3 *tp)
5665{
Matt Carlson4f125f42009-09-01 12:55:02 +00005666 int i;
5667
David S. Millerf47c11e2005-06-24 20:18:35 -07005668 BUG_ON(tp->irq_sync);
5669
5670 tp->irq_sync = 1;
5671 smp_mb();
5672
Matt Carlson4f125f42009-09-01 12:55:02 +00005673 for (i = 0; i < tp->irq_cnt; i++)
5674 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005675}
5676
David S. Millerf47c11e2005-06-24 20:18:35 -07005677/* Fully shutdown all tg3 driver activity elsewhere in the system.
5678 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5679 * with as well. Most of the time, this is not necessary except when
5680 * shutting down the device.
5681 */
5682static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5683{
Michael Chan46966542007-07-11 19:47:19 -07005684 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005685 if (irq_sync)
5686 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005687}
5688
5689static inline void tg3_full_unlock(struct tg3 *tp)
5690{
David S. Millerf47c11e2005-06-24 20:18:35 -07005691 spin_unlock_bh(&tp->lock);
5692}
5693
Michael Chanfcfa0a32006-03-20 22:28:41 -08005694/* One-shot MSI handler - Chip automatically disables interrupt
5695 * after sending MSI so driver doesn't have to do it.
5696 */
David Howells7d12e782006-10-05 14:55:46 +01005697static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005698{
Matt Carlson09943a12009-08-28 14:01:57 +00005699 struct tg3_napi *tnapi = dev_id;
5700 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005701
Matt Carlson898a56f2009-08-28 14:02:40 +00005702 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005703 if (tnapi->rx_rcb)
5704 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005705
5706 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005707 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005708
5709 return IRQ_HANDLED;
5710}
5711
Michael Chan88b06bc2005-04-21 17:13:25 -07005712/* MSI ISR - No need to check for interrupt sharing and no need to
5713 * flush status block and interrupt mailbox. PCI ordering rules
5714 * guarantee that MSI will arrive after the status block.
5715 */
David Howells7d12e782006-10-05 14:55:46 +01005716static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07005717{
Matt Carlson09943a12009-08-28 14:01:57 +00005718 struct tg3_napi *tnapi = dev_id;
5719 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07005720
Matt Carlson898a56f2009-08-28 14:02:40 +00005721 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005722 if (tnapi->rx_rcb)
5723 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07005724 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005725 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07005726 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005727 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07005728 * NIC to stop sending us irqs, engaging "in-intr-handler"
5729 * event coalescing.
5730 */
Matt Carlson5b39de92011-08-31 11:44:50 +00005731 tw32_mailbox(tnapi->int_mbox, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005732 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005733 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005734
Michael Chan88b06bc2005-04-21 17:13:25 -07005735 return IRQ_RETVAL(1);
5736}
5737
David Howells7d12e782006-10-05 14:55:46 +01005738static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739{
Matt Carlson09943a12009-08-28 14:01:57 +00005740 struct tg3_napi *tnapi = dev_id;
5741 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005742 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 unsigned int handled = 1;
5744
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 /* In INTx mode, it is possible for the interrupt to arrive at
5746 * the CPU before the status block posted prior to the interrupt.
5747 * Reading the PCI State register will confirm whether the
5748 * interrupt is ours and will flush the status block.
5749 */
Michael Chand18edcb2007-03-24 20:57:11 -07005750 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00005751 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005752 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5753 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005754 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005755 }
Michael Chand18edcb2007-03-24 20:57:11 -07005756 }
5757
5758 /*
5759 * Writing any value to intr-mbox-0 clears PCI INTA# and
5760 * chip-internal interrupt pending events.
5761 * Writing non-zero to intr-mbox-0 additional tells the
5762 * NIC to stop sending us irqs, engaging "in-intr-handler"
5763 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005764 *
5765 * Flush the mailbox to de-assert the IRQ immediately to prevent
5766 * spurious interrupts. The flush impacts performance but
5767 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005768 */
Michael Chanc04cb342007-05-07 00:26:15 -07005769 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005770 if (tg3_irq_sync(tp))
5771 goto out;
5772 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005773 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005774 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005775 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005776 } else {
5777 /* No work, shared interrupt perhaps? re-enable
5778 * interrupts, and flush that PCI write
5779 */
5780 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5781 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005782 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005783out:
David S. Millerfac9b832005-05-18 22:46:34 -07005784 return IRQ_RETVAL(handled);
5785}
5786
David Howells7d12e782006-10-05 14:55:46 +01005787static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005788{
Matt Carlson09943a12009-08-28 14:01:57 +00005789 struct tg3_napi *tnapi = dev_id;
5790 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005791 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005792 unsigned int handled = 1;
5793
David S. Millerfac9b832005-05-18 22:46:34 -07005794 /* In INTx mode, it is possible for the interrupt to arrive at
5795 * the CPU before the status block posted prior to the interrupt.
5796 * Reading the PCI State register will confirm whether the
5797 * interrupt is ours and will flush the status block.
5798 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005799 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00005800 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005801 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5802 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005803 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 }
Michael Chand18edcb2007-03-24 20:57:11 -07005805 }
5806
5807 /*
5808 * writing any value to intr-mbox-0 clears PCI INTA# and
5809 * chip-internal interrupt pending events.
5810 * writing non-zero to intr-mbox-0 additional tells the
5811 * NIC to stop sending us irqs, engaging "in-intr-handler"
5812 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005813 *
5814 * Flush the mailbox to de-assert the IRQ immediately to prevent
5815 * spurious interrupts. The flush impacts performance but
5816 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005817 */
Michael Chanc04cb342007-05-07 00:26:15 -07005818 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005819
5820 /*
5821 * In a shared interrupt configuration, sometimes other devices'
5822 * interrupts will scream. We record the current status tag here
5823 * so that the above check can report that the screaming interrupts
5824 * are unhandled. Eventually they will be silenced.
5825 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005826 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005827
Michael Chand18edcb2007-03-24 20:57:11 -07005828 if (tg3_irq_sync(tp))
5829 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005830
Matt Carlson72334482009-08-28 14:03:01 +00005831 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005832
Matt Carlson09943a12009-08-28 14:01:57 +00005833 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005834
David S. Millerf47c11e2005-06-24 20:18:35 -07005835out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 return IRQ_RETVAL(handled);
5837}
5838
Michael Chan79381092005-04-21 17:13:59 -07005839/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005840static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005841{
Matt Carlson09943a12009-08-28 14:01:57 +00005842 struct tg3_napi *tnapi = dev_id;
5843 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005844 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005845
Michael Chanf9804dd2005-09-27 12:13:10 -07005846 if ((sblk->status & SD_STATUS_UPDATED) ||
5847 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005848 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005849 return IRQ_RETVAL(1);
5850 }
5851 return IRQ_RETVAL(0);
5852}
5853
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005854static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005855static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
Michael Chanb9ec6c12006-07-25 16:37:27 -07005857/* Restart hardware after configuration changes, self-test, etc.
5858 * Invoked with tp->lock held.
5859 */
5860static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005861 __releases(tp->lock)
5862 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005863{
5864 int err;
5865
5866 err = tg3_init_hw(tp, reset_phy);
5867 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005868 netdev_err(tp->dev,
5869 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005870 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5871 tg3_full_unlock(tp);
5872 del_timer_sync(&tp->timer);
5873 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005874 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005875 dev_close(tp->dev);
5876 tg3_full_lock(tp, 0);
5877 }
5878 return err;
5879}
5880
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881#ifdef CONFIG_NET_POLL_CONTROLLER
5882static void tg3_poll_controller(struct net_device *dev)
5883{
Matt Carlson4f125f42009-09-01 12:55:02 +00005884 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07005885 struct tg3 *tp = netdev_priv(dev);
5886
Matt Carlson4f125f42009-09-01 12:55:02 +00005887 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005888 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889}
5890#endif
5891
David Howellsc4028952006-11-22 14:57:56 +00005892static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893{
David Howellsc4028952006-11-22 14:57:56 +00005894 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005895 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 unsigned int restart_timer;
5897
Michael Chan7faa0062006-02-02 17:29:28 -08005898 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005899
5900 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005901 tg3_full_unlock(tp);
5902 return;
5903 }
5904
5905 tg3_full_unlock(tp);
5906
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005907 tg3_phy_stop(tp);
5908
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 tg3_netif_stop(tp);
5910
David S. Millerf47c11e2005-06-24 20:18:35 -07005911 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912
Joe Perches63c3a662011-04-26 08:12:10 +00005913 restart_timer = tg3_flag(tp, RESTART_TIMER);
5914 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
Joe Perches63c3a662011-04-26 08:12:10 +00005916 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07005917 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5918 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00005919 tg3_flag_set(tp, MBOX_WRITE_REORDER);
5920 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005921 }
5922
Michael Chan944d9802005-05-29 14:57:48 -07005923 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005924 err = tg3_init_hw(tp, 1);
5925 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005926 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927
5928 tg3_netif_start(tp);
5929
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 if (restart_timer)
5931 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005932
Michael Chanb9ec6c12006-07-25 16:37:27 -07005933out:
Michael Chan7faa0062006-02-02 17:29:28 -08005934 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005935
5936 if (!err)
5937 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938}
5939
5940static void tg3_tx_timeout(struct net_device *dev)
5941{
5942 struct tg3 *tp = netdev_priv(dev);
5943
Michael Chanb0408752007-02-13 12:18:30 -08005944 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005945 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005946 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948
5949 schedule_work(&tp->reset_task);
5950}
5951
Michael Chanc58ec932005-09-17 00:46:27 -07005952/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5953static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5954{
5955 u32 base = (u32) mapping & 0xffffffff;
5956
Eric Dumazet807540b2010-09-23 05:40:09 +00005957 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005958}
5959
Michael Chan72f2afb2006-03-06 19:28:35 -08005960/* Test for DMA addresses > 40-bit */
5961static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5962 int len)
5963{
5964#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00005965 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00005966 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005967 return 0;
5968#else
5969 return 0;
5970#endif
5971}
5972
Matt Carlsond1a3b732011-07-27 14:20:51 +00005973static inline void tg3_tx_set_bd(struct tg3_tx_buffer_desc *txbd,
Matt Carlson92cd3a12011-07-27 14:20:47 +00005974 dma_addr_t mapping, u32 len, u32 flags,
5975 u32 mss, u32 vlan)
Matt Carlson2ffcc982011-05-19 12:12:44 +00005976{
Matt Carlson92cd3a12011-07-27 14:20:47 +00005977 txbd->addr_hi = ((u64) mapping >> 32);
5978 txbd->addr_lo = ((u64) mapping & 0xffffffff);
5979 txbd->len_flags = (len << TXD_LEN_SHIFT) | (flags & 0x0000ffff);
5980 txbd->vlan_tag = (mss << TXD_MSS_SHIFT) | (vlan << TXD_VLAN_TAG_SHIFT);
Matt Carlson2ffcc982011-05-19 12:12:44 +00005981}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
Matt Carlson84b67b22011-07-27 14:20:52 +00005983static bool tg3_tx_frag_set(struct tg3_napi *tnapi, u32 *entry, u32 *budget,
Matt Carlsond1a3b732011-07-27 14:20:51 +00005984 dma_addr_t map, u32 len, u32 flags,
5985 u32 mss, u32 vlan)
5986{
5987 struct tg3 *tp = tnapi->tp;
5988 bool hwbug = false;
5989
5990 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
5991 hwbug = 1;
5992
5993 if (tg3_4g_overflow_test(map, len))
5994 hwbug = 1;
5995
5996 if (tg3_40bit_overflow_test(tp, map, len))
5997 hwbug = 1;
5998
Matt Carlsone31aa982011-07-27 14:20:53 +00005999 if (tg3_flag(tp, 4K_FIFO_LIMIT)) {
6000 u32 tmp_flag = flags & ~TXD_FLAG_END;
6001 while (len > TG3_TX_BD_DMA_MAX) {
6002 u32 frag_len = TG3_TX_BD_DMA_MAX;
6003 len -= TG3_TX_BD_DMA_MAX;
6004
6005 if (len) {
6006 tnapi->tx_buffers[*entry].fragmented = true;
6007 /* Avoid the 8byte DMA problem */
6008 if (len <= 8) {
6009 len += TG3_TX_BD_DMA_MAX / 2;
6010 frag_len = TG3_TX_BD_DMA_MAX / 2;
6011 }
6012 } else
6013 tmp_flag = flags;
6014
6015 if (*budget) {
6016 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6017 frag_len, tmp_flag, mss, vlan);
6018 (*budget)--;
6019 *entry = NEXT_TX(*entry);
6020 } else {
6021 hwbug = 1;
6022 break;
6023 }
6024
6025 map += frag_len;
6026 }
6027
6028 if (len) {
6029 if (*budget) {
6030 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6031 len, flags, mss, vlan);
6032 (*budget)--;
6033 *entry = NEXT_TX(*entry);
6034 } else {
6035 hwbug = 1;
6036 }
6037 }
6038 } else {
Matt Carlson84b67b22011-07-27 14:20:52 +00006039 tg3_tx_set_bd(&tnapi->tx_ring[*entry], map,
6040 len, flags, mss, vlan);
Matt Carlsone31aa982011-07-27 14:20:53 +00006041 *entry = NEXT_TX(*entry);
6042 }
Matt Carlsond1a3b732011-07-27 14:20:51 +00006043
6044 return hwbug;
6045}
6046
Matt Carlson0d681b22011-07-27 14:20:49 +00006047static void tg3_tx_skb_unmap(struct tg3_napi *tnapi, u32 entry, int last)
Matt Carlson432aa7e2011-05-19 12:12:45 +00006048{
6049 int i;
Matt Carlson0d681b22011-07-27 14:20:49 +00006050 struct sk_buff *skb;
Matt Carlsondf8944c2011-07-27 14:20:46 +00006051 struct tg3_tx_ring_info *txb = &tnapi->tx_buffers[entry];
Matt Carlson432aa7e2011-05-19 12:12:45 +00006052
Matt Carlson0d681b22011-07-27 14:20:49 +00006053 skb = txb->skb;
6054 txb->skb = NULL;
6055
Matt Carlson432aa7e2011-05-19 12:12:45 +00006056 pci_unmap_single(tnapi->tp->pdev,
6057 dma_unmap_addr(txb, mapping),
6058 skb_headlen(skb),
6059 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 }
6066
Matt Carlson9a2e0fb2011-06-02 13:01:39 +00006067 for (i = 0; i < last; i++) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00006068 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6069
6070 entry = NEXT_TX(entry);
6071 txb = &tnapi->tx_buffers[entry];
6072
6073 pci_unmap_page(tnapi->tp->pdev,
6074 dma_unmap_addr(txb, mapping),
6075 frag->size, PCI_DMA_TODEVICE);
Matt Carlsone01ee142011-07-27 14:20:50 +00006076
6077 while (txb->fragmented) {
6078 txb->fragmented = false;
6079 entry = NEXT_TX(entry);
6080 txb = &tnapi->tx_buffers[entry];
6081 }
Matt Carlson432aa7e2011-05-19 12:12:45 +00006082 }
6083}
6084
Michael Chan72f2afb2006-03-06 19:28:35 -08006085/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006086static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
Matt Carlson432aa7e2011-05-19 12:12:45 +00006087 struct sk_buff *skb,
Matt Carlson84b67b22011-07-27 14:20:52 +00006088 u32 *entry, u32 *budget,
Matt Carlson92cd3a12011-07-27 14:20:47 +00006089 u32 base_flags, u32 mss, u32 vlan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090{
Matt Carlson24f4efd2009-11-13 13:03:35 +00006091 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07006092 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07006093 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006094 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095
Matt Carlson41588ba2008-04-19 18:12:33 -07006096 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
6097 new_skb = skb_copy(skb, GFP_ATOMIC);
6098 else {
6099 int more_headroom = 4 - ((unsigned long)skb->data & 3);
6100
6101 new_skb = skb_copy_expand(skb,
6102 skb_headroom(skb) + more_headroom,
6103 skb_tailroom(skb), GFP_ATOMIC);
6104 }
6105
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07006107 ret = -1;
6108 } else {
6109 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00006110 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
6111 PCI_DMA_TODEVICE);
6112 /* Make sure the mapping succeeded */
6113 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006114 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07006115 ret = -1;
Michael Chanc58ec932005-09-17 00:46:27 -07006116 } else {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006117 base_flags |= TXD_FLAG_END;
6118
Matt Carlson84b67b22011-07-27 14:20:52 +00006119 tnapi->tx_buffers[*entry].skb = new_skb;
6120 dma_unmap_addr_set(&tnapi->tx_buffers[*entry],
Matt Carlson432aa7e2011-05-19 12:12:45 +00006121 mapping, new_addr);
6122
Matt Carlson84b67b22011-07-27 14:20:52 +00006123 if (tg3_tx_frag_set(tnapi, entry, budget, new_addr,
Matt Carlsond1a3b732011-07-27 14:20:51 +00006124 new_skb->len, base_flags,
6125 mss, vlan)) {
Matt Carlson84b67b22011-07-27 14:20:52 +00006126 tg3_tx_skb_unmap(tnapi, *entry, 0);
Matt Carlsond1a3b732011-07-27 14:20:51 +00006127 dev_kfree_skb(new_skb);
6128 ret = -1;
6129 }
Michael Chanc58ec932005-09-17 00:46:27 -07006130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 }
6132
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 dev_kfree_skb(skb);
6134
Michael Chanc58ec932005-09-17 00:46:27 -07006135 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136}
6137
Matt Carlson2ffcc982011-05-19 12:12:44 +00006138static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07006139
6140/* Use GSO to workaround a rare TSO bug that may be triggered when the
6141 * TSO header is greater than 80 bytes.
6142 */
6143static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
6144{
6145 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006146 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07006147
6148 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006149 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07006150 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006151
6152 /* netif_tx_stop_queue() must be done before checking
6153 * checking tx index in tg3_tx_avail() below, because in
6154 * tg3_tx(), we update tx index before checking for
6155 * netif_tx_queue_stopped().
6156 */
6157 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006158 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006159 return NETDEV_TX_BUSY;
6160
6161 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006162 }
6163
6164 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006165 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006166 goto tg3_tso_bug_end;
6167
6168 do {
6169 nskb = segs;
6170 segs = segs->next;
6171 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00006172 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006173 } while (segs);
6174
6175tg3_tso_bug_end:
6176 dev_kfree_skb(skb);
6177
6178 return NETDEV_TX_OK;
6179}
Michael Chan52c0fd82006-06-29 20:15:54 -07006180
Michael Chan5a6f3072006-03-20 22:28:05 -08006181/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00006182 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08006183 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00006184static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006185{
6186 struct tg3 *tp = netdev_priv(dev);
Matt Carlson92cd3a12011-07-27 14:20:47 +00006187 u32 len, entry, base_flags, mss, vlan = 0;
Matt Carlson84b67b22011-07-27 14:20:52 +00006188 u32 budget;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006189 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006190 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006191 struct tg3_napi *tnapi;
6192 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00006193 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006194
Matt Carlson24f4efd2009-11-13 13:03:35 +00006195 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6196 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00006197 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006198 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199
Matt Carlson84b67b22011-07-27 14:20:52 +00006200 budget = tg3_tx_avail(tnapi);
6201
Michael Chan00b70502006-06-17 21:58:45 -07006202 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006203 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006204 * interrupt. Furthermore, IRQ processing runs lockless so we have
6205 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006207 if (unlikely(budget <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006208 if (!netif_tx_queue_stopped(txq)) {
6209 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006210
6211 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006212 netdev_err(dev,
6213 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 return NETDEV_TX_BUSY;
6216 }
6217
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006218 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006220 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006222
Matt Carlsonbe98da62010-07-11 09:31:46 +00006223 mss = skb_shinfo(skb)->gso_size;
6224 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006225 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006226 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227
6228 if (skb_header_cloned(skb) &&
6229 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
6230 dev_kfree_skb(skb);
6231 goto out_unlock;
6232 }
6233
Matt Carlson34195c32010-07-11 09:31:42 +00006234 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006235 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236
Matt Carlson02e96082010-09-15 08:59:59 +00006237 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006238 hdr_len = skb_headlen(skb) - ETH_HLEN;
6239 } else {
6240 u32 ip_tcp_len;
6241
6242 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6243 hdr_len = ip_tcp_len + tcp_opt_len;
6244
6245 iph->check = 0;
6246 iph->tot_len = htons(mss + hdr_len);
6247 }
6248
Michael Chan52c0fd82006-06-29 20:15:54 -07006249 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006250 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006251 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006252
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6254 TXD_FLAG_CPU_POST_DMA);
6255
Joe Perches63c3a662011-04-26 08:12:10 +00006256 if (tg3_flag(tp, HW_TSO_1) ||
6257 tg3_flag(tp, HW_TSO_2) ||
6258 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006259 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006261 } else
6262 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6263 iph->daddr, 0,
6264 IPPROTO_TCP,
6265 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266
Joe Perches63c3a662011-04-26 08:12:10 +00006267 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006268 mss |= (hdr_len & 0xc) << 12;
6269 if (hdr_len & 0x10)
6270 base_flags |= 0x00000010;
6271 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006272 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006273 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006274 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006275 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006276 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 int tsflags;
6278
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006279 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 mss |= (tsflags << 11);
6281 }
6282 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006283 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 int tsflags;
6285
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006286 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 base_flags |= tsflags << 12;
6288 }
6289 }
6290 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006291
Matt Carlson92cd3a12011-07-27 14:20:47 +00006292#ifdef BCM_KERNEL_SUPPORTS_8021Q
6293 if (vlan_tx_tag_present(skb)) {
6294 base_flags |= TXD_FLAG_VLAN;
6295 vlan = vlan_tx_tag_get(skb);
6296 }
6297#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
Joe Perches63c3a662011-04-26 08:12:10 +00006299 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006300 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006301 base_flags |= TXD_FLAG_JMB_PKT;
6302
Alexander Duyckf4188d82009-12-02 16:48:38 +00006303 len = skb_headlen(skb);
6304
6305 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6306 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006307 dev_kfree_skb(skb);
6308 goto out_unlock;
6309 }
6310
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006311 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006312 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313
6314 would_hit_hwbug = 0;
6315
Joe Perches63c3a662011-04-26 08:12:10 +00006316 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006317 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318
Matt Carlson84b67b22011-07-27 14:20:52 +00006319 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping, len, base_flags |
Matt Carlsond1a3b732011-07-27 14:20:51 +00006320 ((skb_shinfo(skb)->nr_frags == 0) ? TXD_FLAG_END : 0),
6321 mss, vlan))
6322 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 /* Now loop through additional data fragments, and queue them. */
6325 if (skb_shinfo(skb)->nr_frags > 0) {
Matt Carlson92cd3a12011-07-27 14:20:47 +00006326 u32 tmp_mss = mss;
6327
6328 if (!tg3_flag(tp, HW_TSO_1) &&
6329 !tg3_flag(tp, HW_TSO_2) &&
6330 !tg3_flag(tp, HW_TSO_3))
6331 tmp_mss = 0;
6332
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 last = skb_shinfo(skb)->nr_frags - 1;
6334 for (i = 0; i <= last; i++) {
6335 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6336
6337 len = frag->size;
Ian Campbelldc234d02011-08-24 22:28:11 +00006338 mapping = skb_frag_dma_map(&tp->pdev->dev, frag, 0,
6339 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006341 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006342 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006343 mapping);
6344 if (pci_dma_mapping_error(tp->pdev, mapping))
6345 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346
Matt Carlson84b67b22011-07-27 14:20:52 +00006347 if (tg3_tx_frag_set(tnapi, &entry, &budget, mapping,
6348 len, base_flags |
6349 ((i == last) ? TXD_FLAG_END : 0),
Matt Carlsond1a3b732011-07-27 14:20:51 +00006350 tmp_mss, vlan))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006351 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 }
6353 }
6354
6355 if (would_hit_hwbug) {
Matt Carlson0d681b22011-07-27 14:20:49 +00006356 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357
6358 /* If the workaround fails due to memory/mapping
6359 * failure, silently drop this packet.
6360 */
Matt Carlson84b67b22011-07-27 14:20:52 +00006361 entry = tnapi->tx_prod;
6362 budget = tg3_tx_avail(tnapi);
6363 if (tigon3_dma_hwbug_workaround(tnapi, skb, &entry, &budget,
6364 base_flags, mss, vlan))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 }
6367
Richard Cochrand515b452011-06-19 03:31:41 +00006368 skb_tx_timestamp(skb);
6369
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006371 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006373 tnapi->tx_prod = entry;
6374 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006375 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006376
6377 /* netif_tx_stop_queue() must be done before checking
6378 * checking tx index in tg3_tx_avail() below, because in
6379 * tg3_tx(), we update tx index before checking for
6380 * netif_tx_queue_stopped().
6381 */
6382 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006383 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006384 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386
6387out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006388 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389
6390 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006391
6392dma_error:
Matt Carlson0d681b22011-07-27 14:20:49 +00006393 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod, i);
Alexander Duyckf4188d82009-12-02 16:48:38 +00006394 dev_kfree_skb(skb);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006395 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006396 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397}
6398
Matt Carlson6e01b202011-08-19 13:58:20 +00006399static void tg3_mac_loopback(struct tg3 *tp, bool enable)
6400{
6401 if (enable) {
6402 tp->mac_mode &= ~(MAC_MODE_HALF_DUPLEX |
6403 MAC_MODE_PORT_MODE_MASK);
6404
6405 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6406
6407 if (!tg3_flag(tp, 5705_PLUS))
6408 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
6409
6410 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
6411 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
6412 else
6413 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
6414 } else {
6415 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6416
6417 if (tg3_flag(tp, 5705_PLUS) ||
6418 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) ||
6419 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
6420 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
6421 }
6422
6423 tw32(MAC_MODE, tp->mac_mode);
6424 udelay(40);
6425}
6426
Matt Carlson941ec902011-08-19 13:58:23 +00006427static int tg3_phy_lpbk_set(struct tg3 *tp, u32 speed, bool extlpbk)
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006428{
Matt Carlson941ec902011-08-19 13:58:23 +00006429 u32 val, bmcr, mac_mode, ptest = 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006430
6431 tg3_phy_toggle_apd(tp, false);
6432 tg3_phy_toggle_automdix(tp, 0);
6433
Matt Carlson941ec902011-08-19 13:58:23 +00006434 if (extlpbk && tg3_phy_set_extloopbk(tp))
6435 return -EIO;
6436
6437 bmcr = BMCR_FULLDPLX;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006438 switch (speed) {
6439 case SPEED_10:
6440 break;
6441 case SPEED_100:
6442 bmcr |= BMCR_SPEED100;
6443 break;
6444 case SPEED_1000:
6445 default:
6446 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
6447 speed = SPEED_100;
6448 bmcr |= BMCR_SPEED100;
6449 } else {
6450 speed = SPEED_1000;
6451 bmcr |= BMCR_SPEED1000;
6452 }
6453 }
6454
Matt Carlson941ec902011-08-19 13:58:23 +00006455 if (extlpbk) {
6456 if (!(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
6457 tg3_readphy(tp, MII_CTRL1000, &val);
6458 val |= CTL1000_AS_MASTER |
6459 CTL1000_ENABLE_MASTER;
6460 tg3_writephy(tp, MII_CTRL1000, val);
6461 } else {
6462 ptest = MII_TG3_FET_PTEST_TRIM_SEL |
6463 MII_TG3_FET_PTEST_TRIM_2;
6464 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest);
6465 }
6466 } else
6467 bmcr |= BMCR_LOOPBACK;
6468
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006469 tg3_writephy(tp, MII_BMCR, bmcr);
6470
6471 /* The write needs to be flushed for the FETs */
6472 if (tp->phy_flags & TG3_PHYFLG_IS_FET)
6473 tg3_readphy(tp, MII_BMCR, &bmcr);
6474
6475 udelay(40);
6476
6477 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
6478 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlson941ec902011-08-19 13:58:23 +00006479 tg3_writephy(tp, MII_TG3_FET_PTEST, ptest |
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006480 MII_TG3_FET_PTEST_FRC_TX_LINK |
6481 MII_TG3_FET_PTEST_FRC_TX_LOCK);
6482
6483 /* The write needs to be flushed for the AC131 */
6484 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
6485 }
6486
6487 /* Reset to prevent losing 1st rx packet intermittently */
6488 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
6489 tg3_flag(tp, 5780_CLASS)) {
6490 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
6491 udelay(10);
6492 tw32_f(MAC_RX_MODE, tp->rx_mode);
6493 }
6494
6495 mac_mode = tp->mac_mode &
6496 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
6497 if (speed == SPEED_1000)
6498 mac_mode |= MAC_MODE_PORT_MODE_GMII;
6499 else
6500 mac_mode |= MAC_MODE_PORT_MODE_MII;
6501
6502 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
6503 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
6504
6505 if (masked_phy_id == TG3_PHY_ID_BCM5401)
6506 mac_mode &= ~MAC_MODE_LINK_POLARITY;
6507 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
6508 mac_mode |= MAC_MODE_LINK_POLARITY;
6509
6510 tg3_writephy(tp, MII_TG3_EXT_CTRL,
6511 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
6512 }
6513
6514 tw32(MAC_MODE, mac_mode);
6515 udelay(40);
Matt Carlson941ec902011-08-19 13:58:23 +00006516
6517 return 0;
Matt Carlson5e5a7f32011-08-19 13:58:21 +00006518}
6519
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006520static void tg3_set_loopback(struct net_device *dev, u32 features)
6521{
6522 struct tg3 *tp = netdev_priv(dev);
6523
6524 if (features & NETIF_F_LOOPBACK) {
6525 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6526 return;
6527
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006528 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006529 tg3_mac_loopback(tp, true);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006530 netif_carrier_on(tp->dev);
6531 spin_unlock_bh(&tp->lock);
6532 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6533 } else {
6534 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6535 return;
6536
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006537 spin_lock_bh(&tp->lock);
Matt Carlson6e01b202011-08-19 13:58:20 +00006538 tg3_mac_loopback(tp, false);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006539 /* Force link status check */
6540 tg3_setup_phy(tp, 1);
6541 spin_unlock_bh(&tp->lock);
6542 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6543 }
6544}
6545
Michał Mirosławdc668912011-04-07 03:35:07 +00006546static u32 tg3_fix_features(struct net_device *dev, u32 features)
6547{
6548 struct tg3 *tp = netdev_priv(dev);
6549
Joe Perches63c3a662011-04-26 08:12:10 +00006550 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006551 features &= ~NETIF_F_ALL_TSO;
6552
6553 return features;
6554}
6555
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006556static int tg3_set_features(struct net_device *dev, u32 features)
6557{
6558 u32 changed = dev->features ^ features;
6559
6560 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6561 tg3_set_loopback(dev, features);
6562
6563 return 0;
6564}
6565
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6567 int new_mtu)
6568{
6569 dev->mtu = new_mtu;
6570
Michael Chanef7f5ec2005-07-25 12:32:25 -07006571 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006572 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006573 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006574 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006575 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006576 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a5882010-04-05 10:19:28 +00006577 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006578 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006579 if (tg3_flag(tp, 5780_CLASS)) {
6580 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00006581 netdev_update_features(dev);
6582 }
Joe Perches63c3a662011-04-26 08:12:10 +00006583 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585}
6586
6587static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6588{
6589 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006590 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591
6592 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6593 return -EINVAL;
6594
6595 if (!netif_running(dev)) {
6596 /* We'll just catch it later when the
6597 * device is up'd.
6598 */
6599 tg3_set_mtu(dev, tp, new_mtu);
6600 return 0;
6601 }
6602
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006603 tg3_phy_stop(tp);
6604
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006606
6607 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608
Michael Chan944d9802005-05-29 14:57:48 -07006609 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610
6611 tg3_set_mtu(dev, tp, new_mtu);
6612
Michael Chanb9ec6c12006-07-25 16:37:27 -07006613 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614
Michael Chanb9ec6c12006-07-25 16:37:27 -07006615 if (!err)
6616 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617
David S. Millerf47c11e2005-06-24 20:18:35 -07006618 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006620 if (!err)
6621 tg3_phy_start(tp);
6622
Michael Chanb9ec6c12006-07-25 16:37:27 -07006623 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624}
6625
Matt Carlson21f581a2009-08-28 14:00:25 +00006626static void tg3_rx_prodring_free(struct tg3 *tp,
6627 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 int i;
6630
Matt Carlson8fea32b2010-09-15 08:59:58 +00006631 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006632 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006633 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006634 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6635 tp->rx_pkt_map_sz);
6636
Joe Perches63c3a662011-04-26 08:12:10 +00006637 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006638 for (i = tpr->rx_jmb_cons_idx;
6639 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006640 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006641 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6642 TG3_RX_JMB_MAP_SZ);
6643 }
6644 }
6645
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006646 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648
Matt Carlson2c49a442010-09-30 10:34:35 +00006649 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006650 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6651 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652
Joe Perches63c3a662011-04-26 08:12:10 +00006653 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006654 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006655 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6656 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 }
6658}
6659
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006660/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661 *
6662 * The chip has been shut down and the driver detached from
6663 * the networking, so no interrupts or new tx packets will
6664 * end up in the driver. tp->{tx,}lock are held and thus
6665 * we may not sleep.
6666 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006667static int tg3_rx_prodring_alloc(struct tg3 *tp,
6668 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669{
Matt Carlson287be122009-08-28 13:58:46 +00006670 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006672 tpr->rx_std_cons_idx = 0;
6673 tpr->rx_std_prod_idx = 0;
6674 tpr->rx_jmb_cons_idx = 0;
6675 tpr->rx_jmb_prod_idx = 0;
6676
Matt Carlson8fea32b2010-09-15 08:59:58 +00006677 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006678 memset(&tpr->rx_std_buffers[0], 0,
6679 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006680 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006681 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006682 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006683 goto done;
6684 }
6685
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006687 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688
Matt Carlson287be122009-08-28 13:58:46 +00006689 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00006690 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006691 tp->dev->mtu > ETH_DATA_LEN)
6692 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6693 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006694
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 /* Initialize invariants of the rings, we only set this
6696 * stuff once. This works because the card does not
6697 * write into the rx buffer posting rings.
6698 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006699 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700 struct tg3_rx_buffer_desc *rxd;
6701
Matt Carlson21f581a2009-08-28 14:00:25 +00006702 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006703 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6705 rxd->opaque = (RXD_OPAQUE_RING_STD |
6706 (i << RXD_OPAQUE_INDEX_SHIFT));
6707 }
6708
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006709 /* Now allocate fresh SKBs for each rx ring. */
6710 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006711 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006712 netdev_warn(tp->dev,
6713 "Using a smaller RX standard ring. Only "
6714 "%d out of %d buffers were allocated "
6715 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006716 if (i == 0)
6717 goto initfail;
6718 tp->rx_pending = i;
6719 break;
6720 }
6721 }
6722
Joe Perches63c3a662011-04-26 08:12:10 +00006723 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006724 goto done;
6725
Matt Carlson2c49a442010-09-30 10:34:35 +00006726 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006727
Joe Perches63c3a662011-04-26 08:12:10 +00006728 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00006729 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730
Matt Carlson2c49a442010-09-30 10:34:35 +00006731 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006732 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733
Matt Carlson0d86df82010-02-17 15:17:00 +00006734 rxd = &tpr->rx_jmb[i].std;
6735 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6736 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6737 RXD_FLAG_JUMBO;
6738 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6739 (i << RXD_OPAQUE_INDEX_SHIFT));
6740 }
6741
6742 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6743 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006744 netdev_warn(tp->dev,
6745 "Using a smaller RX jumbo ring. Only %d "
6746 "out of %d buffers were allocated "
6747 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006748 if (i == 0)
6749 goto initfail;
6750 tp->rx_jumbo_pending = i;
6751 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 }
6753 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006754
6755done:
Michael Chan32d8c572006-07-25 16:38:29 -07006756 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006757
6758initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006759 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006760 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761}
6762
Matt Carlson21f581a2009-08-28 14:00:25 +00006763static void tg3_rx_prodring_fini(struct tg3 *tp,
6764 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765{
Matt Carlson21f581a2009-08-28 14:00:25 +00006766 kfree(tpr->rx_std_buffers);
6767 tpr->rx_std_buffers = NULL;
6768 kfree(tpr->rx_jmb_buffers);
6769 tpr->rx_jmb_buffers = NULL;
6770 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006771 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6772 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006773 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006775 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006776 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6777 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006778 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006780}
6781
Matt Carlson21f581a2009-08-28 14:00:25 +00006782static int tg3_rx_prodring_init(struct tg3 *tp,
6783 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006784{
Matt Carlson2c49a442010-09-30 10:34:35 +00006785 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6786 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006787 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006788 return -ENOMEM;
6789
Matt Carlson4bae65c2010-11-24 08:31:52 +00006790 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6791 TG3_RX_STD_RING_BYTES(tp),
6792 &tpr->rx_std_mapping,
6793 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006794 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006795 goto err_out;
6796
Joe Perches63c3a662011-04-26 08:12:10 +00006797 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006798 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006799 GFP_KERNEL);
6800 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006801 goto err_out;
6802
Matt Carlson4bae65c2010-11-24 08:31:52 +00006803 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6804 TG3_RX_JMB_RING_BYTES(tp),
6805 &tpr->rx_jmb_mapping,
6806 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006807 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006808 goto err_out;
6809 }
6810
6811 return 0;
6812
6813err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006814 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006815 return -ENOMEM;
6816}
6817
6818/* Free up pending packets in all rx/tx rings.
6819 *
6820 * The chip has been shut down and the driver detached from
6821 * the networking, so no interrupts or new tx packets will
6822 * end up in the driver. tp->{tx,}lock is not held and we are not
6823 * in an interrupt context and thus may sleep.
6824 */
6825static void tg3_free_rings(struct tg3 *tp)
6826{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006827 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006828
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006829 for (j = 0; j < tp->irq_cnt; j++) {
6830 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006831
Matt Carlson8fea32b2010-09-15 08:59:58 +00006832 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006833
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006834 if (!tnapi->tx_buffers)
6835 continue;
6836
Matt Carlson0d681b22011-07-27 14:20:49 +00006837 for (i = 0; i < TG3_TX_RING_SIZE; i++) {
6838 struct sk_buff *skb = tnapi->tx_buffers[i].skb;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006839
Matt Carlson0d681b22011-07-27 14:20:49 +00006840 if (!skb)
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006841 continue;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006842
Matt Carlson0d681b22011-07-27 14:20:49 +00006843 tg3_tx_skb_unmap(tnapi, i, skb_shinfo(skb)->nr_frags);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006844
6845 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006846 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006847 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006848}
6849
6850/* Initialize tx/rx rings for packet processing.
6851 *
6852 * The chip has been shut down and the driver detached from
6853 * the networking, so no interrupts or new tx packets will
6854 * end up in the driver. tp->{tx,}lock are held and thus
6855 * we may not sleep.
6856 */
6857static int tg3_init_rings(struct tg3 *tp)
6858{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006859 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006860
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006861 /* Free up all the SKBs. */
6862 tg3_free_rings(tp);
6863
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006864 for (i = 0; i < tp->irq_cnt; i++) {
6865 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006866
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006867 tnapi->last_tag = 0;
6868 tnapi->last_irq_tag = 0;
6869 tnapi->hw_status->status = 0;
6870 tnapi->hw_status->status_tag = 0;
6871 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6872
6873 tnapi->tx_prod = 0;
6874 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006875 if (tnapi->tx_ring)
6876 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006877
6878 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006879 if (tnapi->rx_rcb)
6880 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006881
Matt Carlson8fea32b2010-09-15 08:59:58 +00006882 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006883 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006884 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006885 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006886 }
Matt Carlson72334482009-08-28 14:03:01 +00006887
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006888 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006889}
6890
6891/*
6892 * Must not be invoked with interrupt sources disabled and
6893 * the hardware shutdown down.
6894 */
6895static void tg3_free_consistent(struct tg3 *tp)
6896{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006897 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006898
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006899 for (i = 0; i < tp->irq_cnt; i++) {
6900 struct tg3_napi *tnapi = &tp->napi[i];
6901
6902 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006903 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006904 tnapi->tx_ring, tnapi->tx_desc_mapping);
6905 tnapi->tx_ring = NULL;
6906 }
6907
6908 kfree(tnapi->tx_buffers);
6909 tnapi->tx_buffers = NULL;
6910
6911 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006912 dma_free_coherent(&tp->pdev->dev,
6913 TG3_RX_RCB_RING_BYTES(tp),
6914 tnapi->rx_rcb,
6915 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006916 tnapi->rx_rcb = NULL;
6917 }
6918
Matt Carlson8fea32b2010-09-15 08:59:58 +00006919 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6920
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006921 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006922 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6923 tnapi->hw_status,
6924 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006925 tnapi->hw_status = NULL;
6926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006928
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006930 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6931 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932 tp->hw_stats = NULL;
6933 }
6934}
6935
6936/*
6937 * Must not be invoked with interrupt sources disabled and
6938 * the hardware shutdown down. Can sleep.
6939 */
6940static int tg3_alloc_consistent(struct tg3 *tp)
6941{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006942 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006943
Matt Carlson4bae65c2010-11-24 08:31:52 +00006944 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6945 sizeof(struct tg3_hw_stats),
6946 &tp->stats_mapping,
6947 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 if (!tp->hw_stats)
6949 goto err_out;
6950
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6952
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006953 for (i = 0; i < tp->irq_cnt; i++) {
6954 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006955 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006956
Matt Carlson4bae65c2010-11-24 08:31:52 +00006957 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6958 TG3_HW_STATUS_SIZE,
6959 &tnapi->status_mapping,
6960 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006961 if (!tnapi->hw_status)
6962 goto err_out;
6963
6964 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006965 sblk = tnapi->hw_status;
6966
Matt Carlson8fea32b2010-09-15 08:59:58 +00006967 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6968 goto err_out;
6969
Matt Carlson19cfaec2009-12-03 08:36:20 +00006970 /* If multivector TSS is enabled, vector 0 does not handle
6971 * tx interrupts. Don't allocate any resources for it.
6972 */
Joe Perches63c3a662011-04-26 08:12:10 +00006973 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
6974 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlsondf8944c2011-07-27 14:20:46 +00006975 tnapi->tx_buffers = kzalloc(
6976 sizeof(struct tg3_tx_ring_info) *
6977 TG3_TX_RING_SIZE, GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006978 if (!tnapi->tx_buffers)
6979 goto err_out;
6980
Matt Carlson4bae65c2010-11-24 08:31:52 +00006981 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6982 TG3_TX_RING_BYTES,
6983 &tnapi->tx_desc_mapping,
6984 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006985 if (!tnapi->tx_ring)
6986 goto err_out;
6987 }
6988
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006989 /*
6990 * When RSS is enabled, the status block format changes
6991 * slightly. The "rx_jumbo_consumer", "reserved",
6992 * and "rx_mini_consumer" members get mapped to the
6993 * other three rx return ring producer indexes.
6994 */
6995 switch (i) {
6996 default:
6997 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6998 break;
6999 case 2:
7000 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
7001 break;
7002 case 3:
7003 tnapi->rx_rcb_prod_idx = &sblk->reserved;
7004 break;
7005 case 4:
7006 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
7007 break;
7008 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007009
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007010 /*
7011 * If multivector RSS is enabled, vector 0 does not handle
7012 * rx or tx interrupts. Don't allocate any resources for it.
7013 */
Joe Perches63c3a662011-04-26 08:12:10 +00007014 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00007015 continue;
7016
Matt Carlson4bae65c2010-11-24 08:31:52 +00007017 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
7018 TG3_RX_RCB_RING_BYTES(tp),
7019 &tnapi->rx_rcb_mapping,
7020 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007021 if (!tnapi->rx_rcb)
7022 goto err_out;
7023
7024 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007025 }
7026
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027 return 0;
7028
7029err_out:
7030 tg3_free_consistent(tp);
7031 return -ENOMEM;
7032}
7033
7034#define MAX_WAIT_CNT 1000
7035
7036/* To stop a block, clear the enable bit and poll till it
7037 * clears. tp->lock is held.
7038 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007039static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040{
7041 unsigned int i;
7042 u32 val;
7043
Joe Perches63c3a662011-04-26 08:12:10 +00007044 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 switch (ofs) {
7046 case RCVLSC_MODE:
7047 case DMAC_MODE:
7048 case MBFREE_MODE:
7049 case BUFMGR_MODE:
7050 case MEMARB_MODE:
7051 /* We can't enable/disable these bits of the
7052 * 5705/5750, just say success.
7053 */
7054 return 0;
7055
7056 default:
7057 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 }
7060
7061 val = tr32(ofs);
7062 val &= ~enable_bit;
7063 tw32_f(ofs, val);
7064
7065 for (i = 0; i < MAX_WAIT_CNT; i++) {
7066 udelay(100);
7067 val = tr32(ofs);
7068 if ((val & enable_bit) == 0)
7069 break;
7070 }
7071
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007072 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00007073 dev_err(&tp->pdev->dev,
7074 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
7075 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 return -ENODEV;
7077 }
7078
7079 return 0;
7080}
7081
7082/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007083static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084{
7085 int i, err;
7086
7087 tg3_disable_ints(tp);
7088
7089 tp->rx_mode &= ~RX_MODE_ENABLE;
7090 tw32_f(MAC_RX_MODE, tp->rx_mode);
7091 udelay(10);
7092
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007093 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
7094 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
7095 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
7096 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
7097 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
7098 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007100 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
7101 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
7102 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
7103 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
7104 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
7105 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
7106 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107
7108 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
7109 tw32_f(MAC_MODE, tp->mac_mode);
7110 udelay(40);
7111
7112 tp->tx_mode &= ~TX_MODE_ENABLE;
7113 tw32_f(MAC_TX_MODE, tp->tx_mode);
7114
7115 for (i = 0; i < MAX_WAIT_CNT; i++) {
7116 udelay(100);
7117 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
7118 break;
7119 }
7120 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00007121 dev_err(&tp->pdev->dev,
7122 "%s timed out, TX_MODE_ENABLE will not clear "
7123 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07007124 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125 }
7126
Michael Chane6de8ad2005-05-05 14:42:41 -07007127 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007128 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
7129 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130
7131 tw32(FTQ_RESET, 0xffffffff);
7132 tw32(FTQ_RESET, 0x00000000);
7133
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007134 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
7135 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007137 for (i = 0; i < tp->irq_cnt; i++) {
7138 struct tg3_napi *tnapi = &tp->napi[i];
7139 if (tnapi->hw_status)
7140 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142 if (tp->hw_stats)
7143 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
7144
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145 return err;
7146}
7147
Matt Carlson0d3031d2007-10-10 18:02:43 -07007148static void tg3_ape_send_event(struct tg3 *tp, u32 event)
7149{
7150 int i;
7151 u32 apedata;
7152
Matt Carlsondc6d0742010-09-15 08:59:55 +00007153 /* NCSI does not support APE events */
Joe Perches63c3a662011-04-26 08:12:10 +00007154 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsondc6d0742010-09-15 08:59:55 +00007155 return;
7156
Matt Carlson0d3031d2007-10-10 18:02:43 -07007157 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
7158 if (apedata != APE_SEG_SIG_MAGIC)
7159 return;
7160
7161 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07007162 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007163 return;
7164
7165 /* Wait for up to 1 millisecond for APE to service previous event. */
7166 for (i = 0; i < 10; i++) {
7167 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
7168 return;
7169
7170 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
7171
7172 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
7173 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
7174 event | APE_EVENT_STATUS_EVENT_PENDING);
7175
7176 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
7177
7178 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
7179 break;
7180
7181 udelay(100);
7182 }
7183
7184 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
7185 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
7186}
7187
7188static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
7189{
7190 u32 event;
7191 u32 apedata;
7192
Joe Perches63c3a662011-04-26 08:12:10 +00007193 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007194 return;
7195
7196 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00007197 case RESET_KIND_INIT:
7198 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
7199 APE_HOST_SEG_SIG_MAGIC);
7200 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
7201 APE_HOST_SEG_LEN_MAGIC);
7202 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
7203 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
7204 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00007205 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00007206 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
7207 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00007208 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
7209 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07007210
Matt Carlson33f401a2010-04-05 10:19:27 +00007211 event = APE_EVENT_STATUS_STATE_START;
7212 break;
7213 case RESET_KIND_SHUTDOWN:
7214 /* With the interface we are currently using,
7215 * APE does not track driver state. Wiping
7216 * out the HOST SEGMENT SIGNATURE forces
7217 * the APE to assume OS absent status.
7218 */
7219 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08007220
Matt Carlsondc6d0742010-09-15 08:59:55 +00007221 if (device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00007222 tg3_flag(tp, WOL_ENABLE)) {
Matt Carlsondc6d0742010-09-15 08:59:55 +00007223 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
7224 TG3_APE_HOST_WOL_SPEED_AUTO);
7225 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
7226 } else
7227 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
7228
7229 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
7230
Matt Carlson33f401a2010-04-05 10:19:27 +00007231 event = APE_EVENT_STATUS_STATE_UNLOAD;
7232 break;
7233 case RESET_KIND_SUSPEND:
7234 event = APE_EVENT_STATUS_STATE_SUSPEND;
7235 break;
7236 default:
7237 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007238 }
7239
7240 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
7241
7242 tg3_ape_send_event(tp, event);
7243}
7244
Michael Chane6af3012005-04-21 17:12:05 -07007245/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
7247{
David S. Millerf49639e2006-06-09 11:58:36 -07007248 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
7249 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250
Joe Perches63c3a662011-04-26 08:12:10 +00007251 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 switch (kind) {
7253 case RESET_KIND_INIT:
7254 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7255 DRV_STATE_START);
7256 break;
7257
7258 case RESET_KIND_SHUTDOWN:
7259 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7260 DRV_STATE_UNLOAD);
7261 break;
7262
7263 case RESET_KIND_SUSPEND:
7264 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7265 DRV_STATE_SUSPEND);
7266 break;
7267
7268 default:
7269 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007272
7273 if (kind == RESET_KIND_INIT ||
7274 kind == RESET_KIND_SUSPEND)
7275 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276}
7277
7278/* tp->lock is held. */
7279static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
7280{
Joe Perches63c3a662011-04-26 08:12:10 +00007281 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 switch (kind) {
7283 case RESET_KIND_INIT:
7284 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7285 DRV_STATE_START_DONE);
7286 break;
7287
7288 case RESET_KIND_SHUTDOWN:
7289 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7290 DRV_STATE_UNLOAD_DONE);
7291 break;
7292
7293 default:
7294 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007296 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007297
7298 if (kind == RESET_KIND_SHUTDOWN)
7299 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300}
7301
7302/* tp->lock is held. */
7303static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
7304{
Joe Perches63c3a662011-04-26 08:12:10 +00007305 if (tg3_flag(tp, ENABLE_ASF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306 switch (kind) {
7307 case RESET_KIND_INIT:
7308 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7309 DRV_STATE_START);
7310 break;
7311
7312 case RESET_KIND_SHUTDOWN:
7313 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7314 DRV_STATE_UNLOAD);
7315 break;
7316
7317 case RESET_KIND_SUSPEND:
7318 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7319 DRV_STATE_SUSPEND);
7320 break;
7321
7322 default:
7323 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325 }
7326}
7327
Michael Chan7a6f4362006-09-27 16:03:31 -07007328static int tg3_poll_fw(struct tg3 *tp)
7329{
7330 int i;
7331 u32 val;
7332
Michael Chanb5d37722006-09-27 16:06:21 -07007333 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08007334 /* Wait up to 20ms for init done. */
7335 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07007336 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
7337 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08007338 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07007339 }
7340 return -ENODEV;
7341 }
7342
Michael Chan7a6f4362006-09-27 16:03:31 -07007343 /* Wait for firmware initialization to complete. */
7344 for (i = 0; i < 100000; i++) {
7345 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
7346 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
7347 break;
7348 udelay(10);
7349 }
7350
7351 /* Chip might not be fitted with firmware. Some Sun onboard
7352 * parts are configured like that. So don't signal the timeout
7353 * of the above loop as an error, but do report the lack of
7354 * running firmware once.
7355 */
Joe Perches63c3a662011-04-26 08:12:10 +00007356 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
7357 tg3_flag_set(tp, NO_FWARE_REPORTED);
Michael Chan7a6f4362006-09-27 16:03:31 -07007358
Joe Perches05dbe002010-02-17 19:44:19 +00007359 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07007360 }
7361
Matt Carlson6b10c162010-02-12 14:47:08 +00007362 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7363 /* The 57765 A0 needs a little more
7364 * time to do some important work.
7365 */
7366 mdelay(10);
7367 }
7368
Michael Chan7a6f4362006-09-27 16:03:31 -07007369 return 0;
7370}
7371
Michael Chanee6a99b2007-07-18 21:49:10 -07007372/* Save PCI command register before chip reset */
7373static void tg3_save_pci_state(struct tg3 *tp)
7374{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007375 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007376}
7377
7378/* Restore PCI state after chip reset */
7379static void tg3_restore_pci_state(struct tg3 *tp)
7380{
7381 u32 val;
7382
7383 /* Re-enable indirect register accesses. */
7384 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7385 tp->misc_host_ctrl);
7386
7387 /* Set MAX PCI retry to zero. */
7388 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7389 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007390 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007391 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007392 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007393 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007394 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00007395 PCISTATE_ALLOW_APE_SHMEM_WR |
7396 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007397 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7398
Matt Carlson8a6eac92007-10-21 16:17:55 -07007399 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007400
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007401 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007402 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007403 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007404 else {
7405 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7406 tp->pci_cacheline_sz);
7407 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7408 tp->pci_lat_timer);
7409 }
Michael Chan114342f2007-10-15 02:12:26 -07007410 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007411
Michael Chanee6a99b2007-07-18 21:49:10 -07007412 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007413 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007414 u16 pcix_cmd;
7415
7416 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7417 &pcix_cmd);
7418 pcix_cmd &= ~PCI_X_CMD_ERO;
7419 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7420 pcix_cmd);
7421 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007422
Joe Perches63c3a662011-04-26 08:12:10 +00007423 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007424
7425 /* Chip reset on 5780 will reset MSI enable bit,
7426 * so need to restore it.
7427 */
Joe Perches63c3a662011-04-26 08:12:10 +00007428 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007429 u16 ctrl;
7430
7431 pci_read_config_word(tp->pdev,
7432 tp->msi_cap + PCI_MSI_FLAGS,
7433 &ctrl);
7434 pci_write_config_word(tp->pdev,
7435 tp->msi_cap + PCI_MSI_FLAGS,
7436 ctrl | PCI_MSI_FLAGS_ENABLE);
7437 val = tr32(MSGINT_MODE);
7438 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7439 }
7440 }
7441}
7442
Linus Torvalds1da177e2005-04-16 15:20:36 -07007443/* tp->lock is held. */
7444static int tg3_chip_reset(struct tg3 *tp)
7445{
7446 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007447 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007448 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449
David S. Millerf49639e2006-06-09 11:58:36 -07007450 tg3_nvram_lock(tp);
7451
Matt Carlson77b483f2008-08-15 14:07:24 -07007452 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7453
David S. Millerf49639e2006-06-09 11:58:36 -07007454 /* No matching tg3_nvram_unlock() after this because
7455 * chip reset below will undo the nvram lock.
7456 */
7457 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458
Michael Chanee6a99b2007-07-18 21:49:10 -07007459 /* GRC_MISC_CFG core clock reset will clear the memory
7460 * enable bit in PCI register 4 and the MSI enable bit
7461 * on some chips, so we save relevant registers here.
7462 */
7463 tg3_save_pci_state(tp);
7464
Michael Chand9ab5ad2006-03-20 22:27:35 -08007465 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007466 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007467 tw32(GRC_FASTBOOT_PC, 0);
7468
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 /*
7470 * We must avoid the readl() that normally takes place.
7471 * It locks machines, causes machine checks, and other
7472 * fun things. So, temporarily disable the 5701
7473 * hardware workaround, while we do the reset.
7474 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007475 write_op = tp->write32;
7476 if (write_op == tg3_write_flush_reg32)
7477 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478
Michael Chand18edcb2007-03-24 20:57:11 -07007479 /* Prevent the irq handler from reading or writing PCI registers
7480 * during chip reset when the memory enable bit in the PCI command
7481 * register may be cleared. The chip does not generate interrupt
7482 * at this time, but the irq handler may still be called due to irq
7483 * sharing or irqpoll.
7484 */
Joe Perches63c3a662011-04-26 08:12:10 +00007485 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007486 for (i = 0; i < tp->irq_cnt; i++) {
7487 struct tg3_napi *tnapi = &tp->napi[i];
7488 if (tnapi->hw_status) {
7489 tnapi->hw_status->status = 0;
7490 tnapi->hw_status->status_tag = 0;
7491 }
7492 tnapi->last_tag = 0;
7493 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007494 }
Michael Chand18edcb2007-03-24 20:57:11 -07007495 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007496
7497 for (i = 0; i < tp->irq_cnt; i++)
7498 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007499
Matt Carlson255ca312009-08-25 10:07:27 +00007500 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7501 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7502 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7503 }
7504
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505 /* do the reset */
7506 val = GRC_MISC_CFG_CORECLK_RESET;
7507
Joe Perches63c3a662011-04-26 08:12:10 +00007508 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007509 /* Force PCIe 1.0a mode */
7510 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007511 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007512 tr32(TG3_PCIE_PHY_TSTCTL) ==
7513 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7514 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7515
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7517 tw32(GRC_MISC_CFG, (1 << 29));
7518 val |= (1 << 29);
7519 }
7520 }
7521
Michael Chanb5d37722006-09-27 16:06:21 -07007522 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7523 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7524 tw32(GRC_VCPU_EXT_CTRL,
7525 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7526 }
7527
Matt Carlsonf37500d2010-08-02 11:25:59 +00007528 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007529 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007531
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532 tw32(GRC_MISC_CFG, val);
7533
Michael Chan1ee582d2005-08-09 20:16:46 -07007534 /* restore 5701 hardware bug workaround write method */
7535 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536
7537 /* Unfortunately, we have to delay before the PCI read back.
7538 * Some 575X chips even will not respond to a PCI cfg access
7539 * when the reset command is given to the chip.
7540 *
7541 * How do these hardware designers expect things to work
7542 * properly if the PCI write is posted for a long period
7543 * of time? It is always necessary to have some method by
7544 * which a register read back can occur to push the write
7545 * out which does the reset.
7546 *
7547 * For most tg3 variants the trick below was working.
7548 * Ho hum...
7549 */
7550 udelay(120);
7551
7552 /* Flush PCI posted writes. The normal MMIO registers
7553 * are inaccessible at this time so this is the only
7554 * way to make this reliably (actually, this is no longer
7555 * the case, see above). I tried to use indirect
7556 * register read/write but this upset some 5701 variants.
7557 */
7558 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7559
7560 udelay(120);
7561
Jon Mason708ebb32011-06-27 12:56:50 +00007562 if (tg3_flag(tp, PCI_EXPRESS) && pci_pcie_cap(tp->pdev)) {
Matt Carlsone7126992009-08-25 10:08:16 +00007563 u16 val16;
7564
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7566 int i;
7567 u32 cfg_val;
7568
7569 /* Wait for link training to complete. */
7570 for (i = 0; i < 5000; i++)
7571 udelay(100);
7572
7573 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7574 pci_write_config_dword(tp->pdev, 0xc4,
7575 cfg_val | (1 << 15));
7576 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007577
Matt Carlsone7126992009-08-25 10:08:16 +00007578 /* Clear the "no snoop" and "relaxed ordering" bits. */
7579 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007580 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007581 &val16);
7582 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7583 PCI_EXP_DEVCTL_NOSNOOP_EN);
7584 /*
7585 * Older PCIe devices only support the 128 byte
7586 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007587 */
Joe Perches63c3a662011-04-26 08:12:10 +00007588 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007589 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007590 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007591 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007592 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007593
Matt Carlsoncf790032010-11-24 08:31:48 +00007594 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007595
7596 /* Clear error status */
7597 pci_write_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +00007598 pci_pcie_cap(tp->pdev) + PCI_EXP_DEVSTA,
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007599 PCI_EXP_DEVSTA_CED |
7600 PCI_EXP_DEVSTA_NFED |
7601 PCI_EXP_DEVSTA_FED |
7602 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603 }
7604
Michael Chanee6a99b2007-07-18 21:49:10 -07007605 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007606
Joe Perches63c3a662011-04-26 08:12:10 +00007607 tg3_flag_clear(tp, CHIP_RESETTING);
7608 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007609
Michael Chanee6a99b2007-07-18 21:49:10 -07007610 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007611 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007612 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007613 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614
7615 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7616 tg3_stop_fw(tp);
7617 tw32(0x5000, 0x400);
7618 }
7619
7620 tw32(GRC_MODE, tp->grc_mode);
7621
7622 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007623 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007624
7625 tw32(0xc4, val | (1 << 15));
7626 }
7627
7628 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7629 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7630 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7631 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7632 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7633 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7634 }
7635
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007636 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007637 tp->mac_mode = MAC_MODE_PORT_MODE_TBI;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007638 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007639 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlson9e975cc2011-07-20 10:20:50 +00007640 tp->mac_mode = MAC_MODE_PORT_MODE_GMII;
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007641 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007643 val = 0;
7644
7645 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646 udelay(40);
7647
Matt Carlson77b483f2008-08-15 14:07:24 -07007648 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7649
Michael Chan7a6f4362006-09-27 16:03:31 -07007650 err = tg3_poll_fw(tp);
7651 if (err)
7652 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653
Matt Carlson0a9140c2009-08-28 12:27:50 +00007654 tg3_mdio_start(tp);
7655
Joe Perches63c3a662011-04-26 08:12:10 +00007656 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007657 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7658 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007659 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007660 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007661
7662 tw32(0x7c00, val | (1 << 25));
7663 }
7664
Matt Carlsond78b59f2011-04-05 14:22:46 +00007665 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7666 val = tr32(TG3_CPMU_CLCK_ORIDE);
7667 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7668 }
7669
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007671 tg3_flag_clear(tp, ENABLE_ASF);
7672 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7674 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7675 u32 nic_cfg;
7676
7677 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7678 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007679 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007680 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007681 if (tg3_flag(tp, 5750_PLUS))
7682 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007683 }
7684 }
7685
7686 return 0;
7687}
7688
7689/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007690static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007691{
7692 int err;
7693
7694 tg3_stop_fw(tp);
7695
Michael Chan944d9802005-05-29 14:57:48 -07007696 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007697
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007698 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007699 err = tg3_chip_reset(tp);
7700
Matt Carlsondaba2a62009-04-20 06:58:52 +00007701 __tg3_set_mac_addr(tp, 0);
7702
Michael Chan944d9802005-05-29 14:57:48 -07007703 tg3_write_sig_legacy(tp, kind);
7704 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705
7706 if (err)
7707 return err;
7708
7709 return 0;
7710}
7711
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712#define RX_CPU_SCRATCH_BASE 0x30000
7713#define RX_CPU_SCRATCH_SIZE 0x04000
7714#define TX_CPU_SCRATCH_BASE 0x34000
7715#define TX_CPU_SCRATCH_SIZE 0x04000
7716
7717/* tp->lock is held. */
7718static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7719{
7720 int i;
7721
Joe Perches63c3a662011-04-26 08:12:10 +00007722 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007723
Michael Chanb5d37722006-09-27 16:06:21 -07007724 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7725 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7726
7727 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7728 return 0;
7729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007730 if (offset == RX_CPU_BASE) {
7731 for (i = 0; i < 10000; i++) {
7732 tw32(offset + CPU_STATE, 0xffffffff);
7733 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7734 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7735 break;
7736 }
7737
7738 tw32(offset + CPU_STATE, 0xffffffff);
7739 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7740 udelay(10);
7741 } else {
7742 for (i = 0; i < 10000; i++) {
7743 tw32(offset + CPU_STATE, 0xffffffff);
7744 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7745 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7746 break;
7747 }
7748 }
7749
7750 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007751 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7752 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007753 return -ENODEV;
7754 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007755
7756 /* Clear firmware's nvram arbitration. */
Joe Perches63c3a662011-04-26 08:12:10 +00007757 if (tg3_flag(tp, NVRAM))
Michael Chanec41c7d2006-01-17 02:40:55 -08007758 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007759 return 0;
7760}
7761
7762struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007763 unsigned int fw_base;
7764 unsigned int fw_len;
7765 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007766};
7767
7768/* tp->lock is held. */
7769static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7770 int cpu_scratch_size, struct fw_info *info)
7771{
Michael Chanec41c7d2006-01-17 02:40:55 -08007772 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007773 void (*write_op)(struct tg3 *, u32, u32);
7774
Joe Perches63c3a662011-04-26 08:12:10 +00007775 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007776 netdev_err(tp->dev,
7777 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007778 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779 return -EINVAL;
7780 }
7781
Joe Perches63c3a662011-04-26 08:12:10 +00007782 if (tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007783 write_op = tg3_write_mem;
7784 else
7785 write_op = tg3_write_indirect_reg32;
7786
Michael Chan1b628152005-05-29 14:59:49 -07007787 /* It is possible that bootcode is still loading at this point.
7788 * Get the nvram lock first before halting the cpu.
7789 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007790 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007792 if (!lock_err)
7793 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794 if (err)
7795 goto out;
7796
7797 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7798 write_op(tp, cpu_scratch_base + i, 0);
7799 tw32(cpu_base + CPU_STATE, 0xffffffff);
7800 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007801 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007802 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007803 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007805 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007806
7807 err = 0;
7808
7809out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007810 return err;
7811}
7812
7813/* tp->lock is held. */
7814static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7815{
7816 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007817 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007818 int err, i;
7819
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007820 fw_data = (void *)tp->fw->data;
7821
7822 /* Firmware blob starts with version numbers, followed by
7823 start address and length. We are setting complete length.
7824 length = end_address_of_bss - start_address_of_text.
7825 Remainder is the blob to be loaded contiguously
7826 from start address. */
7827
7828 info.fw_base = be32_to_cpu(fw_data[1]);
7829 info.fw_len = tp->fw->size - 12;
7830 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007831
7832 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7833 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7834 &info);
7835 if (err)
7836 return err;
7837
7838 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7839 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7840 &info);
7841 if (err)
7842 return err;
7843
7844 /* Now startup only the RX cpu. */
7845 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007846 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007847
7848 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007849 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850 break;
7851 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7852 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007853 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007854 udelay(1000);
7855 }
7856 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007857 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7858 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007859 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007860 return -ENODEV;
7861 }
7862 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7863 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7864
7865 return 0;
7866}
7867
Linus Torvalds1da177e2005-04-16 15:20:36 -07007868/* tp->lock is held. */
7869static int tg3_load_tso_firmware(struct tg3 *tp)
7870{
7871 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007872 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007873 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7874 int err, i;
7875
Joe Perches63c3a662011-04-26 08:12:10 +00007876 if (tg3_flag(tp, HW_TSO_1) ||
7877 tg3_flag(tp, HW_TSO_2) ||
7878 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879 return 0;
7880
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007881 fw_data = (void *)tp->fw->data;
7882
7883 /* Firmware blob starts with version numbers, followed by
7884 start address and length. We are setting complete length.
7885 length = end_address_of_bss - start_address_of_text.
7886 Remainder is the blob to be loaded contiguously
7887 from start address. */
7888
7889 info.fw_base = be32_to_cpu(fw_data[1]);
7890 cpu_scratch_size = tp->fw_len;
7891 info.fw_len = tp->fw->size - 12;
7892 info.fw_data = &fw_data[3];
7893
Linus Torvalds1da177e2005-04-16 15:20:36 -07007894 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007895 cpu_base = RX_CPU_BASE;
7896 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007897 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007898 cpu_base = TX_CPU_BASE;
7899 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7900 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7901 }
7902
7903 err = tg3_load_firmware_cpu(tp, cpu_base,
7904 cpu_scratch_base, cpu_scratch_size,
7905 &info);
7906 if (err)
7907 return err;
7908
7909 /* Now startup the cpu. */
7910 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007911 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007912
7913 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007914 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007915 break;
7916 tw32(cpu_base + CPU_STATE, 0xffffffff);
7917 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007918 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007919 udelay(1000);
7920 }
7921 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007922 netdev_err(tp->dev,
7923 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007924 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007925 return -ENODEV;
7926 }
7927 tw32(cpu_base + CPU_STATE, 0xffffffff);
7928 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7929 return 0;
7930}
7931
Linus Torvalds1da177e2005-04-16 15:20:36 -07007932
Linus Torvalds1da177e2005-04-16 15:20:36 -07007933static int tg3_set_mac_addr(struct net_device *dev, void *p)
7934{
7935 struct tg3 *tp = netdev_priv(dev);
7936 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007937 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007938
Michael Chanf9804dd2005-09-27 12:13:10 -07007939 if (!is_valid_ether_addr(addr->sa_data))
7940 return -EINVAL;
7941
Linus Torvalds1da177e2005-04-16 15:20:36 -07007942 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7943
Michael Chane75f7c92006-03-20 21:33:26 -08007944 if (!netif_running(dev))
7945 return 0;
7946
Joe Perches63c3a662011-04-26 08:12:10 +00007947 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007948 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007949
Michael Chan986e0ae2007-05-05 12:10:20 -07007950 addr0_high = tr32(MAC_ADDR_0_HIGH);
7951 addr0_low = tr32(MAC_ADDR_0_LOW);
7952 addr1_high = tr32(MAC_ADDR_1_HIGH);
7953 addr1_low = tr32(MAC_ADDR_1_LOW);
7954
7955 /* Skip MAC addr 1 if ASF is using it. */
7956 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7957 !(addr1_high == 0 && addr1_low == 0))
7958 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007959 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007960 spin_lock_bh(&tp->lock);
7961 __tg3_set_mac_addr(tp, skip_mac_1);
7962 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963
Michael Chanb9ec6c12006-07-25 16:37:27 -07007964 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965}
7966
7967/* tp->lock is held. */
7968static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7969 dma_addr_t mapping, u32 maxlen_flags,
7970 u32 nic_addr)
7971{
7972 tg3_write_mem(tp,
7973 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7974 ((u64) mapping >> 32));
7975 tg3_write_mem(tp,
7976 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7977 ((u64) mapping & 0xffffffff));
7978 tg3_write_mem(tp,
7979 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7980 maxlen_flags);
7981
Joe Perches63c3a662011-04-26 08:12:10 +00007982 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007983 tg3_write_mem(tp,
7984 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7985 nic_addr);
7986}
7987
7988static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007989static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007990{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007991 int i;
7992
Joe Perches63c3a662011-04-26 08:12:10 +00007993 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007994 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7995 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7996 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007997 } else {
7998 tw32(HOSTCC_TXCOL_TICKS, 0);
7999 tw32(HOSTCC_TXMAX_FRAMES, 0);
8000 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008001 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008002
Joe Perches63c3a662011-04-26 08:12:10 +00008003 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008004 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
8005 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
8006 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
8007 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00008008 tw32(HOSTCC_RXCOL_TICKS, 0);
8009 tw32(HOSTCC_RXMAX_FRAMES, 0);
8010 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07008011 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008012
Joe Perches63c3a662011-04-26 08:12:10 +00008013 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07008014 u32 val = ec->stats_block_coalesce_usecs;
8015
Matt Carlsonb6080e12009-09-01 13:12:00 +00008016 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
8017 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
8018
David S. Miller15f98502005-05-18 22:49:26 -07008019 if (!netif_carrier_ok(tp->dev))
8020 val = 0;
8021
8022 tw32(HOSTCC_STAT_COAL_TICKS, val);
8023 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008024
8025 for (i = 0; i < tp->irq_cnt - 1; i++) {
8026 u32 reg;
8027
8028 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
8029 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008030 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
8031 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008032 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
8033 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008034
Joe Perches63c3a662011-04-26 08:12:10 +00008035 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008036 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
8037 tw32(reg, ec->tx_coalesce_usecs);
8038 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
8039 tw32(reg, ec->tx_max_coalesced_frames);
8040 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
8041 tw32(reg, ec->tx_max_coalesced_frames_irq);
8042 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008043 }
8044
8045 for (; i < tp->irq_max - 1; i++) {
8046 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008047 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00008048 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00008049
Joe Perches63c3a662011-04-26 08:12:10 +00008050 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00008051 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
8052 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
8053 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
8054 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00008055 }
David S. Miller15f98502005-05-18 22:49:26 -07008056}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008057
8058/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00008059static void tg3_rings_reset(struct tg3 *tp)
8060{
8061 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008062 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008063 struct tg3_napi *tnapi = &tp->napi[0];
8064
8065 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008066 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008067 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008068 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00008069 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00008070 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8071 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008072 else
8073 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8074
8075 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
8076 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
8077 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
8078 BDINFO_FLAGS_DISABLED);
8079
8080
8081 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00008082 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008083 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00008084 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00008085 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00008086 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8087 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00008088 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
8089 else
8090 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8091
8092 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
8093 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
8094 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
8095 BDINFO_FLAGS_DISABLED);
8096
8097 /* Disable interrupts */
8098 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008099 tp->napi[0].chk_msi_cnt = 0;
8100 tp->napi[0].last_rx_cons = 0;
8101 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00008102
8103 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00008104 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00008105 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008106 tp->napi[i].tx_prod = 0;
8107 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00008108 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008109 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008110 tw32_rx_mbox(tp->napi[i].consmbox, 0);
8111 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson7f230732011-08-31 11:44:48 +00008112 tp->napi[i].chk_msi_cnt = 0;
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008113 tp->napi[i].last_rx_cons = 0;
8114 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008115 }
Joe Perches63c3a662011-04-26 08:12:10 +00008116 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00008117 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008118 } else {
8119 tp->napi[0].tx_prod = 0;
8120 tp->napi[0].tx_cons = 0;
8121 tw32_mailbox(tp->napi[0].prodmbox, 0);
8122 tw32_rx_mbox(tp->napi[0].consmbox, 0);
8123 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008124
8125 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00008126 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00008127 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
8128 for (i = 0; i < 16; i++)
8129 tw32_tx_mbox(mbox + i * 8, 0);
8130 }
8131
8132 txrcb = NIC_SRAM_SEND_RCB;
8133 rxrcb = NIC_SRAM_RCV_RET_RCB;
8134
8135 /* Clear status block in ram. */
8136 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8137
8138 /* Set status block DMA address */
8139 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8140 ((u64) tnapi->status_mapping >> 32));
8141 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8142 ((u64) tnapi->status_mapping & 0xffffffff));
8143
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008144 if (tnapi->tx_ring) {
8145 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8146 (TG3_TX_RING_SIZE <<
8147 BDINFO_FLAGS_MAXLEN_SHIFT),
8148 NIC_SRAM_TX_BUFFER_DESC);
8149 txrcb += TG3_BDINFO_SIZE;
8150 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008151
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008152 if (tnapi->rx_rcb) {
8153 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008154 (tp->rx_ret_ring_mask + 1) <<
8155 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008156 rxrcb += TG3_BDINFO_SIZE;
8157 }
8158
8159 stblk = HOSTCC_STATBLCK_RING1;
8160
8161 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
8162 u64 mapping = (u64)tnapi->status_mapping;
8163 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
8164 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
8165
8166 /* Clear status block in ram. */
8167 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
8168
Matt Carlson19cfaec2009-12-03 08:36:20 +00008169 if (tnapi->tx_ring) {
8170 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
8171 (TG3_TX_RING_SIZE <<
8172 BDINFO_FLAGS_MAXLEN_SHIFT),
8173 NIC_SRAM_TX_BUFFER_DESC);
8174 txrcb += TG3_BDINFO_SIZE;
8175 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008176
8177 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008178 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008179 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
8180
8181 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00008182 rxrcb += TG3_BDINFO_SIZE;
8183 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00008184}
8185
Matt Carlsoneb07a942011-04-20 07:57:36 +00008186static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
8187{
8188 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
8189
Joe Perches63c3a662011-04-26 08:12:10 +00008190 if (!tg3_flag(tp, 5750_PLUS) ||
8191 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00008192 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
8193 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8194 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
8195 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
8196 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
8197 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
8198 else
8199 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
8200
8201 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
8202 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
8203
8204 val = min(nic_rep_thresh, host_rep_thresh);
8205 tw32(RCVBDI_STD_THRESH, val);
8206
Joe Perches63c3a662011-04-26 08:12:10 +00008207 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008208 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
8209
Joe Perches63c3a662011-04-26 08:12:10 +00008210 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008211 return;
8212
Joe Perches63c3a662011-04-26 08:12:10 +00008213 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008214 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
8215 else
8216 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
8217
8218 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
8219
8220 val = min(bdcache_maxcnt / 2, host_rep_thresh);
8221 tw32(RCVBDI_JUMBO_THRESH, val);
8222
Joe Perches63c3a662011-04-26 08:12:10 +00008223 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00008224 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
8225}
8226
Matt Carlson2d31eca2009-09-01 12:53:31 +00008227/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008228static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229{
8230 u32 val, rdmac_mode;
8231 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00008232 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008233
8234 tg3_disable_ints(tp);
8235
8236 tg3_stop_fw(tp);
8237
8238 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
8239
Joe Perches63c3a662011-04-26 08:12:10 +00008240 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07008241 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242
Matt Carlson699c0192010-12-06 08:28:51 +00008243 /* Enable MAC control of LPI */
8244 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
8245 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
8246 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
8247 TG3_CPMU_EEE_LNKIDL_UART_IDL);
8248
8249 tw32_f(TG3_CPMU_EEE_CTRL,
8250 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
8251
Matt Carlsona386b902010-12-06 08:28:53 +00008252 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
8253 TG3_CPMU_EEEMD_LPI_IN_TX |
8254 TG3_CPMU_EEEMD_LPI_IN_RX |
8255 TG3_CPMU_EEEMD_EEE_ENABLE;
8256
8257 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8258 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
8259
Joe Perches63c3a662011-04-26 08:12:10 +00008260 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00008261 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
8262
8263 tw32_f(TG3_CPMU_EEE_MODE, val);
8264
8265 tw32_f(TG3_CPMU_EEE_DBTMR1,
8266 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
8267 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
8268
8269 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00008270 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00008271 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008272 }
8273
Matt Carlson603f1172010-02-12 14:47:10 +00008274 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008275 tg3_phy_reset(tp);
8276
Linus Torvalds1da177e2005-04-16 15:20:36 -07008277 err = tg3_chip_reset(tp);
8278 if (err)
8279 return err;
8280
8281 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8282
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008283 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008284 val = tr32(TG3_CPMU_CTRL);
8285 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8286 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008287
8288 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8289 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8290 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8291 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8292
8293 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8294 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8295 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8296 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8297
8298 val = tr32(TG3_CPMU_HST_ACC);
8299 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8300 val |= CPMU_HST_ACC_MACCLK_6_25;
8301 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008302 }
8303
Matt Carlson33466d92009-04-20 06:57:41 +00008304 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8305 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8306 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8307 PCIE_PWR_MGMT_L1_THRESH_4MS;
8308 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008309
8310 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8311 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8312
8313 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008314
Matt Carlsonf40386c2009-11-02 14:24:02 +00008315 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8316 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008317 }
8318
Joe Perches63c3a662011-04-26 08:12:10 +00008319 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b05902010-01-20 16:58:02 +00008320 u32 grc_mode = tr32(GRC_MODE);
8321
8322 /* Access the lower 1K of PL PCIE block registers. */
8323 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8324 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8325
8326 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8327 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8328 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8329
8330 tw32(GRC_MODE, grc_mode);
8331 }
8332
Matt Carlson5093eed2010-11-24 08:31:45 +00008333 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8334 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8335 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008336
Matt Carlson5093eed2010-11-24 08:31:45 +00008337 /* Access the lower 1K of PL PCIE block registers. */
8338 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8339 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008340
Matt Carlson5093eed2010-11-24 08:31:45 +00008341 val = tr32(TG3_PCIE_TLDLPL_PORT +
8342 TG3_PCIE_PL_LO_PHYCTL5);
8343 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8344 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008345
Matt Carlson5093eed2010-11-24 08:31:45 +00008346 tw32(GRC_MODE, grc_mode);
8347 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008348
Matt Carlson1ff30a52011-05-19 12:12:46 +00008349 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8350 u32 grc_mode = tr32(GRC_MODE);
8351
8352 /* Access the lower 1K of DL PCIE block registers. */
8353 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8354 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8355
8356 val = tr32(TG3_PCIE_TLDLPL_PORT +
8357 TG3_PCIE_DL_LO_FTSMAX);
8358 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8359 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8360 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8361
8362 tw32(GRC_MODE, grc_mode);
8363 }
8364
Matt Carlsona977dbe2010-04-12 06:58:26 +00008365 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8366 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8367 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8368 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008369 }
8370
Linus Torvalds1da177e2005-04-16 15:20:36 -07008371 /* This works around an issue with Athlon chipsets on
8372 * B3 tigon3 silicon. This bit has no effect on any
8373 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008374 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008375 */
Joe Perches63c3a662011-04-26 08:12:10 +00008376 if (!tg3_flag(tp, CPMU_PRESENT)) {
8377 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008378 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8379 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008381
8382 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008383 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008384 val = tr32(TG3PCI_PCISTATE);
8385 val |= PCISTATE_RETRY_SAME_DMA;
8386 tw32(TG3PCI_PCISTATE, val);
8387 }
8388
Joe Perches63c3a662011-04-26 08:12:10 +00008389 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008390 /* Allow reads and writes to the
8391 * APE register and memory space.
8392 */
8393 val = tr32(TG3PCI_PCISTATE);
8394 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00008395 PCISTATE_ALLOW_APE_SHMEM_WR |
8396 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008397 tw32(TG3PCI_PCISTATE, val);
8398 }
8399
Linus Torvalds1da177e2005-04-16 15:20:36 -07008400 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8401 /* Enable some hw fixes. */
8402 val = tr32(TG3PCI_MSI_DATA);
8403 val |= (1 << 26) | (1 << 28) | (1 << 29);
8404 tw32(TG3PCI_MSI_DATA, val);
8405 }
8406
8407 /* Descriptor ring init may make accesses to the
8408 * NIC SRAM area to setup the TX descriptors, so we
8409 * can only do this after the hardware has been
8410 * successfully reset.
8411 */
Michael Chan32d8c572006-07-25 16:38:29 -07008412 err = tg3_init_rings(tp);
8413 if (err)
8414 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415
Joe Perches63c3a662011-04-26 08:12:10 +00008416 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008417 val = tr32(TG3PCI_DMA_RW_CTRL) &
8418 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008419 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8420 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008421 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8422 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8423 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008424 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8425 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8426 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008427 /* This value is determined during the probe time DMA
8428 * engine test, tg3_test_dma.
8429 */
8430 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008432
8433 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8434 GRC_MODE_4X_NIC_SEND_RINGS |
8435 GRC_MODE_NO_TX_PHDR_CSUM |
8436 GRC_MODE_NO_RX_PHDR_CSUM);
8437 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008438
8439 /* Pseudo-header checksum is done by hardware logic and not
8440 * the offload processers, so make the chip do the pseudo-
8441 * header checksums on receive. For transmit it is more
8442 * convenient to do the pseudo-header checksum in software
8443 * as Linux does that on transmit for us in all cases.
8444 */
8445 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446
8447 tw32(GRC_MODE,
8448 tp->grc_mode |
8449 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8450
8451 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8452 val = tr32(GRC_MISC_CFG);
8453 val &= ~0xff;
8454 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8455 tw32(GRC_MISC_CFG, val);
8456
8457 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008458 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459 /* Do nothing. */
8460 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8461 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8462 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8463 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8464 else
8465 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8466 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8467 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008468 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469 int fw_len;
8470
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008471 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008472 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8473 tw32(BUFMGR_MB_POOL_ADDR,
8474 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8475 tw32(BUFMGR_MB_POOL_SIZE,
8476 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008478
Michael Chan0f893dc2005-07-25 12:30:38 -07008479 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008480 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8481 tp->bufmgr_config.mbuf_read_dma_low_water);
8482 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8483 tp->bufmgr_config.mbuf_mac_rx_low_water);
8484 tw32(BUFMGR_MB_HIGH_WATER,
8485 tp->bufmgr_config.mbuf_high_water);
8486 } else {
8487 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8488 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8489 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8490 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8491 tw32(BUFMGR_MB_HIGH_WATER,
8492 tp->bufmgr_config.mbuf_high_water_jumbo);
8493 }
8494 tw32(BUFMGR_DMA_LOW_WATER,
8495 tp->bufmgr_config.dma_low_water);
8496 tw32(BUFMGR_DMA_HIGH_WATER,
8497 tp->bufmgr_config.dma_high_water);
8498
Matt Carlsond309a462010-09-30 10:34:31 +00008499 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8500 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8501 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008502 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8503 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8504 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8505 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008506 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008507 for (i = 0; i < 2000; i++) {
8508 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8509 break;
8510 udelay(10);
8511 }
8512 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008513 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514 return -ENODEV;
8515 }
8516
Matt Carlsoneb07a942011-04-20 07:57:36 +00008517 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8518 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008519
Matt Carlsoneb07a942011-04-20 07:57:36 +00008520 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008521
8522 /* Initialize TG3_BDINFO's at:
8523 * RCVDBDI_STD_BD: standard eth size rx ring
8524 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8525 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8526 *
8527 * like so:
8528 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8529 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8530 * ring attribute flags
8531 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8532 *
8533 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8534 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8535 *
8536 * The size of each ring is fixed in the firmware, but the location is
8537 * configurable.
8538 */
8539 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008540 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008542 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008543 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008544 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8545 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008547 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008548 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008549 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8550 BDINFO_FLAGS_DISABLED);
8551
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008552 /* Program the jumbo buffer descriptor ring control
8553 * blocks on those devices that have them.
8554 */
Matt Carlsona0512942011-07-27 14:20:54 +00008555 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008556 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008557
Joe Perches63c3a662011-04-26 08:12:10 +00008558 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008559 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008560 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008561 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008562 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008563 val = TG3_RX_JMB_RING_SIZE(tp) <<
8564 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008565 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008566 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008567 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008568 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008569 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8570 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008571 } else {
8572 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8573 BDINFO_FLAGS_DISABLED);
8574 }
8575
Joe Perches63c3a662011-04-26 08:12:10 +00008576 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008577 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008578 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008579 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008580 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008581 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8582 val |= (TG3_RX_STD_DMA_SZ << 2);
8583 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008584 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008585 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008586 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008587
8588 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008589
Matt Carlson411da642009-11-13 13:03:46 +00008590 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008591 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008592
Joe Perches63c3a662011-04-26 08:12:10 +00008593 tpr->rx_jmb_prod_idx =
8594 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008595 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008596
Matt Carlson2d31eca2009-09-01 12:53:31 +00008597 tg3_rings_reset(tp);
8598
Linus Torvalds1da177e2005-04-16 15:20:36 -07008599 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008600 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601
8602 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008603 tw32(MAC_RX_MTU_SIZE,
8604 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008605
8606 /* The slot time is changed by tg3_setup_phy if we
8607 * run at gigabit with half duplex.
8608 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008609 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8610 (6 << TX_LENGTHS_IPG_SHIFT) |
8611 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8612
8613 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8614 val |= tr32(MAC_TX_LENGTHS) &
8615 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8616 TX_LENGTHS_CNT_DWN_VAL_MSK);
8617
8618 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008619
8620 /* Receive rules. */
8621 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8622 tw32(RCVLPC_CONFIG, 0x0181);
8623
8624 /* Calculate RDMAC_MODE setting early, we need it to determine
8625 * the RCVLPC_STATE_ENABLE mask.
8626 */
8627 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8628 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8629 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8630 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8631 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008632
Matt Carlsondeabaac2010-11-24 08:31:50 +00008633 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008634 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8635
Matt Carlson57e69832008-05-25 23:48:31 -07008636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008637 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8638 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008639 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8640 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8641 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8642
Matt Carlsonc5908932011-03-09 16:58:25 +00008643 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8644 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008645 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008646 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008647 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8648 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008649 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008650 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8651 }
8652 }
8653
Joe Perches63c3a662011-04-26 08:12:10 +00008654 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008655 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8656
Joe Perches63c3a662011-04-26 08:12:10 +00008657 if (tg3_flag(tp, HW_TSO_1) ||
8658 tg3_flag(tp, HW_TSO_2) ||
8659 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008660 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8661
Matt Carlson108a6c12011-05-19 12:12:47 +00008662 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008663 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008664 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8665 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008666
Matt Carlsonf2096f92011-04-05 14:22:48 +00008667 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8668 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8669
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008670 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8671 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8672 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8673 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008674 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008675 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008676 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8677 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008678 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8679 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8680 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8681 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8682 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8683 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008684 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008685 tw32(TG3_RDMA_RSRVCTRL_REG,
8686 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8687 }
8688
Matt Carlsond78b59f2011-04-05 14:22:46 +00008689 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8690 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008691 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8692 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8693 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8694 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8695 }
8696
Linus Torvalds1da177e2005-04-16 15:20:36 -07008697 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008698 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008699 val = tr32(RCVLPC_STATS_ENABLE);
8700 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8701 tw32(RCVLPC_STATS_ENABLE, val);
8702 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008703 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008704 val = tr32(RCVLPC_STATS_ENABLE);
8705 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8706 tw32(RCVLPC_STATS_ENABLE, val);
8707 } else {
8708 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8709 }
8710 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8711 tw32(SNDDATAI_STATSENAB, 0xffffff);
8712 tw32(SNDDATAI_STATSCTRL,
8713 (SNDDATAI_SCTRL_ENABLE |
8714 SNDDATAI_SCTRL_FASTUPD));
8715
8716 /* Setup host coalescing engine. */
8717 tw32(HOSTCC_MODE, 0);
8718 for (i = 0; i < 2000; i++) {
8719 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8720 break;
8721 udelay(10);
8722 }
8723
Michael Chand244c892005-07-05 14:42:33 -07008724 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008725
Joe Perches63c3a662011-04-26 08:12:10 +00008726 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727 /* Status/statistics block address. See tg3_timer,
8728 * the tg3_periodic_fetch_stats call there, and
8729 * tg3_get_stats to see how this works for 5705/5750 chips.
8730 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008731 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8732 ((u64) tp->stats_mapping >> 32));
8733 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8734 ((u64) tp->stats_mapping & 0xffffffff));
8735 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008736
Linus Torvalds1da177e2005-04-16 15:20:36 -07008737 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008738
8739 /* Clear statistics and status block memory areas */
8740 for (i = NIC_SRAM_STATS_BLK;
8741 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8742 i += sizeof(u32)) {
8743 tg3_write_mem(tp, i, 0);
8744 udelay(40);
8745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008746 }
8747
8748 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8749
8750 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8751 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008752 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008753 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8754
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008755 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8756 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008757 /* reset to prevent losing 1st rx packet intermittently */
8758 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8759 udelay(10);
8760 }
8761
Matt Carlson3bda1252008-08-15 14:08:22 -07008762 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Matt Carlson9e975cc2011-07-20 10:20:50 +00008763 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE |
8764 MAC_MODE_FHDE_ENABLE;
8765 if (tg3_flag(tp, ENABLE_APE))
8766 tp->mac_mode |= MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Joe Perches63c3a662011-04-26 08:12:10 +00008767 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008768 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008769 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8770 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008771 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8772 udelay(40);
8773
Michael Chan314fba32005-04-21 17:07:04 -07008774 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008775 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008776 * register to preserve the GPIO settings for LOMs. The GPIOs,
8777 * whether used as inputs or outputs, are set by boot code after
8778 * reset.
8779 */
Joe Perches63c3a662011-04-26 08:12:10 +00008780 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008781 u32 gpio_mask;
8782
Michael Chan9d26e212006-12-07 00:21:14 -08008783 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8784 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8785 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008786
8787 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8788 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8789 GRC_LCLCTRL_GPIO_OUTPUT3;
8790
Michael Chanaf36e6b2006-03-23 01:28:06 -08008791 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8792 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8793
Gary Zambranoaaf84462007-05-05 11:51:45 -07008794 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008795 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8796
8797 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008798 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008799 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8800 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008802 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8803 udelay(100);
8804
Joe Perches63c3a662011-04-26 08:12:10 +00008805 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008806 val = tr32(MSGINT_MODE);
8807 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
Matt Carlson5b39de92011-08-31 11:44:50 +00008808 if (!tg3_flag(tp, 1SHOT_MSI))
8809 val |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008810 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 Carlson5b39de92011-08-31 11:44:50 +00009425 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, 1SHOT_MSI)) {
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 Carlson5b39de92011-08-31 11:44:50 +00009603 if (!tg3_flag(tp, 1SHOT_MSI))
9604 msi_mode |= MSGINT_MODE_ONE_SHOT_DISABLE;
Matt Carlson679563f2009-09-01 12:55:46 +00009605 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9606 }
9607defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009608 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009609 tp->irq_cnt = 1;
9610 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009611 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009612 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009613 }
Matt Carlson07b01732009-08-28 14:01:15 +00009614}
9615
9616static void tg3_ints_fini(struct tg3 *tp)
9617{
Joe Perches63c3a662011-04-26 08:12:10 +00009618 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009619 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009620 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009621 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009622 tg3_flag_clear(tp, USING_MSI);
9623 tg3_flag_clear(tp, USING_MSIX);
9624 tg3_flag_clear(tp, ENABLE_RSS);
9625 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009626}
9627
Linus Torvalds1da177e2005-04-16 15:20:36 -07009628static int tg3_open(struct net_device *dev)
9629{
9630 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009631 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009632
Matt Carlson9e9fd122009-01-19 16:57:45 -08009633 if (tp->fw_needed) {
9634 err = tg3_request_firmware(tp);
9635 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9636 if (err)
9637 return err;
9638 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009639 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009640 tg3_flag_clear(tp, TSO_CAPABLE);
9641 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009642 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009643 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009644 }
9645 }
9646
Michael Chanc49a1562006-12-17 17:07:29 -08009647 netif_carrier_off(tp->dev);
9648
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009649 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009650 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009651 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009652
9653 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009654
Linus Torvalds1da177e2005-04-16 15:20:36 -07009655 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009656 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009657
David S. Millerf47c11e2005-06-24 20:18:35 -07009658 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009659
Matt Carlson679563f2009-09-01 12:55:46 +00009660 /*
9661 * Setup interrupts first so we know how
9662 * many NAPI resources to allocate
9663 */
9664 tg3_ints_init(tp);
9665
Linus Torvalds1da177e2005-04-16 15:20:36 -07009666 /* The placement of this call is tied
9667 * to the setup and use of Host TX descriptors.
9668 */
9669 err = tg3_alloc_consistent(tp);
9670 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009671 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009672
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009673 tg3_napi_init(tp);
9674
Matt Carlsonfed97812009-09-01 13:10:19 +00009675 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009676
Matt Carlson4f125f42009-09-01 12:55:02 +00009677 for (i = 0; i < tp->irq_cnt; i++) {
9678 struct tg3_napi *tnapi = &tp->napi[i];
9679 err = tg3_request_irq(tp, i);
9680 if (err) {
9681 for (i--; i >= 0; i--)
9682 free_irq(tnapi->irq_vec, tnapi);
9683 break;
9684 }
9685 }
Matt Carlson07b01732009-08-28 14:01:15 +00009686
9687 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009688 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009689
David S. Millerf47c11e2005-06-24 20:18:35 -07009690 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009691
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009692 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009693 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009694 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009695 tg3_free_rings(tp);
9696 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009697 if (tg3_flag(tp, TAGGED_STATUS) &&
9698 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9699 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765)
David S. Millerfac9b832005-05-18 22:46:34 -07009700 tp->timer_offset = HZ;
9701 else
9702 tp->timer_offset = HZ / 10;
9703
9704 BUG_ON(tp->timer_offset > HZ);
9705 tp->timer_counter = tp->timer_multiplier =
9706 (HZ / tp->timer_offset);
9707 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009708 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009709
9710 init_timer(&tp->timer);
9711 tp->timer.expires = jiffies + tp->timer_offset;
9712 tp->timer.data = (unsigned long) tp;
9713 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009714 }
9715
David S. Millerf47c11e2005-06-24 20:18:35 -07009716 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009717
Matt Carlson07b01732009-08-28 14:01:15 +00009718 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009719 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009720
Joe Perches63c3a662011-04-26 08:12:10 +00009721 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009722 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009723
Michael Chan79381092005-04-21 17:13:59 -07009724 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009725 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009726 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009727 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009728 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009729
Matt Carlson679563f2009-09-01 12:55:46 +00009730 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009731 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009732
Joe Perches63c3a662011-04-26 08:12:10 +00009733 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009734 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009735
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009736 tw32(PCIE_TRANSACTION_CFG,
9737 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009738 }
Michael Chan79381092005-04-21 17:13:59 -07009739 }
9740
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009741 tg3_phy_start(tp);
9742
David S. Millerf47c11e2005-06-24 20:18:35 -07009743 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009744
Michael Chan79381092005-04-21 17:13:59 -07009745 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009746 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009747 tg3_enable_ints(tp);
9748
David S. Millerf47c11e2005-06-24 20:18:35 -07009749 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009750
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009751 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009752
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009753 /*
9754 * Reset loopback feature if it was turned on while the device was down
9755 * make sure that it's installed properly now.
9756 */
9757 if (dev->features & NETIF_F_LOOPBACK)
9758 tg3_set_loopback(dev, dev->features);
9759
Linus Torvalds1da177e2005-04-16 15:20:36 -07009760 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009761
Matt Carlson679563f2009-09-01 12:55:46 +00009762err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009763 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9764 struct tg3_napi *tnapi = &tp->napi[i];
9765 free_irq(tnapi->irq_vec, tnapi);
9766 }
Matt Carlson07b01732009-08-28 14:01:15 +00009767
Matt Carlson679563f2009-09-01 12:55:46 +00009768err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009769 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009770 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009771 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009772
9773err_out1:
9774 tg3_ints_fini(tp);
Matt Carlsoncd0d7222011-07-13 09:27:33 +00009775 tg3_frob_aux_power(tp, false);
9776 pci_set_power_state(tp->pdev, PCI_D3hot);
Matt Carlson07b01732009-08-28 14:01:15 +00009777 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009778}
9779
Eric Dumazet511d2222010-07-07 20:44:24 +00009780static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9781 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009782static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9783
9784static int tg3_close(struct net_device *dev)
9785{
Matt Carlson4f125f42009-09-01 12:55:02 +00009786 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009787 struct tg3 *tp = netdev_priv(dev);
9788
Matt Carlsonfed97812009-09-01 13:10:19 +00009789 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009790 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009791
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009792 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009793
9794 del_timer_sync(&tp->timer);
9795
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009796 tg3_phy_stop(tp);
9797
David S. Millerf47c11e2005-06-24 20:18:35 -07009798 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009799
9800 tg3_disable_ints(tp);
9801
Michael Chan944d9802005-05-29 14:57:48 -07009802 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009803 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009804 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805
David S. Millerf47c11e2005-06-24 20:18:35 -07009806 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009807
Matt Carlson4f125f42009-09-01 12:55:02 +00009808 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9809 struct tg3_napi *tnapi = &tp->napi[i];
9810 free_irq(tnapi->irq_vec, tnapi);
9811 }
Matt Carlson07b01732009-08-28 14:01:15 +00009812
9813 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009814
Eric Dumazet511d2222010-07-07 20:44:24 +00009815 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9816
Linus Torvalds1da177e2005-04-16 15:20:36 -07009817 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9818 sizeof(tp->estats_prev));
9819
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009820 tg3_napi_fini(tp);
9821
Linus Torvalds1da177e2005-04-16 15:20:36 -07009822 tg3_free_consistent(tp);
9823
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009824 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009825
9826 netif_carrier_off(tp->dev);
9827
Linus Torvalds1da177e2005-04-16 15:20:36 -07009828 return 0;
9829}
9830
Eric Dumazet511d2222010-07-07 20:44:24 +00009831static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009832{
9833 return ((u64)val->high << 32) | ((u64)val->low);
9834}
9835
Eric Dumazet511d2222010-07-07 20:44:24 +00009836static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837{
9838 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9839
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009840 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009841 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9842 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009843 u32 val;
9844
David S. Millerf47c11e2005-06-24 20:18:35 -07009845 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009846 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9847 tg3_writephy(tp, MII_TG3_TEST1,
9848 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009849 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009850 } else
9851 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009852 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009853
9854 tp->phy_crc_errors += val;
9855
9856 return tp->phy_crc_errors;
9857 }
9858
9859 return get_stat64(&hw_stats->rx_fcs_errors);
9860}
9861
9862#define ESTAT_ADD(member) \
9863 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009864 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009865
9866static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9867{
9868 struct tg3_ethtool_stats *estats = &tp->estats;
9869 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9870 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9871
9872 if (!hw_stats)
9873 return old_estats;
9874
9875 ESTAT_ADD(rx_octets);
9876 ESTAT_ADD(rx_fragments);
9877 ESTAT_ADD(rx_ucast_packets);
9878 ESTAT_ADD(rx_mcast_packets);
9879 ESTAT_ADD(rx_bcast_packets);
9880 ESTAT_ADD(rx_fcs_errors);
9881 ESTAT_ADD(rx_align_errors);
9882 ESTAT_ADD(rx_xon_pause_rcvd);
9883 ESTAT_ADD(rx_xoff_pause_rcvd);
9884 ESTAT_ADD(rx_mac_ctrl_rcvd);
9885 ESTAT_ADD(rx_xoff_entered);
9886 ESTAT_ADD(rx_frame_too_long_errors);
9887 ESTAT_ADD(rx_jabbers);
9888 ESTAT_ADD(rx_undersize_packets);
9889 ESTAT_ADD(rx_in_length_errors);
9890 ESTAT_ADD(rx_out_length_errors);
9891 ESTAT_ADD(rx_64_or_less_octet_packets);
9892 ESTAT_ADD(rx_65_to_127_octet_packets);
9893 ESTAT_ADD(rx_128_to_255_octet_packets);
9894 ESTAT_ADD(rx_256_to_511_octet_packets);
9895 ESTAT_ADD(rx_512_to_1023_octet_packets);
9896 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9897 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9898 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9899 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9900 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9901
9902 ESTAT_ADD(tx_octets);
9903 ESTAT_ADD(tx_collisions);
9904 ESTAT_ADD(tx_xon_sent);
9905 ESTAT_ADD(tx_xoff_sent);
9906 ESTAT_ADD(tx_flow_control);
9907 ESTAT_ADD(tx_mac_errors);
9908 ESTAT_ADD(tx_single_collisions);
9909 ESTAT_ADD(tx_mult_collisions);
9910 ESTAT_ADD(tx_deferred);
9911 ESTAT_ADD(tx_excessive_collisions);
9912 ESTAT_ADD(tx_late_collisions);
9913 ESTAT_ADD(tx_collide_2times);
9914 ESTAT_ADD(tx_collide_3times);
9915 ESTAT_ADD(tx_collide_4times);
9916 ESTAT_ADD(tx_collide_5times);
9917 ESTAT_ADD(tx_collide_6times);
9918 ESTAT_ADD(tx_collide_7times);
9919 ESTAT_ADD(tx_collide_8times);
9920 ESTAT_ADD(tx_collide_9times);
9921 ESTAT_ADD(tx_collide_10times);
9922 ESTAT_ADD(tx_collide_11times);
9923 ESTAT_ADD(tx_collide_12times);
9924 ESTAT_ADD(tx_collide_13times);
9925 ESTAT_ADD(tx_collide_14times);
9926 ESTAT_ADD(tx_collide_15times);
9927 ESTAT_ADD(tx_ucast_packets);
9928 ESTAT_ADD(tx_mcast_packets);
9929 ESTAT_ADD(tx_bcast_packets);
9930 ESTAT_ADD(tx_carrier_sense_errors);
9931 ESTAT_ADD(tx_discards);
9932 ESTAT_ADD(tx_errors);
9933
9934 ESTAT_ADD(dma_writeq_full);
9935 ESTAT_ADD(dma_write_prioq_full);
9936 ESTAT_ADD(rxbds_empty);
9937 ESTAT_ADD(rx_discards);
9938 ESTAT_ADD(rx_errors);
9939 ESTAT_ADD(rx_threshold_hit);
9940
9941 ESTAT_ADD(dma_readq_full);
9942 ESTAT_ADD(dma_read_prioq_full);
9943 ESTAT_ADD(tx_comp_queue_full);
9944
9945 ESTAT_ADD(ring_set_send_prod_index);
9946 ESTAT_ADD(ring_status_update);
9947 ESTAT_ADD(nic_irqs);
9948 ESTAT_ADD(nic_avoided_irqs);
9949 ESTAT_ADD(nic_tx_threshold_hit);
9950
Matt Carlson4452d092011-05-19 12:12:51 +00009951 ESTAT_ADD(mbuf_lwm_thresh_hit);
9952
Linus Torvalds1da177e2005-04-16 15:20:36 -07009953 return estats;
9954}
9955
Eric Dumazet511d2222010-07-07 20:44:24 +00009956static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9957 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009958{
9959 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009960 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009961 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9962
9963 if (!hw_stats)
9964 return old_stats;
9965
9966 stats->rx_packets = old_stats->rx_packets +
9967 get_stat64(&hw_stats->rx_ucast_packets) +
9968 get_stat64(&hw_stats->rx_mcast_packets) +
9969 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009970
Linus Torvalds1da177e2005-04-16 15:20:36 -07009971 stats->tx_packets = old_stats->tx_packets +
9972 get_stat64(&hw_stats->tx_ucast_packets) +
9973 get_stat64(&hw_stats->tx_mcast_packets) +
9974 get_stat64(&hw_stats->tx_bcast_packets);
9975
9976 stats->rx_bytes = old_stats->rx_bytes +
9977 get_stat64(&hw_stats->rx_octets);
9978 stats->tx_bytes = old_stats->tx_bytes +
9979 get_stat64(&hw_stats->tx_octets);
9980
9981 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009982 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009983 stats->tx_errors = old_stats->tx_errors +
9984 get_stat64(&hw_stats->tx_errors) +
9985 get_stat64(&hw_stats->tx_mac_errors) +
9986 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9987 get_stat64(&hw_stats->tx_discards);
9988
9989 stats->multicast = old_stats->multicast +
9990 get_stat64(&hw_stats->rx_mcast_packets);
9991 stats->collisions = old_stats->collisions +
9992 get_stat64(&hw_stats->tx_collisions);
9993
9994 stats->rx_length_errors = old_stats->rx_length_errors +
9995 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9996 get_stat64(&hw_stats->rx_undersize_packets);
9997
9998 stats->rx_over_errors = old_stats->rx_over_errors +
9999 get_stat64(&hw_stats->rxbds_empty);
10000 stats->rx_frame_errors = old_stats->rx_frame_errors +
10001 get_stat64(&hw_stats->rx_align_errors);
10002 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
10003 get_stat64(&hw_stats->tx_discards);
10004 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
10005 get_stat64(&hw_stats->tx_carrier_sense_errors);
10006
10007 stats->rx_crc_errors = old_stats->rx_crc_errors +
10008 calc_crc_errors(tp);
10009
John W. Linville4f63b872005-09-12 14:43:18 -070010010 stats->rx_missed_errors = old_stats->rx_missed_errors +
10011 get_stat64(&hw_stats->rx_discards);
10012
Eric Dumazetb0057c52010-10-10 19:55:52 +000010013 stats->rx_dropped = tp->rx_dropped;
10014
Linus Torvalds1da177e2005-04-16 15:20:36 -070010015 return stats;
10016}
10017
10018static inline u32 calc_crc(unsigned char *buf, int len)
10019{
10020 u32 reg;
10021 u32 tmp;
10022 int j, k;
10023
10024 reg = 0xffffffff;
10025
10026 for (j = 0; j < len; j++) {
10027 reg ^= buf[j];
10028
10029 for (k = 0; k < 8; k++) {
10030 tmp = reg & 0x01;
10031
10032 reg >>= 1;
10033
Matt Carlson859a5882010-04-05 10:19:28 +000010034 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010035 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010036 }
10037 }
10038
10039 return ~reg;
10040}
10041
10042static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
10043{
10044 /* accept or reject all multicast frames */
10045 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
10046 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
10047 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
10048 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
10049}
10050
10051static void __tg3_set_rx_mode(struct net_device *dev)
10052{
10053 struct tg3 *tp = netdev_priv(dev);
10054 u32 rx_mode;
10055
10056 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
10057 RX_MODE_KEEP_VLAN_TAG);
10058
Matt Carlsonbf933c82011-01-25 15:58:49 +000010059#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010060 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
10061 * flag clear.
10062 */
Joe Perches63c3a662011-04-26 08:12:10 +000010063 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010064 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
10065#endif
10066
10067 if (dev->flags & IFF_PROMISC) {
10068 /* Promiscuous mode. */
10069 rx_mode |= RX_MODE_PROMISC;
10070 } else if (dev->flags & IFF_ALLMULTI) {
10071 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010072 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +000010073 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010074 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010075 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010076 } else {
10077 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +000010078 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010079 u32 mc_filter[4] = { 0, };
10080 u32 regidx;
10081 u32 bit;
10082 u32 crc;
10083
Jiri Pirko22bedad2010-04-01 21:22:57 +000010084 netdev_for_each_mc_addr(ha, dev) {
10085 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010086 bit = ~crc & 0x7f;
10087 regidx = (bit & 0x60) >> 5;
10088 bit &= 0x1f;
10089 mc_filter[regidx] |= (1 << bit);
10090 }
10091
10092 tw32(MAC_HASH_REG_0, mc_filter[0]);
10093 tw32(MAC_HASH_REG_1, mc_filter[1]);
10094 tw32(MAC_HASH_REG_2, mc_filter[2]);
10095 tw32(MAC_HASH_REG_3, mc_filter[3]);
10096 }
10097
10098 if (rx_mode != tp->rx_mode) {
10099 tp->rx_mode = rx_mode;
10100 tw32_f(MAC_RX_MODE, rx_mode);
10101 udelay(10);
10102 }
10103}
10104
10105static void tg3_set_rx_mode(struct net_device *dev)
10106{
10107 struct tg3 *tp = netdev_priv(dev);
10108
Michael Chane75f7c92006-03-20 21:33:26 -080010109 if (!netif_running(dev))
10110 return;
10111
David S. Millerf47c11e2005-06-24 20:18:35 -070010112 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010113 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -070010114 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010115}
10116
Linus Torvalds1da177e2005-04-16 15:20:36 -070010117static int tg3_get_regs_len(struct net_device *dev)
10118{
Matt Carlson97bd8e42011-04-13 11:05:04 +000010119 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010120}
10121
10122static void tg3_get_regs(struct net_device *dev,
10123 struct ethtool_regs *regs, void *_p)
10124{
Linus Torvalds1da177e2005-04-16 15:20:36 -070010125 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010126
10127 regs->version = 0;
10128
Matt Carlson97bd8e42011-04-13 11:05:04 +000010129 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010130
Matt Carlson80096062010-08-02 11:26:06 +000010131 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010132 return;
10133
David S. Millerf47c11e2005-06-24 20:18:35 -070010134 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010135
Matt Carlson97bd8e42011-04-13 11:05:04 +000010136 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010137
David S. Millerf47c11e2005-06-24 20:18:35 -070010138 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139}
10140
10141static int tg3_get_eeprom_len(struct net_device *dev)
10142{
10143 struct tg3 *tp = netdev_priv(dev);
10144
10145 return tp->nvram_size;
10146}
10147
Linus Torvalds1da177e2005-04-16 15:20:36 -070010148static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10149{
10150 struct tg3 *tp = netdev_priv(dev);
10151 int ret;
10152 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -080010153 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010154 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010155
Joe Perches63c3a662011-04-26 08:12:10 +000010156 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010157 return -EINVAL;
10158
Matt Carlson80096062010-08-02 11:26:06 +000010159 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010160 return -EAGAIN;
10161
Linus Torvalds1da177e2005-04-16 15:20:36 -070010162 offset = eeprom->offset;
10163 len = eeprom->len;
10164 eeprom->len = 0;
10165
10166 eeprom->magic = TG3_EEPROM_MAGIC;
10167
10168 if (offset & 3) {
10169 /* adjustments to start on required 4 byte boundary */
10170 b_offset = offset & 3;
10171 b_count = 4 - b_offset;
10172 if (b_count > len) {
10173 /* i.e. offset=1 len=2 */
10174 b_count = len;
10175 }
Matt Carlsona9dc5292009-02-25 14:25:30 +000010176 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010177 if (ret)
10178 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +000010179 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010180 len -= b_count;
10181 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010182 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010183 }
10184
Lucas De Marchi25985ed2011-03-30 22:57:33 -030010185 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010186 pd = &data[eeprom->len];
10187 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010188 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010189 if (ret) {
10190 eeprom->len += i;
10191 return ret;
10192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010193 memcpy(pd + i, &val, 4);
10194 }
10195 eeprom->len += i;
10196
10197 if (len & 3) {
10198 /* read last bytes not ending on 4 byte boundary */
10199 pd = &data[eeprom->len];
10200 b_count = len & 3;
10201 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010202 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010203 if (ret)
10204 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010205 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010206 eeprom->len += b_count;
10207 }
10208 return 0;
10209}
10210
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010211static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010212
10213static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
10214{
10215 struct tg3 *tp = netdev_priv(dev);
10216 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -080010217 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010218 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010219 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010220
Matt Carlson80096062010-08-02 11:26:06 +000010221 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -080010222 return -EAGAIN;
10223
Joe Perches63c3a662011-04-26 08:12:10 +000010224 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +000010225 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010226 return -EINVAL;
10227
10228 offset = eeprom->offset;
10229 len = eeprom->len;
10230
10231 if ((b_offset = (offset & 3))) {
10232 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010233 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010234 if (ret)
10235 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010236 len += b_offset;
10237 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -070010238 if (len < 4)
10239 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010240 }
10241
10242 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -070010243 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010244 /* adjustments to end on required 4 byte boundary */
10245 odd_len = 1;
10246 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010247 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010248 if (ret)
10249 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010250 }
10251
10252 buf = data;
10253 if (b_offset || odd_len) {
10254 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010255 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010256 return -ENOMEM;
10257 if (b_offset)
10258 memcpy(buf, &start, 4);
10259 if (odd_len)
10260 memcpy(buf+len-4, &end, 4);
10261 memcpy(buf + b_offset, data, eeprom->len);
10262 }
10263
10264 ret = tg3_nvram_write_block(tp, offset, len, buf);
10265
10266 if (buf != data)
10267 kfree(buf);
10268
10269 return ret;
10270}
10271
10272static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10273{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010274 struct tg3 *tp = netdev_priv(dev);
10275
Joe Perches63c3a662011-04-26 08:12:10 +000010276 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010277 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010278 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010279 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010280 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10281 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010282 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010283
Linus Torvalds1da177e2005-04-16 15:20:36 -070010284 cmd->supported = (SUPPORTED_Autoneg);
10285
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010286 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010287 cmd->supported |= (SUPPORTED_1000baseT_Half |
10288 SUPPORTED_1000baseT_Full);
10289
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010290 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010291 cmd->supported |= (SUPPORTED_100baseT_Half |
10292 SUPPORTED_100baseT_Full |
10293 SUPPORTED_10baseT_Half |
10294 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -080010295 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -070010296 cmd->port = PORT_TP;
10297 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010298 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -070010299 cmd->port = PORT_FIBRE;
10300 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010301
Linus Torvalds1da177e2005-04-16 15:20:36 -070010302 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +000010303 if (tg3_flag(tp, PAUSE_AUTONEG)) {
10304 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
10305 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10306 cmd->advertising |= ADVERTISED_Pause;
10307 } else {
10308 cmd->advertising |= ADVERTISED_Pause |
10309 ADVERTISED_Asym_Pause;
10310 }
10311 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
10312 cmd->advertising |= ADVERTISED_Asym_Pause;
10313 }
10314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010315 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +000010316 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010317 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +000010318 } else {
David Decotigny70739492011-04-27 18:32:40 +000010319 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +000010320 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010321 }
Matt Carlson882e9792009-09-01 13:21:36 +000010322 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010323 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010324 cmd->autoneg = tp->link_config.autoneg;
10325 cmd->maxtxpkt = 0;
10326 cmd->maxrxpkt = 0;
10327 return 0;
10328}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010329
Linus Torvalds1da177e2005-04-16 15:20:36 -070010330static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10331{
10332 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010333 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010334
Joe Perches63c3a662011-04-26 08:12:10 +000010335 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010336 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010337 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010338 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010339 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10340 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010341 }
10342
Matt Carlson7e5856b2009-02-25 14:23:01 +000010343 if (cmd->autoneg != AUTONEG_ENABLE &&
10344 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010345 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010346
10347 if (cmd->autoneg == AUTONEG_DISABLE &&
10348 cmd->duplex != DUPLEX_FULL &&
10349 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010350 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010351
Matt Carlson7e5856b2009-02-25 14:23:01 +000010352 if (cmd->autoneg == AUTONEG_ENABLE) {
10353 u32 mask = ADVERTISED_Autoneg |
10354 ADVERTISED_Pause |
10355 ADVERTISED_Asym_Pause;
10356
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010357 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010358 mask |= ADVERTISED_1000baseT_Half |
10359 ADVERTISED_1000baseT_Full;
10360
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010361 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010362 mask |= ADVERTISED_100baseT_Half |
10363 ADVERTISED_100baseT_Full |
10364 ADVERTISED_10baseT_Half |
10365 ADVERTISED_10baseT_Full |
10366 ADVERTISED_TP;
10367 else
10368 mask |= ADVERTISED_FIBRE;
10369
10370 if (cmd->advertising & ~mask)
10371 return -EINVAL;
10372
10373 mask &= (ADVERTISED_1000baseT_Half |
10374 ADVERTISED_1000baseT_Full |
10375 ADVERTISED_100baseT_Half |
10376 ADVERTISED_100baseT_Full |
10377 ADVERTISED_10baseT_Half |
10378 ADVERTISED_10baseT_Full);
10379
10380 cmd->advertising &= mask;
10381 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010382 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010383 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010384 return -EINVAL;
10385
10386 if (cmd->duplex != DUPLEX_FULL)
10387 return -EINVAL;
10388 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010389 if (speed != SPEED_100 &&
10390 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010391 return -EINVAL;
10392 }
10393 }
10394
David S. Millerf47c11e2005-06-24 20:18:35 -070010395 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010396
10397 tp->link_config.autoneg = cmd->autoneg;
10398 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010399 tp->link_config.advertising = (cmd->advertising |
10400 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010401 tp->link_config.speed = SPEED_INVALID;
10402 tp->link_config.duplex = DUPLEX_INVALID;
10403 } else {
10404 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010405 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010406 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010407 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010408
Michael Chan24fcad62006-12-17 17:06:46 -080010409 tp->link_config.orig_speed = tp->link_config.speed;
10410 tp->link_config.orig_duplex = tp->link_config.duplex;
10411 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10412
Linus Torvalds1da177e2005-04-16 15:20:36 -070010413 if (netif_running(dev))
10414 tg3_setup_phy(tp, 1);
10415
David S. Millerf47c11e2005-06-24 20:18:35 -070010416 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010417
Linus Torvalds1da177e2005-04-16 15:20:36 -070010418 return 0;
10419}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010420
Linus Torvalds1da177e2005-04-16 15:20:36 -070010421static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10422{
10423 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010424
Linus Torvalds1da177e2005-04-16 15:20:36 -070010425 strcpy(info->driver, DRV_MODULE_NAME);
10426 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010427 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010428 strcpy(info->bus_info, pci_name(tp->pdev));
10429}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010430
Linus Torvalds1da177e2005-04-16 15:20:36 -070010431static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10432{
10433 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010434
Joe Perches63c3a662011-04-26 08:12:10 +000010435 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010436 wol->supported = WAKE_MAGIC;
10437 else
10438 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010439 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010440 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010441 wol->wolopts = WAKE_MAGIC;
10442 memset(&wol->sopass, 0, sizeof(wol->sopass));
10443}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010444
Linus Torvalds1da177e2005-04-16 15:20:36 -070010445static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10446{
10447 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010448 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010449
Linus Torvalds1da177e2005-04-16 15:20:36 -070010450 if (wol->wolopts & ~WAKE_MAGIC)
10451 return -EINVAL;
10452 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010453 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010454 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010455
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010456 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10457
David S. Millerf47c11e2005-06-24 20:18:35 -070010458 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010459 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010460 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010461 else
Joe Perches63c3a662011-04-26 08:12:10 +000010462 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010463 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010464
Linus Torvalds1da177e2005-04-16 15:20:36 -070010465 return 0;
10466}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010467
Linus Torvalds1da177e2005-04-16 15:20:36 -070010468static u32 tg3_get_msglevel(struct net_device *dev)
10469{
10470 struct tg3 *tp = netdev_priv(dev);
10471 return tp->msg_enable;
10472}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010473
Linus Torvalds1da177e2005-04-16 15:20:36 -070010474static void tg3_set_msglevel(struct net_device *dev, u32 value)
10475{
10476 struct tg3 *tp = netdev_priv(dev);
10477 tp->msg_enable = value;
10478}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010479
Linus Torvalds1da177e2005-04-16 15:20:36 -070010480static int tg3_nway_reset(struct net_device *dev)
10481{
10482 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010483 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010484
Linus Torvalds1da177e2005-04-16 15:20:36 -070010485 if (!netif_running(dev))
10486 return -EAGAIN;
10487
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010488 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010489 return -EINVAL;
10490
Joe Perches63c3a662011-04-26 08:12:10 +000010491 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010492 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010493 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010494 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010495 } else {
10496 u32 bmcr;
10497
10498 spin_lock_bh(&tp->lock);
10499 r = -EINVAL;
10500 tg3_readphy(tp, MII_BMCR, &bmcr);
10501 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10502 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010503 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010504 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10505 BMCR_ANENABLE);
10506 r = 0;
10507 }
10508 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010509 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010510
Linus Torvalds1da177e2005-04-16 15:20:36 -070010511 return r;
10512}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010513
Linus Torvalds1da177e2005-04-16 15:20:36 -070010514static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10515{
10516 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010517
Matt Carlson2c49a442010-09-30 10:34:35 +000010518 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010519 ering->rx_mini_max_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010520 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010521 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010522 else
10523 ering->rx_jumbo_max_pending = 0;
10524
10525 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010526
10527 ering->rx_pending = tp->rx_pending;
10528 ering->rx_mini_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010529 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010530 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10531 else
10532 ering->rx_jumbo_pending = 0;
10533
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010534 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010535}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010536
Linus Torvalds1da177e2005-04-16 15:20:36 -070010537static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10538{
10539 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010540 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010541
Matt Carlson2c49a442010-09-30 10:34:35 +000010542 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10543 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010544 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10545 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010546 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010547 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010548 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010549
Michael Chanbbe832c2005-06-24 20:20:04 -070010550 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010551 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010552 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010553 irq_sync = 1;
10554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010555
Michael Chanbbe832c2005-06-24 20:20:04 -070010556 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010557
Linus Torvalds1da177e2005-04-16 15:20:36 -070010558 tp->rx_pending = ering->rx_pending;
10559
Joe Perches63c3a662011-04-26 08:12:10 +000010560 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010561 tp->rx_pending > 63)
10562 tp->rx_pending = 63;
10563 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010564
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010565 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010566 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010567
10568 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010569 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010570 err = tg3_restart_hw(tp, 1);
10571 if (!err)
10572 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010573 }
10574
David S. Millerf47c11e2005-06-24 20:18:35 -070010575 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010576
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010577 if (irq_sync && !err)
10578 tg3_phy_start(tp);
10579
Michael Chanb9ec6c12006-07-25 16:37:27 -070010580 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010581}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010582
Linus Torvalds1da177e2005-04-16 15:20:36 -070010583static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10584{
10585 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010586
Joe Perches63c3a662011-04-26 08:12:10 +000010587 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010588
Steve Glendinninge18ce342008-12-16 02:00:00 -080010589 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010590 epause->rx_pause = 1;
10591 else
10592 epause->rx_pause = 0;
10593
Steve Glendinninge18ce342008-12-16 02:00:00 -080010594 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010595 epause->tx_pause = 1;
10596 else
10597 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010598}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010599
Linus Torvalds1da177e2005-04-16 15:20:36 -070010600static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10601{
10602 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010603 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010604
Joe Perches63c3a662011-04-26 08:12:10 +000010605 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010606 u32 newadv;
10607 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010608
Matt Carlson27121682010-02-17 15:16:57 +000010609 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010610
Matt Carlson27121682010-02-17 15:16:57 +000010611 if (!(phydev->supported & SUPPORTED_Pause) ||
10612 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010613 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010614 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010615
Matt Carlson27121682010-02-17 15:16:57 +000010616 tp->link_config.flowctrl = 0;
10617 if (epause->rx_pause) {
10618 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010619
Matt Carlson27121682010-02-17 15:16:57 +000010620 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010621 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010622 newadv = ADVERTISED_Pause;
10623 } else
10624 newadv = ADVERTISED_Pause |
10625 ADVERTISED_Asym_Pause;
10626 } else if (epause->tx_pause) {
10627 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10628 newadv = ADVERTISED_Asym_Pause;
10629 } else
10630 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010631
Matt Carlson27121682010-02-17 15:16:57 +000010632 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010633 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010634 else
Joe Perches63c3a662011-04-26 08:12:10 +000010635 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010636
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010637 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010638 u32 oldadv = phydev->advertising &
10639 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10640 if (oldadv != newadv) {
10641 phydev->advertising &=
10642 ~(ADVERTISED_Pause |
10643 ADVERTISED_Asym_Pause);
10644 phydev->advertising |= newadv;
10645 if (phydev->autoneg) {
10646 /*
10647 * Always renegotiate the link to
10648 * inform our link partner of our
10649 * flow control settings, even if the
10650 * flow control is forced. Let
10651 * tg3_adjust_link() do the final
10652 * flow control setup.
10653 */
10654 return phy_start_aneg(phydev);
10655 }
10656 }
10657
10658 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010659 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010660 } else {
10661 tp->link_config.orig_advertising &=
10662 ~(ADVERTISED_Pause |
10663 ADVERTISED_Asym_Pause);
10664 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010665 }
10666 } else {
10667 int irq_sync = 0;
10668
10669 if (netif_running(dev)) {
10670 tg3_netif_stop(tp);
10671 irq_sync = 1;
10672 }
10673
10674 tg3_full_lock(tp, irq_sync);
10675
10676 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010677 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010678 else
Joe Perches63c3a662011-04-26 08:12:10 +000010679 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010680 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010681 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010682 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010683 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010684 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010685 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010686 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010687 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010688
10689 if (netif_running(dev)) {
10690 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10691 err = tg3_restart_hw(tp, 1);
10692 if (!err)
10693 tg3_netif_start(tp);
10694 }
10695
10696 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010698
Michael Chanb9ec6c12006-07-25 16:37:27 -070010699 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010700}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010701
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010702static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010703{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010704 switch (sset) {
10705 case ETH_SS_TEST:
10706 return TG3_NUM_TEST;
10707 case ETH_SS_STATS:
10708 return TG3_NUM_STATS;
10709 default:
10710 return -EOPNOTSUPP;
10711 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010712}
10713
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010714static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010715{
10716 switch (stringset) {
10717 case ETH_SS_STATS:
10718 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10719 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010720 case ETH_SS_TEST:
10721 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10722 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010723 default:
10724 WARN_ON(1); /* we need a WARN() */
10725 break;
10726 }
10727}
10728
stephen hemminger81b87092011-04-04 08:43:50 +000010729static int tg3_set_phys_id(struct net_device *dev,
10730 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010731{
10732 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010733
10734 if (!netif_running(tp->dev))
10735 return -EAGAIN;
10736
stephen hemminger81b87092011-04-04 08:43:50 +000010737 switch (state) {
10738 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010739 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010740
stephen hemminger81b87092011-04-04 08:43:50 +000010741 case ETHTOOL_ID_ON:
10742 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10743 LED_CTRL_1000MBPS_ON |
10744 LED_CTRL_100MBPS_ON |
10745 LED_CTRL_10MBPS_ON |
10746 LED_CTRL_TRAFFIC_OVERRIDE |
10747 LED_CTRL_TRAFFIC_BLINK |
10748 LED_CTRL_TRAFFIC_LED);
10749 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010750
stephen hemminger81b87092011-04-04 08:43:50 +000010751 case ETHTOOL_ID_OFF:
10752 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10753 LED_CTRL_TRAFFIC_OVERRIDE);
10754 break;
Michael Chan4009a932005-09-05 17:52:54 -070010755
stephen hemminger81b87092011-04-04 08:43:50 +000010756 case ETHTOOL_ID_INACTIVE:
10757 tw32(MAC_LED_CTRL, tp->led_ctrl);
10758 break;
Michael Chan4009a932005-09-05 17:52:54 -070010759 }
stephen hemminger81b87092011-04-04 08:43:50 +000010760
Michael Chan4009a932005-09-05 17:52:54 -070010761 return 0;
10762}
10763
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010764static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010765 struct ethtool_stats *estats, u64 *tmp_stats)
10766{
10767 struct tg3 *tp = netdev_priv(dev);
10768 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10769}
10770
Matt Carlson535a4902011-07-20 10:20:56 +000010771static __be32 *tg3_vpd_readblock(struct tg3 *tp, u32 *vpdlen)
Matt Carlsonc3e94502011-04-13 11:05:08 +000010772{
10773 int i;
10774 __be32 *buf;
10775 u32 offset = 0, len = 0;
10776 u32 magic, val;
10777
Joe Perches63c3a662011-04-26 08:12:10 +000010778 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010779 return NULL;
10780
10781 if (magic == TG3_EEPROM_MAGIC) {
10782 for (offset = TG3_NVM_DIR_START;
10783 offset < TG3_NVM_DIR_END;
10784 offset += TG3_NVM_DIRENT_SIZE) {
10785 if (tg3_nvram_read(tp, offset, &val))
10786 return NULL;
10787
10788 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10789 TG3_NVM_DIRTYPE_EXTVPD)
10790 break;
10791 }
10792
10793 if (offset != TG3_NVM_DIR_END) {
10794 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10795 if (tg3_nvram_read(tp, offset + 4, &offset))
10796 return NULL;
10797
10798 offset = tg3_nvram_logical_addr(tp, offset);
10799 }
10800 }
10801
10802 if (!offset || !len) {
10803 offset = TG3_NVM_VPD_OFF;
10804 len = TG3_NVM_VPD_LEN;
10805 }
10806
10807 buf = kmalloc(len, GFP_KERNEL);
10808 if (buf == NULL)
10809 return NULL;
10810
10811 if (magic == TG3_EEPROM_MAGIC) {
10812 for (i = 0; i < len; i += 4) {
10813 /* The data is in little-endian format in NVRAM.
10814 * Use the big-endian read routines to preserve
10815 * the byte order as it exists in NVRAM.
10816 */
10817 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10818 goto error;
10819 }
10820 } else {
10821 u8 *ptr;
10822 ssize_t cnt;
10823 unsigned int pos = 0;
10824
10825 ptr = (u8 *)&buf[0];
10826 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10827 cnt = pci_read_vpd(tp->pdev, pos,
10828 len - pos, ptr);
10829 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10830 cnt = 0;
10831 else if (cnt < 0)
10832 goto error;
10833 }
10834 if (pos != len)
10835 goto error;
10836 }
10837
Matt Carlson535a4902011-07-20 10:20:56 +000010838 *vpdlen = len;
10839
Matt Carlsonc3e94502011-04-13 11:05:08 +000010840 return buf;
10841
10842error:
10843 kfree(buf);
10844 return NULL;
10845}
10846
Michael Chan566f86a2005-05-29 14:56:58 -070010847#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010848#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10849#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10850#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010851#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10852#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
Matt Carlsonbda18fa2011-07-20 10:20:57 +000010853#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x50
Michael Chanb16250e2006-09-27 16:10:14 -070010854#define NVRAM_SELFBOOT_HW_SIZE 0x20
10855#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010856
10857static int tg3_test_nvram(struct tg3 *tp)
10858{
Matt Carlson535a4902011-07-20 10:20:56 +000010859 u32 csum, magic, len;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010860 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010861 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010862
Joe Perches63c3a662011-04-26 08:12:10 +000010863 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010864 return 0;
10865
Matt Carlsone4f34112009-02-25 14:25:00 +000010866 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010867 return -EIO;
10868
Michael Chan1b277772006-03-20 22:27:48 -080010869 if (magic == TG3_EEPROM_MAGIC)
10870 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010871 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010872 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10873 TG3_EEPROM_SB_FORMAT_1) {
10874 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10875 case TG3_EEPROM_SB_REVISION_0:
10876 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10877 break;
10878 case TG3_EEPROM_SB_REVISION_2:
10879 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10880 break;
10881 case TG3_EEPROM_SB_REVISION_3:
10882 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10883 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010884 case TG3_EEPROM_SB_REVISION_4:
10885 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10886 break;
10887 case TG3_EEPROM_SB_REVISION_5:
10888 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10889 break;
10890 case TG3_EEPROM_SB_REVISION_6:
10891 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10892 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010893 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010894 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010895 }
10896 } else
Michael Chan1b277772006-03-20 22:27:48 -080010897 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010898 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10899 size = NVRAM_SELFBOOT_HW_SIZE;
10900 else
Michael Chan1b277772006-03-20 22:27:48 -080010901 return -EIO;
10902
10903 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010904 if (buf == NULL)
10905 return -ENOMEM;
10906
Michael Chan1b277772006-03-20 22:27:48 -080010907 err = -EIO;
10908 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010909 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10910 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010911 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010912 }
Michael Chan1b277772006-03-20 22:27:48 -080010913 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010914 goto out;
10915
Michael Chan1b277772006-03-20 22:27:48 -080010916 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010917 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010918 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010919 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010920 u8 *buf8 = (u8 *) buf, csum8 = 0;
10921
Al Virob9fc7dc2007-12-17 22:59:57 -080010922 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010923 TG3_EEPROM_SB_REVISION_2) {
10924 /* For rev 2, the csum doesn't include the MBA. */
10925 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10926 csum8 += buf8[i];
10927 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10928 csum8 += buf8[i];
10929 } else {
10930 for (i = 0; i < size; i++)
10931 csum8 += buf8[i];
10932 }
Michael Chan1b277772006-03-20 22:27:48 -080010933
Adrian Bunkad96b482006-04-05 22:21:04 -070010934 if (csum8 == 0) {
10935 err = 0;
10936 goto out;
10937 }
10938
10939 err = -EIO;
10940 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010941 }
Michael Chan566f86a2005-05-29 14:56:58 -070010942
Al Virob9fc7dc2007-12-17 22:59:57 -080010943 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010944 TG3_EEPROM_MAGIC_HW) {
10945 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010946 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010947 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010948
10949 /* Separate the parity bits and the data bytes. */
10950 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10951 if ((i == 0) || (i == 8)) {
10952 int l;
10953 u8 msk;
10954
10955 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10956 parity[k++] = buf8[i] & msk;
10957 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010958 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010959 int l;
10960 u8 msk;
10961
10962 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10963 parity[k++] = buf8[i] & msk;
10964 i++;
10965
10966 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10967 parity[k++] = buf8[i] & msk;
10968 i++;
10969 }
10970 data[j++] = buf8[i];
10971 }
10972
10973 err = -EIO;
10974 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10975 u8 hw8 = hweight8(data[i]);
10976
10977 if ((hw8 & 0x1) && parity[i])
10978 goto out;
10979 else if (!(hw8 & 0x1) && !parity[i])
10980 goto out;
10981 }
10982 err = 0;
10983 goto out;
10984 }
10985
Matt Carlson01c3a392011-03-09 16:58:20 +000010986 err = -EIO;
10987
Michael Chan566f86a2005-05-29 14:56:58 -070010988 /* Bootstrap checksum at offset 0x10 */
10989 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010990 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010991 goto out;
10992
10993 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10994 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010995 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010996 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010997
Matt Carlsonc3e94502011-04-13 11:05:08 +000010998 kfree(buf);
10999
Matt Carlson535a4902011-07-20 10:20:56 +000011000 buf = tg3_vpd_readblock(tp, &len);
Matt Carlsonc3e94502011-04-13 11:05:08 +000011001 if (!buf)
11002 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000011003
Matt Carlson535a4902011-07-20 10:20:56 +000011004 i = pci_vpd_find_tag((u8 *)buf, 0, len, PCI_VPD_LRDT_RO_DATA);
Matt Carlsond4894f32011-03-09 16:58:21 +000011005 if (i > 0) {
11006 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
11007 if (j < 0)
11008 goto out;
11009
Matt Carlson535a4902011-07-20 10:20:56 +000011010 if (i + PCI_VPD_LRDT_TAG_SIZE + j > len)
Matt Carlsond4894f32011-03-09 16:58:21 +000011011 goto out;
11012
11013 i += PCI_VPD_LRDT_TAG_SIZE;
11014 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
11015 PCI_VPD_RO_KEYWORD_CHKSUM);
11016 if (j > 0) {
11017 u8 csum8 = 0;
11018
11019 j += PCI_VPD_INFO_FLD_HDR_SIZE;
11020
11021 for (i = 0; i <= j; i++)
11022 csum8 += ((u8 *)buf)[i];
11023
11024 if (csum8)
11025 goto out;
11026 }
11027 }
11028
Michael Chan566f86a2005-05-29 14:56:58 -070011029 err = 0;
11030
11031out:
11032 kfree(buf);
11033 return err;
11034}
11035
Michael Chanca430072005-05-29 14:57:23 -070011036#define TG3_SERDES_TIMEOUT_SEC 2
11037#define TG3_COPPER_TIMEOUT_SEC 6
11038
11039static int tg3_test_link(struct tg3 *tp)
11040{
11041 int i, max;
11042
11043 if (!netif_running(tp->dev))
11044 return -ENODEV;
11045
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011046 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070011047 max = TG3_SERDES_TIMEOUT_SEC;
11048 else
11049 max = TG3_COPPER_TIMEOUT_SEC;
11050
11051 for (i = 0; i < max; i++) {
11052 if (netif_carrier_ok(tp->dev))
11053 return 0;
11054
11055 if (msleep_interruptible(1000))
11056 break;
11057 }
11058
11059 return -EIO;
11060}
11061
Michael Chana71116d2005-05-29 14:58:11 -070011062/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080011063static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070011064{
Michael Chanb16250e2006-09-27 16:10:14 -070011065 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070011066 u32 offset, read_mask, write_mask, val, save_val, read_val;
11067 static struct {
11068 u16 offset;
11069 u16 flags;
11070#define TG3_FL_5705 0x1
11071#define TG3_FL_NOT_5705 0x2
11072#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070011073#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070011074 u32 read_mask;
11075 u32 write_mask;
11076 } reg_tbl[] = {
11077 /* MAC Control Registers */
11078 { MAC_MODE, TG3_FL_NOT_5705,
11079 0x00000000, 0x00ef6f8c },
11080 { MAC_MODE, TG3_FL_5705,
11081 0x00000000, 0x01ef6b8c },
11082 { MAC_STATUS, TG3_FL_NOT_5705,
11083 0x03800107, 0x00000000 },
11084 { MAC_STATUS, TG3_FL_5705,
11085 0x03800100, 0x00000000 },
11086 { MAC_ADDR_0_HIGH, 0x0000,
11087 0x00000000, 0x0000ffff },
11088 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000011089 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070011090 { MAC_RX_MTU_SIZE, 0x0000,
11091 0x00000000, 0x0000ffff },
11092 { MAC_TX_MODE, 0x0000,
11093 0x00000000, 0x00000070 },
11094 { MAC_TX_LENGTHS, 0x0000,
11095 0x00000000, 0x00003fff },
11096 { MAC_RX_MODE, TG3_FL_NOT_5705,
11097 0x00000000, 0x000007fc },
11098 { MAC_RX_MODE, TG3_FL_5705,
11099 0x00000000, 0x000007dc },
11100 { MAC_HASH_REG_0, 0x0000,
11101 0x00000000, 0xffffffff },
11102 { MAC_HASH_REG_1, 0x0000,
11103 0x00000000, 0xffffffff },
11104 { MAC_HASH_REG_2, 0x0000,
11105 0x00000000, 0xffffffff },
11106 { MAC_HASH_REG_3, 0x0000,
11107 0x00000000, 0xffffffff },
11108
11109 /* Receive Data and Receive BD Initiator Control Registers. */
11110 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
11111 0x00000000, 0xffffffff },
11112 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
11113 0x00000000, 0xffffffff },
11114 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
11115 0x00000000, 0x00000003 },
11116 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
11117 0x00000000, 0xffffffff },
11118 { RCVDBDI_STD_BD+0, 0x0000,
11119 0x00000000, 0xffffffff },
11120 { RCVDBDI_STD_BD+4, 0x0000,
11121 0x00000000, 0xffffffff },
11122 { RCVDBDI_STD_BD+8, 0x0000,
11123 0x00000000, 0xffff0002 },
11124 { RCVDBDI_STD_BD+0xc, 0x0000,
11125 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011126
Michael Chana71116d2005-05-29 14:58:11 -070011127 /* Receive BD Initiator Control Registers. */
11128 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
11129 0x00000000, 0xffffffff },
11130 { RCVBDI_STD_THRESH, TG3_FL_5705,
11131 0x00000000, 0x000003ff },
11132 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
11133 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011134
Michael Chana71116d2005-05-29 14:58:11 -070011135 /* Host Coalescing Control Registers. */
11136 { HOSTCC_MODE, TG3_FL_NOT_5705,
11137 0x00000000, 0x00000004 },
11138 { HOSTCC_MODE, TG3_FL_5705,
11139 0x00000000, 0x000000f6 },
11140 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
11141 0x00000000, 0xffffffff },
11142 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
11143 0x00000000, 0x000003ff },
11144 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
11145 0x00000000, 0xffffffff },
11146 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
11147 0x00000000, 0x000003ff },
11148 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
11149 0x00000000, 0xffffffff },
11150 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11151 0x00000000, 0x000000ff },
11152 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
11153 0x00000000, 0xffffffff },
11154 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
11155 0x00000000, 0x000000ff },
11156 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
11157 0x00000000, 0xffffffff },
11158 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
11159 0x00000000, 0xffffffff },
11160 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11161 0x00000000, 0xffffffff },
11162 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11163 0x00000000, 0x000000ff },
11164 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
11165 0x00000000, 0xffffffff },
11166 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
11167 0x00000000, 0x000000ff },
11168 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
11169 0x00000000, 0xffffffff },
11170 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
11171 0x00000000, 0xffffffff },
11172 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
11173 0x00000000, 0xffffffff },
11174 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
11175 0x00000000, 0xffffffff },
11176 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
11177 0x00000000, 0xffffffff },
11178 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
11179 0xffffffff, 0x00000000 },
11180 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
11181 0xffffffff, 0x00000000 },
11182
11183 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070011184 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011185 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070011186 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070011187 0x00000000, 0x007fffff },
11188 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
11189 0x00000000, 0x0000003f },
11190 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
11191 0x00000000, 0x000001ff },
11192 { BUFMGR_MB_HIGH_WATER, 0x0000,
11193 0x00000000, 0x000001ff },
11194 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
11195 0xffffffff, 0x00000000 },
11196 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
11197 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011198
Michael Chana71116d2005-05-29 14:58:11 -070011199 /* Mailbox Registers */
11200 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
11201 0x00000000, 0x000001ff },
11202 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
11203 0x00000000, 0x000001ff },
11204 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
11205 0x00000000, 0x000007ff },
11206 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
11207 0x00000000, 0x000001ff },
11208
11209 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
11210 };
11211
Michael Chanb16250e2006-09-27 16:10:14 -070011212 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000011213 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070011214 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000011215 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070011216 is_5750 = 1;
11217 }
Michael Chana71116d2005-05-29 14:58:11 -070011218
11219 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
11220 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
11221 continue;
11222
11223 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
11224 continue;
11225
Joe Perches63c3a662011-04-26 08:12:10 +000011226 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070011227 (reg_tbl[i].flags & TG3_FL_NOT_5788))
11228 continue;
11229
Michael Chanb16250e2006-09-27 16:10:14 -070011230 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
11231 continue;
11232
Michael Chana71116d2005-05-29 14:58:11 -070011233 offset = (u32) reg_tbl[i].offset;
11234 read_mask = reg_tbl[i].read_mask;
11235 write_mask = reg_tbl[i].write_mask;
11236
11237 /* Save the original register content */
11238 save_val = tr32(offset);
11239
11240 /* Determine the read-only value. */
11241 read_val = save_val & read_mask;
11242
11243 /* Write zero to the register, then make sure the read-only bits
11244 * are not changed and the read/write bits are all zeros.
11245 */
11246 tw32(offset, 0);
11247
11248 val = tr32(offset);
11249
11250 /* Test the read-only and read/write bits. */
11251 if (((val & read_mask) != read_val) || (val & write_mask))
11252 goto out;
11253
11254 /* Write ones to all the bits defined by RdMask and WrMask, then
11255 * make sure the read-only bits are not changed and the
11256 * read/write bits are all ones.
11257 */
11258 tw32(offset, read_mask | write_mask);
11259
11260 val = tr32(offset);
11261
11262 /* Test the read-only bits. */
11263 if ((val & read_mask) != read_val)
11264 goto out;
11265
11266 /* Test the read/write bits. */
11267 if ((val & write_mask) != write_mask)
11268 goto out;
11269
11270 tw32(offset, save_val);
11271 }
11272
11273 return 0;
11274
11275out:
Michael Chan9f88f292006-12-07 00:22:54 -080011276 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000011277 netdev_err(tp->dev,
11278 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070011279 tw32(offset, save_val);
11280 return -EIO;
11281}
11282
Michael Chan7942e1d2005-05-29 14:58:36 -070011283static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
11284{
Arjan van de Venf71e1302006-03-03 21:33:57 -050011285 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070011286 int i;
11287 u32 j;
11288
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020011289 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070011290 for (j = 0; j < len; j += 4) {
11291 u32 val;
11292
11293 tg3_write_mem(tp, offset + j, test_pattern[i]);
11294 tg3_read_mem(tp, offset + j, &val);
11295 if (val != test_pattern[i])
11296 return -EIO;
11297 }
11298 }
11299 return 0;
11300}
11301
11302static int tg3_test_memory(struct tg3 *tp)
11303{
11304 static struct mem_entry {
11305 u32 offset;
11306 u32 len;
11307 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080011308 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070011309 { 0x00002000, 0x1c000},
11310 { 0xffffffff, 0x00000}
11311 }, mem_tbl_5705[] = {
11312 { 0x00000100, 0x0000c},
11313 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070011314 { 0x00004000, 0x00800},
11315 { 0x00006000, 0x01000},
11316 { 0x00008000, 0x02000},
11317 { 0x00010000, 0x0e000},
11318 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080011319 }, mem_tbl_5755[] = {
11320 { 0x00000200, 0x00008},
11321 { 0x00004000, 0x00800},
11322 { 0x00006000, 0x00800},
11323 { 0x00008000, 0x02000},
11324 { 0x00010000, 0x0c000},
11325 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070011326 }, mem_tbl_5906[] = {
11327 { 0x00000200, 0x00008},
11328 { 0x00004000, 0x00400},
11329 { 0x00006000, 0x00400},
11330 { 0x00008000, 0x01000},
11331 { 0x00010000, 0x01000},
11332 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011333 }, mem_tbl_5717[] = {
11334 { 0x00000200, 0x00008},
11335 { 0x00010000, 0x0a000},
11336 { 0x00020000, 0x13c00},
11337 { 0xffffffff, 0x00000}
11338 }, mem_tbl_57765[] = {
11339 { 0x00000200, 0x00008},
11340 { 0x00004000, 0x00800},
11341 { 0x00006000, 0x09800},
11342 { 0x00010000, 0x0a000},
11343 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011344 };
11345 struct mem_entry *mem_tbl;
11346 int err = 0;
11347 int i;
11348
Joe Perches63c3a662011-04-26 08:12:10 +000011349 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011350 mem_tbl = mem_tbl_5717;
11351 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11352 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011353 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011354 mem_tbl = mem_tbl_5755;
11355 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11356 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011357 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011358 mem_tbl = mem_tbl_5705;
11359 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011360 mem_tbl = mem_tbl_570x;
11361
11362 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011363 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11364 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011365 break;
11366 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011367
Michael Chan7942e1d2005-05-29 14:58:36 -070011368 return err;
11369}
11370
Matt Carlsonbb158d62011-04-25 12:42:47 +000011371#define TG3_TSO_MSS 500
11372
11373#define TG3_TSO_IP_HDR_LEN 20
11374#define TG3_TSO_TCP_HDR_LEN 20
11375#define TG3_TSO_TCP_OPT_LEN 12
11376
11377static const u8 tg3_tso_header[] = {
113780x08, 0x00,
113790x45, 0x00, 0x00, 0x00,
113800x00, 0x00, 0x40, 0x00,
113810x40, 0x06, 0x00, 0x00,
113820x0a, 0x00, 0x00, 0x01,
113830x0a, 0x00, 0x00, 0x02,
113840x0d, 0x00, 0xe0, 0x00,
113850x00, 0x00, 0x01, 0x00,
113860x00, 0x00, 0x02, 0x00,
113870x80, 0x10, 0x10, 0x00,
113880x14, 0x09, 0x00, 0x00,
113890x01, 0x01, 0x08, 0x0a,
113900x11, 0x11, 0x11, 0x11,
113910x11, 0x11, 0x11, 0x11,
11392};
Michael Chan9f40dea2005-09-05 17:53:06 -070011393
Matt Carlson28a45952011-08-19 13:58:22 +000011394static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, bool tso_loopback)
Michael Chanc76949a2005-05-29 14:58:59 -070011395{
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011396 u32 rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011397 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Matt Carlson84b67b22011-07-27 14:20:52 +000011398 u32 budget;
Michael Chanc76949a2005-05-29 14:58:59 -070011399 struct sk_buff *skb, *rx_skb;
11400 u8 *tx_data;
11401 dma_addr_t map;
11402 int num_pkts, tx_len, rx_len, i, err;
11403 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011404 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011405 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011406
Matt Carlsonc8873402010-02-12 14:47:11 +000011407 tnapi = &tp->napi[0];
11408 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011409 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011410 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011411 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011412 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011413 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011414 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011415 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011416
Michael Chanc76949a2005-05-29 14:58:59 -070011417 err = -EIO;
11418
Matt Carlson4852a862011-04-13 11:05:07 +000011419 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011420 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011421 if (!skb)
11422 return -ENOMEM;
11423
Michael Chanc76949a2005-05-29 14:58:59 -070011424 tx_data = skb_put(skb, tx_len);
11425 memcpy(tx_data, tp->dev->dev_addr, 6);
11426 memset(tx_data + 6, 0x0, 8);
11427
Matt Carlson4852a862011-04-13 11:05:07 +000011428 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011429
Matt Carlson28a45952011-08-19 13:58:22 +000011430 if (tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011431 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11432
11433 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11434 TG3_TSO_TCP_OPT_LEN;
11435
11436 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11437 sizeof(tg3_tso_header));
11438 mss = TG3_TSO_MSS;
11439
11440 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11441 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11442
11443 /* Set the total length field in the IP header */
11444 iph->tot_len = htons((u16)(mss + hdr_len));
11445
11446 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11447 TXD_FLAG_CPU_POST_DMA);
11448
Joe Perches63c3a662011-04-26 08:12:10 +000011449 if (tg3_flag(tp, HW_TSO_1) ||
11450 tg3_flag(tp, HW_TSO_2) ||
11451 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011452 struct tcphdr *th;
11453 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11454 th = (struct tcphdr *)&tx_data[val];
11455 th->check = 0;
11456 } else
11457 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11458
Joe Perches63c3a662011-04-26 08:12:10 +000011459 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011460 mss |= (hdr_len & 0xc) << 12;
11461 if (hdr_len & 0x10)
11462 base_flags |= 0x00000010;
11463 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011464 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011465 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011466 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011467 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11468 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11469 } else {
11470 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11471 }
11472
11473 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11474 } else {
11475 num_pkts = 1;
11476 data_off = ETH_HLEN;
11477 }
11478
11479 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011480 tx_data[i] = (u8) (i & 0xff);
11481
Alexander Duyckf4188d82009-12-02 16:48:38 +000011482 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11483 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011484 dev_kfree_skb(skb);
11485 return -EIO;
11486 }
Michael Chanc76949a2005-05-29 14:58:59 -070011487
Matt Carlson0d681b22011-07-27 14:20:49 +000011488 val = tnapi->tx_prod;
11489 tnapi->tx_buffers[val].skb = skb;
11490 dma_unmap_addr_set(&tnapi->tx_buffers[val], mapping, map);
11491
Michael Chanc76949a2005-05-29 14:58:59 -070011492 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011493 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011494
11495 udelay(10);
11496
Matt Carlson898a56f2009-08-28 14:02:40 +000011497 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011498
Matt Carlson84b67b22011-07-27 14:20:52 +000011499 budget = tg3_tx_avail(tnapi);
11500 if (tg3_tx_frag_set(tnapi, &val, &budget, map, tx_len,
Matt Carlsond1a3b732011-07-27 14:20:51 +000011501 base_flags | TXD_FLAG_END, mss, 0)) {
11502 tnapi->tx_buffers[val].skb = NULL;
11503 dev_kfree_skb(skb);
11504 return -EIO;
11505 }
Michael Chanc76949a2005-05-29 14:58:59 -070011506
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011507 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011508
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011509 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11510 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011511
11512 udelay(10);
11513
Matt Carlson303fc922009-11-02 14:27:34 +000011514 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11515 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011516 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011517 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011518
11519 udelay(10);
11520
Matt Carlson898a56f2009-08-28 14:02:40 +000011521 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11522 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011523 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011524 (rx_idx == (rx_start_idx + num_pkts)))
11525 break;
11526 }
11527
Matt Carlson0d681b22011-07-27 14:20:49 +000011528 tg3_tx_skb_unmap(tnapi, tnapi->tx_prod - 1, 0);
Michael Chanc76949a2005-05-29 14:58:59 -070011529 dev_kfree_skb(skb);
11530
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011531 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011532 goto out;
11533
11534 if (rx_idx != rx_start_idx + num_pkts)
11535 goto out;
11536
Matt Carlsonbb158d62011-04-25 12:42:47 +000011537 val = data_off;
11538 while (rx_idx != rx_start_idx) {
11539 desc = &rnapi->rx_rcb[rx_start_idx++];
11540 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11541 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011542
Matt Carlsonbb158d62011-04-25 12:42:47 +000011543 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11544 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011545 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011546
Matt Carlsonbb158d62011-04-25 12:42:47 +000011547 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11548 - ETH_FCS_LEN;
11549
Matt Carlson28a45952011-08-19 13:58:22 +000011550 if (!tso_loopback) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011551 if (rx_len != tx_len)
11552 goto out;
11553
11554 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11555 if (opaque_key != RXD_OPAQUE_RING_STD)
11556 goto out;
11557 } else {
11558 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11559 goto out;
11560 }
11561 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11562 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011563 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011564 goto out;
11565 }
11566
11567 if (opaque_key == RXD_OPAQUE_RING_STD) {
11568 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11569 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11570 mapping);
11571 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
11572 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11573 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11574 mapping);
11575 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011576 goto out;
11577
Matt Carlsonbb158d62011-04-25 12:42:47 +000011578 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11579 PCI_DMA_FROMDEVICE);
11580
11581 for (i = data_off; i < rx_len; i++, val++) {
11582 if (*(rx_skb->data + i) != (u8) (val & 0xff))
11583 goto out;
11584 }
Matt Carlson4852a862011-04-13 11:05:07 +000011585 }
11586
Michael Chanc76949a2005-05-29 14:58:59 -070011587 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011588
Michael Chanc76949a2005-05-29 14:58:59 -070011589 /* tg3_free_rings will unmap and free the rx_skb */
11590out:
11591 return err;
11592}
11593
Matt Carlson00c266b2011-04-25 12:42:46 +000011594#define TG3_STD_LOOPBACK_FAILED 1
11595#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011596#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson28a45952011-08-19 13:58:22 +000011597#define TG3_LOOPBACK_FAILED \
11598 (TG3_STD_LOOPBACK_FAILED | \
11599 TG3_JMB_LOOPBACK_FAILED | \
11600 TG3_TSO_LOOPBACK_FAILED)
Matt Carlson00c266b2011-04-25 12:42:46 +000011601
Matt Carlson941ec902011-08-19 13:58:23 +000011602static int tg3_test_loopback(struct tg3 *tp, u64 *data, bool do_extlpbk)
Michael Chan9f40dea2005-09-05 17:53:06 -070011603{
Matt Carlson28a45952011-08-19 13:58:22 +000011604 int err = -EIO;
Matt Carlson2215e242011-08-19 13:58:19 +000011605 u32 eee_cap;
Michael Chan9f40dea2005-09-05 17:53:06 -070011606
Matt Carlsonab789042011-01-25 15:58:54 +000011607 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11608 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11609
Matt Carlson28a45952011-08-19 13:58:22 +000011610 if (!netif_running(tp->dev)) {
11611 data[0] = TG3_LOOPBACK_FAILED;
11612 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011613 if (do_extlpbk)
11614 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlson28a45952011-08-19 13:58:22 +000011615 goto done;
11616 }
11617
Michael Chanb9ec6c12006-07-25 16:37:27 -070011618 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011619 if (err) {
Matt Carlson28a45952011-08-19 13:58:22 +000011620 data[0] = TG3_LOOPBACK_FAILED;
11621 data[1] = TG3_LOOPBACK_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011622 if (do_extlpbk)
11623 data[2] = TG3_LOOPBACK_FAILED;
Matt Carlsonab789042011-01-25 15:58:54 +000011624 goto done;
11625 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011626
Joe Perches63c3a662011-04-26 08:12:10 +000011627 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011628 int i;
11629
11630 /* Reroute all rx packets to the 1st queue */
11631 for (i = MAC_RSS_INDIR_TBL_0;
11632 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11633 tw32(i, 0x0);
11634 }
11635
Matt Carlson6e01b202011-08-19 13:58:20 +000011636 /* HW errata - mac loopback fails in some cases on 5780.
11637 * Normal traffic and PHY loopback are not affected by
11638 * errata. Also, the MAC loopback test is deprecated for
11639 * all newer ASIC revisions.
11640 */
11641 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
11642 !tg3_flag(tp, CPMU_PRESENT)) {
11643 tg3_mac_loopback(tp, true);
Matt Carlson9936bcf2007-10-10 18:03:07 -070011644
Matt Carlson28a45952011-08-19 13:58:22 +000011645 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11646 data[0] |= TG3_STD_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011647
11648 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011649 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11650 data[0] |= TG3_JMB_LOOPBACK_FAILED;
Matt Carlson6e01b202011-08-19 13:58:20 +000011651
11652 tg3_mac_loopback(tp, false);
11653 }
Matt Carlson4852a862011-04-13 11:05:07 +000011654
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011655 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011656 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011657 int i;
11658
Matt Carlson941ec902011-08-19 13:58:23 +000011659 tg3_phy_lpbk_set(tp, 0, false);
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011660
11661 /* Wait for link */
11662 for (i = 0; i < 100; i++) {
11663 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11664 break;
11665 mdelay(1);
11666 }
11667
Matt Carlson28a45952011-08-19 13:58:22 +000011668 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11669 data[1] |= TG3_STD_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011670 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011671 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11672 data[1] |= TG3_TSO_LOOPBACK_FAILED;
Joe Perches63c3a662011-04-26 08:12:10 +000011673 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson28a45952011-08-19 13:58:22 +000011674 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11675 data[1] |= TG3_JMB_LOOPBACK_FAILED;
Michael Chan9f40dea2005-09-05 17:53:06 -070011676
Matt Carlson941ec902011-08-19 13:58:23 +000011677 if (do_extlpbk) {
11678 tg3_phy_lpbk_set(tp, 0, true);
11679
11680 /* All link indications report up, but the hardware
11681 * isn't really ready for about 20 msec. Double it
11682 * to be sure.
11683 */
11684 mdelay(40);
11685
11686 if (tg3_run_loopback(tp, ETH_FRAME_LEN, false))
11687 data[2] |= TG3_STD_LOOPBACK_FAILED;
11688 if (tg3_flag(tp, TSO_CAPABLE) &&
11689 tg3_run_loopback(tp, ETH_FRAME_LEN, true))
11690 data[2] |= TG3_TSO_LOOPBACK_FAILED;
11691 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
11692 tg3_run_loopback(tp, 9000 + ETH_HLEN, false))
11693 data[2] |= TG3_JMB_LOOPBACK_FAILED;
11694 }
11695
Matt Carlson5e5a7f32011-08-19 13:58:21 +000011696 /* Re-enable gphy autopowerdown. */
11697 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
11698 tg3_phy_toggle_apd(tp, true);
11699 }
Matt Carlson6833c042008-11-21 17:18:59 -080011700
Matt Carlson941ec902011-08-19 13:58:23 +000011701 err = (data[0] | data[1] | data[2]) ? -EIO : 0;
Matt Carlson28a45952011-08-19 13:58:22 +000011702
Matt Carlsonab789042011-01-25 15:58:54 +000011703done:
11704 tp->phy_flags |= eee_cap;
11705
Michael Chan9f40dea2005-09-05 17:53:06 -070011706 return err;
11707}
11708
Michael Chan4cafd3f2005-05-29 14:56:34 -070011709static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11710 u64 *data)
11711{
Michael Chan566f86a2005-05-29 14:56:58 -070011712 struct tg3 *tp = netdev_priv(dev);
Matt Carlson941ec902011-08-19 13:58:23 +000011713 bool doextlpbk = etest->flags & ETH_TEST_FL_EXTERNAL_LB;
Michael Chan566f86a2005-05-29 14:56:58 -070011714
Matt Carlsonbed98292011-07-13 09:27:29 +000011715 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) &&
11716 tg3_power_up(tp)) {
11717 etest->flags |= ETH_TEST_FL_FAILED;
11718 memset(data, 1, sizeof(u64) * TG3_NUM_TEST);
11719 return;
11720 }
Michael Chanbc1c7562006-03-20 17:48:03 -080011721
Michael Chan566f86a2005-05-29 14:56:58 -070011722 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11723
11724 if (tg3_test_nvram(tp) != 0) {
11725 etest->flags |= ETH_TEST_FL_FAILED;
11726 data[0] = 1;
11727 }
Matt Carlson941ec902011-08-19 13:58:23 +000011728 if (!doextlpbk && tg3_test_link(tp)) {
Michael Chanca430072005-05-29 14:57:23 -070011729 etest->flags |= ETH_TEST_FL_FAILED;
11730 data[1] = 1;
11731 }
Michael Chana71116d2005-05-29 14:58:11 -070011732 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011733 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011734
Michael Chanbbe832c2005-06-24 20:20:04 -070011735 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011736 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011737 tg3_netif_stop(tp);
11738 irq_sync = 1;
11739 }
11740
11741 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011742
11743 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011744 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011745 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011746 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011747 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011748 if (!err)
11749 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011750
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011751 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011752 tg3_phy_reset(tp);
11753
Michael Chana71116d2005-05-29 14:58:11 -070011754 if (tg3_test_registers(tp) != 0) {
11755 etest->flags |= ETH_TEST_FL_FAILED;
11756 data[2] = 1;
11757 }
Matt Carlson28a45952011-08-19 13:58:22 +000011758
Michael Chan7942e1d2005-05-29 14:58:36 -070011759 if (tg3_test_memory(tp) != 0) {
11760 etest->flags |= ETH_TEST_FL_FAILED;
11761 data[3] = 1;
11762 }
Matt Carlson28a45952011-08-19 13:58:22 +000011763
Matt Carlson941ec902011-08-19 13:58:23 +000011764 if (doextlpbk)
11765 etest->flags |= ETH_TEST_FL_EXTERNAL_LB_DONE;
11766
11767 if (tg3_test_loopback(tp, &data[4], doextlpbk))
Michael Chanc76949a2005-05-29 14:58:59 -070011768 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011769
David S. Millerf47c11e2005-06-24 20:18:35 -070011770 tg3_full_unlock(tp);
11771
Michael Chand4bc3922005-05-29 14:59:20 -070011772 if (tg3_test_interrupt(tp) != 0) {
11773 etest->flags |= ETH_TEST_FL_FAILED;
Matt Carlson941ec902011-08-19 13:58:23 +000011774 data[7] = 1;
Michael Chand4bc3922005-05-29 14:59:20 -070011775 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011776
11777 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011778
Michael Chana71116d2005-05-29 14:58:11 -070011779 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11780 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011781 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011782 err2 = tg3_restart_hw(tp, 1);
11783 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011784 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011785 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011786
11787 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011788
11789 if (irq_sync && !err2)
11790 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011791 }
Matt Carlson80096062010-08-02 11:26:06 +000011792 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011793 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011794
Michael Chan4cafd3f2005-05-29 14:56:34 -070011795}
11796
Linus Torvalds1da177e2005-04-16 15:20:36 -070011797static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11798{
11799 struct mii_ioctl_data *data = if_mii(ifr);
11800 struct tg3 *tp = netdev_priv(dev);
11801 int err;
11802
Joe Perches63c3a662011-04-26 08:12:10 +000011803 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011804 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011805 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011806 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011807 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011808 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011809 }
11810
Matt Carlson33f401a2010-04-05 10:19:27 +000011811 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011812 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011813 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011814
11815 /* fallthru */
11816 case SIOCGMIIREG: {
11817 u32 mii_regval;
11818
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011819 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011820 break; /* We have no PHY */
11821
Matt Carlson34eea5a2011-04-20 07:57:38 +000011822 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011823 return -EAGAIN;
11824
David S. Millerf47c11e2005-06-24 20:18:35 -070011825 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011826 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011827 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011828
11829 data->val_out = mii_regval;
11830
11831 return err;
11832 }
11833
11834 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011835 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011836 break; /* We have no PHY */
11837
Matt Carlson34eea5a2011-04-20 07:57:38 +000011838 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011839 return -EAGAIN;
11840
David S. Millerf47c11e2005-06-24 20:18:35 -070011841 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011842 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011843 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011844
11845 return err;
11846
11847 default:
11848 /* do nothing */
11849 break;
11850 }
11851 return -EOPNOTSUPP;
11852}
11853
David S. Miller15f98502005-05-18 22:49:26 -070011854static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11855{
11856 struct tg3 *tp = netdev_priv(dev);
11857
11858 memcpy(ec, &tp->coal, sizeof(*ec));
11859 return 0;
11860}
11861
Michael Chand244c892005-07-05 14:42:33 -070011862static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11863{
11864 struct tg3 *tp = netdev_priv(dev);
11865 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11866 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11867
Joe Perches63c3a662011-04-26 08:12:10 +000011868 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011869 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11870 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11871 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11872 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11873 }
11874
11875 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11876 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11877 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11878 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11879 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11880 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11881 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11882 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11883 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11884 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11885 return -EINVAL;
11886
11887 /* No rx interrupts will be generated if both are zero */
11888 if ((ec->rx_coalesce_usecs == 0) &&
11889 (ec->rx_max_coalesced_frames == 0))
11890 return -EINVAL;
11891
11892 /* No tx interrupts will be generated if both are zero */
11893 if ((ec->tx_coalesce_usecs == 0) &&
11894 (ec->tx_max_coalesced_frames == 0))
11895 return -EINVAL;
11896
11897 /* Only copy relevant parameters, ignore all others. */
11898 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11899 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11900 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11901 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11902 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11903 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11904 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11905 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11906 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11907
11908 if (netif_running(dev)) {
11909 tg3_full_lock(tp, 0);
11910 __tg3_set_coalesce(tp, &tp->coal);
11911 tg3_full_unlock(tp);
11912 }
11913 return 0;
11914}
11915
Jeff Garzik7282d492006-09-13 14:30:00 -040011916static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011917 .get_settings = tg3_get_settings,
11918 .set_settings = tg3_set_settings,
11919 .get_drvinfo = tg3_get_drvinfo,
11920 .get_regs_len = tg3_get_regs_len,
11921 .get_regs = tg3_get_regs,
11922 .get_wol = tg3_get_wol,
11923 .set_wol = tg3_set_wol,
11924 .get_msglevel = tg3_get_msglevel,
11925 .set_msglevel = tg3_set_msglevel,
11926 .nway_reset = tg3_nway_reset,
11927 .get_link = ethtool_op_get_link,
11928 .get_eeprom_len = tg3_get_eeprom_len,
11929 .get_eeprom = tg3_get_eeprom,
11930 .set_eeprom = tg3_set_eeprom,
11931 .get_ringparam = tg3_get_ringparam,
11932 .set_ringparam = tg3_set_ringparam,
11933 .get_pauseparam = tg3_get_pauseparam,
11934 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011935 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011936 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011937 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011938 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011939 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011940 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011941 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011942};
11943
11944static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11945{
Michael Chan1b277772006-03-20 22:27:48 -080011946 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011947
11948 tp->nvram_size = EEPROM_CHIP_SIZE;
11949
Matt Carlsone4f34112009-02-25 14:25:00 +000011950 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011951 return;
11952
Michael Chanb16250e2006-09-27 16:10:14 -070011953 if ((magic != TG3_EEPROM_MAGIC) &&
11954 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11955 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011956 return;
11957
11958 /*
11959 * Size the chip by reading offsets at increasing powers of two.
11960 * When we encounter our validation signature, we know the addressing
11961 * has wrapped around, and thus have our chip size.
11962 */
Michael Chan1b277772006-03-20 22:27:48 -080011963 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011964
11965 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011966 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011967 return;
11968
Michael Chan18201802006-03-20 22:29:15 -080011969 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011970 break;
11971
11972 cursize <<= 1;
11973 }
11974
11975 tp->nvram_size = cursize;
11976}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011977
Linus Torvalds1da177e2005-04-16 15:20:36 -070011978static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11979{
11980 u32 val;
11981
Joe Perches63c3a662011-04-26 08:12:10 +000011982 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011983 return;
11984
11985 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011986 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011987 tg3_get_eeprom_size(tp);
11988 return;
11989 }
11990
Matt Carlson6d348f22009-02-25 14:25:52 +000011991 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011992 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011993 /* This is confusing. We want to operate on the
11994 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11995 * call will read from NVRAM and byteswap the data
11996 * according to the byteswapping settings for all
11997 * other register accesses. This ensures the data we
11998 * want will always reside in the lower 16-bits.
11999 * However, the data in NVRAM is in LE format, which
12000 * means the data from the NVRAM read will always be
12001 * opposite the endianness of the CPU. The 16-bit
12002 * byteswap then brings the data to CPU endianness.
12003 */
12004 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012005 return;
12006 }
12007 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070012008 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012009}
12010
12011static void __devinit tg3_get_nvram_info(struct tg3 *tp)
12012{
12013 u32 nvcfg1;
12014
12015 nvcfg1 = tr32(NVRAM_CFG1);
12016 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000012017 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012018 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012019 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12020 tw32(NVRAM_CFG1, nvcfg1);
12021 }
12022
Matt Carlson6ff6f812011-05-19 12:12:54 +000012023 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000012024 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012025 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012026 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
12027 tp->nvram_jedecnum = JEDEC_ATMEL;
12028 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012029 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012030 break;
12031 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
12032 tp->nvram_jedecnum = JEDEC_ATMEL;
12033 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
12034 break;
12035 case FLASH_VENDOR_ATMEL_EEPROM:
12036 tp->nvram_jedecnum = JEDEC_ATMEL;
12037 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012038 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012039 break;
12040 case FLASH_VENDOR_ST:
12041 tp->nvram_jedecnum = JEDEC_ST;
12042 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012043 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012044 break;
12045 case FLASH_VENDOR_SAIFUN:
12046 tp->nvram_jedecnum = JEDEC_SAIFUN;
12047 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
12048 break;
12049 case FLASH_VENDOR_SST_SMALL:
12050 case FLASH_VENDOR_SST_LARGE:
12051 tp->nvram_jedecnum = JEDEC_SST;
12052 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
12053 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012054 }
Matt Carlson8590a602009-08-28 12:29:16 +000012055 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012056 tp->nvram_jedecnum = JEDEC_ATMEL;
12057 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000012058 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012059 }
12060}
12061
Matt Carlsona1b950d2009-09-01 13:20:17 +000012062static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
12063{
12064 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
12065 case FLASH_5752PAGE_SIZE_256:
12066 tp->nvram_pagesize = 256;
12067 break;
12068 case FLASH_5752PAGE_SIZE_512:
12069 tp->nvram_pagesize = 512;
12070 break;
12071 case FLASH_5752PAGE_SIZE_1K:
12072 tp->nvram_pagesize = 1024;
12073 break;
12074 case FLASH_5752PAGE_SIZE_2K:
12075 tp->nvram_pagesize = 2048;
12076 break;
12077 case FLASH_5752PAGE_SIZE_4K:
12078 tp->nvram_pagesize = 4096;
12079 break;
12080 case FLASH_5752PAGE_SIZE_264:
12081 tp->nvram_pagesize = 264;
12082 break;
12083 case FLASH_5752PAGE_SIZE_528:
12084 tp->nvram_pagesize = 528;
12085 break;
12086 }
12087}
12088
Michael Chan361b4ac2005-04-21 17:11:21 -070012089static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
12090{
12091 u32 nvcfg1;
12092
12093 nvcfg1 = tr32(NVRAM_CFG1);
12094
Michael Chane6af3012005-04-21 17:12:05 -070012095 /* NVRAM protection for TPM */
12096 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000012097 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070012098
Michael Chan361b4ac2005-04-21 17:11:21 -070012099 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012100 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
12101 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
12102 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012103 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012104 break;
12105 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12106 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012107 tg3_flag_set(tp, NVRAM_BUFFERED);
12108 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012109 break;
12110 case FLASH_5752VENDOR_ST_M45PE10:
12111 case FLASH_5752VENDOR_ST_M45PE20:
12112 case FLASH_5752VENDOR_ST_M45PE40:
12113 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012114 tg3_flag_set(tp, NVRAM_BUFFERED);
12115 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012116 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070012117 }
12118
Joe Perches63c3a662011-04-26 08:12:10 +000012119 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000012120 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000012121 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070012122 /* For eeprom, set pagesize to maximum eeprom size */
12123 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12124
12125 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12126 tw32(NVRAM_CFG1, nvcfg1);
12127 }
12128}
12129
Michael Chand3c7b882006-03-23 01:28:25 -080012130static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
12131{
Matt Carlson989a9d22007-05-05 11:51:05 -070012132 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080012133
12134 nvcfg1 = tr32(NVRAM_CFG1);
12135
12136 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070012137 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012138 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070012139 protect = 1;
12140 }
Michael Chand3c7b882006-03-23 01:28:25 -080012141
Matt Carlson989a9d22007-05-05 11:51:05 -070012142 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12143 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012144 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12145 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12146 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12147 case FLASH_5755VENDOR_ATMEL_FLASH_5:
12148 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012149 tg3_flag_set(tp, NVRAM_BUFFERED);
12150 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012151 tp->nvram_pagesize = 264;
12152 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
12153 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
12154 tp->nvram_size = (protect ? 0x3e200 :
12155 TG3_NVRAM_SIZE_512KB);
12156 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
12157 tp->nvram_size = (protect ? 0x1f200 :
12158 TG3_NVRAM_SIZE_256KB);
12159 else
12160 tp->nvram_size = (protect ? 0x1f200 :
12161 TG3_NVRAM_SIZE_128KB);
12162 break;
12163 case FLASH_5752VENDOR_ST_M45PE10:
12164 case FLASH_5752VENDOR_ST_M45PE20:
12165 case FLASH_5752VENDOR_ST_M45PE40:
12166 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012167 tg3_flag_set(tp, NVRAM_BUFFERED);
12168 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012169 tp->nvram_pagesize = 256;
12170 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
12171 tp->nvram_size = (protect ?
12172 TG3_NVRAM_SIZE_64KB :
12173 TG3_NVRAM_SIZE_128KB);
12174 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
12175 tp->nvram_size = (protect ?
12176 TG3_NVRAM_SIZE_64KB :
12177 TG3_NVRAM_SIZE_256KB);
12178 else
12179 tp->nvram_size = (protect ?
12180 TG3_NVRAM_SIZE_128KB :
12181 TG3_NVRAM_SIZE_512KB);
12182 break;
Michael Chand3c7b882006-03-23 01:28:25 -080012183 }
12184}
12185
Michael Chan1b277772006-03-20 22:27:48 -080012186static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
12187{
12188 u32 nvcfg1;
12189
12190 nvcfg1 = tr32(NVRAM_CFG1);
12191
12192 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000012193 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
12194 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12195 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
12196 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12197 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012198 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000012199 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080012200
Matt Carlson8590a602009-08-28 12:29:16 +000012201 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12202 tw32(NVRAM_CFG1, nvcfg1);
12203 break;
12204 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12205 case FLASH_5755VENDOR_ATMEL_FLASH_1:
12206 case FLASH_5755VENDOR_ATMEL_FLASH_2:
12207 case FLASH_5755VENDOR_ATMEL_FLASH_3:
12208 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012209 tg3_flag_set(tp, NVRAM_BUFFERED);
12210 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012211 tp->nvram_pagesize = 264;
12212 break;
12213 case FLASH_5752VENDOR_ST_M45PE10:
12214 case FLASH_5752VENDOR_ST_M45PE20:
12215 case FLASH_5752VENDOR_ST_M45PE40:
12216 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012217 tg3_flag_set(tp, NVRAM_BUFFERED);
12218 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012219 tp->nvram_pagesize = 256;
12220 break;
Michael Chan1b277772006-03-20 22:27:48 -080012221 }
12222}
12223
Matt Carlson6b91fa02007-10-10 18:01:09 -070012224static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
12225{
12226 u32 nvcfg1, protect = 0;
12227
12228 nvcfg1 = tr32(NVRAM_CFG1);
12229
12230 /* NVRAM protection for TPM */
12231 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012232 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012233 protect = 1;
12234 }
12235
12236 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
12237 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012238 case FLASH_5761VENDOR_ATMEL_ADB021D:
12239 case FLASH_5761VENDOR_ATMEL_ADB041D:
12240 case FLASH_5761VENDOR_ATMEL_ADB081D:
12241 case FLASH_5761VENDOR_ATMEL_ADB161D:
12242 case FLASH_5761VENDOR_ATMEL_MDB021D:
12243 case FLASH_5761VENDOR_ATMEL_MDB041D:
12244 case FLASH_5761VENDOR_ATMEL_MDB081D:
12245 case FLASH_5761VENDOR_ATMEL_MDB161D:
12246 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012247 tg3_flag_set(tp, NVRAM_BUFFERED);
12248 tg3_flag_set(tp, FLASH);
12249 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000012250 tp->nvram_pagesize = 256;
12251 break;
12252 case FLASH_5761VENDOR_ST_A_M45PE20:
12253 case FLASH_5761VENDOR_ST_A_M45PE40:
12254 case FLASH_5761VENDOR_ST_A_M45PE80:
12255 case FLASH_5761VENDOR_ST_A_M45PE16:
12256 case FLASH_5761VENDOR_ST_M_M45PE20:
12257 case FLASH_5761VENDOR_ST_M_M45PE40:
12258 case FLASH_5761VENDOR_ST_M_M45PE80:
12259 case FLASH_5761VENDOR_ST_M_M45PE16:
12260 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012261 tg3_flag_set(tp, NVRAM_BUFFERED);
12262 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000012263 tp->nvram_pagesize = 256;
12264 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012265 }
12266
12267 if (protect) {
12268 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
12269 } else {
12270 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000012271 case FLASH_5761VENDOR_ATMEL_ADB161D:
12272 case FLASH_5761VENDOR_ATMEL_MDB161D:
12273 case FLASH_5761VENDOR_ST_A_M45PE16:
12274 case FLASH_5761VENDOR_ST_M_M45PE16:
12275 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
12276 break;
12277 case FLASH_5761VENDOR_ATMEL_ADB081D:
12278 case FLASH_5761VENDOR_ATMEL_MDB081D:
12279 case FLASH_5761VENDOR_ST_A_M45PE80:
12280 case FLASH_5761VENDOR_ST_M_M45PE80:
12281 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12282 break;
12283 case FLASH_5761VENDOR_ATMEL_ADB041D:
12284 case FLASH_5761VENDOR_ATMEL_MDB041D:
12285 case FLASH_5761VENDOR_ST_A_M45PE40:
12286 case FLASH_5761VENDOR_ST_M_M45PE40:
12287 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12288 break;
12289 case FLASH_5761VENDOR_ATMEL_ADB021D:
12290 case FLASH_5761VENDOR_ATMEL_MDB021D:
12291 case FLASH_5761VENDOR_ST_A_M45PE20:
12292 case FLASH_5761VENDOR_ST_M_M45PE20:
12293 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12294 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012295 }
12296 }
12297}
12298
Michael Chanb5d37722006-09-27 16:06:21 -070012299static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12300{
12301 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012302 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012303 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12304}
12305
Matt Carlson321d32a2008-11-21 17:22:19 -080012306static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12307{
12308 u32 nvcfg1;
12309
12310 nvcfg1 = tr32(NVRAM_CFG1);
12311
12312 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12313 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12314 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12315 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012316 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012317 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12318
12319 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12320 tw32(NVRAM_CFG1, nvcfg1);
12321 return;
12322 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12323 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12324 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12325 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12326 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12327 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12328 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12329 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012330 tg3_flag_set(tp, NVRAM_BUFFERED);
12331 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012332
12333 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12334 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12335 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12336 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12337 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12338 break;
12339 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12340 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12341 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12342 break;
12343 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12344 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12345 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12346 break;
12347 }
12348 break;
12349 case FLASH_5752VENDOR_ST_M45PE10:
12350 case FLASH_5752VENDOR_ST_M45PE20:
12351 case FLASH_5752VENDOR_ST_M45PE40:
12352 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012353 tg3_flag_set(tp, NVRAM_BUFFERED);
12354 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012355
12356 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12357 case FLASH_5752VENDOR_ST_M45PE10:
12358 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12359 break;
12360 case FLASH_5752VENDOR_ST_M45PE20:
12361 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12362 break;
12363 case FLASH_5752VENDOR_ST_M45PE40:
12364 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12365 break;
12366 }
12367 break;
12368 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012369 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012370 return;
12371 }
12372
Matt Carlsona1b950d2009-09-01 13:20:17 +000012373 tg3_nvram_get_pagesize(tp, nvcfg1);
12374 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012375 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012376}
12377
12378
12379static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12380{
12381 u32 nvcfg1;
12382
12383 nvcfg1 = tr32(NVRAM_CFG1);
12384
12385 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12386 case FLASH_5717VENDOR_ATMEL_EEPROM:
12387 case FLASH_5717VENDOR_MICRO_EEPROM:
12388 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012389 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012390 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12391
12392 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12393 tw32(NVRAM_CFG1, nvcfg1);
12394 return;
12395 case FLASH_5717VENDOR_ATMEL_MDB011D:
12396 case FLASH_5717VENDOR_ATMEL_ADB011B:
12397 case FLASH_5717VENDOR_ATMEL_ADB011D:
12398 case FLASH_5717VENDOR_ATMEL_MDB021D:
12399 case FLASH_5717VENDOR_ATMEL_ADB021B:
12400 case FLASH_5717VENDOR_ATMEL_ADB021D:
12401 case FLASH_5717VENDOR_ATMEL_45USPT:
12402 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012403 tg3_flag_set(tp, NVRAM_BUFFERED);
12404 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012405
12406 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12407 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012408 /* Detect size with tg3_nvram_get_size() */
12409 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012410 case FLASH_5717VENDOR_ATMEL_ADB021B:
12411 case FLASH_5717VENDOR_ATMEL_ADB021D:
12412 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12413 break;
12414 default:
12415 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12416 break;
12417 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012418 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012419 case FLASH_5717VENDOR_ST_M_M25PE10:
12420 case FLASH_5717VENDOR_ST_A_M25PE10:
12421 case FLASH_5717VENDOR_ST_M_M45PE10:
12422 case FLASH_5717VENDOR_ST_A_M45PE10:
12423 case FLASH_5717VENDOR_ST_M_M25PE20:
12424 case FLASH_5717VENDOR_ST_A_M25PE20:
12425 case FLASH_5717VENDOR_ST_M_M45PE20:
12426 case FLASH_5717VENDOR_ST_A_M45PE20:
12427 case FLASH_5717VENDOR_ST_25USPT:
12428 case FLASH_5717VENDOR_ST_45USPT:
12429 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012430 tg3_flag_set(tp, NVRAM_BUFFERED);
12431 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012432
12433 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12434 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012435 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012436 /* Detect size with tg3_nvram_get_size() */
12437 break;
12438 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012439 case FLASH_5717VENDOR_ST_A_M45PE20:
12440 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12441 break;
12442 default:
12443 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12444 break;
12445 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012446 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012447 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012448 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012449 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012450 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012451
12452 tg3_nvram_get_pagesize(tp, nvcfg1);
12453 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012454 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012455}
12456
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012457static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12458{
12459 u32 nvcfg1, nvmpinstrp;
12460
12461 nvcfg1 = tr32(NVRAM_CFG1);
12462 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12463
12464 switch (nvmpinstrp) {
12465 case FLASH_5720_EEPROM_HD:
12466 case FLASH_5720_EEPROM_LD:
12467 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012468 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012469
12470 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12471 tw32(NVRAM_CFG1, nvcfg1);
12472 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12473 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12474 else
12475 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12476 return;
12477 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12478 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12479 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12480 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12481 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12482 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12483 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12484 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12485 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12486 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12487 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12488 case FLASH_5720VENDOR_ATMEL_45USPT:
12489 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012490 tg3_flag_set(tp, NVRAM_BUFFERED);
12491 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012492
12493 switch (nvmpinstrp) {
12494 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12495 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12496 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12497 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12498 break;
12499 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12500 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12501 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12502 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12503 break;
12504 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12505 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12506 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12507 break;
12508 default:
12509 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12510 break;
12511 }
12512 break;
12513 case FLASH_5720VENDOR_M_ST_M25PE10:
12514 case FLASH_5720VENDOR_M_ST_M45PE10:
12515 case FLASH_5720VENDOR_A_ST_M25PE10:
12516 case FLASH_5720VENDOR_A_ST_M45PE10:
12517 case FLASH_5720VENDOR_M_ST_M25PE20:
12518 case FLASH_5720VENDOR_M_ST_M45PE20:
12519 case FLASH_5720VENDOR_A_ST_M25PE20:
12520 case FLASH_5720VENDOR_A_ST_M45PE20:
12521 case FLASH_5720VENDOR_M_ST_M25PE40:
12522 case FLASH_5720VENDOR_M_ST_M45PE40:
12523 case FLASH_5720VENDOR_A_ST_M25PE40:
12524 case FLASH_5720VENDOR_A_ST_M45PE40:
12525 case FLASH_5720VENDOR_M_ST_M25PE80:
12526 case FLASH_5720VENDOR_M_ST_M45PE80:
12527 case FLASH_5720VENDOR_A_ST_M25PE80:
12528 case FLASH_5720VENDOR_A_ST_M45PE80:
12529 case FLASH_5720VENDOR_ST_25USPT:
12530 case FLASH_5720VENDOR_ST_45USPT:
12531 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012532 tg3_flag_set(tp, NVRAM_BUFFERED);
12533 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012534
12535 switch (nvmpinstrp) {
12536 case FLASH_5720VENDOR_M_ST_M25PE20:
12537 case FLASH_5720VENDOR_M_ST_M45PE20:
12538 case FLASH_5720VENDOR_A_ST_M25PE20:
12539 case FLASH_5720VENDOR_A_ST_M45PE20:
12540 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12541 break;
12542 case FLASH_5720VENDOR_M_ST_M25PE40:
12543 case FLASH_5720VENDOR_M_ST_M45PE40:
12544 case FLASH_5720VENDOR_A_ST_M25PE40:
12545 case FLASH_5720VENDOR_A_ST_M45PE40:
12546 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12547 break;
12548 case FLASH_5720VENDOR_M_ST_M25PE80:
12549 case FLASH_5720VENDOR_M_ST_M45PE80:
12550 case FLASH_5720VENDOR_A_ST_M25PE80:
12551 case FLASH_5720VENDOR_A_ST_M45PE80:
12552 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12553 break;
12554 default:
12555 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12556 break;
12557 }
12558 break;
12559 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012560 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012561 return;
12562 }
12563
12564 tg3_nvram_get_pagesize(tp, nvcfg1);
12565 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012566 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012567}
12568
Linus Torvalds1da177e2005-04-16 15:20:36 -070012569/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12570static void __devinit tg3_nvram_init(struct tg3 *tp)
12571{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012572 tw32_f(GRC_EEPROM_ADDR,
12573 (EEPROM_ADDR_FSM_RESET |
12574 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12575 EEPROM_ADDR_CLKPERD_SHIFT)));
12576
Michael Chan9d57f012006-12-07 00:23:25 -080012577 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012578
12579 /* Enable seeprom accesses. */
12580 tw32_f(GRC_LOCAL_CTRL,
12581 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12582 udelay(100);
12583
12584 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12585 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012586 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012587
Michael Chanec41c7d2006-01-17 02:40:55 -080012588 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012589 netdev_warn(tp->dev,
12590 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012591 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012592 return;
12593 }
Michael Chane6af3012005-04-21 17:12:05 -070012594 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012595
Matt Carlson989a9d22007-05-05 11:51:05 -070012596 tp->nvram_size = 0;
12597
Michael Chan361b4ac2005-04-21 17:11:21 -070012598 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12599 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012600 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12601 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012602 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012603 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12604 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012605 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012606 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12607 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012608 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12609 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012610 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12611 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012612 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012613 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12614 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012615 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012616 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12617 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012618 else
12619 tg3_get_nvram_info(tp);
12620
Matt Carlson989a9d22007-05-05 11:51:05 -070012621 if (tp->nvram_size == 0)
12622 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012623
Michael Chane6af3012005-04-21 17:12:05 -070012624 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012625 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012626
12627 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012628 tg3_flag_clear(tp, NVRAM);
12629 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012630
12631 tg3_get_eeprom_size(tp);
12632 }
12633}
12634
Linus Torvalds1da177e2005-04-16 15:20:36 -070012635static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12636 u32 offset, u32 len, u8 *buf)
12637{
12638 int i, j, rc = 0;
12639 u32 val;
12640
12641 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012642 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012643 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012644
12645 addr = offset + i;
12646
12647 memcpy(&data, buf + i, 4);
12648
Matt Carlson62cedd12009-04-20 14:52:29 -070012649 /*
12650 * The SEEPROM interface expects the data to always be opposite
12651 * the native endian format. We accomplish this by reversing
12652 * all the operations that would have been performed on the
12653 * data from a call to tg3_nvram_read_be32().
12654 */
12655 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012656
12657 val = tr32(GRC_EEPROM_ADDR);
12658 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12659
12660 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12661 EEPROM_ADDR_READ);
12662 tw32(GRC_EEPROM_ADDR, val |
12663 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12664 (addr & EEPROM_ADDR_ADDR_MASK) |
12665 EEPROM_ADDR_START |
12666 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012667
Michael Chan9d57f012006-12-07 00:23:25 -080012668 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012669 val = tr32(GRC_EEPROM_ADDR);
12670
12671 if (val & EEPROM_ADDR_COMPLETE)
12672 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012673 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012674 }
12675 if (!(val & EEPROM_ADDR_COMPLETE)) {
12676 rc = -EBUSY;
12677 break;
12678 }
12679 }
12680
12681 return rc;
12682}
12683
12684/* offset and length are dword aligned */
12685static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12686 u8 *buf)
12687{
12688 int ret = 0;
12689 u32 pagesize = tp->nvram_pagesize;
12690 u32 pagemask = pagesize - 1;
12691 u32 nvram_cmd;
12692 u8 *tmp;
12693
12694 tmp = kmalloc(pagesize, GFP_KERNEL);
12695 if (tmp == NULL)
12696 return -ENOMEM;
12697
12698 while (len) {
12699 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012700 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012701
12702 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012703
Linus Torvalds1da177e2005-04-16 15:20:36 -070012704 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012705 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12706 (__be32 *) (tmp + j));
12707 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012708 break;
12709 }
12710 if (ret)
12711 break;
12712
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012713 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012714 size = pagesize;
12715 if (len < size)
12716 size = len;
12717
12718 len -= size;
12719
12720 memcpy(tmp + page_off, buf, size);
12721
12722 offset = offset + (pagesize - page_off);
12723
Michael Chane6af3012005-04-21 17:12:05 -070012724 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012725
12726 /*
12727 * Before we can erase the flash page, we need
12728 * to issue a special "write enable" command.
12729 */
12730 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12731
12732 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12733 break;
12734
12735 /* Erase the target page */
12736 tw32(NVRAM_ADDR, phy_addr);
12737
12738 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12739 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12740
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012741 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012742 break;
12743
12744 /* Issue another write enable to start the write. */
12745 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12746
12747 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12748 break;
12749
12750 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012751 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012752
Al Virob9fc7dc2007-12-17 22:59:57 -080012753 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012754
Al Virob9fc7dc2007-12-17 22:59:57 -080012755 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012756
12757 tw32(NVRAM_ADDR, phy_addr + j);
12758
12759 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12760 NVRAM_CMD_WR;
12761
12762 if (j == 0)
12763 nvram_cmd |= NVRAM_CMD_FIRST;
12764 else if (j == (pagesize - 4))
12765 nvram_cmd |= NVRAM_CMD_LAST;
12766
12767 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12768 break;
12769 }
12770 if (ret)
12771 break;
12772 }
12773
12774 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12775 tg3_nvram_exec_cmd(tp, nvram_cmd);
12776
12777 kfree(tmp);
12778
12779 return ret;
12780}
12781
12782/* offset and length are dword aligned */
12783static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12784 u8 *buf)
12785{
12786 int i, ret = 0;
12787
12788 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012789 u32 page_off, phy_addr, nvram_cmd;
12790 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012791
12792 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012793 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012794
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012795 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012796
Michael Chan18201802006-03-20 22:29:15 -080012797 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012798
12799 tw32(NVRAM_ADDR, phy_addr);
12800
12801 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12802
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012803 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012804 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012805 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012806 nvram_cmd |= NVRAM_CMD_LAST;
12807
12808 if (i == (len - 4))
12809 nvram_cmd |= NVRAM_CMD_LAST;
12810
Matt Carlson321d32a2008-11-21 17:22:19 -080012811 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012812 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012813 (tp->nvram_jedecnum == JEDEC_ST) &&
12814 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012815
12816 if ((ret = tg3_nvram_exec_cmd(tp,
12817 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12818 NVRAM_CMD_DONE)))
12819
12820 break;
12821 }
Joe Perches63c3a662011-04-26 08:12:10 +000012822 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012823 /* We always do complete word writes to eeprom. */
12824 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12825 }
12826
12827 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12828 break;
12829 }
12830 return ret;
12831}
12832
12833/* offset and length are dword aligned */
12834static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12835{
12836 int ret;
12837
Joe Perches63c3a662011-04-26 08:12:10 +000012838 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012839 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12840 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012841 udelay(40);
12842 }
12843
Joe Perches63c3a662011-04-26 08:12:10 +000012844 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012845 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012846 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012847 u32 grc_mode;
12848
Michael Chanec41c7d2006-01-17 02:40:55 -080012849 ret = tg3_nvram_lock(tp);
12850 if (ret)
12851 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012852
Michael Chane6af3012005-04-21 17:12:05 -070012853 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012854 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012855 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012856
12857 grc_mode = tr32(GRC_MODE);
12858 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12859
Joe Perches63c3a662011-04-26 08:12:10 +000012860 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012861 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12862 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012863 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012864 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12865 buf);
12866 }
12867
12868 grc_mode = tr32(GRC_MODE);
12869 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12870
Michael Chane6af3012005-04-21 17:12:05 -070012871 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012872 tg3_nvram_unlock(tp);
12873 }
12874
Joe Perches63c3a662011-04-26 08:12:10 +000012875 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012876 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012877 udelay(40);
12878 }
12879
12880 return ret;
12881}
12882
12883struct subsys_tbl_ent {
12884 u16 subsys_vendor, subsys_devid;
12885 u32 phy_id;
12886};
12887
Matt Carlson24daf2b2010-02-17 15:17:02 +000012888static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012889 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012890 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012891 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012892 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012893 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012894 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012895 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012896 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12897 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12898 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012899 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012900 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012901 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012902 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12903 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12904 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012905 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, 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_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012908 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012909 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012910 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012911 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012912
12913 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012914 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012915 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012916 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012917 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012918 { TG3PCI_SUBVENDOR_ID_3COM,
12919 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12920 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012921 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012922 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012923 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012924
12925 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012926 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012927 TG3PCI_SUBDEVICE_ID_DELL_VIPER, 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_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012930 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012931 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012932 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012933 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012934
12935 /* Compaq boards. */
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, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012938 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012939 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012940 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12941 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12942 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012943 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012944 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012945 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012946
12947 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012948 { TG3PCI_SUBVENDOR_ID_IBM,
12949 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012950};
12951
Matt Carlson24daf2b2010-02-17 15:17:02 +000012952static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012953{
12954 int i;
12955
12956 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12957 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12958 tp->pdev->subsystem_vendor) &&
12959 (subsys_id_to_phy_id[i].subsys_devid ==
12960 tp->pdev->subsystem_device))
12961 return &subsys_id_to_phy_id[i];
12962 }
12963 return NULL;
12964}
12965
Michael Chan7d0c41e2005-04-21 17:06:20 -070012966static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012967{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012968 u32 val;
David S. Millerf49639e2006-06-09 11:58:36 -070012969
Matt Carlson79eb6902010-02-17 15:17:03 +000012970 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012971 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12972
Gary Zambranoa85feb82007-05-05 11:52:19 -070012973 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012974 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12975 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012976
Michael Chanb5d37722006-09-27 16:06:21 -070012977 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012978 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012979 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12980 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012981 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012982 val = tr32(VCPU_CFGSHDW);
12983 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012984 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012985 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012986 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012987 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012988 device_set_wakeup_enable(&tp->pdev->dev, true);
12989 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012990 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012991 }
12992
Linus Torvalds1da177e2005-04-16 15:20:36 -070012993 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12994 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12995 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012996 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012997 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012998
12999 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
13000 tp->nic_sram_data_cfg = nic_cfg;
13001
13002 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
13003 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013004 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13005 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13006 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070013007 (ver > 0) && (ver < 0x100))
13008 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
13009
Matt Carlsona9daf362008-05-25 23:49:44 -070013010 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
13011 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
13012
Linus Torvalds1da177e2005-04-16 15:20:36 -070013013 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
13014 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
13015 eeprom_phy_serdes = 1;
13016
13017 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
13018 if (nic_phy_id != 0) {
13019 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
13020 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
13021
13022 eeprom_phy_id = (id1 >> 16) << 10;
13023 eeprom_phy_id |= (id2 & 0xfc00) << 16;
13024 eeprom_phy_id |= (id2 & 0x03ff) << 0;
13025 } else
13026 eeprom_phy_id = 0;
13027
Michael Chan7d0c41e2005-04-21 17:06:20 -070013028 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070013029 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000013030 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013031 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000013032 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013033 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070013034 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070013035
Joe Perches63c3a662011-04-26 08:12:10 +000013036 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013037 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
13038 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070013039 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070013040 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
13041
13042 switch (led_cfg) {
13043 default:
13044 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
13045 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13046 break;
13047
13048 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
13049 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13050 break;
13051
13052 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
13053 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070013054
13055 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
13056 * read on some older 5700/5701 bootcode.
13057 */
13058 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13059 ASIC_REV_5700 ||
13060 GET_ASIC_REV(tp->pci_chip_rev_id) ==
13061 ASIC_REV_5701)
13062 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
13063
Linus Torvalds1da177e2005-04-16 15:20:36 -070013064 break;
13065
13066 case SHASTA_EXT_LED_SHARED:
13067 tp->led_ctrl = LED_CTRL_MODE_SHARED;
13068 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
13069 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
13070 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13071 LED_CTRL_MODE_PHY_2);
13072 break;
13073
13074 case SHASTA_EXT_LED_MAC:
13075 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
13076 break;
13077
13078 case SHASTA_EXT_LED_COMBO:
13079 tp->led_ctrl = LED_CTRL_MODE_COMBO;
13080 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
13081 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
13082 LED_CTRL_MODE_PHY_2);
13083 break;
13084
Stephen Hemminger855e1112008-04-16 16:37:28 -070013085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013086
13087 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13088 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
13089 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
13090 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
13091
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013092 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
13093 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080013094
Michael Chan9d26e212006-12-07 00:21:14 -080013095 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000013096 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013097 if ((tp->pdev->subsystem_vendor ==
13098 PCI_VENDOR_ID_ARIMA) &&
13099 (tp->pdev->subsystem_device == 0x205a ||
13100 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000013101 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080013102 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000013103 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
13104 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080013105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013106
13107 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000013108 tg3_flag_set(tp, ENABLE_ASF);
13109 if (tg3_flag(tp, 5750_PLUS))
13110 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013111 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013112
13113 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013114 tg3_flag(tp, 5750_PLUS))
13115 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080013116
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013117 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070013118 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000013119 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013120
Joe Perches63c3a662011-04-26 08:12:10 +000013121 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013122 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013123 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000013124 device_set_wakeup_enable(&tp->pdev->dev, true);
13125 }
Matt Carlson0527ba32007-10-10 18:03:30 -070013126
Linus Torvalds1da177e2005-04-16 15:20:36 -070013127 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013128 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013129
13130 /* serdes signal pre-emphasis in register 0x590 set by */
13131 /* bootcode if bit 18 is set */
13132 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013133 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070013134
Joe Perches63c3a662011-04-26 08:12:10 +000013135 if ((tg3_flag(tp, 57765_PLUS) ||
13136 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13137 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080013138 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013139 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080013140
Joe Perches63c3a662011-04-26 08:12:10 +000013141 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000013142 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000013143 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070013144 u32 cfg3;
13145
13146 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
13147 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000013148 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070013149 }
Matt Carlsona9daf362008-05-25 23:49:44 -070013150
Matt Carlson14417062010-02-17 15:16:59 +000013151 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000013152 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070013153 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013154 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070013155 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000013156 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013157 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080013158done:
Joe Perches63c3a662011-04-26 08:12:10 +000013159 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013160 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000013161 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000013162 else
13163 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070013164}
13165
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013166static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
13167{
13168 int i;
13169 u32 val;
13170
13171 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
13172 tw32(OTP_CTRL, cmd);
13173
13174 /* Wait for up to 1 ms for command to execute. */
13175 for (i = 0; i < 100; i++) {
13176 val = tr32(OTP_STATUS);
13177 if (val & OTP_STATUS_CMD_DONE)
13178 break;
13179 udelay(10);
13180 }
13181
13182 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
13183}
13184
13185/* Read the gphy configuration from the OTP region of the chip. The gphy
13186 * configuration is a 32-bit value that straddles the alignment boundary.
13187 * We do two 32-bit reads and then shift and merge the results.
13188 */
13189static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
13190{
13191 u32 bhalf_otp, thalf_otp;
13192
13193 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
13194
13195 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
13196 return 0;
13197
13198 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
13199
13200 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13201 return 0;
13202
13203 thalf_otp = tr32(OTP_READ_DATA);
13204
13205 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
13206
13207 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
13208 return 0;
13209
13210 bhalf_otp = tr32(OTP_READ_DATA);
13211
13212 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
13213}
13214
Matt Carlsone256f8a2011-03-09 16:58:24 +000013215static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
13216{
13217 u32 adv = ADVERTISED_Autoneg |
13218 ADVERTISED_Pause;
13219
13220 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
13221 adv |= ADVERTISED_1000baseT_Half |
13222 ADVERTISED_1000baseT_Full;
13223
13224 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13225 adv |= ADVERTISED_100baseT_Half |
13226 ADVERTISED_100baseT_Full |
13227 ADVERTISED_10baseT_Half |
13228 ADVERTISED_10baseT_Full |
13229 ADVERTISED_TP;
13230 else
13231 adv |= ADVERTISED_FIBRE;
13232
13233 tp->link_config.advertising = adv;
13234 tp->link_config.speed = SPEED_INVALID;
13235 tp->link_config.duplex = DUPLEX_INVALID;
13236 tp->link_config.autoneg = AUTONEG_ENABLE;
13237 tp->link_config.active_speed = SPEED_INVALID;
13238 tp->link_config.active_duplex = DUPLEX_INVALID;
13239 tp->link_config.orig_speed = SPEED_INVALID;
13240 tp->link_config.orig_duplex = DUPLEX_INVALID;
13241 tp->link_config.orig_autoneg = AUTONEG_INVALID;
13242}
13243
Michael Chan7d0c41e2005-04-21 17:06:20 -070013244static int __devinit tg3_phy_probe(struct tg3 *tp)
13245{
13246 u32 hw_phy_id_1, hw_phy_id_2;
13247 u32 hw_phy_id, hw_phy_id_masked;
13248 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013249
Matt Carlsone256f8a2011-03-09 16:58:24 +000013250 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000013251 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000013252 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
13253
Joe Perches63c3a662011-04-26 08:12:10 +000013254 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070013255 return tg3_phy_init(tp);
13256
Linus Torvalds1da177e2005-04-16 15:20:36 -070013257 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010013258 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013259 */
13260 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000013261 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000013262 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013263 } else {
13264 /* Now read the physical PHY_ID from the chip and verify
13265 * that it is sane. If it doesn't look good, we fall back
13266 * to either the hard-coded table based PHY_ID and failing
13267 * that the value found in the eeprom area.
13268 */
13269 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13270 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13271
13272 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13273 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13274 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13275
Matt Carlson79eb6902010-02-17 15:17:03 +000013276 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013277 }
13278
Matt Carlson79eb6902010-02-17 15:17:03 +000013279 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013280 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013281 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013282 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013283 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013284 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013285 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013286 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013287 /* Do nothing, phy ID already set up in
13288 * tg3_get_eeprom_hw_cfg().
13289 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013290 } else {
13291 struct subsys_tbl_ent *p;
13292
13293 /* No eeprom signature? Try the hardcoded
13294 * subsys device table.
13295 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013296 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013297 if (!p)
13298 return -ENODEV;
13299
13300 tp->phy_id = p->phy_id;
13301 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013302 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013303 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013304 }
13305 }
13306
Matt Carlsona6b68da2010-12-06 08:28:52 +000013307 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson5baa5e92011-07-20 10:20:53 +000013308 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13309 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720 ||
13310 (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +000013311 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13312 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13313 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013314 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13315
Matt Carlsone256f8a2011-03-09 16:58:24 +000013316 tg3_phy_init_link_config(tp);
13317
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013318 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013319 !tg3_flag(tp, ENABLE_APE) &&
13320 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013321 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013322
13323 tg3_readphy(tp, MII_BMSR, &bmsr);
13324 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13325 (bmsr & BMSR_LSTATUS))
13326 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013327
Linus Torvalds1da177e2005-04-16 15:20:36 -070013328 err = tg3_phy_reset(tp);
13329 if (err)
13330 return err;
13331
Matt Carlson42b64a42011-05-19 12:12:49 +000013332 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013333
Michael Chan3600d912006-12-07 00:21:48 -080013334 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13335 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13336 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13337 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013338 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13339 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013340
13341 tg3_writephy(tp, MII_BMCR,
13342 BMCR_ANENABLE | BMCR_ANRESTART);
13343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013344 }
13345
13346skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013347 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013348 err = tg3_init_5401phy_dsp(tp);
13349 if (err)
13350 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013351
Linus Torvalds1da177e2005-04-16 15:20:36 -070013352 err = tg3_init_5401phy_dsp(tp);
13353 }
13354
Linus Torvalds1da177e2005-04-16 15:20:36 -070013355 return err;
13356}
13357
Matt Carlson184b8902010-04-05 10:19:25 +000013358static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013359{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013360 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013361 unsigned int block_end, rosize, len;
Matt Carlson535a4902011-07-20 10:20:56 +000013362 u32 vpdlen;
Matt Carlson184b8902010-04-05 10:19:25 +000013363 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013364
Matt Carlson535a4902011-07-20 10:20:56 +000013365 vpd_data = (u8 *)tg3_vpd_readblock(tp, &vpdlen);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013366 if (!vpd_data)
13367 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013368
Matt Carlson535a4902011-07-20 10:20:56 +000013369 i = pci_vpd_find_tag(vpd_data, 0, vpdlen, PCI_VPD_LRDT_RO_DATA);
Matt Carlson4181b2c2010-02-26 14:04:45 +000013370 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013371 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013372
13373 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13374 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13375 i += PCI_VPD_LRDT_TAG_SIZE;
13376
Matt Carlson535a4902011-07-20 10:20:56 +000013377 if (block_end > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013378 goto out_not_found;
13379
Matt Carlson184b8902010-04-05 10:19:25 +000013380 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13381 PCI_VPD_RO_KEYWORD_MFR_ID);
13382 if (j > 0) {
13383 len = pci_vpd_info_field_size(&vpd_data[j]);
13384
13385 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13386 if (j + len > block_end || len != 4 ||
13387 memcmp(&vpd_data[j], "1028", 4))
13388 goto partno;
13389
13390 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13391 PCI_VPD_RO_KEYWORD_VENDOR0);
13392 if (j < 0)
13393 goto partno;
13394
13395 len = pci_vpd_info_field_size(&vpd_data[j]);
13396
13397 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13398 if (j + len > block_end)
13399 goto partno;
13400
13401 memcpy(tp->fw_ver, &vpd_data[j], len);
Matt Carlson535a4902011-07-20 10:20:56 +000013402 strncat(tp->fw_ver, " bc ", vpdlen - len - 1);
Matt Carlson184b8902010-04-05 10:19:25 +000013403 }
13404
13405partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013406 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13407 PCI_VPD_RO_KEYWORD_PARTNO);
13408 if (i < 0)
13409 goto out_not_found;
13410
13411 len = pci_vpd_info_field_size(&vpd_data[i]);
13412
13413 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13414 if (len > TG3_BPN_SIZE ||
Matt Carlson535a4902011-07-20 10:20:56 +000013415 (len + i) > vpdlen)
Matt Carlson4181b2c2010-02-26 14:04:45 +000013416 goto out_not_found;
13417
13418 memcpy(tp->board_part_number, &vpd_data[i], len);
13419
Linus Torvalds1da177e2005-04-16 15:20:36 -070013420out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013421 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013422 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013423 return;
13424
13425out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013426 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13427 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13428 strcpy(tp->board_part_number, "BCM5717");
13429 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13430 strcpy(tp->board_part_number, "BCM5718");
13431 else
13432 goto nomatch;
13433 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13434 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13435 strcpy(tp->board_part_number, "BCM57780");
13436 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13437 strcpy(tp->board_part_number, "BCM57760");
13438 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13439 strcpy(tp->board_part_number, "BCM57790");
13440 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13441 strcpy(tp->board_part_number, "BCM57788");
13442 else
13443 goto nomatch;
13444 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13445 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13446 strcpy(tp->board_part_number, "BCM57761");
13447 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13448 strcpy(tp->board_part_number, "BCM57765");
13449 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13450 strcpy(tp->board_part_number, "BCM57781");
13451 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13452 strcpy(tp->board_part_number, "BCM57785");
13453 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13454 strcpy(tp->board_part_number, "BCM57791");
13455 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13456 strcpy(tp->board_part_number, "BCM57795");
13457 else
13458 goto nomatch;
13459 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013460 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013461 } else {
13462nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013463 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013465}
13466
Matt Carlson9c8a6202007-10-21 16:16:08 -070013467static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13468{
13469 u32 val;
13470
Matt Carlsone4f34112009-02-25 14:25:00 +000013471 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013472 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013473 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013474 val != 0)
13475 return 0;
13476
13477 return 1;
13478}
13479
Matt Carlsonacd9c112009-02-25 14:26:33 +000013480static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13481{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013482 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013483 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013484 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013485
13486 if (tg3_nvram_read(tp, 0xc, &offset) ||
13487 tg3_nvram_read(tp, 0x4, &start))
13488 return;
13489
13490 offset = tg3_nvram_logical_addr(tp, offset);
13491
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013492 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013493 return;
13494
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013495 if ((val & 0xfc000000) == 0x0c000000) {
13496 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013497 return;
13498
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013499 if (val == 0)
13500 newver = true;
13501 }
13502
Matt Carlson75f99362010-04-05 10:19:24 +000013503 dst_off = strlen(tp->fw_ver);
13504
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013505 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013506 if (TG3_VER_SIZE - dst_off < 16 ||
13507 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013508 return;
13509
13510 offset = offset + ver_offset - start;
13511 for (i = 0; i < 16; i += 4) {
13512 __be32 v;
13513 if (tg3_nvram_read_be32(tp, offset + i, &v))
13514 return;
13515
Matt Carlson75f99362010-04-05 10:19:24 +000013516 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013517 }
13518 } else {
13519 u32 major, minor;
13520
13521 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13522 return;
13523
13524 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13525 TG3_NVM_BCVER_MAJSFT;
13526 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013527 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13528 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013529 }
13530}
13531
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013532static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13533{
13534 u32 val, major, minor;
13535
13536 /* Use native endian representation */
13537 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13538 return;
13539
13540 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13541 TG3_NVM_HWSB_CFG1_MAJSFT;
13542 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13543 TG3_NVM_HWSB_CFG1_MINSFT;
13544
13545 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13546}
13547
Matt Carlsondfe00d72008-11-21 17:19:41 -080013548static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13549{
13550 u32 offset, major, minor, build;
13551
Matt Carlson75f99362010-04-05 10:19:24 +000013552 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013553
13554 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13555 return;
13556
13557 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13558 case TG3_EEPROM_SB_REVISION_0:
13559 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13560 break;
13561 case TG3_EEPROM_SB_REVISION_2:
13562 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13563 break;
13564 case TG3_EEPROM_SB_REVISION_3:
13565 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13566 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013567 case TG3_EEPROM_SB_REVISION_4:
13568 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13569 break;
13570 case TG3_EEPROM_SB_REVISION_5:
13571 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13572 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013573 case TG3_EEPROM_SB_REVISION_6:
13574 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13575 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013576 default:
13577 return;
13578 }
13579
Matt Carlsone4f34112009-02-25 14:25:00 +000013580 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013581 return;
13582
13583 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13584 TG3_EEPROM_SB_EDH_BLD_SHFT;
13585 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13586 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13587 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13588
13589 if (minor > 99 || build > 26)
13590 return;
13591
Matt Carlson75f99362010-04-05 10:19:24 +000013592 offset = strlen(tp->fw_ver);
13593 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13594 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013595
13596 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013597 offset = strlen(tp->fw_ver);
13598 if (offset < TG3_VER_SIZE - 1)
13599 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013600 }
13601}
13602
Matt Carlsonacd9c112009-02-25 14:26:33 +000013603static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013604{
13605 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013606 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013607
13608 for (offset = TG3_NVM_DIR_START;
13609 offset < TG3_NVM_DIR_END;
13610 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013611 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013612 return;
13613
13614 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13615 break;
13616 }
13617
13618 if (offset == TG3_NVM_DIR_END)
13619 return;
13620
Joe Perches63c3a662011-04-26 08:12:10 +000013621 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013622 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013623 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013624 return;
13625
Matt Carlsone4f34112009-02-25 14:25:00 +000013626 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013627 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013628 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013629 return;
13630
13631 offset += val - start;
13632
Matt Carlsonacd9c112009-02-25 14:26:33 +000013633 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013634
Matt Carlsonacd9c112009-02-25 14:26:33 +000013635 tp->fw_ver[vlen++] = ',';
13636 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013637
13638 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013639 __be32 v;
13640 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013641 return;
13642
Al Virob9fc7dc2007-12-17 22:59:57 -080013643 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013644
Matt Carlsonacd9c112009-02-25 14:26:33 +000013645 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13646 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013647 break;
13648 }
13649
Matt Carlsonacd9c112009-02-25 14:26:33 +000013650 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13651 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013652 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013653}
13654
Matt Carlson7fd76442009-02-25 14:27:20 +000013655static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13656{
13657 int vlen;
13658 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013659 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013660
Joe Perches63c3a662011-04-26 08:12:10 +000013661 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013662 return;
13663
13664 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13665 if (apedata != APE_SEG_SIG_MAGIC)
13666 return;
13667
13668 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13669 if (!(apedata & APE_FW_STATUS_READY))
13670 return;
13671
13672 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13673
Matt Carlsondc6d0742010-09-15 08:59:55 +000013674 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013675 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013676 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013677 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013678 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013679 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013680
Matt Carlson7fd76442009-02-25 14:27:20 +000013681 vlen = strlen(tp->fw_ver);
13682
Matt Carlsonecc79642010-08-02 11:26:01 +000013683 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13684 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013685 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13686 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13687 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13688 (apedata & APE_FW_VERSION_BLDMSK));
13689}
13690
Matt Carlsonacd9c112009-02-25 14:26:33 +000013691static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13692{
13693 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013694 bool vpd_vers = false;
13695
13696 if (tp->fw_ver[0] != 0)
13697 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013698
Joe Perches63c3a662011-04-26 08:12:10 +000013699 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013700 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013701 return;
13702 }
13703
Matt Carlsonacd9c112009-02-25 14:26:33 +000013704 if (tg3_nvram_read(tp, 0, &val))
13705 return;
13706
13707 if (val == TG3_EEPROM_MAGIC)
13708 tg3_read_bc_ver(tp);
13709 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13710 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013711 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13712 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013713 else
13714 return;
13715
Matt Carlsonc9cab242011-07-13 09:27:27 +000013716 if (vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013717 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013718
Matt Carlsonc9cab242011-07-13 09:27:27 +000013719 if (tg3_flag(tp, ENABLE_APE)) {
13720 if (tg3_flag(tp, ENABLE_ASF))
13721 tg3_read_dash_ver(tp);
13722 } else if (tg3_flag(tp, ENABLE_ASF)) {
13723 tg3_read_mgmtfw_ver(tp);
13724 }
Matt Carlson9c8a6202007-10-21 16:16:08 -070013725
Matt Carlson75f99362010-04-05 10:19:24 +000013726done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013727 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013728}
13729
Michael Chan7544b092007-05-05 13:08:32 -070013730static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13731
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013732static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13733{
Joe Perches63c3a662011-04-26 08:12:10 +000013734 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013735 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013736 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013737 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013738 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013739 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013740}
13741
Matt Carlson41434702011-03-09 16:58:22 +000013742static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013743 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13744 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13745 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13746 { },
13747};
13748
Linus Torvalds1da177e2005-04-16 15:20:36 -070013749static int __devinit tg3_get_invariants(struct tg3 *tp)
13750{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013751 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013752 u32 pci_state_reg, grc_misc_cfg;
13753 u32 val;
13754 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013755 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013756
Linus Torvalds1da177e2005-04-16 15:20:36 -070013757 /* Force memory write invalidate off. If we leave it on,
13758 * then on 5700_BX chips we have to enable a workaround.
13759 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13760 * to match the cacheline size. The Broadcom driver have this
13761 * workaround but turns MWI off all the times so never uses
13762 * it. This seems to suggest that the workaround is insufficient.
13763 */
13764 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13765 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13766 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13767
Matt Carlson16821282011-07-13 09:27:28 +000013768 /* Important! -- Make sure register accesses are byteswapped
13769 * correctly. Also, for those chips that require it, make
13770 * sure that indirect register accesses are enabled before
13771 * the first operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013772 */
13773 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13774 &misc_ctrl_reg);
Matt Carlson16821282011-07-13 09:27:28 +000013775 tp->misc_host_ctrl |= (misc_ctrl_reg &
13776 MISC_HOST_CTRL_CHIPREV);
13777 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13778 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013779
13780 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13781 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013782 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13783 u32 prod_id_asic_rev;
13784
Matt Carlson5001e2f2009-11-13 13:03:51 +000013785 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13786 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013787 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13788 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013789 pci_read_config_dword(tp->pdev,
13790 TG3PCI_GEN2_PRODID_ASICREV,
13791 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013792 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13793 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13794 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13795 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13796 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13797 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13798 pci_read_config_dword(tp->pdev,
13799 TG3PCI_GEN15_PRODID_ASICREV,
13800 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013801 else
13802 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13803 &prod_id_asic_rev);
13804
Matt Carlson321d32a2008-11-21 17:22:19 -080013805 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013807
Michael Chanff645be2005-04-21 17:09:53 -070013808 /* Wrong chip ID in 5752 A0. This code can be removed later
13809 * as A0 is not in production.
13810 */
13811 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13812 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13813
Michael Chan68929142005-08-09 20:17:14 -070013814 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13815 * we need to disable memory and use config. cycles
13816 * only to access all registers. The 5702/03 chips
13817 * can mistakenly decode the special cycles from the
13818 * ICH chipsets as memory write cycles, causing corruption
13819 * of register and memory space. Only certain ICH bridges
13820 * will drive special cycles with non-zero data during the
13821 * address phase which can fall within the 5703's address
13822 * range. This is not an ICH bug as the PCI spec allows
13823 * non-zero address during special cycles. However, only
13824 * these ICH bridges are known to drive non-zero addresses
13825 * during special cycles.
13826 *
13827 * Since special cycles do not cross PCI bridges, we only
13828 * enable this workaround if the 5703 is on the secondary
13829 * bus of these ICH bridges.
13830 */
13831 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13832 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13833 static struct tg3_dev_id {
13834 u32 vendor;
13835 u32 device;
13836 u32 rev;
13837 } ich_chipsets[] = {
13838 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13839 PCI_ANY_ID },
13840 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13841 PCI_ANY_ID },
13842 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13843 0xa },
13844 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13845 PCI_ANY_ID },
13846 { },
13847 };
13848 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13849 struct pci_dev *bridge = NULL;
13850
13851 while (pci_id->vendor != 0) {
13852 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13853 bridge);
13854 if (!bridge) {
13855 pci_id++;
13856 continue;
13857 }
13858 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013859 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013860 continue;
13861 }
13862 if (bridge->subordinate &&
13863 (bridge->subordinate->number ==
13864 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013865 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013866 pci_dev_put(bridge);
13867 break;
13868 }
13869 }
13870 }
13871
Matt Carlson6ff6f812011-05-19 12:12:54 +000013872 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013873 static struct tg3_dev_id {
13874 u32 vendor;
13875 u32 device;
13876 } bridge_chipsets[] = {
13877 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13878 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13879 { },
13880 };
13881 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13882 struct pci_dev *bridge = NULL;
13883
13884 while (pci_id->vendor != 0) {
13885 bridge = pci_get_device(pci_id->vendor,
13886 pci_id->device,
13887 bridge);
13888 if (!bridge) {
13889 pci_id++;
13890 continue;
13891 }
13892 if (bridge->subordinate &&
13893 (bridge->subordinate->number <=
13894 tp->pdev->bus->number) &&
13895 (bridge->subordinate->subordinate >=
13896 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013897 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013898 pci_dev_put(bridge);
13899 break;
13900 }
13901 }
13902 }
13903
Michael Chan4a29cc22006-03-19 13:21:12 -080013904 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13905 * DMA addresses > 40-bit. This bridge may have other additional
13906 * 57xx devices behind it in some 4-port NIC designs for example.
13907 * Any tg3 device found behind the bridge will also need the 40-bit
13908 * DMA workaround.
13909 */
Michael Chana4e2b342005-10-26 15:46:52 -070013910 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13911 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013912 tg3_flag_set(tp, 5780_CLASS);
13913 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013914 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013915 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013916 struct pci_dev *bridge = NULL;
13917
13918 do {
13919 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13920 PCI_DEVICE_ID_SERVERWORKS_EPB,
13921 bridge);
13922 if (bridge && bridge->subordinate &&
13923 (bridge->subordinate->number <=
13924 tp->pdev->bus->number) &&
13925 (bridge->subordinate->subordinate >=
13926 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013927 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013928 pci_dev_put(bridge);
13929 break;
13930 }
13931 } while (bridge);
13932 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013933
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013934 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Matt Carlson3a1e19d2011-07-13 09:27:32 +000013935 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)
Michael Chan7544b092007-05-05 13:08:32 -070013936 tp->pdev_peer = tg3_find_peer(tp);
13937
Matt Carlsonc885e822010-08-02 11:25:57 +000013938 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013939 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13940 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013941 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013942
13943 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013944 tg3_flag(tp, 5717_PLUS))
13945 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013946
Matt Carlson321d32a2008-11-21 17:22:19 -080013947 /* Intentionally exclude ASIC_REV_5906 */
13948 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013949 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013950 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013951 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013952 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013953 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013954 tg3_flag(tp, 57765_PLUS))
13955 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013956
13957 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13958 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013959 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013960 tg3_flag(tp, 5755_PLUS) ||
13961 tg3_flag(tp, 5780_CLASS))
13962 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013963
Matt Carlson6ff6f812011-05-19 12:12:54 +000013964 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013965 tg3_flag(tp, 5750_PLUS))
13966 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013967
Matt Carlson507399f2009-11-13 13:03:37 +000013968 /* Determine TSO capabilities */
Matt Carlsona0512942011-07-27 14:20:54 +000013969 if (tp->pci_chip_rev_id == CHIPREV_ID_5719_A0)
Matt Carlson4d163b72011-01-25 15:58:48 +000013970 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013971 else if (tg3_flag(tp, 57765_PLUS))
13972 tg3_flag_set(tp, HW_TSO_3);
13973 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013974 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013975 tg3_flag_set(tp, HW_TSO_2);
13976 else if (tg3_flag(tp, 5750_PLUS)) {
13977 tg3_flag_set(tp, HW_TSO_1);
13978 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013979 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13980 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013981 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013982 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13983 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13984 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013985 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013986 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13987 tp->fw_needed = FIRMWARE_TG3TSO5;
13988 else
13989 tp->fw_needed = FIRMWARE_TG3TSO;
13990 }
13991
Matt Carlsondabc5c62011-05-19 12:12:52 +000013992 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013993 if (tg3_flag(tp, HW_TSO_1) ||
13994 tg3_flag(tp, HW_TSO_2) ||
13995 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsondabc5c62011-05-19 12:12:52 +000013996 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13997 tg3_flag_set(tp, TSO_CAPABLE);
13998 else {
13999 tg3_flag_clear(tp, TSO_CAPABLE);
14000 tg3_flag_clear(tp, TSO_BUG);
14001 tp->fw_needed = NULL;
14002 }
14003
14004 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
14005 tp->fw_needed = FIRMWARE_TG3;
14006
Matt Carlson507399f2009-11-13 13:03:37 +000014007 tp->irq_max = 1;
14008
Joe Perches63c3a662011-04-26 08:12:10 +000014009 if (tg3_flag(tp, 5750_PLUS)) {
14010 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014011 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
14012 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
14013 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
14014 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
14015 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000014016 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070014017
Joe Perches63c3a662011-04-26 08:12:10 +000014018 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070014019 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014020 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070014021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014022
Joe Perches63c3a662011-04-26 08:12:10 +000014023 if (tg3_flag(tp, 57765_PLUS)) {
14024 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000014025 tp->irq_max = TG3_IRQ_MAX_VECS;
14026 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014027 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000014028
Matt Carlson2ffcc982011-05-19 12:12:44 +000014029 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000014030 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014031
Matt Carlsone31aa982011-07-27 14:20:53 +000014032 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
14033 tg3_flag_set(tp, 4K_FIFO_LIMIT);
14034
Joe Perches63c3a662011-04-26 08:12:10 +000014035 if (tg3_flag(tp, 5717_PLUS))
14036 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000014037
Joe Perches63c3a662011-04-26 08:12:10 +000014038 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlsona0512942011-07-27 14:20:54 +000014039 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0)
Joe Perches63c3a662011-04-26 08:12:10 +000014040 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000014041
Joe Perches63c3a662011-04-26 08:12:10 +000014042 if (!tg3_flag(tp, 5705_PLUS) ||
14043 tg3_flag(tp, 5780_CLASS) ||
14044 tg3_flag(tp, USE_JUMBO_BDFLAG))
14045 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070014046
Matt Carlson52f44902008-11-21 17:17:04 -080014047 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14048 &pci_state_reg);
14049
Jon Mason708ebb32011-06-27 12:56:50 +000014050 if (pci_is_pcie(tp->pdev)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014051 u16 lnkctl;
14052
Joe Perches63c3a662011-04-26 08:12:10 +000014053 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014054
Matt Carlsoncf790032010-11-24 08:31:48 +000014055 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000014056 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
14057 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000014058 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000014059
14060 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080014061
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014062 pci_read_config_word(tp->pdev,
Jon Mason708ebb32011-06-27 12:56:50 +000014063 pci_pcie_cap(tp->pdev) + PCI_EXP_LNKCTL,
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014064 &lnkctl);
14065 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000014066 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
14067 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000014068 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000014069 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000014070 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080014071 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014072 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000014073 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
14074 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000014075 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b05902010-01-20 16:58:02 +000014076 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000014077 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080014078 }
Matt Carlson52f44902008-11-21 17:17:04 -080014079 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Jon Mason708ebb32011-06-27 12:56:50 +000014080 /* BCM5785 devices are effectively PCIe devices, and should
14081 * follow PCIe codepaths, but do not have a PCIe capabilities
14082 * section.
14083 */
Joe Perches63c3a662011-04-26 08:12:10 +000014084 tg3_flag_set(tp, PCI_EXPRESS);
14085 } else if (!tg3_flag(tp, 5705_PLUS) ||
14086 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080014087 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
14088 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000014089 dev_err(&tp->pdev->dev,
14090 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080014091 return -EIO;
14092 }
14093
14094 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000014095 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080014096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014097
Michael Chan399de502005-10-03 14:02:39 -070014098 /* If we have an AMD 762 or VIA K8T800 chipset, write
14099 * reordering to the mailbox registers done by the host
14100 * controller can cause major troubles. We read back from
14101 * every mailbox register write to force the writes to be
14102 * posted to the chip in order.
14103 */
Matt Carlson41434702011-03-09 16:58:22 +000014104 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000014105 !tg3_flag(tp, PCI_EXPRESS))
14106 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070014107
Matt Carlson69fc4052008-12-21 20:19:57 -080014108 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
14109 &tp->pci_cacheline_sz);
14110 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14111 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014112 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14113 tp->pci_lat_timer < 64) {
14114 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080014115 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
14116 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014117 }
14118
Matt Carlson16821282011-07-13 09:27:28 +000014119 /* Important! -- It is critical that the PCI-X hw workaround
14120 * situation is decided before the first MMIO register access.
14121 */
Matt Carlson52f44902008-11-21 17:17:04 -080014122 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
14123 /* 5700 BX chips need to have their TX producer index
14124 * mailboxes written twice to workaround a bug.
14125 */
Joe Perches63c3a662011-04-26 08:12:10 +000014126 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070014127
Matt Carlson52f44902008-11-21 17:17:04 -080014128 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014129 *
14130 * The workaround is to use indirect register accesses
14131 * for all chip writes not to mailbox registers.
14132 */
Joe Perches63c3a662011-04-26 08:12:10 +000014133 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014134 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014135
Joe Perches63c3a662011-04-26 08:12:10 +000014136 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014137
14138 /* The chip can have it's power management PCI config
14139 * space registers clobbered due to this bug.
14140 * So explicitly force the chip into D0 here.
14141 */
Matt Carlson9974a352007-10-07 23:27:28 -070014142 pci_read_config_dword(tp->pdev,
14143 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014144 &pm_reg);
14145 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
14146 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070014147 pci_write_config_dword(tp->pdev,
14148 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070014149 pm_reg);
14150
14151 /* Also, force SERR#/PERR# in PCI command. */
14152 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14153 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
14154 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14155 }
14156 }
14157
Linus Torvalds1da177e2005-04-16 15:20:36 -070014158 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014159 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014160 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000014161 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014162
14163 /* Chip-specific fixup from Broadcom driver */
14164 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
14165 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
14166 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
14167 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
14168 }
14169
Michael Chan1ee582d2005-08-09 20:16:46 -070014170 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070014171 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014172 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070014173 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070014174 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070014175 tp->write32_tx_mbox = tg3_write32;
14176 tp->write32_rx_mbox = tg3_write32;
14177
14178 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000014179 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070014180 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014181 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014182 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070014183 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
14184 /*
14185 * Back to back register writes can cause problems on these
14186 * chips, the workaround is to read back all reg writes
14187 * except those to mailbox regs.
14188 *
14189 * See tg3_write_indirect_reg32().
14190 */
Michael Chan1ee582d2005-08-09 20:16:46 -070014191 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070014192 }
14193
Joe Perches63c3a662011-04-26 08:12:10 +000014194 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070014195 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000014196 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070014197 tp->write32_rx_mbox = tg3_write_flush_reg32;
14198 }
Michael Chan20094932005-08-09 20:16:32 -070014199
Joe Perches63c3a662011-04-26 08:12:10 +000014200 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070014201 tp->read32 = tg3_read_indirect_reg32;
14202 tp->write32 = tg3_write_indirect_reg32;
14203 tp->read32_mbox = tg3_read_indirect_mbox;
14204 tp->write32_mbox = tg3_write_indirect_mbox;
14205 tp->write32_tx_mbox = tg3_write_indirect_mbox;
14206 tp->write32_rx_mbox = tg3_write_indirect_mbox;
14207
14208 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070014209 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070014210
14211 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
14212 pci_cmd &= ~PCI_COMMAND_MEMORY;
14213 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
14214 }
Michael Chanb5d37722006-09-27 16:06:21 -070014215 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14216 tp->read32_mbox = tg3_read32_mbox_5906;
14217 tp->write32_mbox = tg3_write32_mbox_5906;
14218 tp->write32_tx_mbox = tg3_write32_mbox_5906;
14219 tp->write32_rx_mbox = tg3_write32_mbox_5906;
14220 }
Michael Chan68929142005-08-09 20:17:14 -070014221
Michael Chanbbadf502006-04-06 21:46:34 -070014222 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014223 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070014224 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070014225 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000014226 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070014227
Matt Carlson16821282011-07-13 09:27:28 +000014228 /* The memory arbiter has to be enabled in order for SRAM accesses
14229 * to succeed. Normally on powerup the tg3 chip firmware will make
14230 * sure it is enabled, but other entities such as system netboot
14231 * code might disable it.
14232 */
14233 val = tr32(MEMARB_MODE);
14234 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
14235
Matt Carlson69f11c92011-07-13 09:27:30 +000014236 if (tg3_flag(tp, PCIX_MODE)) {
14237 pci_read_config_dword(tp->pdev,
14238 tp->pcix_cap + PCI_X_STATUS, &val);
14239 tp->pci_fn = val & 0x7;
14240 } else {
14241 tp->pci_fn = PCI_FUNC(tp->pdev->devfn) & 3;
14242 }
14243
Michael Chan7d0c41e2005-04-21 17:06:20 -070014244 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000014245 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070014246 * determined before calling tg3_set_power_state() so that
14247 * we know whether or not to switch out of Vaux power.
14248 * When the flag is set, it means that GPIO1 is used for eeprom
14249 * write protect and also implies that it is a LOM where GPIOs
14250 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040014251 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070014252 tg3_get_eeprom_hw_cfg(tp);
14253
Joe Perches63c3a662011-04-26 08:12:10 +000014254 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070014255 /* Allow reads and writes to the
14256 * APE register and memory space.
14257 */
14258 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +000014259 PCISTATE_ALLOW_APE_SHMEM_WR |
14260 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070014261 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
14262 pci_state_reg);
Matt Carlsonc9cab242011-07-13 09:27:27 +000014263
14264 tg3_ape_lock_init(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070014265 }
14266
Matt Carlson9936bcf2007-10-10 18:03:07 -070014267 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070014268 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014269 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014270 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014271 tg3_flag(tp, 57765_PLUS))
14272 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070014273
Matt Carlson16821282011-07-13 09:27:28 +000014274 /* Set up tp->grc_local_ctrl before calling
14275 * tg3_pwrsrc_switch_to_vmain(). GPIO1 driven high
14276 * will bring 5700's external PHY out of reset.
Michael Chan314fba32005-04-21 17:07:04 -070014277 * It is also used as eeprom write protect on LOMs.
14278 */
14279 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014280 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014281 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070014282 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
14283 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070014284 /* Unused GPIO3 must be driven as output on 5752 because there
14285 * are no pull-up resistors on unused GPIO pins.
14286 */
14287 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
14288 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070014289
Matt Carlson321d32a2008-11-21 17:22:19 -080014290 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000014291 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
14292 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080014293 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
14294
Matt Carlson8d519ab2009-04-20 06:58:01 +000014295 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14296 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014297 /* Turn off the debug UART. */
14298 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014299 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014300 /* Keep VMain power. */
14301 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14302 GRC_LCLCTRL_GPIO_OUTPUT0;
14303 }
14304
Matt Carlson16821282011-07-13 09:27:28 +000014305 /* Switch out of Vaux if it is a NIC */
14306 tg3_pwrsrc_switch_to_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014307
Linus Torvalds1da177e2005-04-16 15:20:36 -070014308 /* Derive initial jumbo mode from MTU assigned in
14309 * ether_setup() via the alloc_etherdev() call
14310 */
Joe Perches63c3a662011-04-26 08:12:10 +000014311 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14312 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014313
14314 /* Determine WakeOnLan speed to use. */
14315 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14316 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14317 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14318 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014319 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014320 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014321 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014322 }
14323
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014324 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014325 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014326
Linus Torvalds1da177e2005-04-16 15:20:36 -070014327 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014328 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14329 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014330 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014331 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014332 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14333 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14334 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014335
14336 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14337 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014338 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014339 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014340 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014341
Joe Perches63c3a662011-04-26 08:12:10 +000014342 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014343 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014344 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014345 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014346 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014347 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014348 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014349 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14350 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014351 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14352 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014353 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014354 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014355 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014356 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014357 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014359
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014360 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14361 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14362 tp->phy_otp = tg3_read_otp_phycfg(tp);
14363 if (tp->phy_otp == 0)
14364 tp->phy_otp = TG3_OTP_DEFAULT;
14365 }
14366
Joe Perches63c3a662011-04-26 08:12:10 +000014367 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014368 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14369 else
14370 tp->mi_mode = MAC_MI_MODE_BASE;
14371
Linus Torvalds1da177e2005-04-16 15:20:36 -070014372 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014373 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14374 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14375 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14376
Matt Carlson4d958472011-04-20 07:57:35 +000014377 /* Set these bits to enable statistics workaround. */
14378 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14379 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14380 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14381 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14382 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14383 }
14384
Matt Carlson321d32a2008-11-21 17:22:19 -080014385 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14386 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014387 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014388
Matt Carlson158d7ab2008-05-29 01:37:54 -070014389 err = tg3_mdio_init(tp);
14390 if (err)
14391 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014392
14393 /* Initialize data/descriptor byte/word swapping. */
14394 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014395 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14396 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14397 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14398 GRC_MODE_B2HRX_ENABLE |
14399 GRC_MODE_HTX2B_ENABLE |
14400 GRC_MODE_HOST_STACKUP);
14401 else
14402 val &= GRC_MODE_HOST_STACKUP;
14403
Linus Torvalds1da177e2005-04-16 15:20:36 -070014404 tw32(GRC_MODE, val | tp->grc_mode);
14405
14406 tg3_switch_clocks(tp);
14407
14408 /* Clear this out for sanity. */
14409 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14410
14411 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14412 &pci_state_reg);
14413 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014414 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014415 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14416
14417 if (chiprevid == CHIPREV_ID_5701_A0 ||
14418 chiprevid == CHIPREV_ID_5701_B0 ||
14419 chiprevid == CHIPREV_ID_5701_B2 ||
14420 chiprevid == CHIPREV_ID_5701_B5) {
14421 void __iomem *sram_base;
14422
14423 /* Write some dummy words into the SRAM status block
14424 * area, see if it reads back correctly. If the return
14425 * value is bad, force enable the PCIX workaround.
14426 */
14427 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14428
14429 writel(0x00000000, sram_base);
14430 writel(0x00000000, sram_base + 4);
14431 writel(0xffffffff, sram_base + 4);
14432 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014433 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014434 }
14435 }
14436
14437 udelay(50);
14438 tg3_nvram_init(tp);
14439
14440 grc_misc_cfg = tr32(GRC_MISC_CFG);
14441 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14442
Linus Torvalds1da177e2005-04-16 15:20:36 -070014443 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14444 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14445 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014446 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014447
Joe Perches63c3a662011-04-26 08:12:10 +000014448 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014449 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014450 tg3_flag_set(tp, TAGGED_STATUS);
14451 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014452 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14453 HOSTCC_MODE_CLRTICK_TXBD);
14454
14455 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14456 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14457 tp->misc_host_ctrl);
14458 }
14459
Matt Carlson3bda1252008-08-15 14:08:22 -070014460 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014461 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014462 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014463 else
Matt Carlson6e01b202011-08-19 13:58:20 +000014464 tp->mac_mode = 0;
Matt Carlson3bda1252008-08-15 14:08:22 -070014465
Linus Torvalds1da177e2005-04-16 15:20:36 -070014466 /* these are limited to 10/100 only */
14467 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14468 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14469 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14470 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14471 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14472 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14473 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14474 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14475 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014476 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14477 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014478 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014479 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14480 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014481 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14482 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014483
14484 err = tg3_phy_probe(tp);
14485 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014486 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014487 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014488 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014489 }
14490
Matt Carlson184b8902010-04-05 10:19:25 +000014491 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014492 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014493
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014494 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14495 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014496 } else {
14497 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014498 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014499 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014500 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014501 }
14502
14503 /* 5700 {AX,BX} chips have a broken status block link
14504 * change bit implementation, so we must use the
14505 * status register in those cases.
14506 */
14507 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014508 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014509 else
Joe Perches63c3a662011-04-26 08:12:10 +000014510 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014511
14512 /* The led_ctrl is set during tg3_phy_probe, here we might
14513 * have to force the link status polling mechanism based
14514 * upon subsystem IDs.
14515 */
14516 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014517 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014518 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14519 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014520 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014521 }
14522
14523 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014524 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014525 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014526 else
Joe Perches63c3a662011-04-26 08:12:10 +000014527 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014528
Matt Carlsonbf933c82011-01-25 15:58:49 +000014529 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014530 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014531 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014532 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014533 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014534#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014535 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014536#endif
14537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014538
Matt Carlson2c49a442010-09-30 10:34:35 +000014539 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14540 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014541 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14542
Matt Carlson2c49a442010-09-30 10:34:35 +000014543 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014544
14545 /* Increment the rx prod index on the rx std ring by at most
14546 * 8 for these chips to workaround hw errata.
14547 */
14548 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14549 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14550 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14551 tp->rx_std_max_post = 8;
14552
Joe Perches63c3a662011-04-26 08:12:10 +000014553 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014554 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14555 PCIE_PWR_MGMT_L1_THRESH_MSK;
14556
Linus Torvalds1da177e2005-04-16 15:20:36 -070014557 return err;
14558}
14559
David S. Miller49b6e95f2007-03-29 01:38:42 -070014560#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014561static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14562{
14563 struct net_device *dev = tp->dev;
14564 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014565 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014566 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014567 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014568
David S. Miller49b6e95f2007-03-29 01:38:42 -070014569 addr = of_get_property(dp, "local-mac-address", &len);
14570 if (addr && len == 6) {
14571 memcpy(dev->dev_addr, addr, 6);
14572 memcpy(dev->perm_addr, dev->dev_addr, 6);
14573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014574 }
14575 return -ENODEV;
14576}
14577
14578static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14579{
14580 struct net_device *dev = tp->dev;
14581
14582 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014583 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014584 return 0;
14585}
14586#endif
14587
14588static int __devinit tg3_get_device_address(struct tg3 *tp)
14589{
14590 struct net_device *dev = tp->dev;
14591 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014592 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014593
David S. Miller49b6e95f2007-03-29 01:38:42 -070014594#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014595 if (!tg3_get_macaddr_sparc(tp))
14596 return 0;
14597#endif
14598
14599 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014600 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014601 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014602 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14603 mac_offset = 0xcc;
14604 if (tg3_nvram_lock(tp))
14605 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14606 else
14607 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014608 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson69f11c92011-07-13 09:27:30 +000014609 if (tp->pci_fn & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014610 mac_offset = 0xcc;
Matt Carlson69f11c92011-07-13 09:27:30 +000014611 if (tp->pci_fn > 1)
Matt Carlsona50d0792010-06-05 17:24:37 +000014612 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014613 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014614 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014615
14616 /* First try to get it from MAC address mailbox. */
14617 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14618 if ((hi >> 16) == 0x484b) {
14619 dev->dev_addr[0] = (hi >> 8) & 0xff;
14620 dev->dev_addr[1] = (hi >> 0) & 0xff;
14621
14622 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14623 dev->dev_addr[2] = (lo >> 24) & 0xff;
14624 dev->dev_addr[3] = (lo >> 16) & 0xff;
14625 dev->dev_addr[4] = (lo >> 8) & 0xff;
14626 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014627
Michael Chan008652b2006-03-27 23:14:53 -080014628 /* Some old bootcode may report a 0 MAC address in SRAM */
14629 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14630 }
14631 if (!addr_ok) {
14632 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014633 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014634 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014635 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014636 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14637 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014638 }
14639 /* Finally just fetch it out of the MAC control regs. */
14640 else {
14641 hi = tr32(MAC_ADDR_0_HIGH);
14642 lo = tr32(MAC_ADDR_0_LOW);
14643
14644 dev->dev_addr[5] = lo & 0xff;
14645 dev->dev_addr[4] = (lo >> 8) & 0xff;
14646 dev->dev_addr[3] = (lo >> 16) & 0xff;
14647 dev->dev_addr[2] = (lo >> 24) & 0xff;
14648 dev->dev_addr[1] = hi & 0xff;
14649 dev->dev_addr[0] = (hi >> 8) & 0xff;
14650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014651 }
14652
14653 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014654#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014655 if (!tg3_get_default_macaddr_sparc(tp))
14656 return 0;
14657#endif
14658 return -EINVAL;
14659 }
John W. Linville2ff43692005-09-12 14:44:20 -070014660 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014661 return 0;
14662}
14663
David S. Miller59e6b432005-05-18 22:50:10 -070014664#define BOUNDARY_SINGLE_CACHELINE 1
14665#define BOUNDARY_MULTI_CACHELINE 2
14666
14667static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14668{
14669 int cacheline_size;
14670 u8 byte;
14671 int goal;
14672
14673 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14674 if (byte == 0)
14675 cacheline_size = 1024;
14676 else
14677 cacheline_size = (int) byte * 4;
14678
14679 /* On 5703 and later chips, the boundary bits have no
14680 * effect.
14681 */
14682 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14683 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014684 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014685 goto out;
14686
14687#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14688 goal = BOUNDARY_MULTI_CACHELINE;
14689#else
14690#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14691 goal = BOUNDARY_SINGLE_CACHELINE;
14692#else
14693 goal = 0;
14694#endif
14695#endif
14696
Joe Perches63c3a662011-04-26 08:12:10 +000014697 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014698 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14699 goto out;
14700 }
14701
David S. Miller59e6b432005-05-18 22:50:10 -070014702 if (!goal)
14703 goto out;
14704
14705 /* PCI controllers on most RISC systems tend to disconnect
14706 * when a device tries to burst across a cache-line boundary.
14707 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14708 *
14709 * Unfortunately, for PCI-E there are only limited
14710 * write-side controls for this, and thus for reads
14711 * we will still get the disconnects. We'll also waste
14712 * these PCI cycles for both read and write for chips
14713 * other than 5700 and 5701 which do not implement the
14714 * boundary bits.
14715 */
Joe Perches63c3a662011-04-26 08:12:10 +000014716 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014717 switch (cacheline_size) {
14718 case 16:
14719 case 32:
14720 case 64:
14721 case 128:
14722 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14723 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14724 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14725 } else {
14726 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14727 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14728 }
14729 break;
14730
14731 case 256:
14732 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14733 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14734 break;
14735
14736 default:
14737 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14738 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14739 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014740 }
Joe Perches63c3a662011-04-26 08:12:10 +000014741 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014742 switch (cacheline_size) {
14743 case 16:
14744 case 32:
14745 case 64:
14746 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14747 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14748 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14749 break;
14750 }
14751 /* fallthrough */
14752 case 128:
14753 default:
14754 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14755 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14756 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014757 }
David S. Miller59e6b432005-05-18 22:50:10 -070014758 } else {
14759 switch (cacheline_size) {
14760 case 16:
14761 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14762 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14763 DMA_RWCTRL_WRITE_BNDRY_16);
14764 break;
14765 }
14766 /* fallthrough */
14767 case 32:
14768 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14769 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14770 DMA_RWCTRL_WRITE_BNDRY_32);
14771 break;
14772 }
14773 /* fallthrough */
14774 case 64:
14775 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14776 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14777 DMA_RWCTRL_WRITE_BNDRY_64);
14778 break;
14779 }
14780 /* fallthrough */
14781 case 128:
14782 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14783 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14784 DMA_RWCTRL_WRITE_BNDRY_128);
14785 break;
14786 }
14787 /* fallthrough */
14788 case 256:
14789 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14790 DMA_RWCTRL_WRITE_BNDRY_256);
14791 break;
14792 case 512:
14793 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14794 DMA_RWCTRL_WRITE_BNDRY_512);
14795 break;
14796 case 1024:
14797 default:
14798 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14799 DMA_RWCTRL_WRITE_BNDRY_1024);
14800 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014801 }
David S. Miller59e6b432005-05-18 22:50:10 -070014802 }
14803
14804out:
14805 return val;
14806}
14807
Linus Torvalds1da177e2005-04-16 15:20:36 -070014808static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14809{
14810 struct tg3_internal_buffer_desc test_desc;
14811 u32 sram_dma_descs;
14812 int i, ret;
14813
14814 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14815
14816 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14817 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14818 tw32(RDMAC_STATUS, 0);
14819 tw32(WDMAC_STATUS, 0);
14820
14821 tw32(BUFMGR_MODE, 0);
14822 tw32(FTQ_RESET, 0);
14823
14824 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14825 test_desc.addr_lo = buf_dma & 0xffffffff;
14826 test_desc.nic_mbuf = 0x00002100;
14827 test_desc.len = size;
14828
14829 /*
14830 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14831 * the *second* time the tg3 driver was getting loaded after an
14832 * initial scan.
14833 *
14834 * Broadcom tells me:
14835 * ...the DMA engine is connected to the GRC block and a DMA
14836 * reset may affect the GRC block in some unpredictable way...
14837 * The behavior of resets to individual blocks has not been tested.
14838 *
14839 * Broadcom noted the GRC reset will also reset all sub-components.
14840 */
14841 if (to_device) {
14842 test_desc.cqid_sqid = (13 << 8) | 2;
14843
14844 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14845 udelay(40);
14846 } else {
14847 test_desc.cqid_sqid = (16 << 8) | 7;
14848
14849 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14850 udelay(40);
14851 }
14852 test_desc.flags = 0x00000005;
14853
14854 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14855 u32 val;
14856
14857 val = *(((u32 *)&test_desc) + i);
14858 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14859 sram_dma_descs + (i * sizeof(u32)));
14860 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14861 }
14862 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14863
Matt Carlson859a5882010-04-05 10:19:28 +000014864 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014865 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014866 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014867 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014868
14869 ret = -ENODEV;
14870 for (i = 0; i < 40; i++) {
14871 u32 val;
14872
14873 if (to_device)
14874 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14875 else
14876 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14877 if ((val & 0xffff) == sram_dma_descs) {
14878 ret = 0;
14879 break;
14880 }
14881
14882 udelay(100);
14883 }
14884
14885 return ret;
14886}
14887
David S. Millerded73402005-05-23 13:59:47 -070014888#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014889
Matt Carlson41434702011-03-09 16:58:22 +000014890static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014891 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14892 { },
14893};
14894
Linus Torvalds1da177e2005-04-16 15:20:36 -070014895static int __devinit tg3_test_dma(struct tg3 *tp)
14896{
14897 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014898 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014899 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014900
Matt Carlson4bae65c2010-11-24 08:31:52 +000014901 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14902 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014903 if (!buf) {
14904 ret = -ENOMEM;
14905 goto out_nofree;
14906 }
14907
14908 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14909 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14910
David S. Miller59e6b432005-05-18 22:50:10 -070014911 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014912
Joe Perches63c3a662011-04-26 08:12:10 +000014913 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014914 goto out;
14915
Joe Perches63c3a662011-04-26 08:12:10 +000014916 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014917 /* DMA read watermark not used on PCIE */
14918 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014919 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014920 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14921 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014922 tp->dma_rwctrl |= 0x003f0000;
14923 else
14924 tp->dma_rwctrl |= 0x003f000f;
14925 } else {
14926 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14927 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14928 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014929 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014930
Michael Chan4a29cc22006-03-19 13:21:12 -080014931 /* If the 5704 is behind the EPB bridge, we can
14932 * do the less restrictive ONE_DMA workaround for
14933 * better performance.
14934 */
Joe Perches63c3a662011-04-26 08:12:10 +000014935 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014936 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14937 tp->dma_rwctrl |= 0x8000;
14938 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014939 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14940
Michael Chan49afdeb2007-02-13 12:17:03 -080014941 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14942 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014943 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014944 tp->dma_rwctrl |=
14945 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14946 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14947 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014948 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14949 /* 5780 always in PCIX mode */
14950 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014951 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14952 /* 5714 always in PCIX mode */
14953 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014954 } else {
14955 tp->dma_rwctrl |= 0x001b000f;
14956 }
14957 }
14958
14959 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14960 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14961 tp->dma_rwctrl &= 0xfffffff0;
14962
14963 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14964 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14965 /* Remove this if it causes problems for some boards. */
14966 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14967
14968 /* On 5700/5701 chips, we need to set this bit.
14969 * Otherwise the chip will issue cacheline transactions
14970 * to streamable DMA memory with not all the byte
14971 * enables turned on. This is an error on several
14972 * RISC PCI controllers, in particular sparc64.
14973 *
14974 * On 5703/5704 chips, this bit has been reassigned
14975 * a different meaning. In particular, it is used
14976 * on those chips to enable a PCI-X workaround.
14977 */
14978 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14979 }
14980
14981 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14982
14983#if 0
14984 /* Unneeded, already done by tg3_get_invariants. */
14985 tg3_switch_clocks(tp);
14986#endif
14987
Linus Torvalds1da177e2005-04-16 15:20:36 -070014988 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14989 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14990 goto out;
14991
David S. Miller59e6b432005-05-18 22:50:10 -070014992 /* It is best to perform DMA test with maximum write burst size
14993 * to expose the 5700/5701 write DMA bug.
14994 */
14995 saved_dma_rwctrl = tp->dma_rwctrl;
14996 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14997 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14998
Linus Torvalds1da177e2005-04-16 15:20:36 -070014999 while (1) {
15000 u32 *p = buf, i;
15001
15002 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
15003 p[i] = i;
15004
15005 /* Send the buffer to the chip. */
15006 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
15007 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000015008 dev_err(&tp->pdev->dev,
15009 "%s: Buffer write failed. err = %d\n",
15010 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015011 break;
15012 }
15013
15014#if 0
15015 /* validate data reached card RAM correctly. */
15016 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15017 u32 val;
15018 tg3_read_mem(tp, 0x2100 + (i*4), &val);
15019 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000015020 dev_err(&tp->pdev->dev,
15021 "%s: Buffer corrupted on device! "
15022 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015023 /* ret = -ENODEV here? */
15024 }
15025 p[i] = 0;
15026 }
15027#endif
15028 /* Now read it back. */
15029 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
15030 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000015031 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
15032 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015033 break;
15034 }
15035
15036 /* Verify it. */
15037 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
15038 if (p[i] == i)
15039 continue;
15040
David S. Miller59e6b432005-05-18 22:50:10 -070015041 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15042 DMA_RWCTRL_WRITE_BNDRY_16) {
15043 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015044 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
15045 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15046 break;
15047 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000015048 dev_err(&tp->pdev->dev,
15049 "%s: Buffer corrupted on read back! "
15050 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015051 ret = -ENODEV;
15052 goto out;
15053 }
15054 }
15055
15056 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
15057 /* Success. */
15058 ret = 0;
15059 break;
15060 }
15061 }
David S. Miller59e6b432005-05-18 22:50:10 -070015062 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
15063 DMA_RWCTRL_WRITE_BNDRY_16) {
15064 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070015065 * now look for chipsets that are known to expose the
15066 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070015067 */
Matt Carlson41434702011-03-09 16:58:22 +000015068 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015069 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
15070 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000015071 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070015072 /* Safe to use the calculated DMA boundary. */
15073 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000015074 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070015075
David S. Miller59e6b432005-05-18 22:50:10 -070015076 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
15077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015078
15079out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000015080 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015081out_nofree:
15082 return ret;
15083}
15084
Linus Torvalds1da177e2005-04-16 15:20:36 -070015085static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
15086{
Joe Perches63c3a662011-04-26 08:12:10 +000015087 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000015088 tp->bufmgr_config.mbuf_read_dma_low_water =
15089 DEFAULT_MB_RDMA_LOW_WATER_5705;
15090 tp->bufmgr_config.mbuf_mac_rx_low_water =
15091 DEFAULT_MB_MACRX_LOW_WATER_57765;
15092 tp->bufmgr_config.mbuf_high_water =
15093 DEFAULT_MB_HIGH_WATER_57765;
15094
15095 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15096 DEFAULT_MB_RDMA_LOW_WATER_5705;
15097 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15098 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
15099 tp->bufmgr_config.mbuf_high_water_jumbo =
15100 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000015101 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec172005-07-25 12:31:48 -070015102 tp->bufmgr_config.mbuf_read_dma_low_water =
15103 DEFAULT_MB_RDMA_LOW_WATER_5705;
15104 tp->bufmgr_config.mbuf_mac_rx_low_water =
15105 DEFAULT_MB_MACRX_LOW_WATER_5705;
15106 tp->bufmgr_config.mbuf_high_water =
15107 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070015108 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
15109 tp->bufmgr_config.mbuf_mac_rx_low_water =
15110 DEFAULT_MB_MACRX_LOW_WATER_5906;
15111 tp->bufmgr_config.mbuf_high_water =
15112 DEFAULT_MB_HIGH_WATER_5906;
15113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015114
Michael Chanfdfec172005-07-25 12:31:48 -070015115 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15116 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
15117 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15118 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
15119 tp->bufmgr_config.mbuf_high_water_jumbo =
15120 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
15121 } else {
15122 tp->bufmgr_config.mbuf_read_dma_low_water =
15123 DEFAULT_MB_RDMA_LOW_WATER;
15124 tp->bufmgr_config.mbuf_mac_rx_low_water =
15125 DEFAULT_MB_MACRX_LOW_WATER;
15126 tp->bufmgr_config.mbuf_high_water =
15127 DEFAULT_MB_HIGH_WATER;
15128
15129 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
15130 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
15131 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
15132 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
15133 tp->bufmgr_config.mbuf_high_water_jumbo =
15134 DEFAULT_MB_HIGH_WATER_JUMBO;
15135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015136
15137 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
15138 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
15139}
15140
15141static char * __devinit tg3_phy_string(struct tg3 *tp)
15142{
Matt Carlson79eb6902010-02-17 15:17:03 +000015143 switch (tp->phy_id & TG3_PHY_ID_MASK) {
15144 case TG3_PHY_ID_BCM5400: return "5400";
15145 case TG3_PHY_ID_BCM5401: return "5401";
15146 case TG3_PHY_ID_BCM5411: return "5411";
15147 case TG3_PHY_ID_BCM5701: return "5701";
15148 case TG3_PHY_ID_BCM5703: return "5703";
15149 case TG3_PHY_ID_BCM5704: return "5704";
15150 case TG3_PHY_ID_BCM5705: return "5705";
15151 case TG3_PHY_ID_BCM5750: return "5750";
15152 case TG3_PHY_ID_BCM5752: return "5752";
15153 case TG3_PHY_ID_BCM5714: return "5714";
15154 case TG3_PHY_ID_BCM5780: return "5780";
15155 case TG3_PHY_ID_BCM5755: return "5755";
15156 case TG3_PHY_ID_BCM5787: return "5787";
15157 case TG3_PHY_ID_BCM5784: return "5784";
15158 case TG3_PHY_ID_BCM5756: return "5722/5756";
15159 case TG3_PHY_ID_BCM5906: return "5906";
15160 case TG3_PHY_ID_BCM5761: return "5761";
15161 case TG3_PHY_ID_BCM5718C: return "5718C";
15162 case TG3_PHY_ID_BCM5718S: return "5718S";
15163 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000015164 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000015165 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000015166 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070015167 case 0: return "serdes";
15168 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070015169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015170}
15171
Michael Chanf9804dd2005-09-27 12:13:10 -070015172static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
15173{
Joe Perches63c3a662011-04-26 08:12:10 +000015174 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015175 strcpy(str, "PCI Express");
15176 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000015177 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070015178 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
15179
15180 strcpy(str, "PCIX:");
15181
15182 if ((clock_ctrl == 7) ||
15183 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
15184 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
15185 strcat(str, "133MHz");
15186 else if (clock_ctrl == 0)
15187 strcat(str, "33MHz");
15188 else if (clock_ctrl == 2)
15189 strcat(str, "50MHz");
15190 else if (clock_ctrl == 4)
15191 strcat(str, "66MHz");
15192 else if (clock_ctrl == 6)
15193 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070015194 } else {
15195 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000015196 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070015197 strcat(str, "66MHz");
15198 else
15199 strcat(str, "33MHz");
15200 }
Joe Perches63c3a662011-04-26 08:12:10 +000015201 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070015202 strcat(str, ":32-bit");
15203 else
15204 strcat(str, ":64-bit");
15205 return str;
15206}
15207
Michael Chan8c2dc7e2005-12-19 16:26:02 -080015208static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015209{
15210 struct pci_dev *peer;
15211 unsigned int func, devnr = tp->pdev->devfn & ~7;
15212
15213 for (func = 0; func < 8; func++) {
15214 peer = pci_get_slot(tp->pdev->bus, devnr | func);
15215 if (peer && peer != tp->pdev)
15216 break;
15217 pci_dev_put(peer);
15218 }
Michael Chan16fe9d72005-12-13 21:09:54 -080015219 /* 5704 can be configured in single-port mode, set peer to
15220 * tp->pdev in that case.
15221 */
15222 if (!peer) {
15223 peer = tp->pdev;
15224 return peer;
15225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015226
15227 /*
15228 * We don't need to keep the refcount elevated; there's no way
15229 * to remove one half of this device without removing the other
15230 */
15231 pci_dev_put(peer);
15232
15233 return peer;
15234}
15235
David S. Miller15f98502005-05-18 22:49:26 -070015236static void __devinit tg3_init_coal(struct tg3 *tp)
15237{
15238 struct ethtool_coalesce *ec = &tp->coal;
15239
15240 memset(ec, 0, sizeof(*ec));
15241 ec->cmd = ETHTOOL_GCOALESCE;
15242 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
15243 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
15244 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
15245 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
15246 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
15247 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
15248 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
15249 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
15250 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
15251
15252 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
15253 HOSTCC_MODE_CLRTICK_TXBD)) {
15254 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
15255 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
15256 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
15257 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
15258 }
Michael Chand244c892005-07-05 14:42:33 -070015259
Joe Perches63c3a662011-04-26 08:12:10 +000015260 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070015261 ec->rx_coalesce_usecs_irq = 0;
15262 ec->tx_coalesce_usecs_irq = 0;
15263 ec->stats_block_coalesce_usecs = 0;
15264 }
David S. Miller15f98502005-05-18 22:49:26 -070015265}
15266
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080015267static const struct net_device_ops tg3_netdev_ops = {
15268 .ndo_open = tg3_open,
15269 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080015270 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000015271 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080015272 .ndo_validate_addr = eth_validate_addr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +000015273 .ndo_set_rx_mode = tg3_set_rx_mode,
Stephen Hemminger00829822008-11-20 20:14:53 -080015274 .ndo_set_mac_address = tg3_set_mac_addr,
15275 .ndo_do_ioctl = tg3_ioctl,
15276 .ndo_tx_timeout = tg3_tx_timeout,
15277 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000015278 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015279 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080015280#ifdef CONFIG_NET_POLL_CONTROLLER
15281 .ndo_poll_controller = tg3_poll_controller,
15282#endif
15283};
15284
Linus Torvalds1da177e2005-04-16 15:20:36 -070015285static int __devinit tg3_init_one(struct pci_dev *pdev,
15286 const struct pci_device_id *ent)
15287{
Linus Torvalds1da177e2005-04-16 15:20:36 -070015288 struct net_device *dev;
15289 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000015290 int i, err, pm_cap;
15291 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015292 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015293 u64 dma_mask, persist_dma_mask;
Matt Carlson0da06062011-05-19 12:12:53 +000015294 u32 features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015295
Joe Perches05dbe002010-02-17 19:44:19 +000015296 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015297
15298 err = pci_enable_device(pdev);
15299 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015300 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015301 return err;
15302 }
15303
Linus Torvalds1da177e2005-04-16 15:20:36 -070015304 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15305 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015306 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015307 goto err_out_disable_pdev;
15308 }
15309
15310 pci_set_master(pdev);
15311
15312 /* Find power-management capability. */
15313 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15314 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015315 dev_err(&pdev->dev,
15316 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015317 err = -EIO;
15318 goto err_out_free_res;
15319 }
15320
Matt Carlson16821282011-07-13 09:27:28 +000015321 err = pci_set_power_state(pdev, PCI_D0);
15322 if (err) {
15323 dev_err(&pdev->dev, "Transition to D0 failed, aborting\n");
15324 goto err_out_free_res;
15325 }
15326
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015327 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015328 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015329 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015330 err = -ENOMEM;
Matt Carlson16821282011-07-13 09:27:28 +000015331 goto err_out_power_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015332 }
15333
Linus Torvalds1da177e2005-04-16 15:20:36 -070015334 SET_NETDEV_DEV(dev, &pdev->dev);
15335
Linus Torvalds1da177e2005-04-16 15:20:36 -070015336 tp = netdev_priv(dev);
15337 tp->pdev = pdev;
15338 tp->dev = dev;
15339 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015340 tp->rx_mode = TG3_DEF_RX_MODE;
15341 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015342
Linus Torvalds1da177e2005-04-16 15:20:36 -070015343 if (tg3_debug > 0)
15344 tp->msg_enable = tg3_debug;
15345 else
15346 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15347
15348 /* The word/byte swap controls here control register access byte
15349 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15350 * setting below.
15351 */
15352 tp->misc_host_ctrl =
15353 MISC_HOST_CTRL_MASK_PCI_INT |
15354 MISC_HOST_CTRL_WORD_SWAP |
15355 MISC_HOST_CTRL_INDIR_ACCESS |
15356 MISC_HOST_CTRL_PCISTATE_RW;
15357
15358 /* The NONFRM (non-frame) byte/word swap controls take effect
15359 * on descriptor entries, anything which isn't packet data.
15360 *
15361 * The StrongARM chips on the board (one for tx, one for rx)
15362 * are running in big-endian mode.
15363 */
15364 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15365 GRC_MODE_WSWAP_NONFRM_DATA);
15366#ifdef __BIG_ENDIAN
15367 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15368#endif
15369 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015370 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015371 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015372
Matt Carlsond5fe4882008-11-21 17:20:32 -080015373 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015374 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015375 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015376 err = -ENOMEM;
15377 goto err_out_free_dev;
15378 }
15379
Matt Carlsonc9cab242011-07-13 09:27:27 +000015380 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
15381 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761E ||
15382 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S ||
15383 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761SE ||
15384 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
15385 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
15386 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
15387 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720) {
15388 tg3_flag_set(tp, ENABLE_APE);
15389 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
15390 if (!tp->aperegs) {
15391 dev_err(&pdev->dev,
15392 "Cannot map APE registers, aborting\n");
15393 err = -ENOMEM;
15394 goto err_out_iounmap;
15395 }
15396 }
15397
Linus Torvalds1da177e2005-04-16 15:20:36 -070015398 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15399 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015400
Linus Torvalds1da177e2005-04-16 15:20:36 -070015401 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015402 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015403 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015404 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015405
15406 err = tg3_get_invariants(tp);
15407 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015408 dev_err(&pdev->dev,
15409 "Problem fetching invariants of chip, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015410 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015411 }
15412
Michael Chan4a29cc22006-03-19 13:21:12 -080015413 /* The EPB bridge inside 5714, 5715, and 5780 and any
15414 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015415 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15416 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15417 * do DMA address check in tg3_start_xmit().
15418 */
Joe Perches63c3a662011-04-26 08:12:10 +000015419 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015420 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015421 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015422 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015423#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015424 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015425#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015426 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015427 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015428
15429 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015430 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015431 err = pci_set_dma_mask(pdev, dma_mask);
15432 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015433 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015434 err = pci_set_consistent_dma_mask(pdev,
15435 persist_dma_mask);
15436 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015437 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15438 "DMA for consistent allocations\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015439 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015440 }
15441 }
15442 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015443 if (err || dma_mask == DMA_BIT_MASK(32)) {
15444 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015445 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015446 dev_err(&pdev->dev,
15447 "No usable DMA configuration, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015448 goto err_out_apeunmap;
Michael Chan72f2afb2006-03-06 19:28:35 -080015449 }
15450 }
15451
Michael Chanfdfec172005-07-25 12:31:48 -070015452 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015453
Matt Carlson0da06062011-05-19 12:12:53 +000015454 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15455
15456 /* 5700 B0 chips do not support checksumming correctly due
15457 * to hardware bugs.
15458 */
15459 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15460 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15461
15462 if (tg3_flag(tp, 5755_PLUS))
15463 features |= NETIF_F_IPV6_CSUM;
15464 }
15465
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015466 /* TSO is on by default on chips that support hardware TSO.
15467 * Firmware TSO on older chips gives lower performance, so it
15468 * is off by default, but can be enabled using ethtool.
15469 */
Joe Perches63c3a662011-04-26 08:12:10 +000015470 if ((tg3_flag(tp, HW_TSO_1) ||
15471 tg3_flag(tp, HW_TSO_2) ||
15472 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015473 (features & NETIF_F_IP_CSUM))
15474 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015475 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015476 if (features & NETIF_F_IPV6_CSUM)
15477 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015478 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015479 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015480 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15481 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015482 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015483 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015484 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015486
Matt Carlsond542fe22011-05-19 16:02:43 +000015487 dev->features |= features;
15488 dev->vlan_features |= features;
15489
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015490 /*
15491 * Add loopback capability only for a subset of devices that support
15492 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15493 * loopback for the remaining devices.
15494 */
15495 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15496 !tg3_flag(tp, CPMU_PRESENT))
15497 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015498 features |= NETIF_F_LOOPBACK;
15499
Matt Carlson0da06062011-05-19 12:12:53 +000015500 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015501
Linus Torvalds1da177e2005-04-16 15:20:36 -070015502 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015503 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015504 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015505 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015506 tp->rx_pending = 63;
15507 }
15508
Linus Torvalds1da177e2005-04-16 15:20:36 -070015509 err = tg3_get_device_address(tp);
15510 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015511 dev_err(&pdev->dev,
15512 "Could not obtain valid ethernet address, aborting\n");
Matt Carlsonc9cab242011-07-13 09:27:27 +000015513 goto err_out_apeunmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015514 }
15515
Matt Carlsonc88864d2007-11-12 21:07:01 -080015516 /*
15517 * Reset chip in case UNDI or EFI driver did not shutdown
15518 * DMA self test will enable WDMAC and we'll see (spurious)
15519 * pending DMA on the PCI bus at that point.
15520 */
15521 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15522 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15523 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15524 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15525 }
15526
15527 err = tg3_test_dma(tp);
15528 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015529 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015530 goto err_out_apeunmap;
15531 }
15532
Matt Carlson78f90dc2009-11-13 13:03:42 +000015533 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15534 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15535 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015536 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015537 struct tg3_napi *tnapi = &tp->napi[i];
15538
15539 tnapi->tp = tp;
15540 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15541
15542 tnapi->int_mbox = intmbx;
15543 if (i < 4)
15544 intmbx += 0x8;
15545 else
15546 intmbx += 0x4;
15547
15548 tnapi->consmbox = rcvmbx;
15549 tnapi->prodmbox = sndmbx;
15550
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015551 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015552 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015553 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015554 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015555
Joe Perches63c3a662011-04-26 08:12:10 +000015556 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015557 break;
15558
15559 /*
15560 * If we support MSIX, we'll be using RSS. If we're using
15561 * RSS, the first vector only handles link interrupts and the
15562 * remaining vectors handle rx and tx interrupts. Reuse the
15563 * mailbox values for the next iteration. The values we setup
15564 * above are still useful for the single vectored mode.
15565 */
15566 if (!i)
15567 continue;
15568
15569 rcvmbx += 0x8;
15570
15571 if (sndmbx & 0x4)
15572 sndmbx -= 0x4;
15573 else
15574 sndmbx += 0xc;
15575 }
15576
Matt Carlsonc88864d2007-11-12 21:07:01 -080015577 tg3_init_coal(tp);
15578
Michael Chanc49a1562006-12-17 17:07:29 -080015579 pci_set_drvdata(pdev, dev);
15580
Matt Carlsoncd0d7222011-07-13 09:27:33 +000015581 if (tg3_flag(tp, 5717_PLUS)) {
15582 /* Resume a low-power mode */
15583 tg3_frob_aux_power(tp, false);
15584 }
15585
Linus Torvalds1da177e2005-04-16 15:20:36 -070015586 err = register_netdev(dev);
15587 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015588 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015589 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015590 }
15591
Joe Perches05dbe002010-02-17 19:44:19 +000015592 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15593 tp->board_part_number,
15594 tp->pci_chip_rev_id,
15595 tg3_bus_string(tp, str),
15596 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015597
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015598 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015599 struct phy_device *phydev;
15600 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015601 netdev_info(dev,
15602 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015603 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015604 } else {
15605 char *ethtype;
15606
15607 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15608 ethtype = "10/100Base-TX";
15609 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15610 ethtype = "1000Base-SX";
15611 else
15612 ethtype = "10/100/1000Base-T";
15613
Matt Carlson5129c3a2010-04-05 10:19:23 +000015614 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015615 "(WireSpeed[%d], EEE[%d])\n",
15616 tg3_phy_string(tp), ethtype,
15617 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15618 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015619 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015620
Joe Perches05dbe002010-02-17 19:44:19 +000015621 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015622 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015623 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015624 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015625 tg3_flag(tp, ENABLE_ASF) != 0,
15626 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015627 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15628 tp->dma_rwctrl,
15629 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15630 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015631
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015632 pci_save_state(pdev);
15633
Linus Torvalds1da177e2005-04-16 15:20:36 -070015634 return 0;
15635
Matt Carlson0d3031d2007-10-10 18:02:43 -070015636err_out_apeunmap:
15637 if (tp->aperegs) {
15638 iounmap(tp->aperegs);
15639 tp->aperegs = NULL;
15640 }
15641
Linus Torvalds1da177e2005-04-16 15:20:36 -070015642err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015643 if (tp->regs) {
15644 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015645 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015647
15648err_out_free_dev:
15649 free_netdev(dev);
15650
Matt Carlson16821282011-07-13 09:27:28 +000015651err_out_power_down:
15652 pci_set_power_state(pdev, PCI_D3hot);
15653
Linus Torvalds1da177e2005-04-16 15:20:36 -070015654err_out_free_res:
15655 pci_release_regions(pdev);
15656
15657err_out_disable_pdev:
15658 pci_disable_device(pdev);
15659 pci_set_drvdata(pdev, NULL);
15660 return err;
15661}
15662
15663static void __devexit tg3_remove_one(struct pci_dev *pdev)
15664{
15665 struct net_device *dev = pci_get_drvdata(pdev);
15666
15667 if (dev) {
15668 struct tg3 *tp = netdev_priv(dev);
15669
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015670 if (tp->fw)
15671 release_firmware(tp->fw);
15672
Tejun Heo23f333a2010-12-12 16:45:14 +010015673 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015674
Joe Perches63c3a662011-04-26 08:12:10 +000015675 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015676 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015677 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015678 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015679
Linus Torvalds1da177e2005-04-16 15:20:36 -070015680 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015681 if (tp->aperegs) {
15682 iounmap(tp->aperegs);
15683 tp->aperegs = NULL;
15684 }
Michael Chan68929142005-08-09 20:17:14 -070015685 if (tp->regs) {
15686 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015687 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015689 free_netdev(dev);
15690 pci_release_regions(pdev);
15691 pci_disable_device(pdev);
15692 pci_set_drvdata(pdev, NULL);
15693 }
15694}
15695
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015696#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015697static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015698{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015699 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015700 struct net_device *dev = pci_get_drvdata(pdev);
15701 struct tg3 *tp = netdev_priv(dev);
15702 int err;
15703
15704 if (!netif_running(dev))
15705 return 0;
15706
Tejun Heo23f333a2010-12-12 16:45:14 +010015707 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015708 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015709 tg3_netif_stop(tp);
15710
15711 del_timer_sync(&tp->timer);
15712
David S. Millerf47c11e2005-06-24 20:18:35 -070015713 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015714 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015715 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015716
15717 netif_device_detach(dev);
15718
David S. Millerf47c11e2005-06-24 20:18:35 -070015719 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015720 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015721 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015722 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015723
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015724 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015725 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015726 int err2;
15727
David S. Millerf47c11e2005-06-24 20:18:35 -070015728 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015729
Joe Perches63c3a662011-04-26 08:12:10 +000015730 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015731 err2 = tg3_restart_hw(tp, 1);
15732 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015733 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015734
15735 tp->timer.expires = jiffies + tp->timer_offset;
15736 add_timer(&tp->timer);
15737
15738 netif_device_attach(dev);
15739 tg3_netif_start(tp);
15740
Michael Chanb9ec6c12006-07-25 16:37:27 -070015741out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015742 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015743
15744 if (!err2)
15745 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015746 }
15747
15748 return err;
15749}
15750
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015751static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015752{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015753 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015754 struct net_device *dev = pci_get_drvdata(pdev);
15755 struct tg3 *tp = netdev_priv(dev);
15756 int err;
15757
15758 if (!netif_running(dev))
15759 return 0;
15760
Linus Torvalds1da177e2005-04-16 15:20:36 -070015761 netif_device_attach(dev);
15762
David S. Millerf47c11e2005-06-24 20:18:35 -070015763 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015764
Joe Perches63c3a662011-04-26 08:12:10 +000015765 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015766 err = tg3_restart_hw(tp, 1);
15767 if (err)
15768 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015769
15770 tp->timer.expires = jiffies + tp->timer_offset;
15771 add_timer(&tp->timer);
15772
Linus Torvalds1da177e2005-04-16 15:20:36 -070015773 tg3_netif_start(tp);
15774
Michael Chanb9ec6c12006-07-25 16:37:27 -070015775out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015776 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015777
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015778 if (!err)
15779 tg3_phy_start(tp);
15780
Michael Chanb9ec6c12006-07-25 16:37:27 -070015781 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015782}
15783
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015784static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015785#define TG3_PM_OPS (&tg3_pm_ops)
15786
15787#else
15788
15789#define TG3_PM_OPS NULL
15790
15791#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015792
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015793/**
15794 * tg3_io_error_detected - called when PCI error is detected
15795 * @pdev: Pointer to PCI device
15796 * @state: The current pci connection state
15797 *
15798 * This function is called after a PCI bus error affecting
15799 * this device has been detected.
15800 */
15801static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15802 pci_channel_state_t state)
15803{
15804 struct net_device *netdev = pci_get_drvdata(pdev);
15805 struct tg3 *tp = netdev_priv(netdev);
15806 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15807
15808 netdev_info(netdev, "PCI I/O error detected\n");
15809
15810 rtnl_lock();
15811
15812 if (!netif_running(netdev))
15813 goto done;
15814
15815 tg3_phy_stop(tp);
15816
15817 tg3_netif_stop(tp);
15818
15819 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015820 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015821
15822 /* Want to make sure that the reset task doesn't run */
15823 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015824 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15825 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015826
15827 netif_device_detach(netdev);
15828
15829 /* Clean up software state, even if MMIO is blocked */
15830 tg3_full_lock(tp, 0);
15831 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15832 tg3_full_unlock(tp);
15833
15834done:
15835 if (state == pci_channel_io_perm_failure)
15836 err = PCI_ERS_RESULT_DISCONNECT;
15837 else
15838 pci_disable_device(pdev);
15839
15840 rtnl_unlock();
15841
15842 return err;
15843}
15844
15845/**
15846 * tg3_io_slot_reset - called after the pci bus has been reset.
15847 * @pdev: Pointer to PCI device
15848 *
15849 * Restart the card from scratch, as if from a cold-boot.
15850 * At this point, the card has exprienced a hard reset,
15851 * followed by fixups by BIOS, and has its config space
15852 * set up identically to what it was at cold boot.
15853 */
15854static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15855{
15856 struct net_device *netdev = pci_get_drvdata(pdev);
15857 struct tg3 *tp = netdev_priv(netdev);
15858 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15859 int err;
15860
15861 rtnl_lock();
15862
15863 if (pci_enable_device(pdev)) {
15864 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15865 goto done;
15866 }
15867
15868 pci_set_master(pdev);
15869 pci_restore_state(pdev);
15870 pci_save_state(pdev);
15871
15872 if (!netif_running(netdev)) {
15873 rc = PCI_ERS_RESULT_RECOVERED;
15874 goto done;
15875 }
15876
15877 err = tg3_power_up(tp);
Matt Carlsonbed98292011-07-13 09:27:29 +000015878 if (err)
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015879 goto done;
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015880
15881 rc = PCI_ERS_RESULT_RECOVERED;
15882
15883done:
15884 rtnl_unlock();
15885
15886 return rc;
15887}
15888
15889/**
15890 * tg3_io_resume - called when traffic can start flowing again.
15891 * @pdev: Pointer to PCI device
15892 *
15893 * This callback is called when the error recovery driver tells
15894 * us that its OK to resume normal operation.
15895 */
15896static void tg3_io_resume(struct pci_dev *pdev)
15897{
15898 struct net_device *netdev = pci_get_drvdata(pdev);
15899 struct tg3 *tp = netdev_priv(netdev);
15900 int err;
15901
15902 rtnl_lock();
15903
15904 if (!netif_running(netdev))
15905 goto done;
15906
15907 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015908 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015909 err = tg3_restart_hw(tp, 1);
15910 tg3_full_unlock(tp);
15911 if (err) {
15912 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15913 goto done;
15914 }
15915
15916 netif_device_attach(netdev);
15917
15918 tp->timer.expires = jiffies + tp->timer_offset;
15919 add_timer(&tp->timer);
15920
15921 tg3_netif_start(tp);
15922
15923 tg3_phy_start(tp);
15924
15925done:
15926 rtnl_unlock();
15927}
15928
15929static struct pci_error_handlers tg3_err_handler = {
15930 .error_detected = tg3_io_error_detected,
15931 .slot_reset = tg3_io_slot_reset,
15932 .resume = tg3_io_resume
15933};
15934
Linus Torvalds1da177e2005-04-16 15:20:36 -070015935static struct pci_driver tg3_driver = {
15936 .name = DRV_MODULE_NAME,
15937 .id_table = tg3_pci_tbl,
15938 .probe = tg3_init_one,
15939 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015940 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015941 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015942};
15943
15944static int __init tg3_init(void)
15945{
Jeff Garzik29917622006-08-19 17:48:59 -040015946 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015947}
15948
15949static void __exit tg3_cleanup(void)
15950{
15951 pci_unregister_driver(&tg3_driver);
15952}
15953
15954module_init(tg3_init);
15955module_exit(tg3_cleanup);