blob: 97cd02d080216ce1a11079193c93ac91ddcde10d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tg3.c: Broadcom Tigon3 ethernet driver.
3 *
4 * Copyright (C) 2001, 2002, 2003, 2004 David S. Miller (davem@redhat.com)
5 * Copyright (C) 2001, 2002, 2003 Jeff Garzik (jgarzik@pobox.com)
6 * Copyright (C) 2004 Sun Microsystems Inc.
Matt Carlsonb86fb2c2011-01-25 15:58:57 +00007 * Copyright (C) 2005-2011 Broadcom Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Firmware is:
Michael Chan49cabf42005-06-06 15:15:17 -070010 * Derived from proprietary unpublished source code,
11 * Copyright (C) 2000-2003 Broadcom Corporation.
12 *
13 * Permission is hereby granted for the distribution of this firmware
14 * data in hexadecimal or equivalent format, provided this copyright
15 * notice is accompanying it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/module.h>
20#include <linux/moduleparam.h>
Matt Carlson6867c842010-07-11 09:31:44 +000021#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/compiler.h>
25#include <linux/slab.h>
26#include <linux/delay.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020027#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000029#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/ioport.h>
31#include <linux/pci.h>
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/skbuff.h>
35#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000036#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070038#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070039#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/if_vlan.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070044#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020045#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080046#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030049#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <asm/system.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000052#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000054#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David S. Miller49b6e95f2007-03-29 01:38:42 -070056#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070058#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
Matt Carlson63532392008-11-03 16:49:57 -080061#define BAR_0 0
62#define BAR_2 2
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include "tg3.h"
65
Joe Perches63c3a662011-04-26 08:12:10 +000066/* Functions & macros to verify TG3_FLAGS types */
67
68static inline int _tg3_flag(enum TG3_FLAGS flag, unsigned long *bits)
69{
70 return test_bit(flag, bits);
71}
72
73static inline void _tg3_flag_set(enum TG3_FLAGS flag, unsigned long *bits)
74{
75 set_bit(flag, bits);
76}
77
78static inline void _tg3_flag_clear(enum TG3_FLAGS flag, unsigned long *bits)
79{
80 clear_bit(flag, bits);
81}
82
83#define tg3_flag(tp, flag) \
84 _tg3_flag(TG3_FLAG_##flag, (tp)->tg3_flags)
85#define tg3_flag_set(tp, flag) \
86 _tg3_flag_set(TG3_FLAG_##flag, (tp)->tg3_flags)
87#define tg3_flag_clear(tp, flag) \
88 _tg3_flag_clear(TG3_FLAG_##flag, (tp)->tg3_flags)
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000091#define TG3_MAJ_NUM 3
Matt Carlson43a5f002011-05-19 12:12:56 +000092#define TG3_MIN_NUM 119
Matt Carlson6867c842010-07-11 09:31:44 +000093#define DRV_MODULE_VERSION \
94 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlson43a5f002011-05-19 12:12:56 +000095#define DRV_MODULE_RELDATE "May 18, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#define TG3_DEF_MAC_MODE 0
98#define TG3_DEF_RX_MODE 0
99#define TG3_DEF_TX_MODE 0
100#define TG3_DEF_MSG_ENABLE \
101 (NETIF_MSG_DRV | \
102 NETIF_MSG_PROBE | \
103 NETIF_MSG_LINK | \
104 NETIF_MSG_TIMER | \
105 NETIF_MSG_IFDOWN | \
106 NETIF_MSG_IFUP | \
107 NETIF_MSG_RX_ERR | \
108 NETIF_MSG_TX_ERR)
109
Matt Carlson520b2752011-06-13 13:39:02 +0000110#define TG3_GRC_LCLCTL_PWRSW_DELAY 100
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* length of time before we decide the hardware is borked,
113 * and dev->tx_timeout() should be called to fix the problem
114 */
Joe Perches63c3a662011-04-26 08:12:10 +0000115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#define TG3_TX_TIMEOUT (5 * HZ)
117
118/* hardware minimum and maximum for a single frame's data payload */
119#define TG3_MIN_MTU 60
120#define TG3_MAX_MTU(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000121 (tg3_flag(tp, JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/* These numbers seem to be hard coded in the NIC firmware somehow.
124 * You can't change the ring sizes, but you can change where you place
125 * them in the NIC onboard memory.
126 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000127#define TG3_RX_STD_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000128 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000129 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000131#define TG3_RX_JMB_RING_SIZE(tp) \
Joe Perches63c3a662011-04-26 08:12:10 +0000132 (tg3_flag(tp, LRG_PROD_RING_CAP) ? \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000133 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000135#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/* Do not place this n-ring entries value into the tp struct itself,
138 * we really want to expose these constants to GCC so that modulo et
139 * al. operations are done with shifts and masks instead of with
140 * hw multiply/modulo instructions. Another solution would be to
141 * replace things like '% foo' with '& (foo - 1)'.
142 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144#define TG3_TX_RING_SIZE 512
145#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
146
Matt Carlson2c49a442010-09-30 10:34:35 +0000147#define TG3_RX_STD_RING_BYTES(tp) \
148 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
149#define TG3_RX_JMB_RING_BYTES(tp) \
150 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
151#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000152 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
154 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
156
Matt Carlson287be122009-08-28 13:58:46 +0000157#define TG3_DMA_BYTE_ENAB 64
158
159#define TG3_RX_STD_DMA_SZ 1536
160#define TG3_RX_JMB_DMA_SZ 9046
161
162#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
163
164#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
165#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Matt Carlson2c49a442010-09-30 10:34:35 +0000167#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
168 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000169
Matt Carlson2c49a442010-09-30 10:34:35 +0000170#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
171 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000172
Matt Carlsond2757fc2010-04-12 06:58:27 +0000173/* Due to a hardware bug, the 5701 can only DMA to memory addresses
174 * that are at least dword aligned when used in PCIX mode. The driver
175 * works around this bug by double copying the packet. This workaround
176 * is built into the normal double copy length check for efficiency.
177 *
178 * However, the double copy is only necessary on those architectures
179 * where unaligned memory accesses are inefficient. For those architectures
180 * where unaligned memory accesses incur little penalty, we can reintegrate
181 * the 5701 in the normal rx path. Doing so saves a device structure
182 * dereference by hardcoding the double copy threshold in place.
183 */
184#define TG3_RX_COPY_THRESHOLD 256
185#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
186 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
187#else
188 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
189#endif
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000192#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Matt Carlsonad829262008-11-21 17:16:16 -0800194#define TG3_RAW_IP_ALIGN 2
195
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000196#define TG3_FW_UPDATE_TIMEOUT_SEC 5
197
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800198#define FIRMWARE_TG3 "tigon/tg3.bin"
199#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
200#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000203 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
206MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
207MODULE_LICENSE("GPL");
208MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800209MODULE_FIRMWARE(FIRMWARE_TG3);
210MODULE_FIRMWARE(FIRMWARE_TG3TSO);
211MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
214module_param(tg3_debug, int, 0);
215MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
216
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000217static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
263 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
264 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
265 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700266 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
267 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700268 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
269 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700270 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700271 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
272 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800273 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
274 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000275 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
276 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800277 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
278 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
279 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000280 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000281 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
282 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000283 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
284 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
285 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
286 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
287 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
288 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000289 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000290 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700291 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
292 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
293 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
294 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
295 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
296 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
297 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
Meelis Roos1dcb14d2011-05-25 05:43:47 +0000298 {PCI_DEVICE(0x10cf, 0x11a2)}, /* Fujitsu 1000base-SX with BCM5703SKHB */
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700299 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
302MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
303
Andreas Mohr50da8592006-08-14 23:54:30 -0700304static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000306} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 { "rx_octets" },
308 { "rx_fragments" },
309 { "rx_ucast_packets" },
310 { "rx_mcast_packets" },
311 { "rx_bcast_packets" },
312 { "rx_fcs_errors" },
313 { "rx_align_errors" },
314 { "rx_xon_pause_rcvd" },
315 { "rx_xoff_pause_rcvd" },
316 { "rx_mac_ctrl_rcvd" },
317 { "rx_xoff_entered" },
318 { "rx_frame_too_long_errors" },
319 { "rx_jabbers" },
320 { "rx_undersize_packets" },
321 { "rx_in_length_errors" },
322 { "rx_out_length_errors" },
323 { "rx_64_or_less_octet_packets" },
324 { "rx_65_to_127_octet_packets" },
325 { "rx_128_to_255_octet_packets" },
326 { "rx_256_to_511_octet_packets" },
327 { "rx_512_to_1023_octet_packets" },
328 { "rx_1024_to_1522_octet_packets" },
329 { "rx_1523_to_2047_octet_packets" },
330 { "rx_2048_to_4095_octet_packets" },
331 { "rx_4096_to_8191_octet_packets" },
332 { "rx_8192_to_9022_octet_packets" },
333
334 { "tx_octets" },
335 { "tx_collisions" },
336
337 { "tx_xon_sent" },
338 { "tx_xoff_sent" },
339 { "tx_flow_control" },
340 { "tx_mac_errors" },
341 { "tx_single_collisions" },
342 { "tx_mult_collisions" },
343 { "tx_deferred" },
344 { "tx_excessive_collisions" },
345 { "tx_late_collisions" },
346 { "tx_collide_2times" },
347 { "tx_collide_3times" },
348 { "tx_collide_4times" },
349 { "tx_collide_5times" },
350 { "tx_collide_6times" },
351 { "tx_collide_7times" },
352 { "tx_collide_8times" },
353 { "tx_collide_9times" },
354 { "tx_collide_10times" },
355 { "tx_collide_11times" },
356 { "tx_collide_12times" },
357 { "tx_collide_13times" },
358 { "tx_collide_14times" },
359 { "tx_collide_15times" },
360 { "tx_ucast_packets" },
361 { "tx_mcast_packets" },
362 { "tx_bcast_packets" },
363 { "tx_carrier_sense_errors" },
364 { "tx_discards" },
365 { "tx_errors" },
366
367 { "dma_writeq_full" },
368 { "dma_write_prioq_full" },
369 { "rxbds_empty" },
370 { "rx_discards" },
371 { "rx_errors" },
372 { "rx_threshold_hit" },
373
374 { "dma_readq_full" },
375 { "dma_read_prioq_full" },
376 { "tx_comp_queue_full" },
377
378 { "ring_set_send_prod_index" },
379 { "ring_status_update" },
380 { "nic_irqs" },
381 { "nic_avoided_irqs" },
Matt Carlson4452d092011-05-19 12:12:51 +0000382 { "nic_tx_threshold_hit" },
383
384 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385};
386
Matt Carlson48fa55a2011-04-13 11:05:06 +0000387#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
388
389
Andreas Mohr50da8592006-08-14 23:54:30 -0700390static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700391 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000392} ethtool_test_keys[] = {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700393 { "nvram test (online) " },
394 { "link test (online) " },
395 { "register test (offline)" },
396 { "memory test (offline)" },
397 { "loopback test (offline)" },
398 { "interrupt test (offline)" },
399};
400
Matt Carlson48fa55a2011-04-13 11:05:06 +0000401#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
402
403
Michael Chanb401e9e2005-12-19 16:27:04 -0800404static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
405{
406 writel(val, tp->regs + off);
407}
408
409static u32 tg3_read32(struct tg3 *tp, u32 off)
410{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000411 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800412}
413
Matt Carlson0d3031d2007-10-10 18:02:43 -0700414static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
415{
416 writel(val, tp->aperegs + off);
417}
418
419static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
420{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000421 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
425{
Michael Chan68929142005-08-09 20:17:14 -0700426 unsigned long flags;
427
428 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700429 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
430 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700431 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700432}
433
434static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
435{
436 writel(val, tp->regs + off);
437 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Michael Chan68929142005-08-09 20:17:14 -0700440static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
441{
442 unsigned long flags;
443 u32 val;
444
445 spin_lock_irqsave(&tp->indirect_lock, flags);
446 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
447 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
448 spin_unlock_irqrestore(&tp->indirect_lock, flags);
449 return val;
450}
451
452static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
453{
454 unsigned long flags;
455
456 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
457 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
458 TG3_64BIT_REG_LOW, val);
459 return;
460 }
Matt Carlson66711e62009-11-13 13:03:49 +0000461 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700462 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
463 TG3_64BIT_REG_LOW, val);
464 return;
465 }
466
467 spin_lock_irqsave(&tp->indirect_lock, flags);
468 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
469 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
470 spin_unlock_irqrestore(&tp->indirect_lock, flags);
471
472 /* In indirect mode when disabling interrupts, we also need
473 * to clear the interrupt bit in the GRC local ctrl register.
474 */
475 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
476 (val == 0x1)) {
477 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
478 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
479 }
480}
481
482static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
483{
484 unsigned long flags;
485 u32 val;
486
487 spin_lock_irqsave(&tp->indirect_lock, flags);
488 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
489 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
490 spin_unlock_irqrestore(&tp->indirect_lock, flags);
491 return val;
492}
493
Michael Chanb401e9e2005-12-19 16:27:04 -0800494/* usec_wait specifies the wait time in usec when writing to certain registers
495 * where it is unsafe to read back the register without some delay.
496 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
497 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
498 */
499static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Joe Perches63c3a662011-04-26 08:12:10 +0000501 if (tg3_flag(tp, PCIX_TARGET_HWBUG) || tg3_flag(tp, ICH_WORKAROUND))
Michael Chanb401e9e2005-12-19 16:27:04 -0800502 /* Non-posted methods */
503 tp->write32(tp, off, val);
504 else {
505 /* Posted method */
506 tg3_write32(tp, off, val);
507 if (usec_wait)
508 udelay(usec_wait);
509 tp->read32(tp, off);
510 }
511 /* Wait again after the read for the posted method to guarantee that
512 * the wait time is met.
513 */
514 if (usec_wait)
515 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
Michael Chan09ee9292005-08-09 20:17:00 -0700518static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
519{
520 tp->write32_mbox(tp, off, val);
Joe Perches63c3a662011-04-26 08:12:10 +0000521 if (!tg3_flag(tp, MBOX_WRITE_REORDER) && !tg3_flag(tp, ICH_WORKAROUND))
Michael Chan68929142005-08-09 20:17:14 -0700522 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700523}
524
Michael Chan20094932005-08-09 20:16:32 -0700525static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 void __iomem *mbox = tp->regs + off;
528 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000529 if (tg3_flag(tp, TXD_MBOX_HWBUG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 writel(val, mbox);
Joe Perches63c3a662011-04-26 08:12:10 +0000531 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 readl(mbox);
533}
534
Michael Chanb5d37722006-09-27 16:06:21 -0700535static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
536{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000537 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700538}
539
540static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
541{
542 writel(val, tp->regs + off + GRCMBOX_BASE);
543}
544
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000545#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700546#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000547#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
548#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
549#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700550
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000551#define tw32(reg, val) tp->write32(tp, reg, val)
552#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
553#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
554#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
557{
Michael Chan68929142005-08-09 20:17:14 -0700558 unsigned long flags;
559
Matt Carlson6ff6f812011-05-19 12:12:54 +0000560 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700561 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
562 return;
563
Michael Chan68929142005-08-09 20:17:14 -0700564 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000565 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700566 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
567 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Michael Chanbbadf502006-04-06 21:46:34 -0700569 /* Always leave this as zero. */
570 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
571 } else {
572 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
573 tw32_f(TG3PCI_MEM_WIN_DATA, val);
574
575 /* Always leave this as zero. */
576 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
577 }
Michael Chan68929142005-08-09 20:17:14 -0700578 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
582{
Michael Chan68929142005-08-09 20:17:14 -0700583 unsigned long flags;
584
Matt Carlson6ff6f812011-05-19 12:12:54 +0000585 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 &&
Michael Chanb5d37722006-09-27 16:06:21 -0700586 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
587 *val = 0;
588 return;
589 }
590
Michael Chan68929142005-08-09 20:17:14 -0700591 spin_lock_irqsave(&tp->indirect_lock, flags);
Joe Perches63c3a662011-04-26 08:12:10 +0000592 if (tg3_flag(tp, SRAM_USE_CONFIG)) {
Michael Chanbbadf502006-04-06 21:46:34 -0700593 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
594 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Michael Chanbbadf502006-04-06 21:46:34 -0700596 /* Always leave this as zero. */
597 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
598 } else {
599 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
600 *val = tr32(TG3PCI_MEM_WIN_DATA);
601
602 /* Always leave this as zero. */
603 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
604 }
Michael Chan68929142005-08-09 20:17:14 -0700605 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Matt Carlson0d3031d2007-10-10 18:02:43 -0700608static void tg3_ape_lock_init(struct tg3 *tp)
609{
610 int i;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000611 u32 regbase;
612
613 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
614 regbase = TG3_APE_LOCK_GRANT;
615 else
616 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700617
618 /* Make sure the driver hasn't any stale locks. */
619 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000620 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700621}
622
623static int tg3_ape_lock(struct tg3 *tp, int locknum)
624{
625 int i, off;
626 int ret = 0;
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000627 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700628
Joe Perches63c3a662011-04-26 08:12:10 +0000629 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700630 return 0;
631
632 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000633 case TG3_APE_LOCK_GRC:
634 case TG3_APE_LOCK_MEM:
635 break;
636 default:
637 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700638 }
639
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000640 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
641 req = TG3_APE_LOCK_REQ;
642 gnt = TG3_APE_LOCK_GRANT;
643 } else {
644 req = TG3_APE_PER_LOCK_REQ;
645 gnt = TG3_APE_PER_LOCK_GRANT;
646 }
647
Matt Carlson0d3031d2007-10-10 18:02:43 -0700648 off = 4 * locknum;
649
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000650 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700651
652 /* Wait for up to 1 millisecond to acquire lock. */
653 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000654 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700655 if (status == APE_LOCK_GRANT_DRIVER)
656 break;
657 udelay(10);
658 }
659
660 if (status != APE_LOCK_GRANT_DRIVER) {
661 /* Revoke the lock request. */
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000662 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700663 APE_LOCK_GRANT_DRIVER);
664
665 ret = -EBUSY;
666 }
667
668 return ret;
669}
670
671static void tg3_ape_unlock(struct tg3 *tp, int locknum)
672{
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000673 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700674
Joe Perches63c3a662011-04-26 08:12:10 +0000675 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -0700676 return;
677
678 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000679 case TG3_APE_LOCK_GRC:
680 case TG3_APE_LOCK_MEM:
681 break;
682 default:
683 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700684 }
685
Matt Carlsonf92d9dc12010-06-05 17:24:30 +0000686 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
687 gnt = TG3_APE_LOCK_GRANT;
688 else
689 gnt = TG3_APE_PER_LOCK_GRANT;
690
691 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700692}
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694static void tg3_disable_ints(struct tg3 *tp)
695{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000696 int i;
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 tw32(TG3PCI_MISC_HOST_CTRL,
699 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000700 for (i = 0; i < tp->irq_max; i++)
701 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704static void tg3_enable_ints(struct tg3 *tp)
705{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000706 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000707
Michael Chanbbe832c2005-06-24 20:20:04 -0700708 tp->irq_sync = 0;
709 wmb();
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 tw32(TG3PCI_MISC_HOST_CTRL,
712 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000713
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000714 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000715 for (i = 0; i < tp->irq_cnt; i++) {
716 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000717
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000718 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
Joe Perches63c3a662011-04-26 08:12:10 +0000719 if (tg3_flag(tp, 1SHOT_MSI))
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000720 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
721
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000722 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000723 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000724
725 /* Force an initial interrupt */
Joe Perches63c3a662011-04-26 08:12:10 +0000726 if (!tg3_flag(tp, TAGGED_STATUS) &&
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000727 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
728 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
729 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000730 tw32(HOSTCC_MODE, tp->coal_now);
731
732 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Matt Carlson17375d22009-08-28 14:02:18 +0000735static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700736{
Matt Carlson17375d22009-08-28 14:02:18 +0000737 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000738 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700739 unsigned int work_exists = 0;
740
741 /* check for phy events */
Joe Perches63c3a662011-04-26 08:12:10 +0000742 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Michael Chan04237dd2005-04-25 15:17:17 -0700743 if (sblk->status & SD_STATUS_LINK_CHG)
744 work_exists = 1;
745 }
746 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000747 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000748 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700749 work_exists = 1;
750
751 return work_exists;
752}
753
Matt Carlson17375d22009-08-28 14:02:18 +0000754/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700755 * similar to tg3_enable_ints, but it accurately determines whether there
756 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400757 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 */
Matt Carlson17375d22009-08-28 14:02:18 +0000759static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Matt Carlson17375d22009-08-28 14:02:18 +0000761 struct tg3 *tp = tnapi->tp;
762
Matt Carlson898a56f2009-08-28 14:02:40 +0000763 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 mmiowb();
765
David S. Millerfac9b832005-05-18 22:46:34 -0700766 /* When doing tagged status, this work check is unnecessary.
767 * The last_tag we write above tells the chip which piece of
768 * work we've completed.
769 */
Joe Perches63c3a662011-04-26 08:12:10 +0000770 if (!tg3_flag(tp, TAGGED_STATUS) && tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700771 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000772 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775static void tg3_switch_clocks(struct tg3 *tp)
776{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000777 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 u32 orig_clock_ctrl;
779
Joe Perches63c3a662011-04-26 08:12:10 +0000780 if (tg3_flag(tp, CPMU_PRESENT) || tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700781 return;
782
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000783 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 orig_clock_ctrl = clock_ctrl;
786 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
787 CLOCK_CTRL_CLKRUN_OENABLE |
788 0x1f);
789 tp->pci_clock_ctrl = clock_ctrl;
790
Joe Perches63c3a662011-04-26 08:12:10 +0000791 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800793 tw32_wait_f(TG3PCI_CLOCK_CTRL,
794 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800797 tw32_wait_f(TG3PCI_CLOCK_CTRL,
798 clock_ctrl |
799 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
800 40);
801 tw32_wait_f(TG3PCI_CLOCK_CTRL,
802 clock_ctrl | (CLOCK_CTRL_ALTCLK),
803 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800805 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
808#define PHY_BUSY_LOOPS 5000
809
810static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
811{
812 u32 frame_val;
813 unsigned int loops;
814 int ret;
815
816 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
817 tw32_f(MAC_MI_MODE,
818 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
819 udelay(80);
820 }
821
822 *val = 0x0;
823
Matt Carlson882e9792009-09-01 13:21:36 +0000824 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 MI_COM_PHY_ADDR_MASK);
826 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
827 MI_COM_REG_ADDR_MASK);
828 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 tw32_f(MAC_MI_COM, frame_val);
831
832 loops = PHY_BUSY_LOOPS;
833 while (loops != 0) {
834 udelay(10);
835 frame_val = tr32(MAC_MI_COM);
836
837 if ((frame_val & MI_COM_BUSY) == 0) {
838 udelay(5);
839 frame_val = tr32(MAC_MI_COM);
840 break;
841 }
842 loops -= 1;
843 }
844
845 ret = -EBUSY;
846 if (loops != 0) {
847 *val = frame_val & MI_COM_DATA_MASK;
848 ret = 0;
849 }
850
851 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
852 tw32_f(MAC_MI_MODE, tp->mi_mode);
853 udelay(80);
854 }
855
856 return ret;
857}
858
859static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
860{
861 u32 frame_val;
862 unsigned int loops;
863 int ret;
864
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000865 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson221c5632011-06-13 13:39:01 +0000866 (reg == MII_CTRL1000 || reg == MII_TG3_AUX_CTRL))
Michael Chanb5d37722006-09-27 16:06:21 -0700867 return 0;
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
870 tw32_f(MAC_MI_MODE,
871 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
872 udelay(80);
873 }
874
Matt Carlson882e9792009-09-01 13:21:36 +0000875 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 MI_COM_PHY_ADDR_MASK);
877 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
878 MI_COM_REG_ADDR_MASK);
879 frame_val |= (val & MI_COM_DATA_MASK);
880 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 tw32_f(MAC_MI_COM, frame_val);
883
884 loops = PHY_BUSY_LOOPS;
885 while (loops != 0) {
886 udelay(10);
887 frame_val = tr32(MAC_MI_COM);
888 if ((frame_val & MI_COM_BUSY) == 0) {
889 udelay(5);
890 frame_val = tr32(MAC_MI_COM);
891 break;
892 }
893 loops -= 1;
894 }
895
896 ret = -EBUSY;
897 if (loops != 0)
898 ret = 0;
899
900 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
901 tw32_f(MAC_MI_MODE, tp->mi_mode);
902 udelay(80);
903 }
904
905 return ret;
906}
907
Matt Carlsonb0988c12011-04-20 07:57:39 +0000908static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
909{
910 int err;
911
912 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
913 if (err)
914 goto done;
915
916 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
917 if (err)
918 goto done;
919
920 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
921 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
922 if (err)
923 goto done;
924
925 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
926
927done:
928 return err;
929}
930
931static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
932{
933 int err;
934
935 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
936 if (err)
937 goto done;
938
939 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
940 if (err)
941 goto done;
942
943 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
944 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
945 if (err)
946 goto done;
947
948 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
949
950done:
951 return err;
952}
953
954static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
955{
956 int err;
957
958 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
959 if (!err)
960 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
961
962 return err;
963}
964
965static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
966{
967 int err;
968
969 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
970 if (!err)
971 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
972
973 return err;
974}
975
Matt Carlson15ee95c2011-04-20 07:57:40 +0000976static int tg3_phy_auxctl_read(struct tg3 *tp, int reg, u32 *val)
977{
978 int err;
979
980 err = tg3_writephy(tp, MII_TG3_AUX_CTRL,
981 (reg << MII_TG3_AUXCTL_MISC_RDSEL_SHIFT) |
982 MII_TG3_AUXCTL_SHDWSEL_MISC);
983 if (!err)
984 err = tg3_readphy(tp, MII_TG3_AUX_CTRL, val);
985
986 return err;
987}
988
Matt Carlsonb4bd2922011-04-20 07:57:41 +0000989static int tg3_phy_auxctl_write(struct tg3 *tp, int reg, u32 set)
990{
991 if (reg == MII_TG3_AUXCTL_SHDWSEL_MISC)
992 set |= MII_TG3_AUXCTL_MISC_WREN;
993
994 return tg3_writephy(tp, MII_TG3_AUX_CTRL, set | reg);
995}
996
Matt Carlson1d36ba42011-04-20 07:57:42 +0000997#define TG3_PHY_AUXCTL_SMDSP_ENABLE(tp) \
998 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
999 MII_TG3_AUXCTL_ACTL_SMDSP_ENA | \
1000 MII_TG3_AUXCTL_ACTL_TX_6DB)
1001
1002#define TG3_PHY_AUXCTL_SMDSP_DISABLE(tp) \
1003 tg3_phy_auxctl_write((tp), MII_TG3_AUXCTL_SHDWSEL_AUXCTL, \
1004 MII_TG3_AUXCTL_ACTL_TX_6DB);
1005
Matt Carlson95e28692008-05-25 23:44:14 -07001006static int tg3_bmcr_reset(struct tg3 *tp)
1007{
1008 u32 phy_control;
1009 int limit, err;
1010
1011 /* OK, reset it, and poll the BMCR_RESET bit until it
1012 * clears or we time out.
1013 */
1014 phy_control = BMCR_RESET;
1015 err = tg3_writephy(tp, MII_BMCR, phy_control);
1016 if (err != 0)
1017 return -EBUSY;
1018
1019 limit = 5000;
1020 while (limit--) {
1021 err = tg3_readphy(tp, MII_BMCR, &phy_control);
1022 if (err != 0)
1023 return -EBUSY;
1024
1025 if ((phy_control & BMCR_RESET) == 0) {
1026 udelay(40);
1027 break;
1028 }
1029 udelay(10);
1030 }
Roel Kluind4675b52009-02-12 16:33:27 -08001031 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -07001032 return -EBUSY;
1033
1034 return 0;
1035}
1036
Matt Carlson158d7ab2008-05-29 01:37:54 -07001037static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
1038{
Francois Romieu3d165432009-01-19 16:56:50 -08001039 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001040 u32 val;
1041
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001042 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001043
1044 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001045 val = -EIO;
1046
1047 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001048
1049 return val;
1050}
1051
1052static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
1053{
Francois Romieu3d165432009-01-19 16:56:50 -08001054 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001055 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001056
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001057 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001058
1059 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001060 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001061
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001062 spin_unlock_bh(&tp->lock);
1063
1064 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001065}
1066
1067static int tg3_mdio_reset(struct mii_bus *bp)
1068{
1069 return 0;
1070}
1071
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001072static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -07001073{
1074 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001075 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -07001076
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001077 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001078 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001079 case PHY_ID_BCM50610:
1080 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001081 val = MAC_PHYCFG2_50610_LED_MODES;
1082 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001083 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001084 val = MAC_PHYCFG2_AC131_LED_MODES;
1085 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001086 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001087 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
1088 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001089 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001090 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
1091 break;
1092 default:
Matt Carlsona9daf362008-05-25 23:49:44 -07001093 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001094 }
1095
1096 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
1097 tw32(MAC_PHYCFG2, val);
1098
1099 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001100 val &= ~(MAC_PHYCFG1_RGMII_INT |
1101 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
1102 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001103 tw32(MAC_PHYCFG1, val);
1104
1105 return;
1106 }
1107
Joe Perches63c3a662011-04-26 08:12:10 +00001108 if (!tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001109 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
1110 MAC_PHYCFG2_FMODE_MASK_MASK |
1111 MAC_PHYCFG2_GMODE_MASK_MASK |
1112 MAC_PHYCFG2_ACT_MASK_MASK |
1113 MAC_PHYCFG2_QUAL_MASK_MASK |
1114 MAC_PHYCFG2_INBAND_ENABLE;
1115
1116 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001117
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001118 val = tr32(MAC_PHYCFG1);
1119 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
1120 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Joe Perches63c3a662011-04-26 08:12:10 +00001121 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1122 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001123 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
Joe Perches63c3a662011-04-26 08:12:10 +00001124 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001125 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1126 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001127 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1128 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1129 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001130
Matt Carlsona9daf362008-05-25 23:49:44 -07001131 val = tr32(MAC_EXT_RGMII_MODE);
1132 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1133 MAC_RGMII_MODE_RX_QUALITY |
1134 MAC_RGMII_MODE_RX_ACTIVITY |
1135 MAC_RGMII_MODE_RX_ENG_DET |
1136 MAC_RGMII_MODE_TX_ENABLE |
1137 MAC_RGMII_MODE_TX_LOWPWR |
1138 MAC_RGMII_MODE_TX_RESET);
Joe Perches63c3a662011-04-26 08:12:10 +00001139 if (!tg3_flag(tp, RGMII_INBAND_DISABLE)) {
1140 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001141 val |= MAC_RGMII_MODE_RX_INT_B |
1142 MAC_RGMII_MODE_RX_QUALITY |
1143 MAC_RGMII_MODE_RX_ACTIVITY |
1144 MAC_RGMII_MODE_RX_ENG_DET;
Joe Perches63c3a662011-04-26 08:12:10 +00001145 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001146 val |= MAC_RGMII_MODE_TX_ENABLE |
1147 MAC_RGMII_MODE_TX_LOWPWR |
1148 MAC_RGMII_MODE_TX_RESET;
1149 }
1150 tw32(MAC_EXT_RGMII_MODE, val);
1151}
1152
Matt Carlson158d7ab2008-05-29 01:37:54 -07001153static void tg3_mdio_start(struct tg3 *tp)
1154{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001155 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1156 tw32_f(MAC_MI_MODE, tp->mi_mode);
1157 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001158
Joe Perches63c3a662011-04-26 08:12:10 +00001159 if (tg3_flag(tp, MDIOBUS_INITED) &&
Matt Carlson9ea48182010-02-17 15:17:01 +00001160 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1161 tg3_mdio_config_5785(tp);
1162}
1163
1164static int tg3_mdio_init(struct tg3 *tp)
1165{
1166 int i;
1167 u32 reg;
1168 struct phy_device *phydev;
1169
Joe Perches63c3a662011-04-26 08:12:10 +00001170 if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001171 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001172
Matt Carlson9c7df912010-06-05 17:24:36 +00001173 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001174
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001175 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1176 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1177 else
1178 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1179 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001180 if (is_serdes)
1181 tp->phy_addr += 7;
1182 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001183 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001184
Matt Carlson158d7ab2008-05-29 01:37:54 -07001185 tg3_mdio_start(tp);
1186
Joe Perches63c3a662011-04-26 08:12:10 +00001187 if (!tg3_flag(tp, USE_PHYLIB) || tg3_flag(tp, MDIOBUS_INITED))
Matt Carlson158d7ab2008-05-29 01:37:54 -07001188 return 0;
1189
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001190 tp->mdio_bus = mdiobus_alloc();
1191 if (tp->mdio_bus == NULL)
1192 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001193
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001194 tp->mdio_bus->name = "tg3 mdio bus";
1195 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001196 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001197 tp->mdio_bus->priv = tp;
1198 tp->mdio_bus->parent = &tp->pdev->dev;
1199 tp->mdio_bus->read = &tg3_mdio_read;
1200 tp->mdio_bus->write = &tg3_mdio_write;
1201 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001202 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001203 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001204
1205 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001206 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001207
1208 /* The bus registration will look for all the PHYs on the mdio bus.
1209 * Unfortunately, it does not ensure the PHY is powered up before
1210 * accessing the PHY ID registers. A chip reset is the
1211 * quickest way to bring the device back to an operational state..
1212 */
1213 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1214 tg3_bmcr_reset(tp);
1215
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001216 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001217 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001218 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001219 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001220 return i;
1221 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001222
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001223 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001224
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001225 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001226 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001227 mdiobus_unregister(tp->mdio_bus);
1228 mdiobus_free(tp->mdio_bus);
1229 return -ENODEV;
1230 }
1231
1232 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001233 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001234 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001235 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001236 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001237 case PHY_ID_BCM50610:
1238 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001239 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001240 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001241 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001242 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001243 if (tg3_flag(tp, RGMII_INBAND_DISABLE))
Matt Carlsona9daf362008-05-25 23:49:44 -07001244 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001245 if (tg3_flag(tp, RGMII_EXT_IBND_RX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001246 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00001247 if (tg3_flag(tp, RGMII_EXT_IBND_TX_EN))
Matt Carlsona9daf362008-05-25 23:49:44 -07001248 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001249 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001250 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001251 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001252 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001253 case PHY_ID_RTL8201E:
1254 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001255 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e09d2009-11-02 14:31:11 +00001256 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001257 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001258 break;
1259 }
1260
Joe Perches63c3a662011-04-26 08:12:10 +00001261 tg3_flag_set(tp, MDIOBUS_INITED);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001262
1263 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1264 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001265
1266 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001267}
1268
1269static void tg3_mdio_fini(struct tg3 *tp)
1270{
Joe Perches63c3a662011-04-26 08:12:10 +00001271 if (tg3_flag(tp, MDIOBUS_INITED)) {
1272 tg3_flag_clear(tp, MDIOBUS_INITED);
Lennert Buytenhek298cf9be2008-10-08 16:29:57 -07001273 mdiobus_unregister(tp->mdio_bus);
1274 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001275 }
1276}
1277
Matt Carlson95e28692008-05-25 23:44:14 -07001278/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001279static inline void tg3_generate_fw_event(struct tg3 *tp)
1280{
1281 u32 val;
1282
1283 val = tr32(GRC_RX_CPU_EVENT);
1284 val |= GRC_RX_CPU_DRIVER_EVENT;
1285 tw32_f(GRC_RX_CPU_EVENT, val);
1286
1287 tp->last_event_jiffies = jiffies;
1288}
1289
1290#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1291
1292/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001293static void tg3_wait_for_event_ack(struct tg3 *tp)
1294{
1295 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001296 unsigned int delay_cnt;
1297 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001298
Matt Carlson4ba526c2008-08-15 14:10:04 -07001299 /* If enough time has passed, no wait is necessary. */
1300 time_remain = (long)(tp->last_event_jiffies + 1 +
1301 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1302 (long)jiffies;
1303 if (time_remain < 0)
1304 return;
1305
1306 /* Check if we can shorten the wait time. */
1307 delay_cnt = jiffies_to_usecs(time_remain);
1308 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1309 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1310 delay_cnt = (delay_cnt >> 3) + 1;
1311
1312 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001313 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1314 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001315 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001316 }
1317}
1318
1319/* tp->lock is held. */
1320static void tg3_ump_link_report(struct tg3 *tp)
1321{
1322 u32 reg;
1323 u32 val;
1324
Joe Perches63c3a662011-04-26 08:12:10 +00001325 if (!tg3_flag(tp, 5780_CLASS) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson95e28692008-05-25 23:44:14 -07001326 return;
1327
1328 tg3_wait_for_event_ack(tp);
1329
1330 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1331
1332 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1333
1334 val = 0;
1335 if (!tg3_readphy(tp, MII_BMCR, &reg))
1336 val = reg << 16;
1337 if (!tg3_readphy(tp, MII_BMSR, &reg))
1338 val |= (reg & 0xffff);
1339 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1340
1341 val = 0;
1342 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1343 val = reg << 16;
1344 if (!tg3_readphy(tp, MII_LPA, &reg))
1345 val |= (reg & 0xffff);
1346 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1347
1348 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001349 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001350 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1351 val = reg << 16;
1352 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1353 val |= (reg & 0xffff);
1354 }
1355 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1356
1357 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1358 val = reg << 16;
1359 else
1360 val = 0;
1361 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1362
Matt Carlson4ba526c2008-08-15 14:10:04 -07001363 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001364}
1365
1366static void tg3_link_report(struct tg3 *tp)
1367{
1368 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001369 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001370 tg3_ump_link_report(tp);
1371 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001372 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1373 (tp->link_config.active_speed == SPEED_1000 ?
1374 1000 :
1375 (tp->link_config.active_speed == SPEED_100 ?
1376 100 : 10)),
1377 (tp->link_config.active_duplex == DUPLEX_FULL ?
1378 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001379
Joe Perches05dbe002010-02-17 19:44:19 +00001380 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1381 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1382 "on" : "off",
1383 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1384 "on" : "off");
Matt Carlson47007832011-04-20 07:57:43 +00001385
1386 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP)
1387 netdev_info(tp->dev, "EEE is %s\n",
1388 tp->setlpicnt ? "enabled" : "disabled");
1389
Matt Carlson95e28692008-05-25 23:44:14 -07001390 tg3_ump_link_report(tp);
1391 }
1392}
1393
1394static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1395{
1396 u16 miireg;
1397
Steve Glendinninge18ce342008-12-16 02:00:00 -08001398 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001399 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001400 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001401 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001402 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001403 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1404 else
1405 miireg = 0;
1406
1407 return miireg;
1408}
1409
1410static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1411{
1412 u16 miireg;
1413
Steve Glendinninge18ce342008-12-16 02:00:00 -08001414 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001415 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001416 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001417 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001418 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001419 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1420 else
1421 miireg = 0;
1422
1423 return miireg;
1424}
1425
Matt Carlson95e28692008-05-25 23:44:14 -07001426static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1427{
1428 u8 cap = 0;
1429
1430 if (lcladv & ADVERTISE_1000XPAUSE) {
1431 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1432 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001433 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001434 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001435 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001436 } else {
1437 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001438 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001439 }
1440 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1441 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001442 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001443 }
1444
1445 return cap;
1446}
1447
Matt Carlsonf51f3562008-05-25 23:45:08 -07001448static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001449{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001450 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001451 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001452 u32 old_rx_mode = tp->rx_mode;
1453 u32 old_tx_mode = tp->tx_mode;
1454
Joe Perches63c3a662011-04-26 08:12:10 +00001455 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001456 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001457 else
1458 autoneg = tp->link_config.autoneg;
1459
Joe Perches63c3a662011-04-26 08:12:10 +00001460 if (autoneg == AUTONEG_ENABLE && tg3_flag(tp, PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001461 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001462 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001463 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001464 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001465 } else
1466 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001467
Matt Carlsonf51f3562008-05-25 23:45:08 -07001468 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001469
Steve Glendinninge18ce342008-12-16 02:00:00 -08001470 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001471 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1472 else
1473 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1474
Matt Carlsonf51f3562008-05-25 23:45:08 -07001475 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001476 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001477
Steve Glendinninge18ce342008-12-16 02:00:00 -08001478 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001479 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1480 else
1481 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1482
Matt Carlsonf51f3562008-05-25 23:45:08 -07001483 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001484 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001485}
1486
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001487static void tg3_adjust_link(struct net_device *dev)
1488{
1489 u8 oldflowctrl, linkmesg = 0;
1490 u32 mac_mode, lcl_adv, rmt_adv;
1491 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001492 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001493
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001494 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001495
1496 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1497 MAC_MODE_HALF_DUPLEX);
1498
1499 oldflowctrl = tp->link_config.active_flowctrl;
1500
1501 if (phydev->link) {
1502 lcl_adv = 0;
1503 rmt_adv = 0;
1504
1505 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1506 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001507 else if (phydev->speed == SPEED_1000 ||
1508 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001509 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001510 else
1511 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001512
1513 if (phydev->duplex == DUPLEX_HALF)
1514 mac_mode |= MAC_MODE_HALF_DUPLEX;
1515 else {
1516 lcl_adv = tg3_advert_flowctrl_1000T(
1517 tp->link_config.flowctrl);
1518
1519 if (phydev->pause)
1520 rmt_adv = LPA_PAUSE_CAP;
1521 if (phydev->asym_pause)
1522 rmt_adv |= LPA_PAUSE_ASYM;
1523 }
1524
1525 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1526 } else
1527 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1528
1529 if (mac_mode != tp->mac_mode) {
1530 tp->mac_mode = mac_mode;
1531 tw32_f(MAC_MODE, tp->mac_mode);
1532 udelay(40);
1533 }
1534
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001535 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1536 if (phydev->speed == SPEED_10)
1537 tw32(MAC_MI_STAT,
1538 MAC_MI_STAT_10MBPS_MODE |
1539 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1540 else
1541 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1542 }
1543
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001544 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1545 tw32(MAC_TX_LENGTHS,
1546 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1547 (6 << TX_LENGTHS_IPG_SHIFT) |
1548 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1549 else
1550 tw32(MAC_TX_LENGTHS,
1551 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1552 (6 << TX_LENGTHS_IPG_SHIFT) |
1553 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1554
1555 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1556 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1557 phydev->speed != tp->link_config.active_speed ||
1558 phydev->duplex != tp->link_config.active_duplex ||
1559 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001560 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001561
1562 tp->link_config.active_speed = phydev->speed;
1563 tp->link_config.active_duplex = phydev->duplex;
1564
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001565 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001566
1567 if (linkmesg)
1568 tg3_link_report(tp);
1569}
1570
1571static int tg3_phy_init(struct tg3 *tp)
1572{
1573 struct phy_device *phydev;
1574
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001575 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001576 return 0;
1577
1578 /* Bring the PHY back to a known state. */
1579 tg3_bmcr_reset(tp);
1580
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001581 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001582
1583 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001584 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001585 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001586 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001587 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001588 return PTR_ERR(phydev);
1589 }
1590
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001591 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001592 switch (phydev->interface) {
1593 case PHY_INTERFACE_MODE_GMII:
1594 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001595 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001596 phydev->supported &= (PHY_GBIT_FEATURES |
1597 SUPPORTED_Pause |
1598 SUPPORTED_Asym_Pause);
1599 break;
1600 }
1601 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001602 case PHY_INTERFACE_MODE_MII:
1603 phydev->supported &= (PHY_BASIC_FEATURES |
1604 SUPPORTED_Pause |
1605 SUPPORTED_Asym_Pause);
1606 break;
1607 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001608 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001609 return -EINVAL;
1610 }
1611
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001612 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001613
1614 phydev->advertising = phydev->supported;
1615
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001616 return 0;
1617}
1618
1619static void tg3_phy_start(struct tg3 *tp)
1620{
1621 struct phy_device *phydev;
1622
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001623 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001624 return;
1625
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001626 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001627
Matt Carlson800960682010-08-02 11:26:06 +00001628 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1629 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001630 phydev->speed = tp->link_config.orig_speed;
1631 phydev->duplex = tp->link_config.orig_duplex;
1632 phydev->autoneg = tp->link_config.orig_autoneg;
1633 phydev->advertising = tp->link_config.orig_advertising;
1634 }
1635
1636 phy_start(phydev);
1637
1638 phy_start_aneg(phydev);
1639}
1640
1641static void tg3_phy_stop(struct tg3 *tp)
1642{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001643 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001644 return;
1645
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001646 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001647}
1648
1649static void tg3_phy_fini(struct tg3 *tp)
1650{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001651 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001652 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001653 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001654 }
1655}
1656
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001657static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1658{
1659 u32 phytest;
1660
1661 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1662 u32 phy;
1663
1664 tg3_writephy(tp, MII_TG3_FET_TEST,
1665 phytest | MII_TG3_FET_SHADOW_EN);
1666 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1667 if (enable)
1668 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1669 else
1670 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1671 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1672 }
1673 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1674 }
1675}
1676
Matt Carlson6833c042008-11-21 17:18:59 -08001677static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1678{
1679 u32 reg;
1680
Joe Perches63c3a662011-04-26 08:12:10 +00001681 if (!tg3_flag(tp, 5705_PLUS) ||
1682 (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001683 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001684 return;
1685
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001686 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001687 tg3_phy_fet_toggle_apd(tp, enable);
1688 return;
1689 }
1690
Matt Carlson6833c042008-11-21 17:18:59 -08001691 reg = MII_TG3_MISC_SHDW_WREN |
1692 MII_TG3_MISC_SHDW_SCR5_SEL |
1693 MII_TG3_MISC_SHDW_SCR5_LPED |
1694 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1695 MII_TG3_MISC_SHDW_SCR5_SDTL |
1696 MII_TG3_MISC_SHDW_SCR5_C125OE;
1697 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1698 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1699
1700 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1701
1702
1703 reg = MII_TG3_MISC_SHDW_WREN |
1704 MII_TG3_MISC_SHDW_APD_SEL |
1705 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1706 if (enable)
1707 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1708
1709 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1710}
1711
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001712static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1713{
1714 u32 phy;
1715
Joe Perches63c3a662011-04-26 08:12:10 +00001716 if (!tg3_flag(tp, 5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001717 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001718 return;
1719
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001720 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001721 u32 ephy;
1722
Matt Carlson535ef6e2009-08-25 10:09:36 +00001723 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1724 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1725
1726 tg3_writephy(tp, MII_TG3_FET_TEST,
1727 ephy | MII_TG3_FET_SHADOW_EN);
1728 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001729 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001730 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001731 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001732 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1733 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001734 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001735 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001736 }
1737 } else {
Matt Carlson15ee95c2011-04-20 07:57:40 +00001738 int ret;
1739
1740 ret = tg3_phy_auxctl_read(tp,
1741 MII_TG3_AUXCTL_SHDWSEL_MISC, &phy);
1742 if (!ret) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001743 if (enable)
1744 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1745 else
1746 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001747 tg3_phy_auxctl_write(tp,
1748 MII_TG3_AUXCTL_SHDWSEL_MISC, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001749 }
1750 }
1751}
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753static void tg3_phy_set_wirespeed(struct tg3 *tp)
1754{
Matt Carlson15ee95c2011-04-20 07:57:40 +00001755 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 u32 val;
1757
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001758 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return;
1760
Matt Carlson15ee95c2011-04-20 07:57:40 +00001761 ret = tg3_phy_auxctl_read(tp, MII_TG3_AUXCTL_SHDWSEL_MISC, &val);
1762 if (!ret)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00001763 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_MISC,
1764 val | MII_TG3_AUXCTL_MISC_WIRESPD_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001767static void tg3_phy_apply_otp(struct tg3 *tp)
1768{
1769 u32 otp, phy;
1770
1771 if (!tp->phy_otp)
1772 return;
1773
1774 otp = tp->phy_otp;
1775
Matt Carlson1d36ba42011-04-20 07:57:42 +00001776 if (TG3_PHY_AUXCTL_SMDSP_ENABLE(tp))
1777 return;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001778
1779 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1780 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1781 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1782
1783 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1784 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1785 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1786
1787 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1788 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1789 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1790
1791 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1792 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1793
1794 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1795 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1796
1797 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1798 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1799 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1800
Matt Carlson1d36ba42011-04-20 07:57:42 +00001801 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001802}
1803
Matt Carlson52b02d02010-10-14 10:37:41 +00001804static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1805{
1806 u32 val;
1807
1808 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1809 return;
1810
1811 tp->setlpicnt = 0;
1812
1813 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1814 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001815 tp->link_config.active_duplex == DUPLEX_FULL &&
1816 (tp->link_config.active_speed == SPEED_100 ||
1817 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001818 u32 eeectl;
1819
1820 if (tp->link_config.active_speed == SPEED_1000)
1821 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1822 else
1823 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1824
1825 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1826
Matt Carlson3110f5f52010-12-06 08:28:50 +00001827 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1828 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001829
Matt Carlsonb0c59432011-05-19 12:12:48 +00001830 if (val == TG3_CL45_D7_EEERES_STAT_LP_1000T ||
1831 val == TG3_CL45_D7_EEERES_STAT_LP_100TX)
Matt Carlson52b02d02010-10-14 10:37:41 +00001832 tp->setlpicnt = 2;
1833 }
1834
1835 if (!tp->setlpicnt) {
1836 val = tr32(TG3_CPMU_EEE_MODE);
1837 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1838 }
1839}
1840
Matt Carlsonb0c59432011-05-19 12:12:48 +00001841static void tg3_phy_eee_enable(struct tg3 *tp)
1842{
1843 u32 val;
1844
1845 if (tp->link_config.active_speed == SPEED_1000 &&
1846 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
1847 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
1848 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) &&
1849 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
1850 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0003);
1851 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
1852 }
1853
1854 val = tr32(TG3_CPMU_EEE_MODE);
1855 tw32(TG3_CPMU_EEE_MODE, val | TG3_CPMU_EEEMD_LPI_ENABLE);
1856}
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858static int tg3_wait_macro_done(struct tg3 *tp)
1859{
1860 int limit = 100;
1861
1862 while (limit--) {
1863 u32 tmp32;
1864
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001865 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if ((tmp32 & 0x1000) == 0)
1867 break;
1868 }
1869 }
Roel Kluind4675b52009-02-12 16:33:27 -08001870 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return -EBUSY;
1872
1873 return 0;
1874}
1875
1876static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1877{
1878 static const u32 test_pat[4][6] = {
1879 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1880 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1881 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1882 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1883 };
1884 int chan;
1885
1886 for (chan = 0; chan < 4; chan++) {
1887 int i;
1888
1889 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1890 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001891 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 for (i = 0; i < 6; i++)
1894 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1895 test_pat[chan][i]);
1896
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001897 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (tg3_wait_macro_done(tp)) {
1899 *resetp = 1;
1900 return -EBUSY;
1901 }
1902
1903 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1904 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001905 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (tg3_wait_macro_done(tp)) {
1907 *resetp = 1;
1908 return -EBUSY;
1909 }
1910
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001911 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (tg3_wait_macro_done(tp)) {
1913 *resetp = 1;
1914 return -EBUSY;
1915 }
1916
1917 for (i = 0; i < 6; i += 2) {
1918 u32 low, high;
1919
1920 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1921 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1922 tg3_wait_macro_done(tp)) {
1923 *resetp = 1;
1924 return -EBUSY;
1925 }
1926 low &= 0x7fff;
1927 high &= 0x000f;
1928 if (low != test_pat[chan][i] ||
1929 high != test_pat[chan][i+1]) {
1930 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1931 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1932 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1933
1934 return -EBUSY;
1935 }
1936 }
1937 }
1938
1939 return 0;
1940}
1941
1942static int tg3_phy_reset_chanpat(struct tg3 *tp)
1943{
1944 int chan;
1945
1946 for (chan = 0; chan < 4; chan++) {
1947 int i;
1948
1949 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1950 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001951 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 for (i = 0; i < 6; i++)
1953 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001954 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (tg3_wait_macro_done(tp))
1956 return -EBUSY;
1957 }
1958
1959 return 0;
1960}
1961
1962static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1963{
1964 u32 reg32, phy9_orig;
1965 int retries, do_phy_reset, err;
1966
1967 retries = 10;
1968 do_phy_reset = 1;
1969 do {
1970 if (do_phy_reset) {
1971 err = tg3_bmcr_reset(tp);
1972 if (err)
1973 return err;
1974 do_phy_reset = 0;
1975 }
1976
1977 /* Disable transmitter and interrupt. */
1978 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1979 continue;
1980
1981 reg32 |= 0x3000;
1982 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1983
1984 /* Set full-duplex, 1000 mbps. */
1985 tg3_writephy(tp, MII_BMCR,
Matt Carlson221c5632011-06-13 13:39:01 +00001986 BMCR_FULLDPLX | BMCR_SPEED1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 /* Set to master mode. */
Matt Carlson221c5632011-06-13 13:39:01 +00001989 if (tg3_readphy(tp, MII_CTRL1000, &phy9_orig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 continue;
1991
Matt Carlson221c5632011-06-13 13:39:01 +00001992 tg3_writephy(tp, MII_CTRL1000,
1993 CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Matt Carlson1d36ba42011-04-20 07:57:42 +00001995 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
1996 if (err)
1997 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002000 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
2003 if (!err)
2004 break;
2005 } while (--retries);
2006
2007 err = tg3_phy_reset_chanpat(tp);
2008 if (err)
2009 return err;
2010
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002011 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002014 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Matt Carlson1d36ba42011-04-20 07:57:42 +00002016 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Matt Carlson221c5632011-06-13 13:39:01 +00002018 tg3_writephy(tp, MII_CTRL1000, phy9_orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
2021 reg32 &= ~0x3000;
2022 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
2023 } else if (!err)
2024 err = -EBUSY;
2025
2026 return err;
2027}
2028
2029/* This will reset the tigon3 PHY if there is no valid
2030 * link unless the FORCE argument is non-zero.
2031 */
2032static int tg3_phy_reset(struct tg3 *tp)
2033{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002034 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 int err;
2036
Michael Chan60189dd2006-12-17 17:08:07 -08002037 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002038 val = tr32(GRC_MISC_CFG);
2039 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
2040 udelay(40);
2041 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002042 err = tg3_readphy(tp, MII_BMSR, &val);
2043 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (err != 0)
2045 return -EBUSY;
2046
Michael Chanc8e1e822006-04-29 18:55:17 -07002047 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2048 netif_carrier_off(tp->dev);
2049 tg3_link_report(tp);
2050 }
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2053 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2054 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2055 err = tg3_phy_reset_5703_4_5(tp);
2056 if (err)
2057 return err;
2058 goto out;
2059 }
2060
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002061 cpmuctrl = 0;
2062 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2063 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2064 cpmuctrl = tr32(TG3_CPMU_CTRL);
2065 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2066 tw32(TG3_CPMU_CTRL,
2067 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2068 }
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 err = tg3_bmcr_reset(tp);
2071 if (err)
2072 return err;
2073
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002074 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002075 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2076 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002077
2078 tw32(TG3_CPMU_CTRL, cpmuctrl);
2079 }
2080
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002081 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2082 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002083 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2084 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2085 CPMU_LSPD_1000MB_MACCLK_12_5) {
2086 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2087 udelay(40);
2088 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2089 }
2090 }
2091
Joe Perches63c3a662011-04-26 08:12:10 +00002092 if (tg3_flag(tp, 5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002093 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002094 return 0;
2095
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002096 tg3_phy_apply_otp(tp);
2097
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002098 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002099 tg3_phy_toggle_apd(tp, true);
2100 else
2101 tg3_phy_toggle_apd(tp, false);
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103out:
Matt Carlson1d36ba42011-04-20 07:57:42 +00002104 if ((tp->phy_flags & TG3_PHYFLG_ADC_BUG) &&
2105 !TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002106 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2107 tg3_phydsp_write(tp, 0x000a, 0x0323);
Matt Carlson1d36ba42011-04-20 07:57:42 +00002108 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002110
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002111 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002112 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2113 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002115
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002116 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002117 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2118 tg3_phydsp_write(tp, 0x000a, 0x310b);
2119 tg3_phydsp_write(tp, 0x201f, 0x9506);
2120 tg3_phydsp_write(tp, 0x401f, 0x14e2);
2121 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2122 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002123 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Matt Carlson1d36ba42011-04-20 07:57:42 +00002124 if (!TG3_PHY_AUXCTL_SMDSP_ENABLE(tp)) {
2125 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
2126 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
2127 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2128 tg3_writephy(tp, MII_TG3_TEST1,
2129 MII_TG3_TEST1_TRIM_EN | 0x4);
2130 } else
2131 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
2132
2133 TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
2134 }
Michael Chanc424cb22006-04-29 18:56:34 -07002135 }
Matt Carlson1d36ba42011-04-20 07:57:42 +00002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 /* Set Extended packet length bit (bit 14) on all chips that */
2138 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002139 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /* Cannot do read-modify-write on 5401 */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002141 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Joe Perches63c3a662011-04-26 08:12:10 +00002142 } else if (tg3_flag(tp, JUMBO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 /* Set bit 14 with read-modify-write to preserve other bits */
Matt Carlson15ee95c2011-04-20 07:57:40 +00002144 err = tg3_phy_auxctl_read(tp,
2145 MII_TG3_AUXCTL_SHDWSEL_AUXCTL, &val);
2146 if (!err)
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002147 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL,
2148 val | MII_TG3_AUXCTL_ACTL_EXTPKTLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
2150
2151 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2152 * jumbo frames transmission.
2153 */
Joe Perches63c3a662011-04-26 08:12:10 +00002154 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002155 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002156 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002157 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
2159
Michael Chan715116a2006-09-27 16:09:25 -07002160 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002161 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002162 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002163 }
2164
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002165 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 tg3_phy_set_wirespeed(tp);
2167 return 0;
2168}
2169
Matt Carlson520b2752011-06-13 13:39:02 +00002170static inline int tg3_pwrsrc_switch_to_vmain(struct tg3 *tp)
2171{
2172 if (!tg3_flag(tp, IS_NIC))
2173 return 0;
2174
2175 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl,
2176 TG3_GRC_LCLCTL_PWRSW_DELAY);
2177
2178 return 0;
2179}
2180
2181static void tg3_pwrsrc_die_with_vmain(struct tg3 *tp)
2182{
2183 u32 grc_local_ctrl;
2184
2185 if (!tg3_flag(tp, IS_NIC) ||
2186 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2187 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)
2188 return;
2189
2190 grc_local_ctrl = tp->grc_local_ctrl | GRC_LCLCTRL_GPIO_OE1;
2191
2192 tw32_wait_f(GRC_LOCAL_CTRL,
2193 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2194 TG3_GRC_LCLCTL_PWRSW_DELAY);
2195
2196 tw32_wait_f(GRC_LOCAL_CTRL,
2197 grc_local_ctrl,
2198 TG3_GRC_LCLCTL_PWRSW_DELAY);
2199
2200 tw32_wait_f(GRC_LOCAL_CTRL,
2201 grc_local_ctrl | GRC_LCLCTRL_GPIO_OUTPUT1,
2202 TG3_GRC_LCLCTL_PWRSW_DELAY);
2203}
2204
2205static void tg3_pwrsrc_switch_to_vaux(struct tg3 *tp)
2206{
2207 if (!tg3_flag(tp, IS_NIC))
2208 return;
2209
2210 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2211 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2212 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2213 (GRC_LCLCTRL_GPIO_OE0 |
2214 GRC_LCLCTRL_GPIO_OE1 |
2215 GRC_LCLCTRL_GPIO_OE2 |
2216 GRC_LCLCTRL_GPIO_OUTPUT0 |
2217 GRC_LCLCTRL_GPIO_OUTPUT1),
2218 TG3_GRC_LCLCTL_PWRSW_DELAY);
2219 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2220 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
2221 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2222 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2223 GRC_LCLCTRL_GPIO_OE1 |
2224 GRC_LCLCTRL_GPIO_OE2 |
2225 GRC_LCLCTRL_GPIO_OUTPUT0 |
2226 GRC_LCLCTRL_GPIO_OUTPUT1 |
2227 tp->grc_local_ctrl;
2228 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2229 TG3_GRC_LCLCTL_PWRSW_DELAY);
2230
2231 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2232 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2233 TG3_GRC_LCLCTL_PWRSW_DELAY);
2234
2235 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2236 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl,
2237 TG3_GRC_LCLCTL_PWRSW_DELAY);
2238 } else {
2239 u32 no_gpio2;
2240 u32 grc_local_ctrl = 0;
2241
2242 /* Workaround to prevent overdrawing Amps. */
2243 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
2244 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
2245 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2246 grc_local_ctrl,
2247 TG3_GRC_LCLCTL_PWRSW_DELAY);
2248 }
2249
2250 /* On 5753 and variants, GPIO2 cannot be used. */
2251 no_gpio2 = tp->nic_sram_data_cfg &
2252 NIC_SRAM_DATA_CFG_NO_GPIO2;
2253
2254 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
2255 GRC_LCLCTRL_GPIO_OE1 |
2256 GRC_LCLCTRL_GPIO_OE2 |
2257 GRC_LCLCTRL_GPIO_OUTPUT1 |
2258 GRC_LCLCTRL_GPIO_OUTPUT2;
2259 if (no_gpio2) {
2260 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2261 GRC_LCLCTRL_GPIO_OUTPUT2);
2262 }
2263 tw32_wait_f(GRC_LOCAL_CTRL,
2264 tp->grc_local_ctrl | grc_local_ctrl,
2265 TG3_GRC_LCLCTL_PWRSW_DELAY);
2266
2267 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2268
2269 tw32_wait_f(GRC_LOCAL_CTRL,
2270 tp->grc_local_ctrl | grc_local_ctrl,
2271 TG3_GRC_LCLCTL_PWRSW_DELAY);
2272
2273 if (!no_gpio2) {
2274 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
2275 tw32_wait_f(GRC_LOCAL_CTRL,
2276 tp->grc_local_ctrl | grc_local_ctrl,
2277 TG3_GRC_LCLCTL_PWRSW_DELAY);
2278 }
2279 }
2280}
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282static void tg3_frob_aux_power(struct tg3 *tp)
2283{
Matt Carlson683644b2011-03-09 16:58:23 +00002284 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Matt Carlson334355a2010-01-20 16:58:10 +00002286 /* The GPIOs do something completely different on 57765. */
Joe Perches63c3a662011-04-26 08:12:10 +00002287 if (!tg3_flag(tp, IS_NIC) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002288 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002289 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return;
2291
Matt Carlson683644b2011-03-09 16:58:23 +00002292 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2293 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +00002294 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2295 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
Matt Carlson683644b2011-03-09 16:58:23 +00002296 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002297 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002299 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002300
Michael Chanbc1c7562006-03-20 17:48:03 -08002301 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002302 if (dev_peer) {
2303 struct tg3 *tp_peer = netdev_priv(dev_peer);
2304
Joe Perches63c3a662011-04-26 08:12:10 +00002305 if (tg3_flag(tp_peer, INIT_COMPLETE))
Matt Carlson683644b2011-03-09 16:58:23 +00002306 return;
2307
Joe Perches63c3a662011-04-26 08:12:10 +00002308 if (tg3_flag(tp_peer, WOL_ENABLE) ||
2309 tg3_flag(tp_peer, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002310 need_vaux = true;
2311 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Joe Perches63c3a662011-04-26 08:12:10 +00002314 if (tg3_flag(tp, WOL_ENABLE) || tg3_flag(tp, ENABLE_ASF))
Matt Carlson683644b2011-03-09 16:58:23 +00002315 need_vaux = true;
2316
Matt Carlson520b2752011-06-13 13:39:02 +00002317 if (need_vaux)
2318 tg3_pwrsrc_switch_to_vaux(tp);
2319 else
2320 tg3_pwrsrc_die_with_vmain(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002323static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2324{
2325 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2326 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002327 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002328 if (speed != SPEED_10)
2329 return 1;
2330 } else if (speed == SPEED_10)
2331 return 1;
2332
2333 return 0;
2334}
2335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336static int tg3_setup_phy(struct tg3 *, int);
2337
2338#define RESET_KIND_SHUTDOWN 0
2339#define RESET_KIND_INIT 1
2340#define RESET_KIND_SUSPEND 2
2341
2342static void tg3_write_sig_post_reset(struct tg3 *, int);
2343static int tg3_halt_cpu(struct tg3 *, u32);
2344
Matt Carlson0a459aa2008-11-03 16:54:15 -08002345static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002346{
Matt Carlsonce057f02007-11-12 21:08:03 -08002347 u32 val;
2348
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002349 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002350 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2351 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2352 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2353
2354 sg_dig_ctrl |=
2355 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2356 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2357 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2358 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002359 return;
Michael Chan51297242007-02-13 12:17:57 -08002360 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002361
Michael Chan60189dd2006-12-17 17:08:07 -08002362 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002363 tg3_bmcr_reset(tp);
2364 val = tr32(GRC_MISC_CFG);
2365 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2366 udelay(40);
2367 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002368 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002369 u32 phytest;
2370 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2371 u32 phy;
2372
2373 tg3_writephy(tp, MII_ADVERTISE, 0);
2374 tg3_writephy(tp, MII_BMCR,
2375 BMCR_ANENABLE | BMCR_ANRESTART);
2376
2377 tg3_writephy(tp, MII_TG3_FET_TEST,
2378 phytest | MII_TG3_FET_SHADOW_EN);
2379 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2380 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2381 tg3_writephy(tp,
2382 MII_TG3_FET_SHDW_AUXMODE4,
2383 phy);
2384 }
2385 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2386 }
2387 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002388 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002389 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2390 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002391
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002392 val = MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2393 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2394 MII_TG3_AUXCTL_PCTL_VREG_11V;
2395 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, val);
Michael Chan715116a2006-09-27 16:09:25 -07002396 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002397
Michael Chan15c3b692006-03-22 01:06:52 -08002398 /* The PHY should not be powered down on some chips because
2399 * of bugs.
2400 */
2401 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2402 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2403 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002404 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002405 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002406
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002407 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2408 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002409 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2410 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2411 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2412 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2413 }
2414
Michael Chan15c3b692006-03-22 01:06:52 -08002415 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2416}
2417
Matt Carlson3f007892008-11-03 16:51:36 -08002418/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002419static int tg3_nvram_lock(struct tg3 *tp)
2420{
Joe Perches63c3a662011-04-26 08:12:10 +00002421 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002422 int i;
2423
2424 if (tp->nvram_lock_cnt == 0) {
2425 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2426 for (i = 0; i < 8000; i++) {
2427 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2428 break;
2429 udelay(20);
2430 }
2431 if (i == 8000) {
2432 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2433 return -ENODEV;
2434 }
2435 }
2436 tp->nvram_lock_cnt++;
2437 }
2438 return 0;
2439}
2440
2441/* tp->lock is held. */
2442static void tg3_nvram_unlock(struct tg3 *tp)
2443{
Joe Perches63c3a662011-04-26 08:12:10 +00002444 if (tg3_flag(tp, NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002445 if (tp->nvram_lock_cnt > 0)
2446 tp->nvram_lock_cnt--;
2447 if (tp->nvram_lock_cnt == 0)
2448 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2449 }
2450}
2451
2452/* tp->lock is held. */
2453static void tg3_enable_nvram_access(struct tg3 *tp)
2454{
Joe Perches63c3a662011-04-26 08:12:10 +00002455 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002456 u32 nvaccess = tr32(NVRAM_ACCESS);
2457
2458 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2459 }
2460}
2461
2462/* tp->lock is held. */
2463static void tg3_disable_nvram_access(struct tg3 *tp)
2464{
Joe Perches63c3a662011-04-26 08:12:10 +00002465 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002466 u32 nvaccess = tr32(NVRAM_ACCESS);
2467
2468 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2469 }
2470}
2471
2472static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2473 u32 offset, u32 *val)
2474{
2475 u32 tmp;
2476 int i;
2477
2478 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2479 return -EINVAL;
2480
2481 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2482 EEPROM_ADDR_DEVID_MASK |
2483 EEPROM_ADDR_READ);
2484 tw32(GRC_EEPROM_ADDR,
2485 tmp |
2486 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2487 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2488 EEPROM_ADDR_ADDR_MASK) |
2489 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2490
2491 for (i = 0; i < 1000; i++) {
2492 tmp = tr32(GRC_EEPROM_ADDR);
2493
2494 if (tmp & EEPROM_ADDR_COMPLETE)
2495 break;
2496 msleep(1);
2497 }
2498 if (!(tmp & EEPROM_ADDR_COMPLETE))
2499 return -EBUSY;
2500
Matt Carlson62cedd12009-04-20 14:52:29 -07002501 tmp = tr32(GRC_EEPROM_DATA);
2502
2503 /*
2504 * The data will always be opposite the native endian
2505 * format. Perform a blind byteswap to compensate.
2506 */
2507 *val = swab32(tmp);
2508
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002509 return 0;
2510}
2511
2512#define NVRAM_CMD_TIMEOUT 10000
2513
2514static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2515{
2516 int i;
2517
2518 tw32(NVRAM_CMD, nvram_cmd);
2519 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2520 udelay(10);
2521 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2522 udelay(10);
2523 break;
2524 }
2525 }
2526
2527 if (i == NVRAM_CMD_TIMEOUT)
2528 return -EBUSY;
2529
2530 return 0;
2531}
2532
2533static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2534{
Joe Perches63c3a662011-04-26 08:12:10 +00002535 if (tg3_flag(tp, NVRAM) &&
2536 tg3_flag(tp, NVRAM_BUFFERED) &&
2537 tg3_flag(tp, FLASH) &&
2538 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002539 (tp->nvram_jedecnum == JEDEC_ATMEL))
2540
2541 addr = ((addr / tp->nvram_pagesize) <<
2542 ATMEL_AT45DB0X1B_PAGE_POS) +
2543 (addr % tp->nvram_pagesize);
2544
2545 return addr;
2546}
2547
2548static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2549{
Joe Perches63c3a662011-04-26 08:12:10 +00002550 if (tg3_flag(tp, NVRAM) &&
2551 tg3_flag(tp, NVRAM_BUFFERED) &&
2552 tg3_flag(tp, FLASH) &&
2553 !tg3_flag(tp, NO_NVRAM_ADDR_TRANS) &&
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002554 (tp->nvram_jedecnum == JEDEC_ATMEL))
2555
2556 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2557 tp->nvram_pagesize) +
2558 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2559
2560 return addr;
2561}
2562
Matt Carlsone4f34112009-02-25 14:25:00 +00002563/* NOTE: Data read in from NVRAM is byteswapped according to
2564 * the byteswapping settings for all other register accesses.
2565 * tg3 devices are BE devices, so on a BE machine, the data
2566 * returned will be exactly as it is seen in NVRAM. On a LE
2567 * machine, the 32-bit value will be byteswapped.
2568 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002569static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2570{
2571 int ret;
2572
Joe Perches63c3a662011-04-26 08:12:10 +00002573 if (!tg3_flag(tp, NVRAM))
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002574 return tg3_nvram_read_using_eeprom(tp, offset, val);
2575
2576 offset = tg3_nvram_phys_addr(tp, offset);
2577
2578 if (offset > NVRAM_ADDR_MSK)
2579 return -EINVAL;
2580
2581 ret = tg3_nvram_lock(tp);
2582 if (ret)
2583 return ret;
2584
2585 tg3_enable_nvram_access(tp);
2586
2587 tw32(NVRAM_ADDR, offset);
2588 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2589 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2590
2591 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002592 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002593
2594 tg3_disable_nvram_access(tp);
2595
2596 tg3_nvram_unlock(tp);
2597
2598 return ret;
2599}
2600
Matt Carlsona9dc5292009-02-25 14:25:30 +00002601/* Ensures NVRAM data is in bytestream format. */
2602static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002603{
2604 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002605 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002606 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002607 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002608 return res;
2609}
2610
2611/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002612static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2613{
2614 u32 addr_high, addr_low;
2615 int i;
2616
2617 addr_high = ((tp->dev->dev_addr[0] << 8) |
2618 tp->dev->dev_addr[1]);
2619 addr_low = ((tp->dev->dev_addr[2] << 24) |
2620 (tp->dev->dev_addr[3] << 16) |
2621 (tp->dev->dev_addr[4] << 8) |
2622 (tp->dev->dev_addr[5] << 0));
2623 for (i = 0; i < 4; i++) {
2624 if (i == 1 && skip_mac_1)
2625 continue;
2626 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2627 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2628 }
2629
2630 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2631 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2632 for (i = 0; i < 12; i++) {
2633 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2634 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2635 }
2636 }
2637
2638 addr_high = (tp->dev->dev_addr[0] +
2639 tp->dev->dev_addr[1] +
2640 tp->dev->dev_addr[2] +
2641 tp->dev->dev_addr[3] +
2642 tp->dev->dev_addr[4] +
2643 tp->dev->dev_addr[5]) &
2644 TX_BACKOFF_SEED_MASK;
2645 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2646}
2647
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002648static void tg3_enable_register_access(struct tg3 *tp)
2649{
2650 /*
2651 * Make sure register accesses (indirect or otherwise) will function
2652 * correctly.
2653 */
2654 pci_write_config_dword(tp->pdev,
2655 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2656}
2657
2658static int tg3_power_up(struct tg3 *tp)
2659{
2660 tg3_enable_register_access(tp);
2661
2662 pci_set_power_state(tp->pdev, PCI_D0);
2663
2664 /* Switch out of Vaux if it is a NIC */
Matt Carlson520b2752011-06-13 13:39:02 +00002665 tg3_pwrsrc_switch_to_vmain(tp);
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002666
2667 return 0;
2668}
2669
2670static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
2672 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002673 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002675 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002676
2677 /* Restore the CLKREQ setting. */
Joe Perches63c3a662011-04-26 08:12:10 +00002678 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002679 u16 lnkctl;
2680
2681 pci_read_config_word(tp->pdev,
2682 tp->pcie_cap + PCI_EXP_LNKCTL,
2683 &lnkctl);
2684 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2685 pci_write_config_word(tp->pdev,
2686 tp->pcie_cap + PCI_EXP_LNKCTL,
2687 lnkctl);
2688 }
2689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2691 tw32(TG3PCI_MISC_HOST_CTRL,
2692 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2693
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002694 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00002695 tg3_flag(tp, WOL_ENABLE);
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002696
Joe Perches63c3a662011-04-26 08:12:10 +00002697 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002698 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002699 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson800960682010-08-02 11:26:06 +00002700 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002701 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002702 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002703
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002704 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002705
Matt Carlson800960682010-08-02 11:26:06 +00002706 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002707
2708 tp->link_config.orig_speed = phydev->speed;
2709 tp->link_config.orig_duplex = phydev->duplex;
2710 tp->link_config.orig_autoneg = phydev->autoneg;
2711 tp->link_config.orig_advertising = phydev->advertising;
2712
2713 advertising = ADVERTISED_TP |
2714 ADVERTISED_Pause |
2715 ADVERTISED_Autoneg |
2716 ADVERTISED_10baseT_Half;
2717
Joe Perches63c3a662011-04-26 08:12:10 +00002718 if (tg3_flag(tp, ENABLE_ASF) || device_should_wake) {
2719 if (tg3_flag(tp, WOL_SPEED_100MB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002720 advertising |=
2721 ADVERTISED_100baseT_Half |
2722 ADVERTISED_100baseT_Full |
2723 ADVERTISED_10baseT_Full;
2724 else
2725 advertising |= ADVERTISED_10baseT_Full;
2726 }
2727
2728 phydev->advertising = advertising;
2729
2730 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002731
2732 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002733 if (phyid != PHY_ID_BCMAC131) {
2734 phyid &= PHY_BCM_OUI_MASK;
2735 if (phyid == PHY_BCM_OUI_1 ||
2736 phyid == PHY_BCM_OUI_2 ||
2737 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002738 do_low_power = true;
2739 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002740 }
Matt Carlsondd477002008-05-25 23:45:58 -07002741 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002742 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002743
Matt Carlson800960682010-08-02 11:26:06 +00002744 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2745 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002746 tp->link_config.orig_speed = tp->link_config.speed;
2747 tp->link_config.orig_duplex = tp->link_config.duplex;
2748 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002751 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002752 tp->link_config.speed = SPEED_10;
2753 tp->link_config.duplex = DUPLEX_HALF;
2754 tp->link_config.autoneg = AUTONEG_ENABLE;
2755 tg3_setup_phy(tp, 0);
2756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 }
2758
Michael Chanb5d37722006-09-27 16:06:21 -07002759 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2760 u32 val;
2761
2762 val = tr32(GRC_VCPU_EXT_CTRL);
2763 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
Joe Perches63c3a662011-04-26 08:12:10 +00002764 } else if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002765 int i;
2766 u32 val;
2767
2768 for (i = 0; i < 200; i++) {
2769 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2770 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2771 break;
2772 msleep(1);
2773 }
2774 }
Joe Perches63c3a662011-04-26 08:12:10 +00002775 if (tg3_flag(tp, WOL_CAP))
Gary Zambranoa85feb82007-05-05 11:52:19 -07002776 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2777 WOL_DRV_STATE_SHUTDOWN |
2778 WOL_DRV_WOL |
2779 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002780
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002781 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 u32 mac_mode;
2783
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002784 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00002785 if (do_low_power &&
2786 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
2787 tg3_phy_auxctl_write(tp,
2788 MII_TG3_AUXCTL_SHDWSEL_PWRCTL,
2789 MII_TG3_AUXCTL_PCTL_WOL_EN |
2790 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2791 MII_TG3_AUXCTL_PCTL_CL_AB_TXDAC);
Matt Carlsondd477002008-05-25 23:45:58 -07002792 udelay(40);
2793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002795 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002796 mac_mode = MAC_MODE_PORT_MODE_GMII;
2797 else
2798 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002800 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2801 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2802 ASIC_REV_5700) {
Joe Perches63c3a662011-04-26 08:12:10 +00002803 u32 speed = tg3_flag(tp, WOL_SPEED_100MB) ?
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002804 SPEED_100 : SPEED_10;
2805 if (tg3_5700_link_polarity(tp, speed))
2806 mac_mode |= MAC_MODE_LINK_POLARITY;
2807 else
2808 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 } else {
2811 mac_mode = MAC_MODE_PORT_MODE_TBI;
2812 }
2813
Joe Perches63c3a662011-04-26 08:12:10 +00002814 if (!tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 tw32(MAC_LED_CTRL, tp->led_ctrl);
2816
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002817 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00002818 if ((tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS)) &&
2819 (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)))
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002820 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Joe Perches63c3a662011-04-26 08:12:10 +00002822 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002823 mac_mode |= MAC_MODE_APE_TX_EN |
2824 MAC_MODE_APE_RX_EN |
2825 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 tw32_f(MAC_MODE, mac_mode);
2828 udelay(100);
2829
2830 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2831 udelay(10);
2832 }
2833
Joe Perches63c3a662011-04-26 08:12:10 +00002834 if (!tg3_flag(tp, WOL_SPEED_100MB) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2836 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2837 u32 base_val;
2838
2839 base_val = tp->pci_clock_ctrl;
2840 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2841 CLOCK_CTRL_TXCLK_DISABLE);
2842
Michael Chanb401e9e2005-12-19 16:27:04 -08002843 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2844 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Joe Perches63c3a662011-04-26 08:12:10 +00002845 } else if (tg3_flag(tp, 5780_CLASS) ||
2846 tg3_flag(tp, CPMU_PRESENT) ||
Matt Carlson6ff6f812011-05-19 12:12:54 +00002847 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002848 /* do nothing */
Joe Perches63c3a662011-04-26 08:12:10 +00002849 } else if (!(tg3_flag(tp, 5750_PLUS) && tg3_flag(tp, ENABLE_ASF))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 u32 newbits1, newbits2;
2851
2852 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2853 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2854 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2855 CLOCK_CTRL_TXCLK_DISABLE |
2856 CLOCK_CTRL_ALTCLK);
2857 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
Joe Perches63c3a662011-04-26 08:12:10 +00002858 } else if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 newbits1 = CLOCK_CTRL_625_CORE;
2860 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2861 } else {
2862 newbits1 = CLOCK_CTRL_ALTCLK;
2863 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2864 }
2865
Michael Chanb401e9e2005-12-19 16:27:04 -08002866 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2867 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Michael Chanb401e9e2005-12-19 16:27:04 -08002869 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2870 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Joe Perches63c3a662011-04-26 08:12:10 +00002872 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 u32 newbits3;
2874
2875 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2876 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2877 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2878 CLOCK_CTRL_TXCLK_DISABLE |
2879 CLOCK_CTRL_44MHZ_CORE);
2880 } else {
2881 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2882 }
2883
Michael Chanb401e9e2005-12-19 16:27:04 -08002884 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2885 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
2887 }
2888
Joe Perches63c3a662011-04-26 08:12:10 +00002889 if (!(device_should_wake) && !tg3_flag(tp, ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002890 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 tg3_frob_aux_power(tp);
2893
2894 /* Workaround for unstable PLL clock */
2895 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2896 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2897 u32 val = tr32(0x7d00);
2898
2899 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2900 tw32(0x7d00, val);
Joe Perches63c3a662011-04-26 08:12:10 +00002901 if (!tg3_flag(tp, ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002902 int err;
2903
2904 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002906 if (!err)
2907 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 }
2910
Michael Chanbbadf502006-04-06 21:46:34 -07002911 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 return 0;
2914}
2915
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002916static void tg3_power_down(struct tg3 *tp)
2917{
2918 tg3_power_down_prepare(tp);
2919
Joe Perches63c3a662011-04-26 08:12:10 +00002920 pci_wake_from_d3(tp->pdev, tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002921 pci_set_power_state(tp->pdev, PCI_D3hot);
2922}
2923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2925{
2926 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2927 case MII_TG3_AUX_STAT_10HALF:
2928 *speed = SPEED_10;
2929 *duplex = DUPLEX_HALF;
2930 break;
2931
2932 case MII_TG3_AUX_STAT_10FULL:
2933 *speed = SPEED_10;
2934 *duplex = DUPLEX_FULL;
2935 break;
2936
2937 case MII_TG3_AUX_STAT_100HALF:
2938 *speed = SPEED_100;
2939 *duplex = DUPLEX_HALF;
2940 break;
2941
2942 case MII_TG3_AUX_STAT_100FULL:
2943 *speed = SPEED_100;
2944 *duplex = DUPLEX_FULL;
2945 break;
2946
2947 case MII_TG3_AUX_STAT_1000HALF:
2948 *speed = SPEED_1000;
2949 *duplex = DUPLEX_HALF;
2950 break;
2951
2952 case MII_TG3_AUX_STAT_1000FULL:
2953 *speed = SPEED_1000;
2954 *duplex = DUPLEX_FULL;
2955 break;
2956
2957 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002958 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002959 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2960 SPEED_10;
2961 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2962 DUPLEX_HALF;
2963 break;
2964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 *speed = SPEED_INVALID;
2966 *duplex = DUPLEX_INVALID;
2967 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969}
2970
Matt Carlson42b64a42011-05-19 12:12:49 +00002971static int tg3_phy_autoneg_cfg(struct tg3 *tp, u32 advertise, u32 flowctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
Matt Carlson42b64a42011-05-19 12:12:49 +00002973 int err = 0;
2974 u32 val, new_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Matt Carlson42b64a42011-05-19 12:12:49 +00002976 new_adv = ADVERTISE_CSMA;
2977 if (advertise & ADVERTISED_10baseT_Half)
2978 new_adv |= ADVERTISE_10HALF;
2979 if (advertise & ADVERTISED_10baseT_Full)
2980 new_adv |= ADVERTISE_10FULL;
2981 if (advertise & ADVERTISED_100baseT_Half)
2982 new_adv |= ADVERTISE_100HALF;
2983 if (advertise & ADVERTISED_100baseT_Full)
2984 new_adv |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Matt Carlson42b64a42011-05-19 12:12:49 +00002986 new_adv |= tg3_advert_flowctrl_1000T(flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Matt Carlson42b64a42011-05-19 12:12:49 +00002988 err = tg3_writephy(tp, MII_ADVERTISE, new_adv);
2989 if (err)
2990 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Matt Carlson42b64a42011-05-19 12:12:49 +00002992 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
2993 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002994
Matt Carlson42b64a42011-05-19 12:12:49 +00002995 new_adv = 0;
2996 if (advertise & ADVERTISED_1000baseT_Half)
Matt Carlson221c5632011-06-13 13:39:01 +00002997 new_adv |= ADVERTISE_1000HALF;
Matt Carlson42b64a42011-05-19 12:12:49 +00002998 if (advertise & ADVERTISED_1000baseT_Full)
Matt Carlson221c5632011-06-13 13:39:01 +00002999 new_adv |= ADVERTISE_1000FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003000
Matt Carlson42b64a42011-05-19 12:12:49 +00003001 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3002 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
Matt Carlson221c5632011-06-13 13:39:01 +00003003 new_adv |= CTL1000_AS_MASTER | CTL1000_ENABLE_MASTER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Matt Carlson221c5632011-06-13 13:39:01 +00003005 err = tg3_writephy(tp, MII_CTRL1000, new_adv);
Matt Carlson42b64a42011-05-19 12:12:49 +00003006 if (err)
3007 goto done;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003008
Matt Carlson42b64a42011-05-19 12:12:49 +00003009 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
3010 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Matt Carlson42b64a42011-05-19 12:12:49 +00003012 tw32(TG3_CPMU_EEE_MODE,
3013 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08003014
Matt Carlson42b64a42011-05-19 12:12:49 +00003015 err = TG3_PHY_AUXCTL_SMDSP_ENABLE(tp);
3016 if (!err) {
3017 u32 err2;
Matt Carlson52b02d02010-10-14 10:37:41 +00003018
Matt Carlson21a00ab2011-01-25 15:58:55 +00003019 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
3020 case ASIC_REV_5717:
3021 case ASIC_REV_57765:
3022 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
3023 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
3024 MII_TG3_DSP_CH34TP2_HIBW01);
3025 /* Fall through */
3026 case ASIC_REV_5719:
3027 val = MII_TG3_DSP_TAP26_ALNOKO |
3028 MII_TG3_DSP_TAP26_RMRXSTO |
3029 MII_TG3_DSP_TAP26_OPCSINPT;
3030 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3031 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003032
Matt Carlsona6b68da2010-12-06 08:28:52 +00003033 val = 0;
Matt Carlson42b64a42011-05-19 12:12:49 +00003034 /* Advertise 100-BaseTX EEE ability */
3035 if (advertise & ADVERTISED_100baseT_Full)
3036 val |= MDIO_AN_EEE_ADV_100TX;
3037 /* Advertise 1000-BaseT EEE ability */
3038 if (advertise & ADVERTISED_1000baseT_Full)
3039 val |= MDIO_AN_EEE_ADV_1000T;
3040 err = tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003041
Matt Carlson42b64a42011-05-19 12:12:49 +00003042 err2 = TG3_PHY_AUXCTL_SMDSP_DISABLE(tp);
3043 if (!err)
3044 err = err2;
3045 }
3046
3047done:
3048 return err;
3049}
3050
3051static void tg3_phy_copper_begin(struct tg3 *tp)
3052{
3053 u32 new_adv;
3054 int i;
3055
3056 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
3057 new_adv = ADVERTISED_10baseT_Half |
3058 ADVERTISED_10baseT_Full;
3059 if (tg3_flag(tp, WOL_SPEED_100MB))
3060 new_adv |= ADVERTISED_100baseT_Half |
3061 ADVERTISED_100baseT_Full;
3062
3063 tg3_phy_autoneg_cfg(tp, new_adv,
3064 FLOW_CTRL_TX | FLOW_CTRL_RX);
3065 } else if (tp->link_config.speed == SPEED_INVALID) {
3066 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
3067 tp->link_config.advertising &=
3068 ~(ADVERTISED_1000baseT_Half |
3069 ADVERTISED_1000baseT_Full);
3070
3071 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
3072 tp->link_config.flowctrl);
3073 } else {
3074 /* Asking for a specific link mode. */
3075 if (tp->link_config.speed == SPEED_1000) {
3076 if (tp->link_config.duplex == DUPLEX_FULL)
3077 new_adv = ADVERTISED_1000baseT_Full;
3078 else
3079 new_adv = ADVERTISED_1000baseT_Half;
3080 } else if (tp->link_config.speed == SPEED_100) {
3081 if (tp->link_config.duplex == DUPLEX_FULL)
3082 new_adv = ADVERTISED_100baseT_Full;
3083 else
3084 new_adv = ADVERTISED_100baseT_Half;
3085 } else {
3086 if (tp->link_config.duplex == DUPLEX_FULL)
3087 new_adv = ADVERTISED_10baseT_Full;
3088 else
3089 new_adv = ADVERTISED_10baseT_Half;
3090 }
3091
3092 tg3_phy_autoneg_cfg(tp, new_adv,
3093 tp->link_config.flowctrl);
Matt Carlson52b02d02010-10-14 10:37:41 +00003094 }
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3097 tp->link_config.speed != SPEED_INVALID) {
3098 u32 bmcr, orig_bmcr;
3099
3100 tp->link_config.active_speed = tp->link_config.speed;
3101 tp->link_config.active_duplex = tp->link_config.duplex;
3102
3103 bmcr = 0;
3104 switch (tp->link_config.speed) {
3105 default:
3106 case SPEED_10:
3107 break;
3108
3109 case SPEED_100:
3110 bmcr |= BMCR_SPEED100;
3111 break;
3112
3113 case SPEED_1000:
Matt Carlson221c5632011-06-13 13:39:01 +00003114 bmcr |= BMCR_SPEED1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
3118 if (tp->link_config.duplex == DUPLEX_FULL)
3119 bmcr |= BMCR_FULLDPLX;
3120
3121 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3122 (bmcr != orig_bmcr)) {
3123 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3124 for (i = 0; i < 1500; i++) {
3125 u32 tmp;
3126
3127 udelay(10);
3128 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3129 tg3_readphy(tp, MII_BMSR, &tmp))
3130 continue;
3131 if (!(tmp & BMSR_LSTATUS)) {
3132 udelay(40);
3133 break;
3134 }
3135 }
3136 tg3_writephy(tp, MII_BMCR, bmcr);
3137 udelay(40);
3138 }
3139 } else {
3140 tg3_writephy(tp, MII_BMCR,
3141 BMCR_ANENABLE | BMCR_ANRESTART);
3142 }
3143}
3144
3145static int tg3_init_5401phy_dsp(struct tg3 *tp)
3146{
3147 int err;
3148
3149 /* Turn off tap power management. */
3150 /* Set Extended packet length bit */
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003151 err = tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_AUXCTL, 0x4c20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003153 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3154 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3155 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3156 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3157 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
3159 udelay(40);
3160
3161 return err;
3162}
3163
Michael Chan3600d912006-12-07 00:21:48 -08003164static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
Michael Chan3600d912006-12-07 00:21:48 -08003166 u32 adv_reg, all_mask = 0;
3167
3168 if (mask & ADVERTISED_10baseT_Half)
3169 all_mask |= ADVERTISE_10HALF;
3170 if (mask & ADVERTISED_10baseT_Full)
3171 all_mask |= ADVERTISE_10FULL;
3172 if (mask & ADVERTISED_100baseT_Half)
3173 all_mask |= ADVERTISE_100HALF;
3174 if (mask & ADVERTISED_100baseT_Full)
3175 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
3177 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3178 return 0;
3179
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 if ((adv_reg & all_mask) != all_mask)
3181 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003182 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 u32 tg3_ctrl;
3184
Michael Chan3600d912006-12-07 00:21:48 -08003185 all_mask = 0;
3186 if (mask & ADVERTISED_1000baseT_Half)
3187 all_mask |= ADVERTISE_1000HALF;
3188 if (mask & ADVERTISED_1000baseT_Full)
3189 all_mask |= ADVERTISE_1000FULL;
3190
Matt Carlson221c5632011-06-13 13:39:01 +00003191 if (tg3_readphy(tp, MII_CTRL1000, &tg3_ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 return 0;
3193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 if ((tg3_ctrl & all_mask) != all_mask)
3195 return 0;
3196 }
3197 return 1;
3198}
3199
Matt Carlsonef167e22007-12-20 20:10:01 -08003200static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3201{
3202 u32 curadv, reqadv;
3203
3204 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3205 return 1;
3206
3207 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3208 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3209
3210 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3211 if (curadv != reqadv)
3212 return 0;
3213
Joe Perches63c3a662011-04-26 08:12:10 +00003214 if (tg3_flag(tp, PAUSE_AUTONEG))
Matt Carlsonef167e22007-12-20 20:10:01 -08003215 tg3_readphy(tp, MII_LPA, rmtadv);
3216 } else {
3217 /* Reprogram the advertisement register, even if it
3218 * does not affect the current link. If the link
3219 * gets renegotiated in the future, we can save an
3220 * additional renegotiation cycle by advertising
3221 * it correctly in the first place.
3222 */
3223 if (curadv != reqadv) {
3224 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3225 ADVERTISE_PAUSE_ASYM);
3226 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3227 }
3228 }
3229
3230 return 1;
3231}
3232
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3234{
3235 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003236 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003237 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 u16 current_speed;
3239 u8 current_duplex;
3240 int i, err;
3241
3242 tw32(MAC_EVENT, 0);
3243
3244 tw32_f(MAC_STATUS,
3245 (MAC_STATUS_SYNC_CHANGED |
3246 MAC_STATUS_CFG_CHANGED |
3247 MAC_STATUS_MI_COMPLETION |
3248 MAC_STATUS_LNKSTATE_CHANGED));
3249 udelay(40);
3250
Matt Carlson8ef21422008-05-02 16:47:53 -07003251 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3252 tw32_f(MAC_MI_MODE,
3253 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3254 udelay(80);
3255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003257 tg3_phy_auxctl_write(tp, MII_TG3_AUXCTL_SHDWSEL_PWRCTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
3259 /* Some third-party PHYs need to be reset on link going
3260 * down.
3261 */
3262 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3263 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3264 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3265 netif_carrier_ok(tp->dev)) {
3266 tg3_readphy(tp, MII_BMSR, &bmsr);
3267 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3268 !(bmsr & BMSR_LSTATUS))
3269 force_reset = 1;
3270 }
3271 if (force_reset)
3272 tg3_phy_reset(tp);
3273
Matt Carlson79eb6902010-02-17 15:17:03 +00003274 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 tg3_readphy(tp, MII_BMSR, &bmsr);
3276 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
Joe Perches63c3a662011-04-26 08:12:10 +00003277 !tg3_flag(tp, INIT_COMPLETE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 bmsr = 0;
3279
3280 if (!(bmsr & BMSR_LSTATUS)) {
3281 err = tg3_init_5401phy_dsp(tp);
3282 if (err)
3283 return err;
3284
3285 tg3_readphy(tp, MII_BMSR, &bmsr);
3286 for (i = 0; i < 1000; i++) {
3287 udelay(10);
3288 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3289 (bmsr & BMSR_LSTATUS)) {
3290 udelay(40);
3291 break;
3292 }
3293 }
3294
Matt Carlson79eb6902010-02-17 15:17:03 +00003295 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3296 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 !(bmsr & BMSR_LSTATUS) &&
3298 tp->link_config.active_speed == SPEED_1000) {
3299 err = tg3_phy_reset(tp);
3300 if (!err)
3301 err = tg3_init_5401phy_dsp(tp);
3302 if (err)
3303 return err;
3304 }
3305 }
3306 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3307 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3308 /* 5701 {A0,B0} CRC bug workaround */
3309 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003310 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3311 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3312 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 }
3314
3315 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003316 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3317 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003319 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003321 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3323
3324 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3325 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3326 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3327 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3328 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3329 else
3330 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3331 }
3332
3333 current_link_up = 0;
3334 current_speed = SPEED_INVALID;
3335 current_duplex = DUPLEX_INVALID;
3336
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003337 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Matt Carlson15ee95c2011-04-20 07:57:40 +00003338 err = tg3_phy_auxctl_read(tp,
3339 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3340 &val);
3341 if (!err && !(val & (1 << 10))) {
Matt Carlsonb4bd2922011-04-20 07:57:41 +00003342 tg3_phy_auxctl_write(tp,
3343 MII_TG3_AUXCTL_SHDWSEL_MISCTEST,
3344 val | (1 << 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 goto relink;
3346 }
3347 }
3348
3349 bmsr = 0;
3350 for (i = 0; i < 100; i++) {
3351 tg3_readphy(tp, MII_BMSR, &bmsr);
3352 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3353 (bmsr & BMSR_LSTATUS))
3354 break;
3355 udelay(40);
3356 }
3357
3358 if (bmsr & BMSR_LSTATUS) {
3359 u32 aux_stat, bmcr;
3360
3361 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3362 for (i = 0; i < 2000; i++) {
3363 udelay(10);
3364 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3365 aux_stat)
3366 break;
3367 }
3368
3369 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3370 &current_speed,
3371 &current_duplex);
3372
3373 bmcr = 0;
3374 for (i = 0; i < 200; i++) {
3375 tg3_readphy(tp, MII_BMCR, &bmcr);
3376 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3377 continue;
3378 if (bmcr && bmcr != 0x7fff)
3379 break;
3380 udelay(10);
3381 }
3382
Matt Carlsonef167e22007-12-20 20:10:01 -08003383 lcl_adv = 0;
3384 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Matt Carlsonef167e22007-12-20 20:10:01 -08003386 tp->link_config.active_speed = current_speed;
3387 tp->link_config.active_duplex = current_duplex;
3388
3389 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3390 if ((bmcr & BMCR_ANENABLE) &&
3391 tg3_copper_is_advertising_all(tp,
3392 tp->link_config.advertising)) {
3393 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3394 &rmt_adv))
3395 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 }
3397 } else {
3398 if (!(bmcr & BMCR_ANENABLE) &&
3399 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003400 tp->link_config.duplex == current_duplex &&
3401 tp->link_config.flowctrl ==
3402 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 }
3405 }
3406
Matt Carlsonef167e22007-12-20 20:10:01 -08003407 if (current_link_up == 1 &&
3408 tp->link_config.active_duplex == DUPLEX_FULL)
3409 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 }
3411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412relink:
Matt Carlson800960682010-08-02 11:26:06 +00003413 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 tg3_phy_copper_begin(tp);
3415
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003416 tg3_readphy(tp, MII_BMSR, &bmsr);
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00003417 if ((!tg3_readphy(tp, MII_BMSR, &bmsr) && (bmsr & BMSR_LSTATUS)) ||
3418 (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 current_link_up = 1;
3420 }
3421
3422 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3423 if (current_link_up == 1) {
3424 if (tp->link_config.active_speed == SPEED_100 ||
3425 tp->link_config.active_speed == SPEED_10)
3426 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3427 else
3428 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003429 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003430 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3431 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3433
3434 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3435 if (tp->link_config.active_duplex == DUPLEX_HALF)
3436 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3437
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003439 if (current_link_up == 1 &&
3440 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003442 else
3443 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 }
3445
3446 /* ??? Without this setting Netgear GA302T PHY does not
3447 * ??? send/receive packets...
3448 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003449 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3451 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3452 tw32_f(MAC_MI_MODE, tp->mi_mode);
3453 udelay(80);
3454 }
3455
3456 tw32_f(MAC_MODE, tp->mac_mode);
3457 udelay(40);
3458
Matt Carlson52b02d02010-10-14 10:37:41 +00003459 tg3_phy_eee_adjust(tp, current_link_up);
3460
Joe Perches63c3a662011-04-26 08:12:10 +00003461 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 /* Polled via timer. */
3463 tw32_f(MAC_EVENT, 0);
3464 } else {
3465 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3466 }
3467 udelay(40);
3468
3469 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3470 current_link_up == 1 &&
3471 tp->link_config.active_speed == SPEED_1000 &&
Joe Perches63c3a662011-04-26 08:12:10 +00003472 (tg3_flag(tp, PCIX_MODE) || tg3_flag(tp, PCI_HIGH_SPEED))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 udelay(120);
3474 tw32_f(MAC_STATUS,
3475 (MAC_STATUS_SYNC_CHANGED |
3476 MAC_STATUS_CFG_CHANGED));
3477 udelay(40);
3478 tg3_write_mem(tp,
3479 NIC_SRAM_FIRMWARE_MBOX,
3480 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3481 }
3482
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003483 /* Prevent send BD corruption. */
Joe Perches63c3a662011-04-26 08:12:10 +00003484 if (tg3_flag(tp, CLKREQ_BUG)) {
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003485 u16 oldlnkctl, newlnkctl;
3486
3487 pci_read_config_word(tp->pdev,
3488 tp->pcie_cap + PCI_EXP_LNKCTL,
3489 &oldlnkctl);
3490 if (tp->link_config.active_speed == SPEED_100 ||
3491 tp->link_config.active_speed == SPEED_10)
3492 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3493 else
3494 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3495 if (newlnkctl != oldlnkctl)
3496 pci_write_config_word(tp->pdev,
3497 tp->pcie_cap + PCI_EXP_LNKCTL,
3498 newlnkctl);
3499 }
3500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 if (current_link_up != netif_carrier_ok(tp->dev)) {
3502 if (current_link_up)
3503 netif_carrier_on(tp->dev);
3504 else
3505 netif_carrier_off(tp->dev);
3506 tg3_link_report(tp);
3507 }
3508
3509 return 0;
3510}
3511
3512struct tg3_fiber_aneginfo {
3513 int state;
3514#define ANEG_STATE_UNKNOWN 0
3515#define ANEG_STATE_AN_ENABLE 1
3516#define ANEG_STATE_RESTART_INIT 2
3517#define ANEG_STATE_RESTART 3
3518#define ANEG_STATE_DISABLE_LINK_OK 4
3519#define ANEG_STATE_ABILITY_DETECT_INIT 5
3520#define ANEG_STATE_ABILITY_DETECT 6
3521#define ANEG_STATE_ACK_DETECT_INIT 7
3522#define ANEG_STATE_ACK_DETECT 8
3523#define ANEG_STATE_COMPLETE_ACK_INIT 9
3524#define ANEG_STATE_COMPLETE_ACK 10
3525#define ANEG_STATE_IDLE_DETECT_INIT 11
3526#define ANEG_STATE_IDLE_DETECT 12
3527#define ANEG_STATE_LINK_OK 13
3528#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3529#define ANEG_STATE_NEXT_PAGE_WAIT 15
3530
3531 u32 flags;
3532#define MR_AN_ENABLE 0x00000001
3533#define MR_RESTART_AN 0x00000002
3534#define MR_AN_COMPLETE 0x00000004
3535#define MR_PAGE_RX 0x00000008
3536#define MR_NP_LOADED 0x00000010
3537#define MR_TOGGLE_TX 0x00000020
3538#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3539#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3540#define MR_LP_ADV_SYM_PAUSE 0x00000100
3541#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3542#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3543#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3544#define MR_LP_ADV_NEXT_PAGE 0x00001000
3545#define MR_TOGGLE_RX 0x00002000
3546#define MR_NP_RX 0x00004000
3547
3548#define MR_LINK_OK 0x80000000
3549
3550 unsigned long link_time, cur_time;
3551
3552 u32 ability_match_cfg;
3553 int ability_match_count;
3554
3555 char ability_match, idle_match, ack_match;
3556
3557 u32 txconfig, rxconfig;
3558#define ANEG_CFG_NP 0x00000080
3559#define ANEG_CFG_ACK 0x00000040
3560#define ANEG_CFG_RF2 0x00000020
3561#define ANEG_CFG_RF1 0x00000010
3562#define ANEG_CFG_PS2 0x00000001
3563#define ANEG_CFG_PS1 0x00008000
3564#define ANEG_CFG_HD 0x00004000
3565#define ANEG_CFG_FD 0x00002000
3566#define ANEG_CFG_INVAL 0x00001f06
3567
3568};
3569#define ANEG_OK 0
3570#define ANEG_DONE 1
3571#define ANEG_TIMER_ENAB 2
3572#define ANEG_FAILED -1
3573
3574#define ANEG_STATE_SETTLE_TIME 10000
3575
3576static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3577 struct tg3_fiber_aneginfo *ap)
3578{
Matt Carlson5be73b42007-12-20 20:09:29 -08003579 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 unsigned long delta;
3581 u32 rx_cfg_reg;
3582 int ret;
3583
3584 if (ap->state == ANEG_STATE_UNKNOWN) {
3585 ap->rxconfig = 0;
3586 ap->link_time = 0;
3587 ap->cur_time = 0;
3588 ap->ability_match_cfg = 0;
3589 ap->ability_match_count = 0;
3590 ap->ability_match = 0;
3591 ap->idle_match = 0;
3592 ap->ack_match = 0;
3593 }
3594 ap->cur_time++;
3595
3596 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3597 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3598
3599 if (rx_cfg_reg != ap->ability_match_cfg) {
3600 ap->ability_match_cfg = rx_cfg_reg;
3601 ap->ability_match = 0;
3602 ap->ability_match_count = 0;
3603 } else {
3604 if (++ap->ability_match_count > 1) {
3605 ap->ability_match = 1;
3606 ap->ability_match_cfg = rx_cfg_reg;
3607 }
3608 }
3609 if (rx_cfg_reg & ANEG_CFG_ACK)
3610 ap->ack_match = 1;
3611 else
3612 ap->ack_match = 0;
3613
3614 ap->idle_match = 0;
3615 } else {
3616 ap->idle_match = 1;
3617 ap->ability_match_cfg = 0;
3618 ap->ability_match_count = 0;
3619 ap->ability_match = 0;
3620 ap->ack_match = 0;
3621
3622 rx_cfg_reg = 0;
3623 }
3624
3625 ap->rxconfig = rx_cfg_reg;
3626 ret = ANEG_OK;
3627
Matt Carlson33f401a2010-04-05 10:19:27 +00003628 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 case ANEG_STATE_UNKNOWN:
3630 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3631 ap->state = ANEG_STATE_AN_ENABLE;
3632
3633 /* fallthru */
3634 case ANEG_STATE_AN_ENABLE:
3635 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3636 if (ap->flags & MR_AN_ENABLE) {
3637 ap->link_time = 0;
3638 ap->cur_time = 0;
3639 ap->ability_match_cfg = 0;
3640 ap->ability_match_count = 0;
3641 ap->ability_match = 0;
3642 ap->idle_match = 0;
3643 ap->ack_match = 0;
3644
3645 ap->state = ANEG_STATE_RESTART_INIT;
3646 } else {
3647 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3648 }
3649 break;
3650
3651 case ANEG_STATE_RESTART_INIT:
3652 ap->link_time = ap->cur_time;
3653 ap->flags &= ~(MR_NP_LOADED);
3654 ap->txconfig = 0;
3655 tw32(MAC_TX_AUTO_NEG, 0);
3656 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3657 tw32_f(MAC_MODE, tp->mac_mode);
3658 udelay(40);
3659
3660 ret = ANEG_TIMER_ENAB;
3661 ap->state = ANEG_STATE_RESTART;
3662
3663 /* fallthru */
3664 case ANEG_STATE_RESTART:
3665 delta = ap->cur_time - ap->link_time;
Matt Carlson859a588792010-04-05 10:19:28 +00003666 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a588792010-04-05 10:19:28 +00003668 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 break;
3671
3672 case ANEG_STATE_DISABLE_LINK_OK:
3673 ret = ANEG_DONE;
3674 break;
3675
3676 case ANEG_STATE_ABILITY_DETECT_INIT:
3677 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003678 ap->txconfig = ANEG_CFG_FD;
3679 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3680 if (flowctrl & ADVERTISE_1000XPAUSE)
3681 ap->txconfig |= ANEG_CFG_PS1;
3682 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3683 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3685 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3686 tw32_f(MAC_MODE, tp->mac_mode);
3687 udelay(40);
3688
3689 ap->state = ANEG_STATE_ABILITY_DETECT;
3690 break;
3691
3692 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a588792010-04-05 10:19:28 +00003693 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 break;
3696
3697 case ANEG_STATE_ACK_DETECT_INIT:
3698 ap->txconfig |= ANEG_CFG_ACK;
3699 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3700 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3701 tw32_f(MAC_MODE, tp->mac_mode);
3702 udelay(40);
3703
3704 ap->state = ANEG_STATE_ACK_DETECT;
3705
3706 /* fallthru */
3707 case ANEG_STATE_ACK_DETECT:
3708 if (ap->ack_match != 0) {
3709 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3710 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3711 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3712 } else {
3713 ap->state = ANEG_STATE_AN_ENABLE;
3714 }
3715 } else if (ap->ability_match != 0 &&
3716 ap->rxconfig == 0) {
3717 ap->state = ANEG_STATE_AN_ENABLE;
3718 }
3719 break;
3720
3721 case ANEG_STATE_COMPLETE_ACK_INIT:
3722 if (ap->rxconfig & ANEG_CFG_INVAL) {
3723 ret = ANEG_FAILED;
3724 break;
3725 }
3726 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3727 MR_LP_ADV_HALF_DUPLEX |
3728 MR_LP_ADV_SYM_PAUSE |
3729 MR_LP_ADV_ASYM_PAUSE |
3730 MR_LP_ADV_REMOTE_FAULT1 |
3731 MR_LP_ADV_REMOTE_FAULT2 |
3732 MR_LP_ADV_NEXT_PAGE |
3733 MR_TOGGLE_RX |
3734 MR_NP_RX);
3735 if (ap->rxconfig & ANEG_CFG_FD)
3736 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3737 if (ap->rxconfig & ANEG_CFG_HD)
3738 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3739 if (ap->rxconfig & ANEG_CFG_PS1)
3740 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3741 if (ap->rxconfig & ANEG_CFG_PS2)
3742 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3743 if (ap->rxconfig & ANEG_CFG_RF1)
3744 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3745 if (ap->rxconfig & ANEG_CFG_RF2)
3746 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3747 if (ap->rxconfig & ANEG_CFG_NP)
3748 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3749
3750 ap->link_time = ap->cur_time;
3751
3752 ap->flags ^= (MR_TOGGLE_TX);
3753 if (ap->rxconfig & 0x0008)
3754 ap->flags |= MR_TOGGLE_RX;
3755 if (ap->rxconfig & ANEG_CFG_NP)
3756 ap->flags |= MR_NP_RX;
3757 ap->flags |= MR_PAGE_RX;
3758
3759 ap->state = ANEG_STATE_COMPLETE_ACK;
3760 ret = ANEG_TIMER_ENAB;
3761 break;
3762
3763 case ANEG_STATE_COMPLETE_ACK:
3764 if (ap->ability_match != 0 &&
3765 ap->rxconfig == 0) {
3766 ap->state = ANEG_STATE_AN_ENABLE;
3767 break;
3768 }
3769 delta = ap->cur_time - ap->link_time;
3770 if (delta > ANEG_STATE_SETTLE_TIME) {
3771 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3772 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3773 } else {
3774 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3775 !(ap->flags & MR_NP_RX)) {
3776 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3777 } else {
3778 ret = ANEG_FAILED;
3779 }
3780 }
3781 }
3782 break;
3783
3784 case ANEG_STATE_IDLE_DETECT_INIT:
3785 ap->link_time = ap->cur_time;
3786 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3787 tw32_f(MAC_MODE, tp->mac_mode);
3788 udelay(40);
3789
3790 ap->state = ANEG_STATE_IDLE_DETECT;
3791 ret = ANEG_TIMER_ENAB;
3792 break;
3793
3794 case ANEG_STATE_IDLE_DETECT:
3795 if (ap->ability_match != 0 &&
3796 ap->rxconfig == 0) {
3797 ap->state = ANEG_STATE_AN_ENABLE;
3798 break;
3799 }
3800 delta = ap->cur_time - ap->link_time;
3801 if (delta > ANEG_STATE_SETTLE_TIME) {
3802 /* XXX another gem from the Broadcom driver :( */
3803 ap->state = ANEG_STATE_LINK_OK;
3804 }
3805 break;
3806
3807 case ANEG_STATE_LINK_OK:
3808 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3809 ret = ANEG_DONE;
3810 break;
3811
3812 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3813 /* ??? unimplemented */
3814 break;
3815
3816 case ANEG_STATE_NEXT_PAGE_WAIT:
3817 /* ??? unimplemented */
3818 break;
3819
3820 default:
3821 ret = ANEG_FAILED;
3822 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
3825 return ret;
3826}
3827
Matt Carlson5be73b42007-12-20 20:09:29 -08003828static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829{
3830 int res = 0;
3831 struct tg3_fiber_aneginfo aninfo;
3832 int status = ANEG_FAILED;
3833 unsigned int tick;
3834 u32 tmp;
3835
3836 tw32_f(MAC_TX_AUTO_NEG, 0);
3837
3838 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3839 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3840 udelay(40);
3841
3842 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3843 udelay(40);
3844
3845 memset(&aninfo, 0, sizeof(aninfo));
3846 aninfo.flags |= MR_AN_ENABLE;
3847 aninfo.state = ANEG_STATE_UNKNOWN;
3848 aninfo.cur_time = 0;
3849 tick = 0;
3850 while (++tick < 195000) {
3851 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3852 if (status == ANEG_DONE || status == ANEG_FAILED)
3853 break;
3854
3855 udelay(1);
3856 }
3857
3858 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3859 tw32_f(MAC_MODE, tp->mac_mode);
3860 udelay(40);
3861
Matt Carlson5be73b42007-12-20 20:09:29 -08003862 *txflags = aninfo.txconfig;
3863 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
3865 if (status == ANEG_DONE &&
3866 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3867 MR_LP_ADV_FULL_DUPLEX)))
3868 res = 1;
3869
3870 return res;
3871}
3872
3873static void tg3_init_bcm8002(struct tg3 *tp)
3874{
3875 u32 mac_status = tr32(MAC_STATUS);
3876 int i;
3877
3878 /* Reset when initting first time or we have a link. */
Joe Perches63c3a662011-04-26 08:12:10 +00003879 if (tg3_flag(tp, INIT_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 !(mac_status & MAC_STATUS_PCS_SYNCED))
3881 return;
3882
3883 /* Set PLL lock range. */
3884 tg3_writephy(tp, 0x16, 0x8007);
3885
3886 /* SW reset */
3887 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3888
3889 /* Wait for reset to complete. */
3890 /* XXX schedule_timeout() ... */
3891 for (i = 0; i < 500; i++)
3892 udelay(10);
3893
3894 /* Config mode; select PMA/Ch 1 regs. */
3895 tg3_writephy(tp, 0x10, 0x8411);
3896
3897 /* Enable auto-lock and comdet, select txclk for tx. */
3898 tg3_writephy(tp, 0x11, 0x0a10);
3899
3900 tg3_writephy(tp, 0x18, 0x00a0);
3901 tg3_writephy(tp, 0x16, 0x41ff);
3902
3903 /* Assert and deassert POR. */
3904 tg3_writephy(tp, 0x13, 0x0400);
3905 udelay(40);
3906 tg3_writephy(tp, 0x13, 0x0000);
3907
3908 tg3_writephy(tp, 0x11, 0x0a50);
3909 udelay(40);
3910 tg3_writephy(tp, 0x11, 0x0a10);
3911
3912 /* Wait for signal to stabilize */
3913 /* XXX schedule_timeout() ... */
3914 for (i = 0; i < 15000; i++)
3915 udelay(10);
3916
3917 /* Deselect the channel register so we can read the PHYID
3918 * later.
3919 */
3920 tg3_writephy(tp, 0x10, 0x8011);
3921}
3922
3923static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3924{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003925 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 u32 sg_dig_ctrl, sg_dig_status;
3927 u32 serdes_cfg, expected_sg_dig_ctrl;
3928 int workaround, port_a;
3929 int current_link_up;
3930
3931 serdes_cfg = 0;
3932 expected_sg_dig_ctrl = 0;
3933 workaround = 0;
3934 port_a = 1;
3935 current_link_up = 0;
3936
3937 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3938 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3939 workaround = 1;
3940 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3941 port_a = 0;
3942
3943 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3944 /* preserve bits 20-23 for voltage regulator */
3945 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3946 }
3947
3948 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3949
3950 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003951 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 if (workaround) {
3953 u32 val = serdes_cfg;
3954
3955 if (port_a)
3956 val |= 0xc010000;
3957 else
3958 val |= 0x4010000;
3959 tw32_f(MAC_SERDES_CFG, val);
3960 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003961
3962 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 }
3964 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3965 tg3_setup_flow_control(tp, 0, 0);
3966 current_link_up = 1;
3967 }
3968 goto out;
3969 }
3970
3971 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003972 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
Matt Carlson82cd3d12007-12-20 20:09:00 -08003974 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3975 if (flowctrl & ADVERTISE_1000XPAUSE)
3976 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3977 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3978 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
3980 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003981 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003982 tp->serdes_counter &&
3983 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3984 MAC_STATUS_RCVD_CFG)) ==
3985 MAC_STATUS_PCS_SYNCED)) {
3986 tp->serdes_counter--;
3987 current_link_up = 1;
3988 goto out;
3989 }
3990restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 if (workaround)
3992 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003993 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 udelay(5);
3995 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3996
Michael Chan3d3ebe72006-09-27 15:59:15 -07003997 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003998 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
4000 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004001 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 mac_status = tr32(MAC_STATUS);
4003
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004004 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08004006 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
Matt Carlson82cd3d12007-12-20 20:09:00 -08004008 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
4009 local_adv |= ADVERTISE_1000XPAUSE;
4010 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
4011 local_adv |= ADVERTISE_1000XPSE_ASYM;
4012
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004013 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004014 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004015 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08004016 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
4018 tg3_setup_flow_control(tp, local_adv, remote_adv);
4019 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004020 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004021 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004022 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07004023 if (tp->serdes_counter)
4024 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 else {
4026 if (workaround) {
4027 u32 val = serdes_cfg;
4028
4029 if (port_a)
4030 val |= 0xc010000;
4031 else
4032 val |= 0x4010000;
4033
4034 tw32_f(MAC_SERDES_CFG, val);
4035 }
4036
Matt Carlsonc98f6e32007-12-20 20:08:32 -08004037 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 udelay(40);
4039
4040 /* Link parallel detection - link is up */
4041 /* only if we have PCS_SYNC and not */
4042 /* receiving config code words */
4043 mac_status = tr32(MAC_STATUS);
4044 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
4045 !(mac_status & MAC_STATUS_RCVD_CFG)) {
4046 tg3_setup_flow_control(tp, 0, 0);
4047 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004048 tp->phy_flags |=
4049 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004050 tp->serdes_counter =
4051 SERDES_PARALLEL_DET_TIMEOUT;
4052 } else
4053 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 }
4055 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07004056 } else {
4057 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004058 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 }
4060
4061out:
4062 return current_link_up;
4063}
4064
4065static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
4066{
4067 int current_link_up = 0;
4068
Michael Chan5cf64b8a2007-05-05 12:11:21 -07004069 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
4072 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004073 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004075
Matt Carlson5be73b42007-12-20 20:09:29 -08004076 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4077 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
Matt Carlson5be73b42007-12-20 20:09:29 -08004079 if (txflags & ANEG_CFG_PS1)
4080 local_adv |= ADVERTISE_1000XPAUSE;
4081 if (txflags & ANEG_CFG_PS2)
4082 local_adv |= ADVERTISE_1000XPSE_ASYM;
4083
4084 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4085 remote_adv |= LPA_1000XPAUSE;
4086 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4087 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
4089 tg3_setup_flow_control(tp, local_adv, remote_adv);
4090
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 current_link_up = 1;
4092 }
4093 for (i = 0; i < 30; i++) {
4094 udelay(20);
4095 tw32_f(MAC_STATUS,
4096 (MAC_STATUS_SYNC_CHANGED |
4097 MAC_STATUS_CFG_CHANGED));
4098 udelay(40);
4099 if ((tr32(MAC_STATUS) &
4100 (MAC_STATUS_SYNC_CHANGED |
4101 MAC_STATUS_CFG_CHANGED)) == 0)
4102 break;
4103 }
4104
4105 mac_status = tr32(MAC_STATUS);
4106 if (current_link_up == 0 &&
4107 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4108 !(mac_status & MAC_STATUS_RCVD_CFG))
4109 current_link_up = 1;
4110 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004111 tg3_setup_flow_control(tp, 0, 0);
4112
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 /* Forcing 1000FD link up. */
4114 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
4116 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4117 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004118
4119 tw32_f(MAC_MODE, tp->mac_mode);
4120 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 }
4122
4123out:
4124 return current_link_up;
4125}
4126
4127static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4128{
4129 u32 orig_pause_cfg;
4130 u16 orig_active_speed;
4131 u8 orig_active_duplex;
4132 u32 mac_status;
4133 int current_link_up;
4134 int i;
4135
Matt Carlson8d018622007-12-20 20:05:44 -08004136 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 orig_active_speed = tp->link_config.active_speed;
4138 orig_active_duplex = tp->link_config.active_duplex;
4139
Joe Perches63c3a662011-04-26 08:12:10 +00004140 if (!tg3_flag(tp, HW_AUTONEG) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 netif_carrier_ok(tp->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00004142 tg3_flag(tp, INIT_COMPLETE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 mac_status = tr32(MAC_STATUS);
4144 mac_status &= (MAC_STATUS_PCS_SYNCED |
4145 MAC_STATUS_SIGNAL_DET |
4146 MAC_STATUS_CFG_CHANGED |
4147 MAC_STATUS_RCVD_CFG);
4148 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4149 MAC_STATUS_SIGNAL_DET)) {
4150 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4151 MAC_STATUS_CFG_CHANGED));
4152 return 0;
4153 }
4154 }
4155
4156 tw32_f(MAC_TX_AUTO_NEG, 0);
4157
4158 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4159 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4160 tw32_f(MAC_MODE, tp->mac_mode);
4161 udelay(40);
4162
Matt Carlson79eb6902010-02-17 15:17:03 +00004163 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 tg3_init_bcm8002(tp);
4165
4166 /* Enable link change event even when serdes polling. */
4167 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4168 udelay(40);
4169
4170 current_link_up = 0;
4171 mac_status = tr32(MAC_STATUS);
4172
Joe Perches63c3a662011-04-26 08:12:10 +00004173 if (tg3_flag(tp, HW_AUTONEG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4175 else
4176 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4177
Matt Carlson898a56f2009-08-28 14:02:40 +00004178 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004180 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
4182 for (i = 0; i < 100; i++) {
4183 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4184 MAC_STATUS_CFG_CHANGED));
4185 udelay(5);
4186 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004187 MAC_STATUS_CFG_CHANGED |
4188 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 break;
4190 }
4191
4192 mac_status = tr32(MAC_STATUS);
4193 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4194 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004195 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4196 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 tw32_f(MAC_MODE, (tp->mac_mode |
4198 MAC_MODE_SEND_CONFIGS));
4199 udelay(1);
4200 tw32_f(MAC_MODE, tp->mac_mode);
4201 }
4202 }
4203
4204 if (current_link_up == 1) {
4205 tp->link_config.active_speed = SPEED_1000;
4206 tp->link_config.active_duplex = DUPLEX_FULL;
4207 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4208 LED_CTRL_LNKLED_OVERRIDE |
4209 LED_CTRL_1000MBPS_ON));
4210 } else {
4211 tp->link_config.active_speed = SPEED_INVALID;
4212 tp->link_config.active_duplex = DUPLEX_INVALID;
4213 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4214 LED_CTRL_LNKLED_OVERRIDE |
4215 LED_CTRL_TRAFFIC_OVERRIDE));
4216 }
4217
4218 if (current_link_up != netif_carrier_ok(tp->dev)) {
4219 if (current_link_up)
4220 netif_carrier_on(tp->dev);
4221 else
4222 netif_carrier_off(tp->dev);
4223 tg3_link_report(tp);
4224 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004225 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 if (orig_pause_cfg != now_pause_cfg ||
4227 orig_active_speed != tp->link_config.active_speed ||
4228 orig_active_duplex != tp->link_config.active_duplex)
4229 tg3_link_report(tp);
4230 }
4231
4232 return 0;
4233}
4234
Michael Chan747e8f82005-07-25 12:33:22 -07004235static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4236{
4237 int current_link_up, err = 0;
4238 u32 bmsr, bmcr;
4239 u16 current_speed;
4240 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004241 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004242
4243 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4244 tw32_f(MAC_MODE, tp->mac_mode);
4245 udelay(40);
4246
4247 tw32(MAC_EVENT, 0);
4248
4249 tw32_f(MAC_STATUS,
4250 (MAC_STATUS_SYNC_CHANGED |
4251 MAC_STATUS_CFG_CHANGED |
4252 MAC_STATUS_MI_COMPLETION |
4253 MAC_STATUS_LNKSTATE_CHANGED));
4254 udelay(40);
4255
4256 if (force_reset)
4257 tg3_phy_reset(tp);
4258
4259 current_link_up = 0;
4260 current_speed = SPEED_INVALID;
4261 current_duplex = DUPLEX_INVALID;
4262
4263 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4264 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004265 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4266 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4267 bmsr |= BMSR_LSTATUS;
4268 else
4269 bmsr &= ~BMSR_LSTATUS;
4270 }
Michael Chan747e8f82005-07-25 12:33:22 -07004271
4272 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4273
4274 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004275 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004276 /* do nothing, just check for link up at the end */
4277 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4278 u32 adv, new_adv;
4279
4280 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4281 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4282 ADVERTISE_1000XPAUSE |
4283 ADVERTISE_1000XPSE_ASYM |
4284 ADVERTISE_SLCT);
4285
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004286 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004287
4288 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4289 new_adv |= ADVERTISE_1000XHALF;
4290 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4291 new_adv |= ADVERTISE_1000XFULL;
4292
4293 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4294 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4295 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4296 tg3_writephy(tp, MII_BMCR, bmcr);
4297
4298 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004299 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004300 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004301
4302 return err;
4303 }
4304 } else {
4305 u32 new_bmcr;
4306
4307 bmcr &= ~BMCR_SPEED1000;
4308 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4309
4310 if (tp->link_config.duplex == DUPLEX_FULL)
4311 new_bmcr |= BMCR_FULLDPLX;
4312
4313 if (new_bmcr != bmcr) {
4314 /* BMCR_SPEED1000 is a reserved bit that needs
4315 * to be set on write.
4316 */
4317 new_bmcr |= BMCR_SPEED1000;
4318
4319 /* Force a linkdown */
4320 if (netif_carrier_ok(tp->dev)) {
4321 u32 adv;
4322
4323 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4324 adv &= ~(ADVERTISE_1000XFULL |
4325 ADVERTISE_1000XHALF |
4326 ADVERTISE_SLCT);
4327 tg3_writephy(tp, MII_ADVERTISE, adv);
4328 tg3_writephy(tp, MII_BMCR, bmcr |
4329 BMCR_ANRESTART |
4330 BMCR_ANENABLE);
4331 udelay(10);
4332 netif_carrier_off(tp->dev);
4333 }
4334 tg3_writephy(tp, MII_BMCR, new_bmcr);
4335 bmcr = new_bmcr;
4336 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4337 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004338 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
4339 ASIC_REV_5714) {
4340 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4341 bmsr |= BMSR_LSTATUS;
4342 else
4343 bmsr &= ~BMSR_LSTATUS;
4344 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004345 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004346 }
4347 }
4348
4349 if (bmsr & BMSR_LSTATUS) {
4350 current_speed = SPEED_1000;
4351 current_link_up = 1;
4352 if (bmcr & BMCR_FULLDPLX)
4353 current_duplex = DUPLEX_FULL;
4354 else
4355 current_duplex = DUPLEX_HALF;
4356
Matt Carlsonef167e22007-12-20 20:10:01 -08004357 local_adv = 0;
4358 remote_adv = 0;
4359
Michael Chan747e8f82005-07-25 12:33:22 -07004360 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004361 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004362
4363 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4364 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4365 common = local_adv & remote_adv;
4366 if (common & (ADVERTISE_1000XHALF |
4367 ADVERTISE_1000XFULL)) {
4368 if (common & ADVERTISE_1000XFULL)
4369 current_duplex = DUPLEX_FULL;
4370 else
4371 current_duplex = DUPLEX_HALF;
Joe Perches63c3a662011-04-26 08:12:10 +00004372 } else if (!tg3_flag(tp, 5780_CLASS)) {
Matt Carlson57d8b882010-06-05 17:24:35 +00004373 /* Link is up via parallel detect */
Matt Carlson859a588792010-04-05 10:19:28 +00004374 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004375 current_link_up = 0;
Matt Carlson859a588792010-04-05 10:19:28 +00004376 }
Michael Chan747e8f82005-07-25 12:33:22 -07004377 }
4378 }
4379
Matt Carlsonef167e22007-12-20 20:10:01 -08004380 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4381 tg3_setup_flow_control(tp, local_adv, remote_adv);
4382
Michael Chan747e8f82005-07-25 12:33:22 -07004383 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4384 if (tp->link_config.active_duplex == DUPLEX_HALF)
4385 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4386
4387 tw32_f(MAC_MODE, tp->mac_mode);
4388 udelay(40);
4389
4390 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4391
4392 tp->link_config.active_speed = current_speed;
4393 tp->link_config.active_duplex = current_duplex;
4394
4395 if (current_link_up != netif_carrier_ok(tp->dev)) {
4396 if (current_link_up)
4397 netif_carrier_on(tp->dev);
4398 else {
4399 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004400 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004401 }
4402 tg3_link_report(tp);
4403 }
4404 return err;
4405}
4406
4407static void tg3_serdes_parallel_detect(struct tg3 *tp)
4408{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004409 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004410 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004411 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004412 return;
4413 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004414
Michael Chan747e8f82005-07-25 12:33:22 -07004415 if (!netif_carrier_ok(tp->dev) &&
4416 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4417 u32 bmcr;
4418
4419 tg3_readphy(tp, MII_BMCR, &bmcr);
4420 if (bmcr & BMCR_ANENABLE) {
4421 u32 phy1, phy2;
4422
4423 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004424 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4425 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004426
4427 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004428 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4429 MII_TG3_DSP_EXP1_INT_STAT);
4430 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4431 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004432
4433 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4434 /* We have signal detect and not receiving
4435 * config code words, link is up by parallel
4436 * detection.
4437 */
4438
4439 bmcr &= ~BMCR_ANENABLE;
4440 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4441 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004442 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004443 }
4444 }
Matt Carlson859a588792010-04-05 10:19:28 +00004445 } else if (netif_carrier_ok(tp->dev) &&
4446 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004447 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004448 u32 phy2;
4449
4450 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004451 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4452 MII_TG3_DSP_EXP1_INT_STAT);
4453 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004454 if (phy2 & 0x20) {
4455 u32 bmcr;
4456
4457 /* Config code words received, turn on autoneg. */
4458 tg3_readphy(tp, MII_BMCR, &bmcr);
4459 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4460
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004461 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004462
4463 }
4464 }
4465}
4466
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4468{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004469 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 int err;
4471
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004472 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004474 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004475 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a588792010-04-05 10:19:28 +00004476 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004479 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004480 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004481
4482 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4483 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4484 scale = 65;
4485 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4486 scale = 6;
4487 else
4488 scale = 12;
4489
4490 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4491 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4492 tw32(GRC_MISC_CFG, val);
4493 }
4494
Matt Carlsonf2096f92011-04-05 14:22:48 +00004495 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4496 (6 << TX_LENGTHS_IPG_SHIFT);
4497 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4498 val |= tr32(MAC_TX_LENGTHS) &
4499 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4500 TX_LENGTHS_CNT_DWN_VAL_MSK);
4501
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 if (tp->link_config.active_speed == SPEED_1000 &&
4503 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004504 tw32(MAC_TX_LENGTHS, val |
4505 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004507 tw32(MAC_TX_LENGTHS, val |
4508 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
Joe Perches63c3a662011-04-26 08:12:10 +00004510 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 if (netif_carrier_ok(tp->dev)) {
4512 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004513 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 } else {
4515 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4516 }
4517 }
4518
Joe Perches63c3a662011-04-26 08:12:10 +00004519 if (tg3_flag(tp, ASPM_WORKAROUND)) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004520 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004521 if (!netif_carrier_ok(tp->dev))
4522 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4523 tp->pwrmgmt_thresh;
4524 else
4525 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4526 tw32(PCIE_PWR_MGMT_THRESH, val);
4527 }
4528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 return err;
4530}
4531
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004532static inline int tg3_irq_sync(struct tg3 *tp)
4533{
4534 return tp->irq_sync;
4535}
4536
Matt Carlson97bd8e42011-04-13 11:05:04 +00004537static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4538{
4539 int i;
4540
4541 dst = (u32 *)((u8 *)dst + off);
4542 for (i = 0; i < len; i += sizeof(u32))
4543 *dst++ = tr32(off + i);
4544}
4545
4546static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4547{
4548 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4549 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4550 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4551 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4552 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4553 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4554 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4555 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4556 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4557 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4558 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4559 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4560 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4561 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4562 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4563 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4564 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4565 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4566 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4567
Joe Perches63c3a662011-04-26 08:12:10 +00004568 if (tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004569 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4570
4571 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4572 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4573 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4574 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4575 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4576 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4577 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4578 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4579
Joe Perches63c3a662011-04-26 08:12:10 +00004580 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004581 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4582 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4583 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4584 }
4585
4586 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4587 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4588 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4589 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4590 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4591
Joe Perches63c3a662011-04-26 08:12:10 +00004592 if (tg3_flag(tp, NVRAM))
Matt Carlson97bd8e42011-04-13 11:05:04 +00004593 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4594}
4595
4596static void tg3_dump_state(struct tg3 *tp)
4597{
4598 int i;
4599 u32 *regs;
4600
4601 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4602 if (!regs) {
4603 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4604 return;
4605 }
4606
Joe Perches63c3a662011-04-26 08:12:10 +00004607 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson97bd8e42011-04-13 11:05:04 +00004608 /* Read up to but not including private PCI registers */
4609 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4610 regs[i / sizeof(u32)] = tr32(i);
4611 } else
4612 tg3_dump_legacy_regs(tp, regs);
4613
4614 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4615 if (!regs[i + 0] && !regs[i + 1] &&
4616 !regs[i + 2] && !regs[i + 3])
4617 continue;
4618
4619 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4620 i * 4,
4621 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4622 }
4623
4624 kfree(regs);
4625
4626 for (i = 0; i < tp->irq_cnt; i++) {
4627 struct tg3_napi *tnapi = &tp->napi[i];
4628
4629 /* SW status block */
4630 netdev_err(tp->dev,
4631 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4632 i,
4633 tnapi->hw_status->status,
4634 tnapi->hw_status->status_tag,
4635 tnapi->hw_status->rx_jumbo_consumer,
4636 tnapi->hw_status->rx_consumer,
4637 tnapi->hw_status->rx_mini_consumer,
4638 tnapi->hw_status->idx[0].rx_producer,
4639 tnapi->hw_status->idx[0].tx_consumer);
4640
4641 netdev_err(tp->dev,
4642 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4643 i,
4644 tnapi->last_tag, tnapi->last_irq_tag,
4645 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4646 tnapi->rx_rcb_ptr,
4647 tnapi->prodring.rx_std_prod_idx,
4648 tnapi->prodring.rx_std_cons_idx,
4649 tnapi->prodring.rx_jmb_prod_idx,
4650 tnapi->prodring.rx_jmb_cons_idx);
4651 }
4652}
4653
Michael Chandf3e6542006-05-26 17:48:07 -07004654/* This is called whenever we suspect that the system chipset is re-
4655 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4656 * is bogus tx completions. We try to recover by setting the
4657 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4658 * in the workqueue.
4659 */
4660static void tg3_tx_recover(struct tg3 *tp)
4661{
Joe Perches63c3a662011-04-26 08:12:10 +00004662 BUG_ON(tg3_flag(tp, MBOX_WRITE_REORDER) ||
Michael Chandf3e6542006-05-26 17:48:07 -07004663 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4664
Matt Carlson5129c3a2010-04-05 10:19:23 +00004665 netdev_warn(tp->dev,
4666 "The system may be re-ordering memory-mapped I/O "
4667 "cycles to the network device, attempting to recover. "
4668 "Please report the problem to the driver maintainer "
4669 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004670
4671 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00004672 tg3_flag_set(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07004673 spin_unlock(&tp->lock);
4674}
4675
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004676static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004677{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004678 /* Tell compiler to fetch tx indices from memory. */
4679 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004680 return tnapi->tx_pending -
4681 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004682}
4683
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684/* Tigon3 never reports partial packet sends. So we do not
4685 * need special logic to handle SKBs that have not had all
4686 * of their frags sent yet, like SunGEM does.
4687 */
Matt Carlson17375d22009-08-28 14:02:18 +00004688static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689{
Matt Carlson17375d22009-08-28 14:02:18 +00004690 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004691 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004692 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004693 struct netdev_queue *txq;
4694 int index = tnapi - tp->napi;
4695
Joe Perches63c3a662011-04-26 08:12:10 +00004696 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004697 index--;
4698
4699 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
4701 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004702 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004704 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
Michael Chandf3e6542006-05-26 17:48:07 -07004706 if (unlikely(skb == NULL)) {
4707 tg3_tx_recover(tp);
4708 return;
4709 }
4710
Alexander Duyckf4188d82009-12-02 16:48:38 +00004711 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004712 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004713 skb_headlen(skb),
4714 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
4716 ri->skb = NULL;
4717
4718 sw_idx = NEXT_TX(sw_idx);
4719
4720 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004721 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004722 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4723 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004724
4725 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004726 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004727 skb_shinfo(skb)->frags[i].size,
4728 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 sw_idx = NEXT_TX(sw_idx);
4730 }
4731
David S. Millerf47c11e2005-06-24 20:18:35 -07004732 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004733
4734 if (unlikely(tx_bug)) {
4735 tg3_tx_recover(tp);
4736 return;
4737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 }
4739
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004740 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
Michael Chan1b2a7202006-08-07 21:46:02 -07004742 /* Need to make the tx_cons update visible to tg3_start_xmit()
4743 * before checking for netif_queue_stopped(). Without the
4744 * memory barrier, there is a small possibility that tg3_start_xmit()
4745 * will miss it and cause the queue to be stopped forever.
4746 */
4747 smp_mb();
4748
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004749 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004750 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004751 __netif_tx_lock(txq, smp_processor_id());
4752 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004753 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004754 netif_tx_wake_queue(txq);
4755 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757}
4758
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004759static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4760{
4761 if (!ri->skb)
4762 return;
4763
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004764 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004765 map_sz, PCI_DMA_FROMDEVICE);
4766 dev_kfree_skb_any(ri->skb);
4767 ri->skb = NULL;
4768}
4769
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770/* Returns size of skb allocated or < 0 on error.
4771 *
4772 * We only need to fill in the address because the other members
4773 * of the RX descriptor are invariant, see tg3_init_rings.
4774 *
4775 * Note the purposeful assymetry of cpu vs. chip accesses. For
4776 * posting buffers we only dirty the first cache line of the RX
4777 * descriptor (containing the address). Whereas for the RX status
4778 * buffers the cpu only reads the last cacheline of the RX descriptor
4779 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4780 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004781static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004782 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783{
4784 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004785 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 struct sk_buff *skb;
4787 dma_addr_t mapping;
4788 int skb_size, dest_idx;
4789
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 switch (opaque_key) {
4791 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004792 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004793 desc = &tpr->rx_std[dest_idx];
4794 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004795 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 break;
4797
4798 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004799 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004800 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004801 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004802 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 break;
4804
4805 default:
4806 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
4809 /* Do not overwrite any of the map or rp information
4810 * until we are sure we can commit to a new buffer.
4811 *
4812 * Callers depend upon this behavior and assume that
4813 * we leave everything unchanged if we fail.
4814 */
Matt Carlson287be122009-08-28 13:58:46 +00004815 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 if (skb == NULL)
4817 return -ENOMEM;
4818
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 skb_reserve(skb, tp->rx_offset);
4820
Matt Carlson287be122009-08-28 13:58:46 +00004821 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004823 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4824 dev_kfree_skb(skb);
4825 return -EIO;
4826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
4828 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004829 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 desc->addr_hi = ((u64)mapping >> 32);
4832 desc->addr_lo = ((u64)mapping & 0xffffffff);
4833
4834 return skb_size;
4835}
4836
4837/* We only need to move over in the address because the other
4838 * members of the RX descriptor are invariant. See notes above
4839 * tg3_alloc_rx_skb for full details.
4840 */
Matt Carlsona3896162009-11-13 13:03:44 +00004841static void tg3_recycle_rx(struct tg3_napi *tnapi,
4842 struct tg3_rx_prodring_set *dpr,
4843 u32 opaque_key, int src_idx,
4844 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845{
Matt Carlson17375d22009-08-28 14:02:18 +00004846 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4848 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004849 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004850 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
4852 switch (opaque_key) {
4853 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004854 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004855 dest_desc = &dpr->rx_std[dest_idx];
4856 dest_map = &dpr->rx_std_buffers[dest_idx];
4857 src_desc = &spr->rx_std[src_idx];
4858 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 break;
4860
4861 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004862 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004863 dest_desc = &dpr->rx_jmb[dest_idx].std;
4864 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4865 src_desc = &spr->rx_jmb[src_idx].std;
4866 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 break;
4868
4869 default:
4870 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
4873 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004874 dma_unmap_addr_set(dest_map, mapping,
4875 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 dest_desc->addr_hi = src_desc->addr_hi;
4877 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004878
4879 /* Ensure that the update to the skb happens after the physical
4880 * addresses have been transferred to the new BD location.
4881 */
4882 smp_wmb();
4883
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 src_map->skb = NULL;
4885}
4886
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887/* The RX ring scheme is composed of multiple rings which post fresh
4888 * buffers to the chip, and one special ring the chip uses to report
4889 * status back to the host.
4890 *
4891 * The special ring reports the status of received packets to the
4892 * host. The chip does not write into the original descriptor the
4893 * RX buffer was obtained from. The chip simply takes the original
4894 * descriptor as provided by the host, updates the status and length
4895 * field, then writes this into the next status ring entry.
4896 *
4897 * Each ring the host uses to post buffers to the chip is described
4898 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4899 * it is first placed into the on-chip ram. When the packet's length
4900 * is known, it walks down the TG3_BDINFO entries to select the ring.
4901 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4902 * which is within the range of the new packet's length is chosen.
4903 *
4904 * The "separate ring for rx status" scheme may sound queer, but it makes
4905 * sense from a cache coherency perspective. If only the host writes
4906 * to the buffer post rings, and only the chip writes to the rx status
4907 * rings, then cache lines never move beyond shared-modified state.
4908 * If both the host and chip were to write into the same ring, cache line
4909 * eviction could occur since both entities want it in an exclusive state.
4910 */
Matt Carlson17375d22009-08-28 14:02:18 +00004911static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912{
Matt Carlson17375d22009-08-28 14:02:18 +00004913 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004914 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004915 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004916 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004917 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004919 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004921 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 /*
4923 * We need to order the read of hw_idx and the read of
4924 * the opaque cookie.
4925 */
4926 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 work_mask = 0;
4928 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004929 std_prod_idx = tpr->rx_std_prod_idx;
4930 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004932 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004933 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 unsigned int len;
4935 struct sk_buff *skb;
4936 dma_addr_t dma_addr;
4937 u32 opaque_key, desc_idx, *post_ptr;
4938
4939 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4940 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4941 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004942 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004943 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004944 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004945 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004946 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004948 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004949 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004950 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004951 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004952 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954
4955 work_mask |= opaque_key;
4956
4957 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4958 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4959 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004960 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 desc_idx, *post_ptr);
4962 drop_it_no_recycle:
4963 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004964 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 goto next_pkt;
4966 }
4967
Matt Carlsonad829262008-11-21 17:16:16 -08004968 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4969 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
Matt Carlsond2757fc2010-04-12 06:58:27 +00004971 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 int skb_size;
4973
Matt Carlson86b21e52009-11-13 13:03:45 +00004974 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004975 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 if (skb_size < 0)
4977 goto drop_it;
4978
Matt Carlson287be122009-08-28 13:58:46 +00004979 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 PCI_DMA_FROMDEVICE);
4981
Matt Carlson61e800c2010-02-17 15:16:54 +00004982 /* Ensure that the update to the skb happens
4983 * after the usage of the old DMA mapping.
4984 */
4985 smp_wmb();
4986
4987 ri->skb = NULL;
4988
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 skb_put(skb, len);
4990 } else {
4991 struct sk_buff *copy_skb;
4992
Matt Carlsona3896162009-11-13 13:03:44 +00004993 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 desc_idx, *post_ptr);
4995
Matt Carlsonbf933c82011-01-25 15:58:49 +00004996 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00004997 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 if (copy_skb == NULL)
4999 goto drop_it_no_recycle;
5000
Matt Carlsonbf933c82011-01-25 15:58:49 +00005001 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 skb_put(copy_skb, len);
5003 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03005004 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
5006
5007 /* We'll reuse the original ring buffer. */
5008 skb = copy_skb;
5009 }
5010
Michał Mirosławdc668912011-04-07 03:35:07 +00005011 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
5013 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
5014 >> RXD_TCPCSUM_SHIFT) == 0xffff))
5015 skb->ip_summed = CHECKSUM_UNNECESSARY;
5016 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07005017 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
5019 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005020
5021 if (len > (tp->dev->mtu + ETH_HLEN) &&
5022 skb->protocol != htons(ETH_P_8021Q)) {
5023 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00005024 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00005025 }
5026
Matt Carlson9dc7a112010-04-12 06:58:28 +00005027 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00005028 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
5029 __vlan_hwaccel_put_tag(skb,
5030 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00005031
Matt Carlsonbf933c82011-01-25 15:58:49 +00005032 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 received++;
5035 budget--;
5036
5037next_pkt:
5038 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07005039
5040 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005041 tpr->rx_std_prod_idx = std_prod_idx &
5042 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00005043 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5044 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07005045 work_mask &= ~RXD_OPAQUE_RING_STD;
5046 rx_std_posted = 0;
5047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07005049 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00005050 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07005051
5052 /* Refresh hw_idx to see if there is new work */
5053 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005054 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07005055 rmb();
5056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 }
5058
5059 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00005060 tnapi->rx_rcb_ptr = sw_idx;
5061 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
5063 /* Refill RX ring(s). */
Joe Perches63c3a662011-04-26 08:12:10 +00005064 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005065 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005066 tpr->rx_std_prod_idx = std_prod_idx &
5067 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005068 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5069 tpr->rx_std_prod_idx);
5070 }
5071 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00005072 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5073 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005074 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5075 tpr->rx_jmb_prod_idx);
5076 }
5077 mmiowb();
5078 } else if (work_mask) {
5079 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5080 * updated before the producer indices can be updated.
5081 */
5082 smp_wmb();
5083
Matt Carlson2c49a442010-09-30 10:34:35 +00005084 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5085 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005086
Matt Carlsone4af1af2010-02-12 14:47:05 +00005087 if (tnapi != &tp->napi[1])
5088 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
5091 return received;
5092}
5093
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005094static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 /* handle link change and other phy events */
Joe Perches63c3a662011-04-26 08:12:10 +00005097 if (!(tg3_flag(tp, USE_LINKCHG_REG) || tg3_flag(tp, POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005098 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5099
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 if (sblk->status & SD_STATUS_LINK_CHG) {
5101 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005102 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005103 spin_lock(&tp->lock);
Joe Perches63c3a662011-04-26 08:12:10 +00005104 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsondd477002008-05-25 23:45:58 -07005105 tw32_f(MAC_STATUS,
5106 (MAC_STATUS_SYNC_CHANGED |
5107 MAC_STATUS_CFG_CHANGED |
5108 MAC_STATUS_MI_COMPLETION |
5109 MAC_STATUS_LNKSTATE_CHANGED));
5110 udelay(40);
5111 } else
5112 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005113 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 }
5115 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005116}
5117
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005118static int tg3_rx_prodring_xfer(struct tg3 *tp,
5119 struct tg3_rx_prodring_set *dpr,
5120 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005121{
5122 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005123 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005124
5125 while (1) {
5126 src_prod_idx = spr->rx_std_prod_idx;
5127
5128 /* Make sure updates to the rx_std_buffers[] entries and the
5129 * standard producer index are seen in the correct order.
5130 */
5131 smp_rmb();
5132
5133 if (spr->rx_std_cons_idx == src_prod_idx)
5134 break;
5135
5136 if (spr->rx_std_cons_idx < src_prod_idx)
5137 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5138 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005139 cpycnt = tp->rx_std_ring_mask + 1 -
5140 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005141
Matt Carlson2c49a442010-09-30 10:34:35 +00005142 cpycnt = min(cpycnt,
5143 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005144
5145 si = spr->rx_std_cons_idx;
5146 di = dpr->rx_std_prod_idx;
5147
Matt Carlsone92967b2010-02-12 14:47:06 +00005148 for (i = di; i < di + cpycnt; i++) {
5149 if (dpr->rx_std_buffers[i].skb) {
5150 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005151 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005152 break;
5153 }
5154 }
5155
5156 if (!cpycnt)
5157 break;
5158
5159 /* Ensure that updates to the rx_std_buffers ring and the
5160 * shadowed hardware producer ring from tg3_recycle_skb() are
5161 * ordered correctly WRT the skb check above.
5162 */
5163 smp_rmb();
5164
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005165 memcpy(&dpr->rx_std_buffers[di],
5166 &spr->rx_std_buffers[si],
5167 cpycnt * sizeof(struct ring_info));
5168
5169 for (i = 0; i < cpycnt; i++, di++, si++) {
5170 struct tg3_rx_buffer_desc *sbd, *dbd;
5171 sbd = &spr->rx_std[si];
5172 dbd = &dpr->rx_std[di];
5173 dbd->addr_hi = sbd->addr_hi;
5174 dbd->addr_lo = sbd->addr_lo;
5175 }
5176
Matt Carlson2c49a442010-09-30 10:34:35 +00005177 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5178 tp->rx_std_ring_mask;
5179 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5180 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005181 }
5182
5183 while (1) {
5184 src_prod_idx = spr->rx_jmb_prod_idx;
5185
5186 /* Make sure updates to the rx_jmb_buffers[] entries and
5187 * the jumbo producer index are seen in the correct order.
5188 */
5189 smp_rmb();
5190
5191 if (spr->rx_jmb_cons_idx == src_prod_idx)
5192 break;
5193
5194 if (spr->rx_jmb_cons_idx < src_prod_idx)
5195 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5196 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005197 cpycnt = tp->rx_jmb_ring_mask + 1 -
5198 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005199
5200 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005201 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005202
5203 si = spr->rx_jmb_cons_idx;
5204 di = dpr->rx_jmb_prod_idx;
5205
Matt Carlsone92967b2010-02-12 14:47:06 +00005206 for (i = di; i < di + cpycnt; i++) {
5207 if (dpr->rx_jmb_buffers[i].skb) {
5208 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005209 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005210 break;
5211 }
5212 }
5213
5214 if (!cpycnt)
5215 break;
5216
5217 /* Ensure that updates to the rx_jmb_buffers ring and the
5218 * shadowed hardware producer ring from tg3_recycle_skb() are
5219 * ordered correctly WRT the skb check above.
5220 */
5221 smp_rmb();
5222
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005223 memcpy(&dpr->rx_jmb_buffers[di],
5224 &spr->rx_jmb_buffers[si],
5225 cpycnt * sizeof(struct ring_info));
5226
5227 for (i = 0; i < cpycnt; i++, di++, si++) {
5228 struct tg3_rx_buffer_desc *sbd, *dbd;
5229 sbd = &spr->rx_jmb[si].std;
5230 dbd = &dpr->rx_jmb[di].std;
5231 dbd->addr_hi = sbd->addr_hi;
5232 dbd->addr_lo = sbd->addr_lo;
5233 }
5234
Matt Carlson2c49a442010-09-30 10:34:35 +00005235 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5236 tp->rx_jmb_ring_mask;
5237 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5238 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005239 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005240
5241 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005242}
5243
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005244static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5245{
5246 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
5248 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005249 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005250 tg3_tx(tnapi);
Joe Perches63c3a662011-04-26 08:12:10 +00005251 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005252 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 }
5254
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 /* run RX thread, within the bounds set by NAPI.
5256 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005257 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005259 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005260 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
Joe Perches63c3a662011-04-26 08:12:10 +00005262 if (tg3_flag(tp, ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005263 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005264 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005265 u32 std_prod_idx = dpr->rx_std_prod_idx;
5266 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005267
Matt Carlsone4af1af2010-02-12 14:47:05 +00005268 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005269 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005270 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005271
5272 wmb();
5273
Matt Carlsone4af1af2010-02-12 14:47:05 +00005274 if (std_prod_idx != dpr->rx_std_prod_idx)
5275 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5276 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005277
Matt Carlsone4af1af2010-02-12 14:47:05 +00005278 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5279 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5280 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005281
5282 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005283
5284 if (err)
5285 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005286 }
5287
David S. Miller6f535762007-10-11 18:08:29 -07005288 return work_done;
5289}
David S. Millerf7383c22005-05-18 22:50:53 -07005290
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005291static int tg3_poll_msix(struct napi_struct *napi, int budget)
5292{
5293 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5294 struct tg3 *tp = tnapi->tp;
5295 int work_done = 0;
5296 struct tg3_hw_status *sblk = tnapi->hw_status;
5297
5298 while (1) {
5299 work_done = tg3_poll_work(tnapi, work_done, budget);
5300
Joe Perches63c3a662011-04-26 08:12:10 +00005301 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005302 goto tx_recovery;
5303
5304 if (unlikely(work_done >= budget))
5305 break;
5306
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005307 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005308 * to tell the hw how much work has been processed,
5309 * so we must read it before checking for more work.
5310 */
5311 tnapi->last_tag = sblk->status_tag;
5312 tnapi->last_irq_tag = tnapi->last_tag;
5313 rmb();
5314
5315 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005316 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5317 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005318 napi_complete(napi);
5319 /* Reenable interrupts. */
5320 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5321 mmiowb();
5322 break;
5323 }
5324 }
5325
5326 return work_done;
5327
5328tx_recovery:
5329 /* work_done is guaranteed to be less than budget. */
5330 napi_complete(napi);
5331 schedule_work(&tp->reset_task);
5332 return work_done;
5333}
5334
Matt Carlsone64de4e2011-04-13 11:05:05 +00005335static void tg3_process_error(struct tg3 *tp)
5336{
5337 u32 val;
5338 bool real_error = false;
5339
Joe Perches63c3a662011-04-26 08:12:10 +00005340 if (tg3_flag(tp, ERROR_PROCESSED))
Matt Carlsone64de4e2011-04-13 11:05:05 +00005341 return;
5342
5343 /* Check Flow Attention register */
5344 val = tr32(HOSTCC_FLOW_ATTN);
5345 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5346 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5347 real_error = true;
5348 }
5349
5350 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5351 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5352 real_error = true;
5353 }
5354
5355 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5356 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5357 real_error = true;
5358 }
5359
5360 if (!real_error)
5361 return;
5362
5363 tg3_dump_state(tp);
5364
Joe Perches63c3a662011-04-26 08:12:10 +00005365 tg3_flag_set(tp, ERROR_PROCESSED);
Matt Carlsone64de4e2011-04-13 11:05:05 +00005366 schedule_work(&tp->reset_task);
5367}
5368
David S. Miller6f535762007-10-11 18:08:29 -07005369static int tg3_poll(struct napi_struct *napi, int budget)
5370{
Matt Carlson8ef04422009-08-28 14:01:37 +00005371 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5372 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005373 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005374 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005375
5376 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005377 if (sblk->status & SD_STATUS_ERROR)
5378 tg3_process_error(tp);
5379
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005380 tg3_poll_link(tp);
5381
Matt Carlson17375d22009-08-28 14:02:18 +00005382 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005383
Joe Perches63c3a662011-04-26 08:12:10 +00005384 if (unlikely(tg3_flag(tp, TX_RECOVERY_PENDING)))
David S. Miller6f535762007-10-11 18:08:29 -07005385 goto tx_recovery;
5386
5387 if (unlikely(work_done >= budget))
5388 break;
5389
Joe Perches63c3a662011-04-26 08:12:10 +00005390 if (tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson17375d22009-08-28 14:02:18 +00005391 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005392 * to tell the hw how much work has been processed,
5393 * so we must read it before checking for more work.
5394 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005395 tnapi->last_tag = sblk->status_tag;
5396 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005397 rmb();
5398 } else
5399 sblk->status &= ~SD_STATUS_UPDATED;
5400
Matt Carlson17375d22009-08-28 14:02:18 +00005401 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005402 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005403 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005404 break;
5405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 }
5407
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005408 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005409
5410tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005411 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005412 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005413 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005414 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415}
5416
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005417static void tg3_napi_disable(struct tg3 *tp)
5418{
5419 int i;
5420
5421 for (i = tp->irq_cnt - 1; i >= 0; i--)
5422 napi_disable(&tp->napi[i].napi);
5423}
5424
5425static void tg3_napi_enable(struct tg3 *tp)
5426{
5427 int i;
5428
5429 for (i = 0; i < tp->irq_cnt; i++)
5430 napi_enable(&tp->napi[i].napi);
5431}
5432
5433static void tg3_napi_init(struct tg3 *tp)
5434{
5435 int i;
5436
5437 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5438 for (i = 1; i < tp->irq_cnt; i++)
5439 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5440}
5441
5442static void tg3_napi_fini(struct tg3 *tp)
5443{
5444 int i;
5445
5446 for (i = 0; i < tp->irq_cnt; i++)
5447 netif_napi_del(&tp->napi[i].napi);
5448}
5449
5450static inline void tg3_netif_stop(struct tg3 *tp)
5451{
5452 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5453 tg3_napi_disable(tp);
5454 netif_tx_disable(tp->dev);
5455}
5456
5457static inline void tg3_netif_start(struct tg3 *tp)
5458{
5459 /* NOTE: unconditional netif_tx_wake_all_queues is only
5460 * appropriate so long as all callers are assured to
5461 * have free tx slots (such as after tg3_init_hw)
5462 */
5463 netif_tx_wake_all_queues(tp->dev);
5464
5465 tg3_napi_enable(tp);
5466 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5467 tg3_enable_ints(tp);
5468}
5469
David S. Millerf47c11e2005-06-24 20:18:35 -07005470static void tg3_irq_quiesce(struct tg3 *tp)
5471{
Matt Carlson4f125f42009-09-01 12:55:02 +00005472 int i;
5473
David S. Millerf47c11e2005-06-24 20:18:35 -07005474 BUG_ON(tp->irq_sync);
5475
5476 tp->irq_sync = 1;
5477 smp_mb();
5478
Matt Carlson4f125f42009-09-01 12:55:02 +00005479 for (i = 0; i < tp->irq_cnt; i++)
5480 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005481}
5482
David S. Millerf47c11e2005-06-24 20:18:35 -07005483/* Fully shutdown all tg3 driver activity elsewhere in the system.
5484 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5485 * with as well. Most of the time, this is not necessary except when
5486 * shutting down the device.
5487 */
5488static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5489{
Michael Chan46966542007-07-11 19:47:19 -07005490 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005491 if (irq_sync)
5492 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005493}
5494
5495static inline void tg3_full_unlock(struct tg3 *tp)
5496{
David S. Millerf47c11e2005-06-24 20:18:35 -07005497 spin_unlock_bh(&tp->lock);
5498}
5499
Michael Chanfcfa0a32006-03-20 22:28:41 -08005500/* One-shot MSI handler - Chip automatically disables interrupt
5501 * after sending MSI so driver doesn't have to do it.
5502 */
David Howells7d12e782006-10-05 14:55:46 +01005503static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005504{
Matt Carlson09943a12009-08-28 14:01:57 +00005505 struct tg3_napi *tnapi = dev_id;
5506 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005507
Matt Carlson898a56f2009-08-28 14:02:40 +00005508 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005509 if (tnapi->rx_rcb)
5510 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005511
5512 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005513 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005514
5515 return IRQ_HANDLED;
5516}
5517
Michael Chan88b06bc22005-04-21 17:13:25 -07005518/* MSI ISR - No need to check for interrupt sharing and no need to
5519 * flush status block and interrupt mailbox. PCI ordering rules
5520 * guarantee that MSI will arrive after the status block.
5521 */
David Howells7d12e782006-10-05 14:55:46 +01005522static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc22005-04-21 17:13:25 -07005523{
Matt Carlson09943a12009-08-28 14:01:57 +00005524 struct tg3_napi *tnapi = dev_id;
5525 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc22005-04-21 17:13:25 -07005526
Matt Carlson898a56f2009-08-28 14:02:40 +00005527 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005528 if (tnapi->rx_rcb)
5529 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc22005-04-21 17:13:25 -07005530 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005531 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc22005-04-21 17:13:25 -07005532 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005533 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc22005-04-21 17:13:25 -07005534 * NIC to stop sending us irqs, engaging "in-intr-handler"
5535 * event coalescing.
5536 */
5537 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005538 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005539 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005540
Michael Chan88b06bc22005-04-21 17:13:25 -07005541 return IRQ_RETVAL(1);
5542}
5543
David Howells7d12e782006-10-05 14:55:46 +01005544static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545{
Matt Carlson09943a12009-08-28 14:01:57 +00005546 struct tg3_napi *tnapi = dev_id;
5547 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005548 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 unsigned int handled = 1;
5550
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 /* In INTx mode, it is possible for the interrupt to arrive at
5552 * the CPU before the status block posted prior to the interrupt.
5553 * Reading the PCI State register will confirm whether the
5554 * interrupt is ours and will flush the status block.
5555 */
Michael Chand18edcb2007-03-24 20:57:11 -07005556 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
Joe Perches63c3a662011-04-26 08:12:10 +00005557 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005558 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5559 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005560 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005561 }
Michael Chand18edcb2007-03-24 20:57:11 -07005562 }
5563
5564 /*
5565 * Writing any value to intr-mbox-0 clears PCI INTA# and
5566 * chip-internal interrupt pending events.
5567 * Writing non-zero to intr-mbox-0 additional tells the
5568 * NIC to stop sending us irqs, engaging "in-intr-handler"
5569 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005570 *
5571 * Flush the mailbox to de-assert the IRQ immediately to prevent
5572 * spurious interrupts. The flush impacts performance but
5573 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005574 */
Michael Chanc04cb342007-05-07 00:26:15 -07005575 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005576 if (tg3_irq_sync(tp))
5577 goto out;
5578 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005579 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005580 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005581 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005582 } else {
5583 /* No work, shared interrupt perhaps? re-enable
5584 * interrupts, and flush that PCI write
5585 */
5586 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5587 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005588 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005589out:
David S. Millerfac9b832005-05-18 22:46:34 -07005590 return IRQ_RETVAL(handled);
5591}
5592
David Howells7d12e782006-10-05 14:55:46 +01005593static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005594{
Matt Carlson09943a12009-08-28 14:01:57 +00005595 struct tg3_napi *tnapi = dev_id;
5596 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005597 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005598 unsigned int handled = 1;
5599
David S. Millerfac9b832005-05-18 22:46:34 -07005600 /* In INTx mode, it is possible for the interrupt to arrive at
5601 * the CPU before the status block posted prior to the interrupt.
5602 * Reading the PCI State register will confirm whether the
5603 * interrupt is ours and will flush the status block.
5604 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005605 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Joe Perches63c3a662011-04-26 08:12:10 +00005606 if (tg3_flag(tp, CHIP_RESETTING) ||
Michael Chand18edcb2007-03-24 20:57:11 -07005607 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5608 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005609 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 }
Michael Chand18edcb2007-03-24 20:57:11 -07005611 }
5612
5613 /*
5614 * writing any value to intr-mbox-0 clears PCI INTA# and
5615 * chip-internal interrupt pending events.
5616 * writing non-zero to intr-mbox-0 additional tells the
5617 * NIC to stop sending us irqs, engaging "in-intr-handler"
5618 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005619 *
5620 * Flush the mailbox to de-assert the IRQ immediately to prevent
5621 * spurious interrupts. The flush impacts performance but
5622 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005623 */
Michael Chanc04cb342007-05-07 00:26:15 -07005624 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005625
5626 /*
5627 * In a shared interrupt configuration, sometimes other devices'
5628 * interrupts will scream. We record the current status tag here
5629 * so that the above check can report that the screaming interrupts
5630 * are unhandled. Eventually they will be silenced.
5631 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005632 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005633
Michael Chand18edcb2007-03-24 20:57:11 -07005634 if (tg3_irq_sync(tp))
5635 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005636
Matt Carlson72334482009-08-28 14:03:01 +00005637 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005638
Matt Carlson09943a12009-08-28 14:01:57 +00005639 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005640
David S. Millerf47c11e2005-06-24 20:18:35 -07005641out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 return IRQ_RETVAL(handled);
5643}
5644
Michael Chan79381092005-04-21 17:13:59 -07005645/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005646static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005647{
Matt Carlson09943a12009-08-28 14:01:57 +00005648 struct tg3_napi *tnapi = dev_id;
5649 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005650 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005651
Michael Chanf9804dd2005-09-27 12:13:10 -07005652 if ((sblk->status & SD_STATUS_UPDATED) ||
5653 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005654 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005655 return IRQ_RETVAL(1);
5656 }
5657 return IRQ_RETVAL(0);
5658}
5659
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005660static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005661static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662
Michael Chanb9ec6c12006-07-25 16:37:27 -07005663/* Restart hardware after configuration changes, self-test, etc.
5664 * Invoked with tp->lock held.
5665 */
5666static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005667 __releases(tp->lock)
5668 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005669{
5670 int err;
5671
5672 err = tg3_init_hw(tp, reset_phy);
5673 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005674 netdev_err(tp->dev,
5675 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005676 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5677 tg3_full_unlock(tp);
5678 del_timer_sync(&tp->timer);
5679 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005680 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005681 dev_close(tp->dev);
5682 tg3_full_lock(tp, 0);
5683 }
5684 return err;
5685}
5686
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687#ifdef CONFIG_NET_POLL_CONTROLLER
5688static void tg3_poll_controller(struct net_device *dev)
5689{
Matt Carlson4f125f42009-09-01 12:55:02 +00005690 int i;
Michael Chan88b06bc22005-04-21 17:13:25 -07005691 struct tg3 *tp = netdev_priv(dev);
5692
Matt Carlson4f125f42009-09-01 12:55:02 +00005693 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005694 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695}
5696#endif
5697
David Howellsc4028952006-11-22 14:57:56 +00005698static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699{
David Howellsc4028952006-11-22 14:57:56 +00005700 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005701 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 unsigned int restart_timer;
5703
Michael Chan7faa0062006-02-02 17:29:28 -08005704 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005705
5706 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005707 tg3_full_unlock(tp);
5708 return;
5709 }
5710
5711 tg3_full_unlock(tp);
5712
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005713 tg3_phy_stop(tp);
5714
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 tg3_netif_stop(tp);
5716
David S. Millerf47c11e2005-06-24 20:18:35 -07005717 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718
Joe Perches63c3a662011-04-26 08:12:10 +00005719 restart_timer = tg3_flag(tp, RESTART_TIMER);
5720 tg3_flag_clear(tp, RESTART_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721
Joe Perches63c3a662011-04-26 08:12:10 +00005722 if (tg3_flag(tp, TX_RECOVERY_PENDING)) {
Michael Chandf3e6542006-05-26 17:48:07 -07005723 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5724 tp->write32_rx_mbox = tg3_write_flush_reg32;
Joe Perches63c3a662011-04-26 08:12:10 +00005725 tg3_flag_set(tp, MBOX_WRITE_REORDER);
5726 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
Michael Chandf3e6542006-05-26 17:48:07 -07005727 }
5728
Michael Chan944d9802005-05-29 14:57:48 -07005729 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005730 err = tg3_init_hw(tp, 1);
5731 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005732 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733
5734 tg3_netif_start(tp);
5735
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 if (restart_timer)
5737 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005738
Michael Chanb9ec6c12006-07-25 16:37:27 -07005739out:
Michael Chan7faa0062006-02-02 17:29:28 -08005740 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005741
5742 if (!err)
5743 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744}
5745
5746static void tg3_tx_timeout(struct net_device *dev)
5747{
5748 struct tg3 *tp = netdev_priv(dev);
5749
Michael Chanb0408752007-02-13 12:18:30 -08005750 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005751 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005752 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754
5755 schedule_work(&tp->reset_task);
5756}
5757
Michael Chanc58ec932005-09-17 00:46:27 -07005758/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5759static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5760{
5761 u32 base = (u32) mapping & 0xffffffff;
5762
Eric Dumazet807540b2010-09-23 05:40:09 +00005763 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005764}
5765
Michael Chan72f2afb2006-03-06 19:28:35 -08005766/* Test for DMA addresses > 40-bit */
5767static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5768 int len)
5769{
5770#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Joe Perches63c3a662011-04-26 08:12:10 +00005771 if (tg3_flag(tp, 40BIT_DMA_BUG))
Eric Dumazet807540b2010-09-23 05:40:09 +00005772 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005773 return 0;
5774#else
5775 return 0;
5776#endif
5777}
5778
Matt Carlson2ffcc982011-05-19 12:12:44 +00005779static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
5780 dma_addr_t mapping, int len, u32 flags,
5781 u32 mss_and_is_end)
5782{
5783 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
5784 int is_end = (mss_and_is_end & 0x1);
5785 u32 mss = (mss_and_is_end >> 1);
5786 u32 vlan_tag = 0;
5787
5788 if (is_end)
5789 flags |= TXD_FLAG_END;
5790 if (flags & TXD_FLAG_VLAN) {
5791 vlan_tag = flags >> 16;
5792 flags &= 0xffff;
5793 }
5794 vlan_tag |= (mss << TXD_MSS_SHIFT);
5795
5796 txd->addr_hi = ((u64) mapping >> 32);
5797 txd->addr_lo = ((u64) mapping & 0xffffffff);
5798 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5799 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5800}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
Matt Carlson432aa7e2011-05-19 12:12:45 +00005802static void tg3_skb_error_unmap(struct tg3_napi *tnapi,
5803 struct sk_buff *skb, int last)
5804{
5805 int i;
5806 u32 entry = tnapi->tx_prod;
5807 struct ring_info *txb = &tnapi->tx_buffers[entry];
5808
5809 pci_unmap_single(tnapi->tp->pdev,
5810 dma_unmap_addr(txb, mapping),
5811 skb_headlen(skb),
5812 PCI_DMA_TODEVICE);
Matt Carlson9a2e0fb2011-06-02 13:01:39 +00005813 for (i = 0; i < last; i++) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005814 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5815
5816 entry = NEXT_TX(entry);
5817 txb = &tnapi->tx_buffers[entry];
5818
5819 pci_unmap_page(tnapi->tp->pdev,
5820 dma_unmap_addr(txb, mapping),
5821 frag->size, PCI_DMA_TODEVICE);
5822 }
5823}
5824
Michael Chan72f2afb2006-03-06 19:28:35 -08005825/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005826static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
Matt Carlson432aa7e2011-05-19 12:12:45 +00005827 struct sk_buff *skb,
5828 u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005830 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005831 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005832 dma_addr_t new_addr = 0;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005833 u32 entry = tnapi->tx_prod;
5834 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
Matt Carlson41588ba2008-04-19 18:12:33 -07005836 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5837 new_skb = skb_copy(skb, GFP_ATOMIC);
5838 else {
5839 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5840
5841 new_skb = skb_copy_expand(skb,
5842 skb_headroom(skb) + more_headroom,
5843 skb_tailroom(skb), GFP_ATOMIC);
5844 }
5845
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005847 ret = -1;
5848 } else {
5849 /* New SKB is guaranteed to be linear. */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005850 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5851 PCI_DMA_TODEVICE);
5852 /* Make sure the mapping succeeded */
5853 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5854 ret = -1;
5855 dev_kfree_skb(new_skb);
David S. Miller90079ce2008-09-11 04:52:51 -07005856
Michael Chanc58ec932005-09-17 00:46:27 -07005857 /* Make sure new skb does not cross any 4G boundaries.
5858 * Drop the packet if it does.
5859 */
Matt Carlsoneb69d562011-06-13 13:38:57 +00005860 } else if (tg3_4g_overflow_test(new_addr, new_skb->len)) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005861 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5862 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005863 ret = -1;
5864 dev_kfree_skb(new_skb);
Michael Chanc58ec932005-09-17 00:46:27 -07005865 } else {
Matt Carlson432aa7e2011-05-19 12:12:45 +00005866 tnapi->tx_buffers[entry].skb = new_skb;
5867 dma_unmap_addr_set(&tnapi->tx_buffers[entry],
5868 mapping, new_addr);
5869
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005870 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005871 base_flags, 1 | (mss << 1));
Michael Chanc58ec932005-09-17 00:46:27 -07005872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 }
5874
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 dev_kfree_skb(skb);
5876
Michael Chanc58ec932005-09-17 00:46:27 -07005877 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878}
5879
Matt Carlson2ffcc982011-05-19 12:12:44 +00005880static netdev_tx_t tg3_start_xmit(struct sk_buff *, struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005881
5882/* Use GSO to workaround a rare TSO bug that may be triggered when the
5883 * TSO header is greater than 80 bytes.
5884 */
5885static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5886{
5887 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005888 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005889
5890 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005891 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005892 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005893
5894 /* netif_tx_stop_queue() must be done before checking
5895 * checking tx index in tg3_tx_avail() below, because in
5896 * tg3_tx(), we update tx index before checking for
5897 * netif_tx_queue_stopped().
5898 */
5899 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005900 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08005901 return NETDEV_TX_BUSY;
5902
5903 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005904 }
5905
5906 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07005907 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07005908 goto tg3_tso_bug_end;
5909
5910 do {
5911 nskb = segs;
5912 segs = segs->next;
5913 nskb->next = NULL;
Matt Carlson2ffcc982011-05-19 12:12:44 +00005914 tg3_start_xmit(nskb, tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07005915 } while (segs);
5916
5917tg3_tso_bug_end:
5918 dev_kfree_skb(skb);
5919
5920 return NETDEV_TX_OK;
5921}
Michael Chan52c0fd82006-06-29 20:15:54 -07005922
Michael Chan5a6f3072006-03-20 22:28:05 -08005923/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
Joe Perches63c3a662011-04-26 08:12:10 +00005924 * support TG3_FLAG_HW_TSO_1 or firmware TSO only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005925 */
Matt Carlson2ffcc982011-05-19 12:12:44 +00005926static netdev_tx_t tg3_start_xmit(struct sk_buff *skb, struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08005927{
5928 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08005929 u32 len, entry, base_flags, mss;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005930 int i = -1, would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07005931 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005932 struct tg3_napi *tnapi;
5933 struct netdev_queue *txq;
Matt Carlson432aa7e2011-05-19 12:12:45 +00005934 unsigned int last;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005935
Matt Carlson24f4efd2009-11-13 13:03:35 +00005936 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5937 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Joe Perches63c3a662011-04-26 08:12:10 +00005938 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlson24f4efd2009-11-13 13:03:35 +00005939 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
Michael Chan00b70502006-06-17 21:58:45 -07005941 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005942 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07005943 * interrupt. Furthermore, IRQ processing runs lockless so we have
5944 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005946 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00005947 if (!netif_tx_queue_stopped(txq)) {
5948 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005949
5950 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005951 netdev_err(dev,
5952 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08005953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 return NETDEV_TX_BUSY;
5955 }
5956
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005957 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07005959 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00005961
Matt Carlsonbe98da62010-07-11 09:31:46 +00005962 mss = skb_shinfo(skb)->gso_size;
5963 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005964 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00005965 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966
5967 if (skb_header_cloned(skb) &&
5968 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5969 dev_kfree_skb(skb);
5970 goto out_unlock;
5971 }
5972
Matt Carlson34195c32010-07-11 09:31:42 +00005973 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005974 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975
Matt Carlson02e96082010-09-15 08:59:59 +00005976 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00005977 hdr_len = skb_headlen(skb) - ETH_HLEN;
5978 } else {
5979 u32 ip_tcp_len;
5980
5981 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
5982 hdr_len = ip_tcp_len + tcp_opt_len;
5983
5984 iph->check = 0;
5985 iph->tot_len = htons(mss + hdr_len);
5986 }
5987
Michael Chan52c0fd82006-06-29 20:15:54 -07005988 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Joe Perches63c3a662011-04-26 08:12:10 +00005989 tg3_flag(tp, TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00005990 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07005991
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5993 TXD_FLAG_CPU_POST_DMA);
5994
Joe Perches63c3a662011-04-26 08:12:10 +00005995 if (tg3_flag(tp, HW_TSO_1) ||
5996 tg3_flag(tp, HW_TSO_2) ||
5997 tg3_flag(tp, HW_TSO_3)) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005998 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006000 } else
6001 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6002 iph->daddr, 0,
6003 IPPROTO_TCP,
6004 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
Joe Perches63c3a662011-04-26 08:12:10 +00006006 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlson615774f2009-11-13 13:03:39 +00006007 mss |= (hdr_len & 0xc) << 12;
6008 if (hdr_len & 0x10)
6009 base_flags |= 0x00000010;
6010 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +00006011 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006012 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +00006013 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006014 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006015 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 int tsflags;
6017
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006018 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 mss |= (tsflags << 11);
6020 }
6021 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006022 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 int tsflags;
6024
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006025 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 base_flags |= tsflags << 12;
6027 }
6028 }
6029 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006030
Jesse Grosseab6d182010-10-20 13:56:03 +00006031 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 base_flags |= (TXD_FLAG_VLAN |
6033 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
Joe Perches63c3a662011-04-26 08:12:10 +00006035 if (tg3_flag(tp, USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006036 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006037 base_flags |= TXD_FLAG_JMB_PKT;
6038
Alexander Duyckf4188d82009-12-02 16:48:38 +00006039 len = skb_headlen(skb);
6040
6041 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6042 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006043 dev_kfree_skb(skb);
6044 goto out_unlock;
6045 }
6046
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006047 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006048 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049
6050 would_hit_hwbug = 0;
6051
Joe Perches63c3a662011-04-26 08:12:10 +00006052 if (tg3_flag(tp, SHORT_DMA_BUG) && len <= 8)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006053 would_hit_hwbug = 1;
6054
Matt Carlsoneb69d562011-06-13 13:38:57 +00006055 if (tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006056 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006057
Matt Carlsondaf9a552011-06-13 13:38:56 +00006058 if (tg3_40bit_overflow_test(tp, mapping, len))
Matt Carlson0e1406d2009-11-02 12:33:33 +00006059 would_hit_hwbug = 1;
6060
Joe Perches63c3a662011-04-26 08:12:10 +00006061 if (tg3_flag(tp, 5701_DMA_BUG))
Michael Chanc58ec932005-09-17 00:46:27 -07006062 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006064 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6066
6067 entry = NEXT_TX(entry);
6068
6069 /* Now loop through additional data fragments, and queue them. */
6070 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 last = skb_shinfo(skb)->nr_frags - 1;
6072 for (i = 0; i <= last; i++) {
6073 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6074
6075 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006076 mapping = pci_map_page(tp->pdev,
6077 frag->page,
6078 frag->page_offset,
6079 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006081 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006082 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006083 mapping);
6084 if (pci_dma_mapping_error(tp->pdev, mapping))
6085 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
Joe Perches63c3a662011-04-26 08:12:10 +00006087 if (tg3_flag(tp, SHORT_DMA_BUG) &&
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006088 len <= 8)
6089 would_hit_hwbug = 1;
6090
Matt Carlsoneb69d562011-06-13 13:38:57 +00006091 if (tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006092 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
Matt Carlsondaf9a552011-06-13 13:38:56 +00006094 if (tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006095 would_hit_hwbug = 1;
6096
Joe Perches63c3a662011-04-26 08:12:10 +00006097 if (tg3_flag(tp, HW_TSO_1) ||
6098 tg3_flag(tp, HW_TSO_2) ||
6099 tg3_flag(tp, HW_TSO_3))
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006100 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 base_flags, (i == last)|(mss << 1));
6102 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006103 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 base_flags, (i == last));
6105
6106 entry = NEXT_TX(entry);
6107 }
6108 }
6109
6110 if (would_hit_hwbug) {
Matt Carlson432aa7e2011-05-19 12:12:45 +00006111 tg3_skb_error_unmap(tnapi, skb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112
6113 /* If the workaround fails due to memory/mapping
6114 * failure, silently drop this packet.
6115 */
Matt Carlson432aa7e2011-05-19 12:12:45 +00006116 if (tigon3_dma_hwbug_workaround(tnapi, skb, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 goto out_unlock;
6118
Matt Carlson432aa7e2011-05-19 12:12:45 +00006119 entry = NEXT_TX(tnapi->tx_prod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 }
6121
Richard Cochrand515b452011-06-19 03:31:41 +00006122 skb_tx_timestamp(skb);
6123
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006125 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006127 tnapi->tx_prod = entry;
6128 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006129 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006130
6131 /* netif_tx_stop_queue() must be done before checking
6132 * checking tx index in tg3_tx_avail() below, because in
6133 * tg3_tx(), we update tx index before checking for
6134 * netif_tx_queue_stopped().
6135 */
6136 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006137 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006138 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140
6141out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006142 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143
6144 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006145
6146dma_error:
Matt Carlson432aa7e2011-05-19 12:12:45 +00006147 tg3_skb_error_unmap(tnapi, skb, i);
Alexander Duyckf4188d82009-12-02 16:48:38 +00006148 dev_kfree_skb(skb);
Matt Carlson432aa7e2011-05-19 12:12:45 +00006149 tnapi->tx_buffers[tnapi->tx_prod].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006150 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151}
6152
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006153static void tg3_set_loopback(struct net_device *dev, u32 features)
6154{
6155 struct tg3 *tp = netdev_priv(dev);
6156
6157 if (features & NETIF_F_LOOPBACK) {
6158 if (tp->mac_mode & MAC_MODE_PORT_INT_LPBACK)
6159 return;
6160
6161 /*
6162 * Clear MAC_MODE_HALF_DUPLEX or you won't get packets back in
6163 * loopback mode if Half-Duplex mode was negotiated earlier.
6164 */
6165 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
6166
6167 /* Enable internal MAC loopback mode */
6168 tp->mac_mode |= MAC_MODE_PORT_INT_LPBACK;
6169 spin_lock_bh(&tp->lock);
6170 tw32(MAC_MODE, tp->mac_mode);
6171 netif_carrier_on(tp->dev);
6172 spin_unlock_bh(&tp->lock);
6173 netdev_info(dev, "Internal MAC loopback mode enabled.\n");
6174 } else {
6175 if (!(tp->mac_mode & MAC_MODE_PORT_INT_LPBACK))
6176 return;
6177
6178 /* Disable internal MAC loopback mode */
6179 tp->mac_mode &= ~MAC_MODE_PORT_INT_LPBACK;
6180 spin_lock_bh(&tp->lock);
6181 tw32(MAC_MODE, tp->mac_mode);
6182 /* Force link status check */
6183 tg3_setup_phy(tp, 1);
6184 spin_unlock_bh(&tp->lock);
6185 netdev_info(dev, "Internal MAC loopback mode disabled.\n");
6186 }
6187}
6188
Michał Mirosławdc668912011-04-07 03:35:07 +00006189static u32 tg3_fix_features(struct net_device *dev, u32 features)
6190{
6191 struct tg3 *tp = netdev_priv(dev);
6192
Joe Perches63c3a662011-04-26 08:12:10 +00006193 if (dev->mtu > ETH_DATA_LEN && tg3_flag(tp, 5780_CLASS))
Michał Mirosławdc668912011-04-07 03:35:07 +00006194 features &= ~NETIF_F_ALL_TSO;
6195
6196 return features;
6197}
6198
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00006199static int tg3_set_features(struct net_device *dev, u32 features)
6200{
6201 u32 changed = dev->features ^ features;
6202
6203 if ((changed & NETIF_F_LOOPBACK) && netif_running(dev))
6204 tg3_set_loopback(dev, features);
6205
6206 return 0;
6207}
6208
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6210 int new_mtu)
6211{
6212 dev->mtu = new_mtu;
6213
Michael Chanef7f5ec2005-07-25 12:32:25 -07006214 if (new_mtu > ETH_DATA_LEN) {
Joe Perches63c3a662011-04-26 08:12:10 +00006215 if (tg3_flag(tp, 5780_CLASS)) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006216 netdev_update_features(dev);
Joe Perches63c3a662011-04-26 08:12:10 +00006217 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00006218 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006219 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Matt Carlson859a588792010-04-05 10:19:28 +00006220 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006221 } else {
Joe Perches63c3a662011-04-26 08:12:10 +00006222 if (tg3_flag(tp, 5780_CLASS)) {
6223 tg3_flag_set(tp, TSO_CAPABLE);
Michał Mirosławdc668912011-04-07 03:35:07 +00006224 netdev_update_features(dev);
6225 }
Joe Perches63c3a662011-04-26 08:12:10 +00006226 tg3_flag_clear(tp, JUMBO_RING_ENABLE);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228}
6229
6230static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6231{
6232 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006233 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234
6235 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6236 return -EINVAL;
6237
6238 if (!netif_running(dev)) {
6239 /* We'll just catch it later when the
6240 * device is up'd.
6241 */
6242 tg3_set_mtu(dev, tp, new_mtu);
6243 return 0;
6244 }
6245
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006246 tg3_phy_stop(tp);
6247
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006249
6250 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251
Michael Chan944d9802005-05-29 14:57:48 -07006252 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253
6254 tg3_set_mtu(dev, tp, new_mtu);
6255
Michael Chanb9ec6c12006-07-25 16:37:27 -07006256 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257
Michael Chanb9ec6c12006-07-25 16:37:27 -07006258 if (!err)
6259 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260
David S. Millerf47c11e2005-06-24 20:18:35 -07006261 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006263 if (!err)
6264 tg3_phy_start(tp);
6265
Michael Chanb9ec6c12006-07-25 16:37:27 -07006266 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267}
6268
Matt Carlson21f581a2009-08-28 14:00:25 +00006269static void tg3_rx_prodring_free(struct tg3 *tp,
6270 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 int i;
6273
Matt Carlson8fea32b2010-09-15 08:59:58 +00006274 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006275 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006276 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006277 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6278 tp->rx_pkt_map_sz);
6279
Joe Perches63c3a662011-04-26 08:12:10 +00006280 if (tg3_flag(tp, JUMBO_CAPABLE)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006281 for (i = tpr->rx_jmb_cons_idx;
6282 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006283 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006284 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6285 TG3_RX_JMB_MAP_SZ);
6286 }
6287 }
6288
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006289 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291
Matt Carlson2c49a442010-09-30 10:34:35 +00006292 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006293 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6294 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295
Joe Perches63c3a662011-04-26 08:12:10 +00006296 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006297 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006298 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6299 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 }
6301}
6302
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006303/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 *
6305 * The chip has been shut down and the driver detached from
6306 * the networking, so no interrupts or new tx packets will
6307 * end up in the driver. tp->{tx,}lock are held and thus
6308 * we may not sleep.
6309 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006310static int tg3_rx_prodring_alloc(struct tg3 *tp,
6311 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312{
Matt Carlson287be122009-08-28 13:58:46 +00006313 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006315 tpr->rx_std_cons_idx = 0;
6316 tpr->rx_std_prod_idx = 0;
6317 tpr->rx_jmb_cons_idx = 0;
6318 tpr->rx_jmb_prod_idx = 0;
6319
Matt Carlson8fea32b2010-09-15 08:59:58 +00006320 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006321 memset(&tpr->rx_std_buffers[0], 0,
6322 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006323 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006324 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006325 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006326 goto done;
6327 }
6328
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006330 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331
Matt Carlson287be122009-08-28 13:58:46 +00006332 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00006333 if (tg3_flag(tp, 5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006334 tp->dev->mtu > ETH_DATA_LEN)
6335 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6336 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006337
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 /* Initialize invariants of the rings, we only set this
6339 * stuff once. This works because the card does not
6340 * write into the rx buffer posting rings.
6341 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006342 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 struct tg3_rx_buffer_desc *rxd;
6344
Matt Carlson21f581a2009-08-28 14:00:25 +00006345 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006346 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6348 rxd->opaque = (RXD_OPAQUE_RING_STD |
6349 (i << RXD_OPAQUE_INDEX_SHIFT));
6350 }
6351
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006352 /* Now allocate fresh SKBs for each rx ring. */
6353 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006354 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006355 netdev_warn(tp->dev,
6356 "Using a smaller RX standard ring. Only "
6357 "%d out of %d buffers were allocated "
6358 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006359 if (i == 0)
6360 goto initfail;
6361 tp->rx_pending = i;
6362 break;
6363 }
6364 }
6365
Joe Perches63c3a662011-04-26 08:12:10 +00006366 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006367 goto done;
6368
Matt Carlson2c49a442010-09-30 10:34:35 +00006369 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006370
Joe Perches63c3a662011-04-26 08:12:10 +00006371 if (!tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson0d86df82010-02-17 15:17:00 +00006372 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373
Matt Carlson2c49a442010-09-30 10:34:35 +00006374 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006375 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376
Matt Carlson0d86df82010-02-17 15:17:00 +00006377 rxd = &tpr->rx_jmb[i].std;
6378 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6379 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6380 RXD_FLAG_JUMBO;
6381 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6382 (i << RXD_OPAQUE_INDEX_SHIFT));
6383 }
6384
6385 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6386 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006387 netdev_warn(tp->dev,
6388 "Using a smaller RX jumbo ring. Only %d "
6389 "out of %d buffers were allocated "
6390 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006391 if (i == 0)
6392 goto initfail;
6393 tp->rx_jumbo_pending = i;
6394 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 }
6396 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006397
6398done:
Michael Chan32d8c572006-07-25 16:38:29 -07006399 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006400
6401initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006402 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006403 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404}
6405
Matt Carlson21f581a2009-08-28 14:00:25 +00006406static void tg3_rx_prodring_fini(struct tg3 *tp,
6407 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408{
Matt Carlson21f581a2009-08-28 14:00:25 +00006409 kfree(tpr->rx_std_buffers);
6410 tpr->rx_std_buffers = NULL;
6411 kfree(tpr->rx_jmb_buffers);
6412 tpr->rx_jmb_buffers = NULL;
6413 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006414 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6415 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006416 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006418 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006419 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6420 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006421 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006423}
6424
Matt Carlson21f581a2009-08-28 14:00:25 +00006425static int tg3_rx_prodring_init(struct tg3 *tp,
6426 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006427{
Matt Carlson2c49a442010-09-30 10:34:35 +00006428 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6429 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006430 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006431 return -ENOMEM;
6432
Matt Carlson4bae65c2010-11-24 08:31:52 +00006433 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6434 TG3_RX_STD_RING_BYTES(tp),
6435 &tpr->rx_std_mapping,
6436 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006437 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006438 goto err_out;
6439
Joe Perches63c3a662011-04-26 08:12:10 +00006440 if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006441 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006442 GFP_KERNEL);
6443 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006444 goto err_out;
6445
Matt Carlson4bae65c2010-11-24 08:31:52 +00006446 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6447 TG3_RX_JMB_RING_BYTES(tp),
6448 &tpr->rx_jmb_mapping,
6449 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006450 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006451 goto err_out;
6452 }
6453
6454 return 0;
6455
6456err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006457 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006458 return -ENOMEM;
6459}
6460
6461/* Free up pending packets in all rx/tx rings.
6462 *
6463 * The chip has been shut down and the driver detached from
6464 * the networking, so no interrupts or new tx packets will
6465 * end up in the driver. tp->{tx,}lock is not held and we are not
6466 * in an interrupt context and thus may sleep.
6467 */
6468static void tg3_free_rings(struct tg3 *tp)
6469{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006470 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006471
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006472 for (j = 0; j < tp->irq_cnt; j++) {
6473 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006474
Matt Carlson8fea32b2010-09-15 08:59:58 +00006475 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006476
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006477 if (!tnapi->tx_buffers)
6478 continue;
6479
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006480 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006481 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006482 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006483 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006484
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006485 txp = &tnapi->tx_buffers[i];
6486 skb = txp->skb;
6487
6488 if (skb == NULL) {
6489 i++;
6490 continue;
6491 }
6492
Alexander Duyckf4188d82009-12-02 16:48:38 +00006493 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006494 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006495 skb_headlen(skb),
6496 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006497 txp->skb = NULL;
6498
Alexander Duyckf4188d82009-12-02 16:48:38 +00006499 i++;
6500
6501 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6502 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6503 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006504 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006505 skb_shinfo(skb)->frags[k].size,
6506 PCI_DMA_TODEVICE);
6507 i++;
6508 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006509
6510 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006511 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006512 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006513}
6514
6515/* Initialize tx/rx rings for packet processing.
6516 *
6517 * The chip has been shut down and the driver detached from
6518 * the networking, so no interrupts or new tx packets will
6519 * end up in the driver. tp->{tx,}lock are held and thus
6520 * we may not sleep.
6521 */
6522static int tg3_init_rings(struct tg3 *tp)
6523{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006524 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006525
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006526 /* Free up all the SKBs. */
6527 tg3_free_rings(tp);
6528
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006529 for (i = 0; i < tp->irq_cnt; i++) {
6530 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006531
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006532 tnapi->last_tag = 0;
6533 tnapi->last_irq_tag = 0;
6534 tnapi->hw_status->status = 0;
6535 tnapi->hw_status->status_tag = 0;
6536 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6537
6538 tnapi->tx_prod = 0;
6539 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006540 if (tnapi->tx_ring)
6541 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006542
6543 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006544 if (tnapi->rx_rcb)
6545 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006546
Matt Carlson8fea32b2010-09-15 08:59:58 +00006547 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006548 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006549 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006550 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006551 }
Matt Carlson72334482009-08-28 14:03:01 +00006552
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006553 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006554}
6555
6556/*
6557 * Must not be invoked with interrupt sources disabled and
6558 * the hardware shutdown down.
6559 */
6560static void tg3_free_consistent(struct tg3 *tp)
6561{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006562 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006563
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006564 for (i = 0; i < tp->irq_cnt; i++) {
6565 struct tg3_napi *tnapi = &tp->napi[i];
6566
6567 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006568 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006569 tnapi->tx_ring, tnapi->tx_desc_mapping);
6570 tnapi->tx_ring = NULL;
6571 }
6572
6573 kfree(tnapi->tx_buffers);
6574 tnapi->tx_buffers = NULL;
6575
6576 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006577 dma_free_coherent(&tp->pdev->dev,
6578 TG3_RX_RCB_RING_BYTES(tp),
6579 tnapi->rx_rcb,
6580 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006581 tnapi->rx_rcb = NULL;
6582 }
6583
Matt Carlson8fea32b2010-09-15 08:59:58 +00006584 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6585
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006586 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006587 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6588 tnapi->hw_status,
6589 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006590 tnapi->hw_status = NULL;
6591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006593
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006595 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6596 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 tp->hw_stats = NULL;
6598 }
6599}
6600
6601/*
6602 * Must not be invoked with interrupt sources disabled and
6603 * the hardware shutdown down. Can sleep.
6604 */
6605static int tg3_alloc_consistent(struct tg3 *tp)
6606{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006607 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006608
Matt Carlson4bae65c2010-11-24 08:31:52 +00006609 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6610 sizeof(struct tg3_hw_stats),
6611 &tp->stats_mapping,
6612 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 if (!tp->hw_stats)
6614 goto err_out;
6615
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6617
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006618 for (i = 0; i < tp->irq_cnt; i++) {
6619 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006620 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006621
Matt Carlson4bae65c2010-11-24 08:31:52 +00006622 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6623 TG3_HW_STATUS_SIZE,
6624 &tnapi->status_mapping,
6625 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006626 if (!tnapi->hw_status)
6627 goto err_out;
6628
6629 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006630 sblk = tnapi->hw_status;
6631
Matt Carlson8fea32b2010-09-15 08:59:58 +00006632 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6633 goto err_out;
6634
Matt Carlson19cfaec2009-12-03 08:36:20 +00006635 /* If multivector TSS is enabled, vector 0 does not handle
6636 * tx interrupts. Don't allocate any resources for it.
6637 */
Joe Perches63c3a662011-04-26 08:12:10 +00006638 if ((!i && !tg3_flag(tp, ENABLE_TSS)) ||
6639 (i && tg3_flag(tp, ENABLE_TSS))) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00006640 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6641 TG3_TX_RING_SIZE,
6642 GFP_KERNEL);
6643 if (!tnapi->tx_buffers)
6644 goto err_out;
6645
Matt Carlson4bae65c2010-11-24 08:31:52 +00006646 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6647 TG3_TX_RING_BYTES,
6648 &tnapi->tx_desc_mapping,
6649 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006650 if (!tnapi->tx_ring)
6651 goto err_out;
6652 }
6653
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006654 /*
6655 * When RSS is enabled, the status block format changes
6656 * slightly. The "rx_jumbo_consumer", "reserved",
6657 * and "rx_mini_consumer" members get mapped to the
6658 * other three rx return ring producer indexes.
6659 */
6660 switch (i) {
6661 default:
6662 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6663 break;
6664 case 2:
6665 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6666 break;
6667 case 3:
6668 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6669 break;
6670 case 4:
6671 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6672 break;
6673 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006674
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006675 /*
6676 * If multivector RSS is enabled, vector 0 does not handle
6677 * rx or tx interrupts. Don't allocate any resources for it.
6678 */
Joe Perches63c3a662011-04-26 08:12:10 +00006679 if (!i && tg3_flag(tp, ENABLE_RSS))
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006680 continue;
6681
Matt Carlson4bae65c2010-11-24 08:31:52 +00006682 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6683 TG3_RX_RCB_RING_BYTES(tp),
6684 &tnapi->rx_rcb_mapping,
6685 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006686 if (!tnapi->rx_rcb)
6687 goto err_out;
6688
6689 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006690 }
6691
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692 return 0;
6693
6694err_out:
6695 tg3_free_consistent(tp);
6696 return -ENOMEM;
6697}
6698
6699#define MAX_WAIT_CNT 1000
6700
6701/* To stop a block, clear the enable bit and poll till it
6702 * clears. tp->lock is held.
6703 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006704static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705{
6706 unsigned int i;
6707 u32 val;
6708
Joe Perches63c3a662011-04-26 08:12:10 +00006709 if (tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 switch (ofs) {
6711 case RCVLSC_MODE:
6712 case DMAC_MODE:
6713 case MBFREE_MODE:
6714 case BUFMGR_MODE:
6715 case MEMARB_MODE:
6716 /* We can't enable/disable these bits of the
6717 * 5705/5750, just say success.
6718 */
6719 return 0;
6720
6721 default:
6722 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 }
6725
6726 val = tr32(ofs);
6727 val &= ~enable_bit;
6728 tw32_f(ofs, val);
6729
6730 for (i = 0; i < MAX_WAIT_CNT; i++) {
6731 udelay(100);
6732 val = tr32(ofs);
6733 if ((val & enable_bit) == 0)
6734 break;
6735 }
6736
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006737 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006738 dev_err(&tp->pdev->dev,
6739 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6740 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741 return -ENODEV;
6742 }
6743
6744 return 0;
6745}
6746
6747/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006748static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749{
6750 int i, err;
6751
6752 tg3_disable_ints(tp);
6753
6754 tp->rx_mode &= ~RX_MODE_ENABLE;
6755 tw32_f(MAC_RX_MODE, tp->rx_mode);
6756 udelay(10);
6757
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006758 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6759 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6760 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6761 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6762 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6763 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006765 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6766 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6767 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6768 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6769 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6770 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6771 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772
6773 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6774 tw32_f(MAC_MODE, tp->mac_mode);
6775 udelay(40);
6776
6777 tp->tx_mode &= ~TX_MODE_ENABLE;
6778 tw32_f(MAC_TX_MODE, tp->tx_mode);
6779
6780 for (i = 0; i < MAX_WAIT_CNT; i++) {
6781 udelay(100);
6782 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6783 break;
6784 }
6785 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006786 dev_err(&tp->pdev->dev,
6787 "%s timed out, TX_MODE_ENABLE will not clear "
6788 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006789 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 }
6791
Michael Chane6de8ad2005-05-05 14:42:41 -07006792 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006793 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6794 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795
6796 tw32(FTQ_RESET, 0xffffffff);
6797 tw32(FTQ_RESET, 0x00000000);
6798
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006799 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6800 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006802 for (i = 0; i < tp->irq_cnt; i++) {
6803 struct tg3_napi *tnapi = &tp->napi[i];
6804 if (tnapi->hw_status)
6805 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 if (tp->hw_stats)
6808 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6809
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 return err;
6811}
6812
Matt Carlson0d3031d2007-10-10 18:02:43 -07006813static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6814{
6815 int i;
6816 u32 apedata;
6817
Matt Carlsondc6d0742010-09-15 08:59:55 +00006818 /* NCSI does not support APE events */
Joe Perches63c3a662011-04-26 08:12:10 +00006819 if (tg3_flag(tp, APE_HAS_NCSI))
Matt Carlsondc6d0742010-09-15 08:59:55 +00006820 return;
6821
Matt Carlson0d3031d2007-10-10 18:02:43 -07006822 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6823 if (apedata != APE_SEG_SIG_MAGIC)
6824 return;
6825
6826 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006827 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006828 return;
6829
6830 /* Wait for up to 1 millisecond for APE to service previous event. */
6831 for (i = 0; i < 10; i++) {
6832 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6833 return;
6834
6835 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6836
6837 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6838 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6839 event | APE_EVENT_STATUS_EVENT_PENDING);
6840
6841 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6842
6843 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6844 break;
6845
6846 udelay(100);
6847 }
6848
6849 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6850 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6851}
6852
6853static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6854{
6855 u32 event;
6856 u32 apedata;
6857
Joe Perches63c3a662011-04-26 08:12:10 +00006858 if (!tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006859 return;
6860
6861 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006862 case RESET_KIND_INIT:
6863 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6864 APE_HOST_SEG_SIG_MAGIC);
6865 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6866 APE_HOST_SEG_LEN_MAGIC);
6867 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6868 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6869 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006870 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006871 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6872 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006873 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6874 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006875
Matt Carlson33f401a2010-04-05 10:19:27 +00006876 event = APE_EVENT_STATUS_STATE_START;
6877 break;
6878 case RESET_KIND_SHUTDOWN:
6879 /* With the interface we are currently using,
6880 * APE does not track driver state. Wiping
6881 * out the HOST SEGMENT SIGNATURE forces
6882 * the APE to assume OS absent status.
6883 */
6884 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006885
Matt Carlsondc6d0742010-09-15 08:59:55 +00006886 if (device_may_wakeup(&tp->pdev->dev) &&
Joe Perches63c3a662011-04-26 08:12:10 +00006887 tg3_flag(tp, WOL_ENABLE)) {
Matt Carlsondc6d0742010-09-15 08:59:55 +00006888 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6889 TG3_APE_HOST_WOL_SPEED_AUTO);
6890 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6891 } else
6892 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6893
6894 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6895
Matt Carlson33f401a2010-04-05 10:19:27 +00006896 event = APE_EVENT_STATUS_STATE_UNLOAD;
6897 break;
6898 case RESET_KIND_SUSPEND:
6899 event = APE_EVENT_STATUS_STATE_SUSPEND;
6900 break;
6901 default:
6902 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006903 }
6904
6905 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6906
6907 tg3_ape_send_event(tp, event);
6908}
6909
Michael Chane6af3012005-04-21 17:12:05 -07006910/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6912{
David S. Millerf49639e2006-06-09 11:58:36 -07006913 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6914 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915
Joe Perches63c3a662011-04-26 08:12:10 +00006916 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917 switch (kind) {
6918 case RESET_KIND_INIT:
6919 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6920 DRV_STATE_START);
6921 break;
6922
6923 case RESET_KIND_SHUTDOWN:
6924 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6925 DRV_STATE_UNLOAD);
6926 break;
6927
6928 case RESET_KIND_SUSPEND:
6929 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6930 DRV_STATE_SUSPEND);
6931 break;
6932
6933 default:
6934 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006937
6938 if (kind == RESET_KIND_INIT ||
6939 kind == RESET_KIND_SUSPEND)
6940 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941}
6942
6943/* tp->lock is held. */
6944static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
6945{
Joe Perches63c3a662011-04-26 08:12:10 +00006946 if (tg3_flag(tp, ASF_NEW_HANDSHAKE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 switch (kind) {
6948 case RESET_KIND_INIT:
6949 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6950 DRV_STATE_START_DONE);
6951 break;
6952
6953 case RESET_KIND_SHUTDOWN:
6954 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6955 DRV_STATE_UNLOAD_DONE);
6956 break;
6957
6958 default:
6959 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07006962
6963 if (kind == RESET_KIND_SHUTDOWN)
6964 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965}
6966
6967/* tp->lock is held. */
6968static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
6969{
Joe Perches63c3a662011-04-26 08:12:10 +00006970 if (tg3_flag(tp, ENABLE_ASF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 switch (kind) {
6972 case RESET_KIND_INIT:
6973 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6974 DRV_STATE_START);
6975 break;
6976
6977 case RESET_KIND_SHUTDOWN:
6978 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6979 DRV_STATE_UNLOAD);
6980 break;
6981
6982 case RESET_KIND_SUSPEND:
6983 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
6984 DRV_STATE_SUSPEND);
6985 break;
6986
6987 default:
6988 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 }
6991}
6992
Michael Chan7a6f4362006-09-27 16:03:31 -07006993static int tg3_poll_fw(struct tg3 *tp)
6994{
6995 int i;
6996 u32 val;
6997
Michael Chanb5d37722006-09-27 16:06:21 -07006998 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08006999 /* Wait up to 20ms for init done. */
7000 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07007001 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
7002 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08007003 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07007004 }
7005 return -ENODEV;
7006 }
7007
Michael Chan7a6f4362006-09-27 16:03:31 -07007008 /* Wait for firmware initialization to complete. */
7009 for (i = 0; i < 100000; i++) {
7010 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
7011 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
7012 break;
7013 udelay(10);
7014 }
7015
7016 /* Chip might not be fitted with firmware. Some Sun onboard
7017 * parts are configured like that. So don't signal the timeout
7018 * of the above loop as an error, but do report the lack of
7019 * running firmware once.
7020 */
Joe Perches63c3a662011-04-26 08:12:10 +00007021 if (i >= 100000 && !tg3_flag(tp, NO_FWARE_REPORTED)) {
7022 tg3_flag_set(tp, NO_FWARE_REPORTED);
Michael Chan7a6f4362006-09-27 16:03:31 -07007023
Joe Perches05dbe002010-02-17 19:44:19 +00007024 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07007025 }
7026
Matt Carlson6b10c162010-02-12 14:47:08 +00007027 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7028 /* The 57765 A0 needs a little more
7029 * time to do some important work.
7030 */
7031 mdelay(10);
7032 }
7033
Michael Chan7a6f4362006-09-27 16:03:31 -07007034 return 0;
7035}
7036
Michael Chanee6a99b2007-07-18 21:49:10 -07007037/* Save PCI command register before chip reset */
7038static void tg3_save_pci_state(struct tg3 *tp)
7039{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007040 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007041}
7042
7043/* Restore PCI state after chip reset */
7044static void tg3_restore_pci_state(struct tg3 *tp)
7045{
7046 u32 val;
7047
7048 /* Re-enable indirect register accesses. */
7049 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7050 tp->misc_host_ctrl);
7051
7052 /* Set MAX PCI retry to zero. */
7053 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7054 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007055 tg3_flag(tp, PCIX_MODE))
Michael Chanee6a99b2007-07-18 21:49:10 -07007056 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007057 /* Allow reads and writes to the APE register and memory space. */
Joe Perches63c3a662011-04-26 08:12:10 +00007058 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson0d3031d2007-10-10 18:02:43 -07007059 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00007060 PCISTATE_ALLOW_APE_SHMEM_WR |
7061 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007062 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7063
Matt Carlson8a6eac92007-10-21 16:17:55 -07007064 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007065
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007066 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +00007067 if (tg3_flag(tp, PCI_EXPRESS))
Matt Carlsoncf790032010-11-24 08:31:48 +00007068 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007069 else {
7070 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7071 tp->pci_cacheline_sz);
7072 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7073 tp->pci_lat_timer);
7074 }
Michael Chan114342f2007-10-15 02:12:26 -07007075 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007076
Michael Chanee6a99b2007-07-18 21:49:10 -07007077 /* Make sure PCI-X relaxed ordering bit is clear. */
Joe Perches63c3a662011-04-26 08:12:10 +00007078 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07007079 u16 pcix_cmd;
7080
7081 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7082 &pcix_cmd);
7083 pcix_cmd &= ~PCI_X_CMD_ERO;
7084 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7085 pcix_cmd);
7086 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007087
Joe Perches63c3a662011-04-26 08:12:10 +00007088 if (tg3_flag(tp, 5780_CLASS)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007089
7090 /* Chip reset on 5780 will reset MSI enable bit,
7091 * so need to restore it.
7092 */
Joe Perches63c3a662011-04-26 08:12:10 +00007093 if (tg3_flag(tp, USING_MSI)) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007094 u16 ctrl;
7095
7096 pci_read_config_word(tp->pdev,
7097 tp->msi_cap + PCI_MSI_FLAGS,
7098 &ctrl);
7099 pci_write_config_word(tp->pdev,
7100 tp->msi_cap + PCI_MSI_FLAGS,
7101 ctrl | PCI_MSI_FLAGS_ENABLE);
7102 val = tr32(MSGINT_MODE);
7103 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7104 }
7105 }
7106}
7107
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108static void tg3_stop_fw(struct tg3 *);
7109
7110/* tp->lock is held. */
7111static int tg3_chip_reset(struct tg3 *tp)
7112{
7113 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007114 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007115 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116
David S. Millerf49639e2006-06-09 11:58:36 -07007117 tg3_nvram_lock(tp);
7118
Matt Carlson77b483f2008-08-15 14:07:24 -07007119 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7120
David S. Millerf49639e2006-06-09 11:58:36 -07007121 /* No matching tg3_nvram_unlock() after this because
7122 * chip reset below will undo the nvram lock.
7123 */
7124 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125
Michael Chanee6a99b2007-07-18 21:49:10 -07007126 /* GRC_MISC_CFG core clock reset will clear the memory
7127 * enable bit in PCI register 4 and the MSI enable bit
7128 * on some chips, so we save relevant registers here.
7129 */
7130 tg3_save_pci_state(tp);
7131
Michael Chand9ab5ad2006-03-20 22:27:35 -08007132 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Joe Perches63c3a662011-04-26 08:12:10 +00007133 tg3_flag(tp, 5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007134 tw32(GRC_FASTBOOT_PC, 0);
7135
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 /*
7137 * We must avoid the readl() that normally takes place.
7138 * It locks machines, causes machine checks, and other
7139 * fun things. So, temporarily disable the 5701
7140 * hardware workaround, while we do the reset.
7141 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007142 write_op = tp->write32;
7143 if (write_op == tg3_write_flush_reg32)
7144 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145
Michael Chand18edcb2007-03-24 20:57:11 -07007146 /* Prevent the irq handler from reading or writing PCI registers
7147 * during chip reset when the memory enable bit in the PCI command
7148 * register may be cleared. The chip does not generate interrupt
7149 * at this time, but the irq handler may still be called due to irq
7150 * sharing or irqpoll.
7151 */
Joe Perches63c3a662011-04-26 08:12:10 +00007152 tg3_flag_set(tp, CHIP_RESETTING);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007153 for (i = 0; i < tp->irq_cnt; i++) {
7154 struct tg3_napi *tnapi = &tp->napi[i];
7155 if (tnapi->hw_status) {
7156 tnapi->hw_status->status = 0;
7157 tnapi->hw_status->status_tag = 0;
7158 }
7159 tnapi->last_tag = 0;
7160 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007161 }
Michael Chand18edcb2007-03-24 20:57:11 -07007162 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007163
7164 for (i = 0; i < tp->irq_cnt; i++)
7165 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007166
Matt Carlson255ca312009-08-25 10:07:27 +00007167 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7168 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7169 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7170 }
7171
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172 /* do the reset */
7173 val = GRC_MISC_CFG_CORECLK_RESET;
7174
Joe Perches63c3a662011-04-26 08:12:10 +00007175 if (tg3_flag(tp, PCI_EXPRESS)) {
Matt Carlson88075d92010-08-02 11:25:58 +00007176 /* Force PCIe 1.0a mode */
7177 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007178 !tg3_flag(tp, 57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007179 tr32(TG3_PCIE_PHY_TSTCTL) ==
7180 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7181 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7182
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7184 tw32(GRC_MISC_CFG, (1 << 29));
7185 val |= (1 << 29);
7186 }
7187 }
7188
Michael Chanb5d37722006-09-27 16:06:21 -07007189 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7190 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7191 tw32(GRC_VCPU_EXT_CTRL,
7192 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7193 }
7194
Matt Carlsonf37500d2010-08-02 11:25:59 +00007195 /* Manage gphy power for all CPMU absent PCIe devices. */
Joe Perches63c3a662011-04-26 08:12:10 +00007196 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007198
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 tw32(GRC_MISC_CFG, val);
7200
Michael Chan1ee582d2005-08-09 20:16:46 -07007201 /* restore 5701 hardware bug workaround write method */
7202 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203
7204 /* Unfortunately, we have to delay before the PCI read back.
7205 * Some 575X chips even will not respond to a PCI cfg access
7206 * when the reset command is given to the chip.
7207 *
7208 * How do these hardware designers expect things to work
7209 * properly if the PCI write is posted for a long period
7210 * of time? It is always necessary to have some method by
7211 * which a register read back can occur to push the write
7212 * out which does the reset.
7213 *
7214 * For most tg3 variants the trick below was working.
7215 * Ho hum...
7216 */
7217 udelay(120);
7218
7219 /* Flush PCI posted writes. The normal MMIO registers
7220 * are inaccessible at this time so this is the only
7221 * way to make this reliably (actually, this is no longer
7222 * the case, see above). I tried to use indirect
7223 * register read/write but this upset some 5701 variants.
7224 */
7225 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7226
7227 udelay(120);
7228
Joe Perches63c3a662011-04-26 08:12:10 +00007229 if (tg3_flag(tp, PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007230 u16 val16;
7231
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7233 int i;
7234 u32 cfg_val;
7235
7236 /* Wait for link training to complete. */
7237 for (i = 0; i < 5000; i++)
7238 udelay(100);
7239
7240 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7241 pci_write_config_dword(tp->pdev, 0xc4,
7242 cfg_val | (1 << 15));
7243 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007244
Matt Carlsone7126992009-08-25 10:08:16 +00007245 /* Clear the "no snoop" and "relaxed ordering" bits. */
7246 pci_read_config_word(tp->pdev,
7247 tp->pcie_cap + PCI_EXP_DEVCTL,
7248 &val16);
7249 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7250 PCI_EXP_DEVCTL_NOSNOOP_EN);
7251 /*
7252 * Older PCIe devices only support the 128 byte
7253 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007254 */
Joe Perches63c3a662011-04-26 08:12:10 +00007255 if (!tg3_flag(tp, CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007256 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007257 pci_write_config_word(tp->pdev,
7258 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007259 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007260
Matt Carlsoncf790032010-11-24 08:31:48 +00007261 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007262
7263 /* Clear error status */
7264 pci_write_config_word(tp->pdev,
7265 tp->pcie_cap + PCI_EXP_DEVSTA,
7266 PCI_EXP_DEVSTA_CED |
7267 PCI_EXP_DEVSTA_NFED |
7268 PCI_EXP_DEVSTA_FED |
7269 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270 }
7271
Michael Chanee6a99b2007-07-18 21:49:10 -07007272 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273
Joe Perches63c3a662011-04-26 08:12:10 +00007274 tg3_flag_clear(tp, CHIP_RESETTING);
7275 tg3_flag_clear(tp, ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007276
Michael Chanee6a99b2007-07-18 21:49:10 -07007277 val = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007278 if (tg3_flag(tp, 5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -07007279 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007280 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281
7282 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7283 tg3_stop_fw(tp);
7284 tw32(0x5000, 0x400);
7285 }
7286
7287 tw32(GRC_MODE, tp->grc_mode);
7288
7289 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007290 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291
7292 tw32(0xc4, val | (1 << 15));
7293 }
7294
7295 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7296 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7297 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7298 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7299 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7300 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7301 }
7302
Joe Perches63c3a662011-04-26 08:12:10 +00007303 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007304 tp->mac_mode = MAC_MODE_APE_TX_EN |
7305 MAC_MODE_APE_RX_EN |
7306 MAC_MODE_TDE_ENABLE;
7307
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007308 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007309 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7310 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007311 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007312 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7313 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007315 val = 0;
7316
7317 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 udelay(40);
7319
Matt Carlson77b483f2008-08-15 14:07:24 -07007320 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7321
Michael Chan7a6f4362006-09-27 16:03:31 -07007322 err = tg3_poll_fw(tp);
7323 if (err)
7324 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325
Matt Carlson0a9140c2009-08-28 12:27:50 +00007326 tg3_mdio_start(tp);
7327
Joe Perches63c3a662011-04-26 08:12:10 +00007328 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007329 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7330 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +00007331 !tg3_flag(tp, 57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007332 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333
7334 tw32(0x7c00, val | (1 << 25));
7335 }
7336
Matt Carlsond78b59f2011-04-05 14:22:46 +00007337 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7338 val = tr32(TG3_CPMU_CLCK_ORIDE);
7339 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7340 }
7341
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342 /* Reprobe ASF enable state. */
Joe Perches63c3a662011-04-26 08:12:10 +00007343 tg3_flag_clear(tp, ENABLE_ASF);
7344 tg3_flag_clear(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007345 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7346 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7347 u32 nic_cfg;
7348
7349 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7350 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +00007351 tg3_flag_set(tp, ENABLE_ASF);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007352 tp->last_event_jiffies = jiffies;
Joe Perches63c3a662011-04-26 08:12:10 +00007353 if (tg3_flag(tp, 5750_PLUS))
7354 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 }
7356 }
7357
7358 return 0;
7359}
7360
7361/* tp->lock is held. */
7362static void tg3_stop_fw(struct tg3 *tp)
7363{
Joe Perches63c3a662011-04-26 08:12:10 +00007364 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007365 /* Wait for RX cpu to ACK the previous event. */
7366 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367
7368 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007369
7370 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371
Matt Carlson7c5026a2008-05-02 16:49:29 -07007372 /* Wait for RX cpu to ACK this event. */
7373 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374 }
7375}
7376
7377/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007378static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379{
7380 int err;
7381
7382 tg3_stop_fw(tp);
7383
Michael Chan944d9802005-05-29 14:57:48 -07007384 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007386 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387 err = tg3_chip_reset(tp);
7388
Matt Carlsondaba2a62009-04-20 06:58:52 +00007389 __tg3_set_mac_addr(tp, 0);
7390
Michael Chan944d9802005-05-29 14:57:48 -07007391 tg3_write_sig_legacy(tp, kind);
7392 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393
7394 if (err)
7395 return err;
7396
7397 return 0;
7398}
7399
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400#define RX_CPU_SCRATCH_BASE 0x30000
7401#define RX_CPU_SCRATCH_SIZE 0x04000
7402#define TX_CPU_SCRATCH_BASE 0x34000
7403#define TX_CPU_SCRATCH_SIZE 0x04000
7404
7405/* tp->lock is held. */
7406static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7407{
7408 int i;
7409
Joe Perches63c3a662011-04-26 08:12:10 +00007410 BUG_ON(offset == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411
Michael Chanb5d37722006-09-27 16:06:21 -07007412 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7413 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7414
7415 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7416 return 0;
7417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418 if (offset == RX_CPU_BASE) {
7419 for (i = 0; i < 10000; i++) {
7420 tw32(offset + CPU_STATE, 0xffffffff);
7421 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7422 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7423 break;
7424 }
7425
7426 tw32(offset + CPU_STATE, 0xffffffff);
7427 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7428 udelay(10);
7429 } else {
7430 for (i = 0; i < 10000; i++) {
7431 tw32(offset + CPU_STATE, 0xffffffff);
7432 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7433 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7434 break;
7435 }
7436 }
7437
7438 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007439 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7440 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007441 return -ENODEV;
7442 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007443
7444 /* Clear firmware's nvram arbitration. */
Joe Perches63c3a662011-04-26 08:12:10 +00007445 if (tg3_flag(tp, NVRAM))
Michael Chanec41c7d2006-01-17 02:40:55 -08007446 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007447 return 0;
7448}
7449
7450struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007451 unsigned int fw_base;
7452 unsigned int fw_len;
7453 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454};
7455
7456/* tp->lock is held. */
7457static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7458 int cpu_scratch_size, struct fw_info *info)
7459{
Michael Chanec41c7d2006-01-17 02:40:55 -08007460 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461 void (*write_op)(struct tg3 *, u32, u32);
7462
Joe Perches63c3a662011-04-26 08:12:10 +00007463 if (cpu_base == TX_CPU_BASE && tg3_flag(tp, 5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007464 netdev_err(tp->dev,
7465 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007466 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467 return -EINVAL;
7468 }
7469
Joe Perches63c3a662011-04-26 08:12:10 +00007470 if (tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007471 write_op = tg3_write_mem;
7472 else
7473 write_op = tg3_write_indirect_reg32;
7474
Michael Chan1b628152005-05-29 14:59:49 -07007475 /* It is possible that bootcode is still loading at this point.
7476 * Get the nvram lock first before halting the cpu.
7477 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007478 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007480 if (!lock_err)
7481 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482 if (err)
7483 goto out;
7484
7485 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7486 write_op(tp, cpu_scratch_base + i, 0);
7487 tw32(cpu_base + CPU_STATE, 0xffffffff);
7488 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007489 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007491 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007492 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007493 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494
7495 err = 0;
7496
7497out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498 return err;
7499}
7500
7501/* tp->lock is held. */
7502static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7503{
7504 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007505 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506 int err, i;
7507
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007508 fw_data = (void *)tp->fw->data;
7509
7510 /* Firmware blob starts with version numbers, followed by
7511 start address and length. We are setting complete length.
7512 length = end_address_of_bss - start_address_of_text.
7513 Remainder is the blob to be loaded contiguously
7514 from start address. */
7515
7516 info.fw_base = be32_to_cpu(fw_data[1]);
7517 info.fw_len = tp->fw->size - 12;
7518 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519
7520 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7521 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7522 &info);
7523 if (err)
7524 return err;
7525
7526 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7527 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7528 &info);
7529 if (err)
7530 return err;
7531
7532 /* Now startup only the RX cpu. */
7533 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007534 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535
7536 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007537 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 break;
7539 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7540 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007541 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542 udelay(1000);
7543 }
7544 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007545 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7546 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007547 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 return -ENODEV;
7549 }
7550 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7551 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7552
7553 return 0;
7554}
7555
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556/* tp->lock is held. */
7557static int tg3_load_tso_firmware(struct tg3 *tp)
7558{
7559 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007560 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7562 int err, i;
7563
Joe Perches63c3a662011-04-26 08:12:10 +00007564 if (tg3_flag(tp, HW_TSO_1) ||
7565 tg3_flag(tp, HW_TSO_2) ||
7566 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007567 return 0;
7568
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007569 fw_data = (void *)tp->fw->data;
7570
7571 /* Firmware blob starts with version numbers, followed by
7572 start address and length. We are setting complete length.
7573 length = end_address_of_bss - start_address_of_text.
7574 Remainder is the blob to be loaded contiguously
7575 from start address. */
7576
7577 info.fw_base = be32_to_cpu(fw_data[1]);
7578 cpu_scratch_size = tp->fw_len;
7579 info.fw_len = tp->fw->size - 12;
7580 info.fw_data = &fw_data[3];
7581
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 cpu_base = RX_CPU_BASE;
7584 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 cpu_base = TX_CPU_BASE;
7587 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7588 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7589 }
7590
7591 err = tg3_load_firmware_cpu(tp, cpu_base,
7592 cpu_scratch_base, cpu_scratch_size,
7593 &info);
7594 if (err)
7595 return err;
7596
7597 /* Now startup the cpu. */
7598 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007599 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007600
7601 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007602 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007603 break;
7604 tw32(cpu_base + CPU_STATE, 0xffffffff);
7605 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007606 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607 udelay(1000);
7608 }
7609 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007610 netdev_err(tp->dev,
7611 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007612 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007613 return -ENODEV;
7614 }
7615 tw32(cpu_base + CPU_STATE, 0xffffffff);
7616 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7617 return 0;
7618}
7619
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620
Linus Torvalds1da177e2005-04-16 15:20:36 -07007621static int tg3_set_mac_addr(struct net_device *dev, void *p)
7622{
7623 struct tg3 *tp = netdev_priv(dev);
7624 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007625 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007626
Michael Chanf9804dd2005-09-27 12:13:10 -07007627 if (!is_valid_ether_addr(addr->sa_data))
7628 return -EINVAL;
7629
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7631
Michael Chane75f7c92006-03-20 21:33:26 -08007632 if (!netif_running(dev))
7633 return 0;
7634
Joe Perches63c3a662011-04-26 08:12:10 +00007635 if (tg3_flag(tp, ENABLE_ASF)) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007636 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007637
Michael Chan986e0ae2007-05-05 12:10:20 -07007638 addr0_high = tr32(MAC_ADDR_0_HIGH);
7639 addr0_low = tr32(MAC_ADDR_0_LOW);
7640 addr1_high = tr32(MAC_ADDR_1_HIGH);
7641 addr1_low = tr32(MAC_ADDR_1_LOW);
7642
7643 /* Skip MAC addr 1 if ASF is using it. */
7644 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7645 !(addr1_high == 0 && addr1_low == 0))
7646 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007647 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007648 spin_lock_bh(&tp->lock);
7649 __tg3_set_mac_addr(tp, skip_mac_1);
7650 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651
Michael Chanb9ec6c12006-07-25 16:37:27 -07007652 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653}
7654
7655/* tp->lock is held. */
7656static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7657 dma_addr_t mapping, u32 maxlen_flags,
7658 u32 nic_addr)
7659{
7660 tg3_write_mem(tp,
7661 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7662 ((u64) mapping >> 32));
7663 tg3_write_mem(tp,
7664 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7665 ((u64) mapping & 0xffffffff));
7666 tg3_write_mem(tp,
7667 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7668 maxlen_flags);
7669
Joe Perches63c3a662011-04-26 08:12:10 +00007670 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007671 tg3_write_mem(tp,
7672 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7673 nic_addr);
7674}
7675
7676static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007677static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007678{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007679 int i;
7680
Joe Perches63c3a662011-04-26 08:12:10 +00007681 if (!tg3_flag(tp, ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007682 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7683 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7684 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007685 } else {
7686 tw32(HOSTCC_TXCOL_TICKS, 0);
7687 tw32(HOSTCC_TXMAX_FRAMES, 0);
7688 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007689 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007690
Joe Perches63c3a662011-04-26 08:12:10 +00007691 if (!tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007692 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7693 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7694 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7695 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007696 tw32(HOSTCC_RXCOL_TICKS, 0);
7697 tw32(HOSTCC_RXMAX_FRAMES, 0);
7698 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007699 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007700
Joe Perches63c3a662011-04-26 08:12:10 +00007701 if (!tg3_flag(tp, 5705_PLUS)) {
David S. Miller15f98502005-05-18 22:49:26 -07007702 u32 val = ec->stats_block_coalesce_usecs;
7703
Matt Carlsonb6080e12009-09-01 13:12:00 +00007704 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7705 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7706
David S. Miller15f98502005-05-18 22:49:26 -07007707 if (!netif_carrier_ok(tp->dev))
7708 val = 0;
7709
7710 tw32(HOSTCC_STAT_COAL_TICKS, val);
7711 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007712
7713 for (i = 0; i < tp->irq_cnt - 1; i++) {
7714 u32 reg;
7715
7716 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7717 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007718 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7719 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007720 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7721 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007722
Joe Perches63c3a662011-04-26 08:12:10 +00007723 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007724 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7725 tw32(reg, ec->tx_coalesce_usecs);
7726 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7727 tw32(reg, ec->tx_max_coalesced_frames);
7728 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7729 tw32(reg, ec->tx_max_coalesced_frames_irq);
7730 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007731 }
7732
7733 for (; i < tp->irq_max - 1; i++) {
7734 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007735 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007736 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007737
Joe Perches63c3a662011-04-26 08:12:10 +00007738 if (tg3_flag(tp, ENABLE_TSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007739 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7740 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7741 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7742 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007743 }
David S. Miller15f98502005-05-18 22:49:26 -07007744}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745
7746/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007747static void tg3_rings_reset(struct tg3 *tp)
7748{
7749 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007750 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007751 struct tg3_napi *tnapi = &tp->napi[0];
7752
7753 /* Disable all transmit rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007754 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007755 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Joe Perches63c3a662011-04-26 08:12:10 +00007756 else if (tg3_flag(tp, 5717_PLUS))
Matt Carlson3d377282010-10-14 10:37:39 +00007757 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007758 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7759 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007760 else
7761 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7762
7763 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7764 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7765 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7766 BDINFO_FLAGS_DISABLED);
7767
7768
7769 /* Disable all receive return rings but the first. */
Joe Perches63c3a662011-04-26 08:12:10 +00007770 if (tg3_flag(tp, 5717_PLUS))
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007771 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
Joe Perches63c3a662011-04-26 08:12:10 +00007772 else if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007773 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007774 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7775 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007776 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7777 else
7778 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7779
7780 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7781 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7782 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7783 BDINFO_FLAGS_DISABLED);
7784
7785 /* Disable interrupts */
7786 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00007787 tp->napi[0].chk_msi_cnt = 0;
7788 tp->napi[0].last_rx_cons = 0;
7789 tp->napi[0].last_tx_cons = 0;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007790
7791 /* Zero mailbox registers. */
Joe Perches63c3a662011-04-26 08:12:10 +00007792 if (tg3_flag(tp, SUPPORT_MSIX)) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007793 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007794 tp->napi[i].tx_prod = 0;
7795 tp->napi[i].tx_cons = 0;
Joe Perches63c3a662011-04-26 08:12:10 +00007796 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007797 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007798 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7799 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00007800 tp->napi[0].chk_msi_cnt = 0;
7801 tp->napi[i].last_rx_cons = 0;
7802 tp->napi[i].last_tx_cons = 0;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007803 }
Joe Perches63c3a662011-04-26 08:12:10 +00007804 if (!tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc2353a32010-01-20 16:58:08 +00007805 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007806 } else {
7807 tp->napi[0].tx_prod = 0;
7808 tp->napi[0].tx_cons = 0;
7809 tw32_mailbox(tp->napi[0].prodmbox, 0);
7810 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7811 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007812
7813 /* Make sure the NIC-based send BD rings are disabled. */
Joe Perches63c3a662011-04-26 08:12:10 +00007814 if (!tg3_flag(tp, 5705_PLUS)) {
Matt Carlson2d31eca2009-09-01 12:53:31 +00007815 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7816 for (i = 0; i < 16; i++)
7817 tw32_tx_mbox(mbox + i * 8, 0);
7818 }
7819
7820 txrcb = NIC_SRAM_SEND_RCB;
7821 rxrcb = NIC_SRAM_RCV_RET_RCB;
7822
7823 /* Clear status block in ram. */
7824 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7825
7826 /* Set status block DMA address */
7827 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7828 ((u64) tnapi->status_mapping >> 32));
7829 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7830 ((u64) tnapi->status_mapping & 0xffffffff));
7831
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007832 if (tnapi->tx_ring) {
7833 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7834 (TG3_TX_RING_SIZE <<
7835 BDINFO_FLAGS_MAXLEN_SHIFT),
7836 NIC_SRAM_TX_BUFFER_DESC);
7837 txrcb += TG3_BDINFO_SIZE;
7838 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007839
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007840 if (tnapi->rx_rcb) {
7841 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007842 (tp->rx_ret_ring_mask + 1) <<
7843 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007844 rxrcb += TG3_BDINFO_SIZE;
7845 }
7846
7847 stblk = HOSTCC_STATBLCK_RING1;
7848
7849 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7850 u64 mapping = (u64)tnapi->status_mapping;
7851 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7852 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7853
7854 /* Clear status block in ram. */
7855 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7856
Matt Carlson19cfaec2009-12-03 08:36:20 +00007857 if (tnapi->tx_ring) {
7858 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7859 (TG3_TX_RING_SIZE <<
7860 BDINFO_FLAGS_MAXLEN_SHIFT),
7861 NIC_SRAM_TX_BUFFER_DESC);
7862 txrcb += TG3_BDINFO_SIZE;
7863 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007864
7865 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007866 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007867 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7868
7869 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007870 rxrcb += TG3_BDINFO_SIZE;
7871 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007872}
7873
Matt Carlsoneb07a942011-04-20 07:57:36 +00007874static void tg3_setup_rxbd_thresholds(struct tg3 *tp)
7875{
7876 u32 val, bdcache_maxcnt, host_rep_thresh, nic_rep_thresh;
7877
Joe Perches63c3a662011-04-26 08:12:10 +00007878 if (!tg3_flag(tp, 5750_PLUS) ||
7879 tg3_flag(tp, 5780_CLASS) ||
Matt Carlsoneb07a942011-04-20 07:57:36 +00007880 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
7881 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
7882 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5700;
7883 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7884 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787)
7885 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5755;
7886 else
7887 bdcache_maxcnt = TG3_SRAM_RX_STD_BDCACHE_SIZE_5906;
7888
7889 nic_rep_thresh = min(bdcache_maxcnt / 2, tp->rx_std_max_post);
7890 host_rep_thresh = max_t(u32, tp->rx_pending / 8, 1);
7891
7892 val = min(nic_rep_thresh, host_rep_thresh);
7893 tw32(RCVBDI_STD_THRESH, val);
7894
Joe Perches63c3a662011-04-26 08:12:10 +00007895 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007896 tw32(STD_REPLENISH_LWM, bdcache_maxcnt);
7897
Joe Perches63c3a662011-04-26 08:12:10 +00007898 if (!tg3_flag(tp, JUMBO_CAPABLE) || tg3_flag(tp, 5780_CLASS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007899 return;
7900
Joe Perches63c3a662011-04-26 08:12:10 +00007901 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007902 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5700;
7903 else
7904 bdcache_maxcnt = TG3_SRAM_RX_JMB_BDCACHE_SIZE_5717;
7905
7906 host_rep_thresh = max_t(u32, tp->rx_jumbo_pending / 8, 1);
7907
7908 val = min(bdcache_maxcnt / 2, host_rep_thresh);
7909 tw32(RCVBDI_JUMBO_THRESH, val);
7910
Joe Perches63c3a662011-04-26 08:12:10 +00007911 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoneb07a942011-04-20 07:57:36 +00007912 tw32(JMB_REPLENISH_LWM, bdcache_maxcnt);
7913}
7914
Matt Carlson2d31eca2009-09-01 12:53:31 +00007915/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007916static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007917{
7918 u32 val, rdmac_mode;
7919 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007920 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007921
7922 tg3_disable_ints(tp);
7923
7924 tg3_stop_fw(tp);
7925
7926 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7927
Joe Perches63c3a662011-04-26 08:12:10 +00007928 if (tg3_flag(tp, INIT_COMPLETE))
Michael Chane6de8ad2005-05-05 14:42:41 -07007929 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007930
Matt Carlson699c0192010-12-06 08:28:51 +00007931 /* Enable MAC control of LPI */
7932 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7933 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7934 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7935 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7936
7937 tw32_f(TG3_CPMU_EEE_CTRL,
7938 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7939
Matt Carlsona386b902010-12-06 08:28:53 +00007940 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7941 TG3_CPMU_EEEMD_LPI_IN_TX |
7942 TG3_CPMU_EEEMD_LPI_IN_RX |
7943 TG3_CPMU_EEEMD_EEE_ENABLE;
7944
7945 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
7946 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
7947
Joe Perches63c3a662011-04-26 08:12:10 +00007948 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsona386b902010-12-06 08:28:53 +00007949 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
7950
7951 tw32_f(TG3_CPMU_EEE_MODE, val);
7952
7953 tw32_f(TG3_CPMU_EEE_DBTMR1,
7954 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
7955 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
7956
7957 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00007958 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00007959 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00007960 }
7961
Matt Carlson603f1172010-02-12 14:47:10 +00007962 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08007963 tg3_phy_reset(tp);
7964
Linus Torvalds1da177e2005-04-16 15:20:36 -07007965 err = tg3_chip_reset(tp);
7966 if (err)
7967 return err;
7968
7969 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
7970
Matt Carlsonbcb37f62008-11-03 16:52:09 -08007971 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07007972 val = tr32(TG3_CPMU_CTRL);
7973 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
7974 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08007975
7976 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
7977 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
7978 val |= CPMU_LSPD_10MB_MACCLK_6_25;
7979 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
7980
7981 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
7982 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
7983 val |= CPMU_LNK_AWARE_MACCLK_6_25;
7984 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
7985
7986 val = tr32(TG3_CPMU_HST_ACC);
7987 val &= ~CPMU_HST_ACC_MACCLK_MASK;
7988 val |= CPMU_HST_ACC_MACCLK_6_25;
7989 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07007990 }
7991
Matt Carlson33466d92009-04-20 06:57:41 +00007992 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7993 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
7994 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
7995 PCIE_PWR_MGMT_L1_THRESH_4MS;
7996 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00007997
7998 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
7999 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8000
8001 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008002
Matt Carlsonf40386c2009-11-02 14:24:02 +00008003 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8004 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008005 }
8006
Joe Perches63c3a662011-04-26 08:12:10 +00008007 if (tg3_flag(tp, L1PLLPD_EN)) {
Matt Carlson614b0592010-01-20 16:58:02 +00008008 u32 grc_mode = tr32(GRC_MODE);
8009
8010 /* Access the lower 1K of PL PCIE block registers. */
8011 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8012 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8013
8014 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8015 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8016 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8017
8018 tw32(GRC_MODE, grc_mode);
8019 }
8020
Matt Carlson5093eed2010-11-24 08:31:45 +00008021 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8022 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8023 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008024
Matt Carlson5093eed2010-11-24 08:31:45 +00008025 /* Access the lower 1K of PL PCIE block registers. */
8026 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8027 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008028
Matt Carlson5093eed2010-11-24 08:31:45 +00008029 val = tr32(TG3_PCIE_TLDLPL_PORT +
8030 TG3_PCIE_PL_LO_PHYCTL5);
8031 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8032 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008033
Matt Carlson5093eed2010-11-24 08:31:45 +00008034 tw32(GRC_MODE, grc_mode);
8035 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008036
Matt Carlson1ff30a52011-05-19 12:12:46 +00008037 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_57765_AX) {
8038 u32 grc_mode = tr32(GRC_MODE);
8039
8040 /* Access the lower 1K of DL PCIE block registers. */
8041 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8042 tw32(GRC_MODE, val | GRC_MODE_PCIE_DL_SEL);
8043
8044 val = tr32(TG3_PCIE_TLDLPL_PORT +
8045 TG3_PCIE_DL_LO_FTSMAX);
8046 val &= ~TG3_PCIE_DL_LO_FTSMAX_MSK;
8047 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_DL_LO_FTSMAX,
8048 val | TG3_PCIE_DL_LO_FTSMAX_VAL);
8049
8050 tw32(GRC_MODE, grc_mode);
8051 }
8052
Matt Carlsona977dbe2010-04-12 06:58:26 +00008053 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8054 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8055 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8056 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008057 }
8058
Linus Torvalds1da177e2005-04-16 15:20:36 -07008059 /* This works around an issue with Athlon chipsets on
8060 * B3 tigon3 silicon. This bit has no effect on any
8061 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008062 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063 */
Joe Perches63c3a662011-04-26 08:12:10 +00008064 if (!tg3_flag(tp, CPMU_PRESENT)) {
8065 if (!tg3_flag(tp, PCI_EXPRESS))
Matt Carlson795d01c2007-10-07 23:28:17 -07008066 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8067 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008069
8070 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
Joe Perches63c3a662011-04-26 08:12:10 +00008071 tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008072 val = tr32(TG3PCI_PCISTATE);
8073 val |= PCISTATE_RETRY_SAME_DMA;
8074 tw32(TG3PCI_PCISTATE, val);
8075 }
8076
Joe Perches63c3a662011-04-26 08:12:10 +00008077 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -07008078 /* Allow reads and writes to the
8079 * APE register and memory space.
8080 */
8081 val = tr32(TG3PCI_PCISTATE);
8082 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +00008083 PCISTATE_ALLOW_APE_SHMEM_WR |
8084 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008085 tw32(TG3PCI_PCISTATE, val);
8086 }
8087
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8089 /* Enable some hw fixes. */
8090 val = tr32(TG3PCI_MSI_DATA);
8091 val |= (1 << 26) | (1 << 28) | (1 << 29);
8092 tw32(TG3PCI_MSI_DATA, val);
8093 }
8094
8095 /* Descriptor ring init may make accesses to the
8096 * NIC SRAM area to setup the TX descriptors, so we
8097 * can only do this after the hardware has been
8098 * successfully reset.
8099 */
Michael Chan32d8c572006-07-25 16:38:29 -07008100 err = tg3_init_rings(tp);
8101 if (err)
8102 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008103
Joe Perches63c3a662011-04-26 08:12:10 +00008104 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008105 val = tr32(TG3PCI_DMA_RW_CTRL) &
8106 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008107 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8108 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlson0aebff42011-04-25 12:42:45 +00008109 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765 &&
8110 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
8111 val |= DMA_RWCTRL_TAGGED_STAT_WA;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008112 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8113 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8114 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008115 /* This value is determined during the probe time DMA
8116 * engine test, tg3_test_dma.
8117 */
8118 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008120
8121 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8122 GRC_MODE_4X_NIC_SEND_RINGS |
8123 GRC_MODE_NO_TX_PHDR_CSUM |
8124 GRC_MODE_NO_RX_PHDR_CSUM);
8125 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008126
8127 /* Pseudo-header checksum is done by hardware logic and not
8128 * the offload processers, so make the chip do the pseudo-
8129 * header checksums on receive. For transmit it is more
8130 * convenient to do the pseudo-header checksum in software
8131 * as Linux does that on transmit for us in all cases.
8132 */
8133 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008134
8135 tw32(GRC_MODE,
8136 tp->grc_mode |
8137 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8138
8139 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8140 val = tr32(GRC_MISC_CFG);
8141 val &= ~0xff;
8142 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8143 tw32(GRC_MISC_CFG, val);
8144
8145 /* Initialize MBUF/DESC pool. */
Joe Perches63c3a662011-04-26 08:12:10 +00008146 if (tg3_flag(tp, 5750_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008147 /* Do nothing. */
8148 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8149 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8150 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8151 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8152 else
8153 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8154 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8155 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Joe Perches63c3a662011-04-26 08:12:10 +00008156 } else if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008157 int fw_len;
8158
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008159 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008160 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8161 tw32(BUFMGR_MB_POOL_ADDR,
8162 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8163 tw32(BUFMGR_MB_POOL_SIZE,
8164 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008166
Michael Chan0f893dc2005-07-25 12:30:38 -07008167 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8169 tp->bufmgr_config.mbuf_read_dma_low_water);
8170 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8171 tp->bufmgr_config.mbuf_mac_rx_low_water);
8172 tw32(BUFMGR_MB_HIGH_WATER,
8173 tp->bufmgr_config.mbuf_high_water);
8174 } else {
8175 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8176 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8177 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8178 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8179 tw32(BUFMGR_MB_HIGH_WATER,
8180 tp->bufmgr_config.mbuf_high_water_jumbo);
8181 }
8182 tw32(BUFMGR_DMA_LOW_WATER,
8183 tp->bufmgr_config.dma_low_water);
8184 tw32(BUFMGR_DMA_HIGH_WATER,
8185 tp->bufmgr_config.dma_high_water);
8186
Matt Carlsond309a462010-09-30 10:34:31 +00008187 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8188 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8189 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008190 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8191 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8192 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8193 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008194 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195 for (i = 0; i < 2000; i++) {
8196 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8197 break;
8198 udelay(10);
8199 }
8200 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008201 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008202 return -ENODEV;
8203 }
8204
Matt Carlsoneb07a942011-04-20 07:57:36 +00008205 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8206 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
Michael Chanb5d37722006-09-27 16:06:21 -07008207
Matt Carlsoneb07a942011-04-20 07:57:36 +00008208 tg3_setup_rxbd_thresholds(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008209
8210 /* Initialize TG3_BDINFO's at:
8211 * RCVDBDI_STD_BD: standard eth size rx ring
8212 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8213 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8214 *
8215 * like so:
8216 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8217 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8218 * ring attribute flags
8219 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8220 *
8221 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8222 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8223 *
8224 * The size of each ring is fixed in the firmware, but the location is
8225 * configurable.
8226 */
8227 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008228 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008230 ((u64) tpr->rx_std_mapping & 0xffffffff));
Joe Perches63c3a662011-04-26 08:12:10 +00008231 if (!tg3_flag(tp, 5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008232 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8233 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008234
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008235 /* Disable the mini ring */
Joe Perches63c3a662011-04-26 08:12:10 +00008236 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8238 BDINFO_FLAGS_DISABLED);
8239
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008240 /* Program the jumbo buffer descriptor ring control
8241 * blocks on those devices that have them.
8242 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008243 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008244 (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008245
Joe Perches63c3a662011-04-26 08:12:10 +00008246 if (tg3_flag(tp, JUMBO_RING_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008247 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008248 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008250 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008251 val = TG3_RX_JMB_RING_SIZE(tp) <<
8252 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008254 val | BDINFO_FLAGS_USE_EXT_RECV);
Joe Perches63c3a662011-04-26 08:12:10 +00008255 if (!tg3_flag(tp, USE_JUMBO_BDFLAG) ||
Matt Carlsona50d0792010-06-05 17:24:37 +00008256 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008257 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8258 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259 } else {
8260 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8261 BDINFO_FLAGS_DISABLED);
8262 }
8263
Joe Perches63c3a662011-04-26 08:12:10 +00008264 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008265 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008266 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008267 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008268 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008269 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8270 val |= (TG3_RX_STD_DMA_SZ << 2);
8271 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008272 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008273 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008274 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008275
8276 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008277
Matt Carlson411da642009-11-13 13:03:46 +00008278 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008279 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008280
Joe Perches63c3a662011-04-26 08:12:10 +00008281 tpr->rx_jmb_prod_idx =
8282 tg3_flag(tp, JUMBO_RING_ENABLE) ? tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008283 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008284
Matt Carlson2d31eca2009-09-01 12:53:31 +00008285 tg3_rings_reset(tp);
8286
Linus Torvalds1da177e2005-04-16 15:20:36 -07008287 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008288 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008289
8290 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008291 tw32(MAC_RX_MTU_SIZE,
8292 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008293
8294 /* The slot time is changed by tg3_setup_phy if we
8295 * run at gigabit with half duplex.
8296 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008297 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8298 (6 << TX_LENGTHS_IPG_SHIFT) |
8299 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8300
8301 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8302 val |= tr32(MAC_TX_LENGTHS) &
8303 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8304 TX_LENGTHS_CNT_DWN_VAL_MSK);
8305
8306 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008307
8308 /* Receive rules. */
8309 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8310 tw32(RCVLPC_CONFIG, 0x0181);
8311
8312 /* Calculate RDMAC_MODE setting early, we need it to determine
8313 * the RCVLPC_STATE_ENABLE mask.
8314 */
8315 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8316 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8317 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8318 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8319 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008320
Matt Carlsondeabaac2010-11-24 08:31:50 +00008321 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008322 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8323
Matt Carlson57e69832008-05-25 23:48:31 -07008324 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008325 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8326 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008327 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8328 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8329 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8330
Matt Carlsonc5908932011-03-09 16:58:25 +00008331 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8332 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008333 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008334 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008335 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8336 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008337 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008338 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8339 }
8340 }
8341
Joe Perches63c3a662011-04-26 08:12:10 +00008342 if (tg3_flag(tp, PCI_EXPRESS))
Michael Chan85e94ce2005-04-21 17:05:28 -07008343 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8344
Joe Perches63c3a662011-04-26 08:12:10 +00008345 if (tg3_flag(tp, HW_TSO_1) ||
8346 tg3_flag(tp, HW_TSO_2) ||
8347 tg3_flag(tp, HW_TSO_3))
Matt Carlson027455a2008-12-21 20:19:30 -08008348 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8349
Matt Carlson108a6c12011-05-19 12:12:47 +00008350 if (tg3_flag(tp, 57765_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +00008351 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008352 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8353 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008354
Matt Carlsonf2096f92011-04-05 14:22:48 +00008355 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8356 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8357
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008358 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8359 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8360 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8361 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +00008362 tg3_flag(tp, 57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008363 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008364 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8365 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008366 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8367 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8368 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8369 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8370 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8371 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008372 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008373 tw32(TG3_RDMA_RSRVCTRL_REG,
8374 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8375 }
8376
Matt Carlsond78b59f2011-04-05 14:22:46 +00008377 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8378 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008379 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8380 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8381 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8382 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8383 }
8384
Linus Torvalds1da177e2005-04-16 15:20:36 -07008385 /* Receive/send statistics. */
Joe Perches63c3a662011-04-26 08:12:10 +00008386 if (tg3_flag(tp, 5750_PLUS)) {
Michael Chan16613942006-06-29 20:15:13 -07008387 val = tr32(RCVLPC_STATS_ENABLE);
8388 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8389 tw32(RCVLPC_STATS_ENABLE, val);
8390 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008391 tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392 val = tr32(RCVLPC_STATS_ENABLE);
8393 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8394 tw32(RCVLPC_STATS_ENABLE, val);
8395 } else {
8396 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8397 }
8398 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8399 tw32(SNDDATAI_STATSENAB, 0xffffff);
8400 tw32(SNDDATAI_STATSCTRL,
8401 (SNDDATAI_SCTRL_ENABLE |
8402 SNDDATAI_SCTRL_FASTUPD));
8403
8404 /* Setup host coalescing engine. */
8405 tw32(HOSTCC_MODE, 0);
8406 for (i = 0; i < 2000; i++) {
8407 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8408 break;
8409 udelay(10);
8410 }
8411
Michael Chand244c892005-07-05 14:42:33 -07008412 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008413
Joe Perches63c3a662011-04-26 08:12:10 +00008414 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415 /* Status/statistics block address. See tg3_timer,
8416 * the tg3_periodic_fetch_stats call there, and
8417 * tg3_get_stats to see how this works for 5705/5750 chips.
8418 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008419 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8420 ((u64) tp->stats_mapping >> 32));
8421 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8422 ((u64) tp->stats_mapping & 0xffffffff));
8423 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008424
Linus Torvalds1da177e2005-04-16 15:20:36 -07008425 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008426
8427 /* Clear statistics and status block memory areas */
8428 for (i = NIC_SRAM_STATS_BLK;
8429 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8430 i += sizeof(u32)) {
8431 tg3_write_mem(tp, i, 0);
8432 udelay(40);
8433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008434 }
8435
8436 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8437
8438 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8439 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008440 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008441 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8442
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008443 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8444 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008445 /* reset to prevent losing 1st rx packet intermittently */
8446 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8447 udelay(10);
8448 }
8449
Joe Perches63c3a662011-04-26 08:12:10 +00008450 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008451 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008452 else
8453 tp->mac_mode = 0;
8454 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008455 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008456 if (!tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008457 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008458 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8459 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008460 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8461 udelay(40);
8462
Michael Chan314fba32005-04-21 17:07:04 -07008463 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Joe Perches63c3a662011-04-26 08:12:10 +00008464 * If TG3_FLAG_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008465 * register to preserve the GPIO settings for LOMs. The GPIOs,
8466 * whether used as inputs or outputs, are set by boot code after
8467 * reset.
8468 */
Joe Perches63c3a662011-04-26 08:12:10 +00008469 if (!tg3_flag(tp, IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008470 u32 gpio_mask;
8471
Michael Chan9d26e212006-12-07 00:21:14 -08008472 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8473 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8474 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008475
8476 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8477 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8478 GRC_LCLCTRL_GPIO_OUTPUT3;
8479
Michael Chanaf36e6b2006-03-23 01:28:06 -08008480 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8481 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8482
Gary Zambranoaaf84462007-05-05 11:51:45 -07008483 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008484 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8485
8486 /* GPIO1 must be driven high for eeprom write protect */
Joe Perches63c3a662011-04-26 08:12:10 +00008487 if (tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan9d26e212006-12-07 00:21:14 -08008488 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8489 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008491 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8492 udelay(100);
8493
Joe Perches63c3a662011-04-26 08:12:10 +00008494 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008495 val = tr32(MSGINT_MODE);
8496 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8497 tw32(MSGINT_MODE, val);
8498 }
8499
Joe Perches63c3a662011-04-26 08:12:10 +00008500 if (!tg3_flag(tp, 5705_PLUS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8502 udelay(40);
8503 }
8504
8505 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8506 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8507 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8508 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8509 WDMAC_MODE_LNGREAD_ENAB);
8510
Matt Carlsonc5908932011-03-09 16:58:25 +00008511 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8512 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +00008513 if (tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008514 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8515 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8516 /* nothing */
8517 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008518 !tg3_flag(tp, IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008519 val |= WDMAC_MODE_RX_ACCEL;
8520 }
8521 }
8522
Michael Chand9ab5ad2006-03-20 22:27:35 -08008523 /* Enable host coalescing bug fix */
Joe Perches63c3a662011-04-26 08:12:10 +00008524 if (tg3_flag(tp, 5755_PLUS))
Matt Carlsonf51f3562008-05-25 23:45:08 -07008525 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008526
Matt Carlson788a0352009-11-02 14:26:03 +00008527 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8528 val |= WDMAC_MODE_BURST_ALL_DATA;
8529
Linus Torvalds1da177e2005-04-16 15:20:36 -07008530 tw32_f(WDMAC_MODE, val);
8531 udelay(40);
8532
Joe Perches63c3a662011-04-26 08:12:10 +00008533 if (tg3_flag(tp, PCIX_MODE)) {
Matt Carlson9974a352007-10-07 23:27:28 -07008534 u16 pcix_cmd;
8535
8536 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8537 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008538 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008539 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8540 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008541 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008542 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8543 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008544 }
Matt Carlson9974a352007-10-07 23:27:28 -07008545 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8546 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008547 }
8548
8549 tw32_f(RDMAC_MODE, rdmac_mode);
8550 udelay(40);
8551
8552 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008553 if (!tg3_flag(tp, 5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008554 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008555
8556 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8557 tw32(SNDDATAC_MODE,
8558 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8559 else
8560 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8561
Linus Torvalds1da177e2005-04-16 15:20:36 -07008562 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8563 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008564 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Joe Perches63c3a662011-04-26 08:12:10 +00008565 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008566 val |= RCVDBDI_MODE_LRG_RING_SZ;
8567 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008568 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Joe Perches63c3a662011-04-26 08:12:10 +00008569 if (tg3_flag(tp, HW_TSO_1) ||
8570 tg3_flag(tp, HW_TSO_2) ||
8571 tg3_flag(tp, HW_TSO_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008572 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008573 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008574 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008575 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8576 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008577 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8578
8579 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8580 err = tg3_load_5701_a0_firmware_fix(tp);
8581 if (err)
8582 return err;
8583 }
8584
Joe Perches63c3a662011-04-26 08:12:10 +00008585 if (tg3_flag(tp, TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008586 err = tg3_load_tso_firmware(tp);
8587 if (err)
8588 return err;
8589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590
8591 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008592
Joe Perches63c3a662011-04-26 08:12:10 +00008593 if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsonb1d05212010-06-05 17:24:31 +00008594 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8595 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008596
8597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8598 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8599 tp->tx_mode &= ~val;
8600 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8601 }
8602
Linus Torvalds1da177e2005-04-16 15:20:36 -07008603 tw32_f(MAC_TX_MODE, tp->tx_mode);
8604 udelay(100);
8605
Joe Perches63c3a662011-04-26 08:12:10 +00008606 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008607 u32 reg = MAC_RSS_INDIR_TBL_0;
8608 u8 *ent = (u8 *)&val;
8609
8610 /* Setup the indirection table */
8611 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8612 int idx = i % sizeof(val);
8613
Matt Carlson5efeeea2010-07-11 09:31:40 +00008614 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008615 if (idx == sizeof(val) - 1) {
8616 tw32(reg, val);
8617 reg += 4;
8618 }
8619 }
8620
8621 /* Setup the "secret" hash key. */
8622 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8623 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8624 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8625 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8626 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8627 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8628 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8629 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8630 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8631 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8632 }
8633
Linus Torvalds1da177e2005-04-16 15:20:36 -07008634 tp->rx_mode = RX_MODE_ENABLE;
Joe Perches63c3a662011-04-26 08:12:10 +00008635 if (tg3_flag(tp, 5755_PLUS))
Michael Chanaf36e6b2006-03-23 01:28:06 -08008636 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8637
Joe Perches63c3a662011-04-26 08:12:10 +00008638 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008639 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8640 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8641 RX_MODE_RSS_IPV6_HASH_EN |
8642 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8643 RX_MODE_RSS_IPV4_HASH_EN |
8644 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8645
Linus Torvalds1da177e2005-04-16 15:20:36 -07008646 tw32_f(MAC_RX_MODE, tp->rx_mode);
8647 udelay(10);
8648
Linus Torvalds1da177e2005-04-16 15:20:36 -07008649 tw32(MAC_LED_CTRL, tp->led_ctrl);
8650
8651 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008652 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008653 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8654 udelay(10);
8655 }
8656 tw32_f(MAC_RX_MODE, tp->rx_mode);
8657 udelay(10);
8658
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008659 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008660 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008661 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008662 /* Set drive transmission level to 1.2V */
8663 /* only if the signal pre-emphasis bit is not set */
8664 val = tr32(MAC_SERDES_CFG);
8665 val &= 0xfffff000;
8666 val |= 0x880;
8667 tw32(MAC_SERDES_CFG, val);
8668 }
8669 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8670 tw32(MAC_SERDES_CFG, 0x616000);
8671 }
8672
8673 /* Prevent chip from dropping frames when flow control
8674 * is enabled.
8675 */
Matt Carlson666bc832010-01-20 16:58:03 +00008676 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8677 val = 1;
8678 else
8679 val = 2;
8680 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008681
8682 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008683 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008684 /* Use hardware link auto-negotiation */
Joe Perches63c3a662011-04-26 08:12:10 +00008685 tg3_flag_set(tp, HW_AUTONEG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008686 }
8687
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008688 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +00008689 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Michael Chand4d2c552006-03-20 17:47:20 -08008690 u32 tmp;
8691
8692 tmp = tr32(SERDES_RX_CTRL);
8693 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8694 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8695 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8696 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8697 }
8698
Joe Perches63c3a662011-04-26 08:12:10 +00008699 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson800960682010-08-02 11:26:06 +00008700 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8701 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008702 tp->link_config.speed = tp->link_config.orig_speed;
8703 tp->link_config.duplex = tp->link_config.orig_duplex;
8704 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008706
Matt Carlsondd477002008-05-25 23:45:58 -07008707 err = tg3_setup_phy(tp, 0);
8708 if (err)
8709 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008710
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008711 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8712 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008713 u32 tmp;
8714
8715 /* Clear CRC stats. */
8716 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8717 tg3_writephy(tp, MII_TG3_TEST1,
8718 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008719 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008721 }
8722 }
8723
8724 __tg3_set_rx_mode(tp->dev);
8725
8726 /* Initialize receive rules. */
8727 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8728 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8729 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8730 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8731
Joe Perches63c3a662011-04-26 08:12:10 +00008732 if (tg3_flag(tp, 5705_PLUS) && !tg3_flag(tp, 5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008733 limit = 8;
8734 else
8735 limit = 16;
Joe Perches63c3a662011-04-26 08:12:10 +00008736 if (tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008737 limit -= 4;
8738 switch (limit) {
8739 case 16:
8740 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8741 case 15:
8742 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8743 case 14:
8744 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8745 case 13:
8746 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8747 case 12:
8748 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8749 case 11:
8750 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8751 case 10:
8752 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8753 case 9:
8754 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8755 case 8:
8756 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8757 case 7:
8758 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8759 case 6:
8760 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8761 case 5:
8762 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8763 case 4:
8764 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8765 case 3:
8766 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8767 case 2:
8768 case 1:
8769
8770 default:
8771 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008773
Joe Perches63c3a662011-04-26 08:12:10 +00008774 if (tg3_flag(tp, ENABLE_APE))
Matt Carlson9ce768e2007-10-11 19:49:11 -07008775 /* Write our heartbeat update interval to APE. */
8776 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8777 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008778
Linus Torvalds1da177e2005-04-16 15:20:36 -07008779 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8780
Linus Torvalds1da177e2005-04-16 15:20:36 -07008781 return 0;
8782}
8783
8784/* Called at device open time to get the chip ready for
8785 * packet processing. Invoked with tp->lock held.
8786 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008787static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008788{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008789 tg3_switch_clocks(tp);
8790
8791 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8792
Matt Carlson2f751b62008-08-04 23:17:34 -07008793 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008794}
8795
8796#define TG3_STAT_ADD32(PSTAT, REG) \
8797do { u32 __val = tr32(REG); \
8798 (PSTAT)->low += __val; \
8799 if ((PSTAT)->low < __val) \
8800 (PSTAT)->high += 1; \
8801} while (0)
8802
8803static void tg3_periodic_fetch_stats(struct tg3 *tp)
8804{
8805 struct tg3_hw_stats *sp = tp->hw_stats;
8806
8807 if (!netif_carrier_ok(tp->dev))
8808 return;
8809
8810 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8811 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8812 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8813 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8814 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8815 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8816 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8817 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8818 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8819 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8820 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8821 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8822 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8823
8824 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8825 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8826 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8827 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8828 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8829 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8830 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8831 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8832 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8833 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8834 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8835 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8836 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8837 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008838
8839 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson310050f2011-05-19 12:12:55 +00008840 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
8841 tp->pci_chip_rev_id != CHIPREV_ID_5719_A0 &&
8842 tp->pci_chip_rev_id != CHIPREV_ID_5720_A0) {
Matt Carlson4d958472011-04-20 07:57:35 +00008843 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8844 } else {
8845 u32 val = tr32(HOSTCC_FLOW_ATTN);
8846 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
8847 if (val) {
8848 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
8849 sp->rx_discards.low += val;
8850 if (sp->rx_discards.low < val)
8851 sp->rx_discards.high += 1;
8852 }
8853 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
8854 }
Michael Chan463d3052006-05-22 16:36:27 -07008855 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008856}
8857
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008858static void tg3_chk_missed_msi(struct tg3 *tp)
8859{
8860 u32 i;
8861
8862 for (i = 0; i < tp->irq_cnt; i++) {
8863 struct tg3_napi *tnapi = &tp->napi[i];
8864
8865 if (tg3_has_work(tnapi)) {
8866 if (tnapi->last_rx_cons == tnapi->rx_rcb_ptr &&
8867 tnapi->last_tx_cons == tnapi->tx_cons) {
8868 if (tnapi->chk_msi_cnt < 1) {
8869 tnapi->chk_msi_cnt++;
8870 return;
8871 }
8872 tw32_mailbox(tnapi->int_mbox,
8873 tnapi->last_tag << 24);
8874 }
8875 }
8876 tnapi->chk_msi_cnt = 0;
8877 tnapi->last_rx_cons = tnapi->rx_rcb_ptr;
8878 tnapi->last_tx_cons = tnapi->tx_cons;
8879 }
8880}
8881
Linus Torvalds1da177e2005-04-16 15:20:36 -07008882static void tg3_timer(unsigned long __opaque)
8883{
8884 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008885
Michael Chanf475f162006-03-27 23:20:14 -08008886 if (tp->irq_sync)
8887 goto restart_timer;
8888
David S. Millerf47c11e2005-06-24 20:18:35 -07008889 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008890
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00008891 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8892 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8893 tg3_chk_missed_msi(tp);
8894
Joe Perches63c3a662011-04-26 08:12:10 +00008895 if (!tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -07008896 /* All of this garbage is because when using non-tagged
8897 * IRQ status the mailbox/status_block protocol the chip
8898 * uses with the cpu is race prone.
8899 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008900 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008901 tw32(GRC_LOCAL_CTRL,
8902 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8903 } else {
8904 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008905 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008907
David S. Millerfac9b832005-05-18 22:46:34 -07008908 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +00008909 tg3_flag_set(tp, RESTART_TIMER);
David S. Millerf47c11e2005-06-24 20:18:35 -07008910 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008911 schedule_work(&tp->reset_task);
8912 return;
8913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008914 }
8915
Linus Torvalds1da177e2005-04-16 15:20:36 -07008916 /* This part only runs once per second. */
8917 if (!--tp->timer_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008918 if (tg3_flag(tp, 5705_PLUS))
David S. Millerfac9b832005-05-18 22:46:34 -07008919 tg3_periodic_fetch_stats(tp);
8920
Matt Carlsonb0c59432011-05-19 12:12:48 +00008921 if (tp->setlpicnt && !--tp->setlpicnt)
8922 tg3_phy_eee_enable(tp);
Matt Carlson52b02d02010-10-14 10:37:41 +00008923
Joe Perches63c3a662011-04-26 08:12:10 +00008924 if (tg3_flag(tp, USE_LINKCHG_REG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925 u32 mac_stat;
8926 int phy_event;
8927
8928 mac_stat = tr32(MAC_STATUS);
8929
8930 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008931 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008932 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8933 phy_event = 1;
8934 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8935 phy_event = 1;
8936
8937 if (phy_event)
8938 tg3_setup_phy(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +00008939 } else if (tg3_flag(tp, POLL_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008940 u32 mac_stat = tr32(MAC_STATUS);
8941 int need_setup = 0;
8942
8943 if (netif_carrier_ok(tp->dev) &&
8944 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8945 need_setup = 1;
8946 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008947 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008948 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8949 MAC_STATUS_SIGNAL_DET))) {
8950 need_setup = 1;
8951 }
8952 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008953 if (!tp->serdes_counter) {
8954 tw32_f(MAC_MODE,
8955 (tp->mac_mode &
8956 ~MAC_MODE_PORT_MODE_MASK));
8957 udelay(40);
8958 tw32_f(MAC_MODE, tp->mac_mode);
8959 udelay(40);
8960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008961 tg3_setup_phy(tp, 0);
8962 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008963 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +00008964 tg3_flag(tp, 5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008965 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008967
8968 tp->timer_counter = tp->timer_multiplier;
8969 }
8970
Michael Chan130b8e42006-09-27 16:00:40 -07008971 /* Heartbeat is only sent once every 2 seconds.
8972 *
8973 * The heartbeat is to tell the ASF firmware that the host
8974 * driver is still alive. In the event that the OS crashes,
8975 * ASF needs to reset the hardware to free up the FIFO space
8976 * that may be filled with rx packets destined for the host.
8977 * If the FIFO is full, ASF will no longer function properly.
8978 *
8979 * Unintended resets have been reported on real time kernels
8980 * where the timer doesn't run on time. Netpoll will also have
8981 * same problem.
8982 *
8983 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8984 * to check the ring condition when the heartbeat is expiring
8985 * before doing the reset. This will prevent most unintended
8986 * resets.
8987 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008988 if (!--tp->asf_counter) {
Joe Perches63c3a662011-04-26 08:12:10 +00008989 if (tg3_flag(tp, ENABLE_ASF) && !tg3_flag(tp, ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07008990 tg3_wait_for_event_ack(tp);
8991
Michael Chanbbadf502006-04-06 21:46:34 -07008992 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07008993 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07008994 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00008995 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
8996 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07008997
8998 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008999 }
9000 tp->asf_counter = tp->asf_multiplier;
9001 }
9002
David S. Millerf47c11e2005-06-24 20:18:35 -07009003 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009004
Michael Chanf475f162006-03-27 23:20:14 -08009005restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009006 tp->timer.expires = jiffies + tp->timer_offset;
9007 add_timer(&tp->timer);
9008}
9009
Matt Carlson4f125f42009-09-01 12:55:02 +00009010static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009011{
David Howells7d12e782006-10-05 14:55:46 +01009012 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009013 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009014 char *name;
9015 struct tg3_napi *tnapi = &tp->napi[irq_num];
9016
9017 if (tp->irq_cnt == 1)
9018 name = tp->dev->name;
9019 else {
9020 name = &tnapi->irq_lbl[0];
9021 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9022 name[IFNAMSIZ-1] = 0;
9023 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009024
Joe Perches63c3a662011-04-26 08:12:10 +00009025 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009026 fn = tg3_msi;
Joe Perches63c3a662011-04-26 08:12:10 +00009027 if (tg3_flag(tp, 1SHOT_MSI))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009028 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009029 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009030 } else {
9031 fn = tg3_interrupt;
Joe Perches63c3a662011-04-26 08:12:10 +00009032 if (tg3_flag(tp, TAGGED_STATUS))
Michael Chanfcfa0a32006-03-20 22:28:41 -08009033 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009034 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009035 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009036
9037 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009038}
9039
Michael Chan79381092005-04-21 17:13:59 -07009040static int tg3_test_interrupt(struct tg3 *tp)
9041{
Matt Carlson09943a12009-08-28 14:01:57 +00009042 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009043 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009044 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009045 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009046
Michael Chand4bc3922005-05-29 14:59:20 -07009047 if (!netif_running(dev))
9048 return -ENODEV;
9049
Michael Chan79381092005-04-21 17:13:59 -07009050 tg3_disable_ints(tp);
9051
Matt Carlson4f125f42009-09-01 12:55:02 +00009052 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009053
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009054 /*
9055 * Turn off MSI one shot mode. Otherwise this test has no
9056 * observable way to know whether the interrupt was delivered.
9057 */
Joe Perches63c3a662011-04-26 08:12:10 +00009058 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009059 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9060 tw32(MSGINT_MODE, val);
9061 }
9062
Matt Carlson4f125f42009-09-01 12:55:02 +00009063 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009064 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009065 if (err)
9066 return err;
9067
Matt Carlson898a56f2009-08-28 14:02:40 +00009068 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009069 tg3_enable_ints(tp);
9070
9071 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009072 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009073
9074 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009075 u32 int_mbox, misc_host_ctrl;
9076
Matt Carlson898a56f2009-08-28 14:02:40 +00009077 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009078 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9079
9080 if ((int_mbox != 0) ||
9081 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9082 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009083 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009084 }
9085
Michael Chan79381092005-04-21 17:13:59 -07009086 msleep(10);
9087 }
9088
9089 tg3_disable_ints(tp);
9090
Matt Carlson4f125f42009-09-01 12:55:02 +00009091 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009092
Matt Carlson4f125f42009-09-01 12:55:02 +00009093 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009094
9095 if (err)
9096 return err;
9097
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009098 if (intr_ok) {
9099 /* Reenable MSI one shot mode. */
Joe Perches63c3a662011-04-26 08:12:10 +00009100 if (tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009101 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9102 tw32(MSGINT_MODE, val);
9103 }
Michael Chan79381092005-04-21 17:13:59 -07009104 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009105 }
Michael Chan79381092005-04-21 17:13:59 -07009106
9107 return -EIO;
9108}
9109
9110/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9111 * successfully restored
9112 */
9113static int tg3_test_msi(struct tg3 *tp)
9114{
Michael Chan79381092005-04-21 17:13:59 -07009115 int err;
9116 u16 pci_cmd;
9117
Joe Perches63c3a662011-04-26 08:12:10 +00009118 if (!tg3_flag(tp, USING_MSI))
Michael Chan79381092005-04-21 17:13:59 -07009119 return 0;
9120
9121 /* Turn off SERR reporting in case MSI terminates with Master
9122 * Abort.
9123 */
9124 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9125 pci_write_config_word(tp->pdev, PCI_COMMAND,
9126 pci_cmd & ~PCI_COMMAND_SERR);
9127
9128 err = tg3_test_interrupt(tp);
9129
9130 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9131
9132 if (!err)
9133 return 0;
9134
9135 /* other failures */
9136 if (err != -EIO)
9137 return err;
9138
9139 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009140 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9141 "to INTx mode. Please report this failure to the PCI "
9142 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009143
Matt Carlson4f125f42009-09-01 12:55:02 +00009144 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009145
Michael Chan79381092005-04-21 17:13:59 -07009146 pci_disable_msi(tp->pdev);
9147
Joe Perches63c3a662011-04-26 08:12:10 +00009148 tg3_flag_clear(tp, USING_MSI);
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009149 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009150
Matt Carlson4f125f42009-09-01 12:55:02 +00009151 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009152 if (err)
9153 return err;
9154
9155 /* Need to reset the chip because the MSI cycle may have terminated
9156 * with Master Abort.
9157 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009158 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009159
Michael Chan944d9802005-05-29 14:57:48 -07009160 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009161 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009162
David S. Millerf47c11e2005-06-24 20:18:35 -07009163 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009164
9165 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009166 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009167
9168 return err;
9169}
9170
Matt Carlson9e9fd122009-01-19 16:57:45 -08009171static int tg3_request_firmware(struct tg3 *tp)
9172{
9173 const __be32 *fw_data;
9174
9175 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009176 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9177 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009178 return -ENOENT;
9179 }
9180
9181 fw_data = (void *)tp->fw->data;
9182
9183 /* Firmware blob starts with version numbers, followed by
9184 * start address and _full_ length including BSS sections
9185 * (which must be longer than the actual data, of course
9186 */
9187
9188 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9189 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009190 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9191 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009192 release_firmware(tp->fw);
9193 tp->fw = NULL;
9194 return -EINVAL;
9195 }
9196
9197 /* We no longer need firmware; we have it. */
9198 tp->fw_needed = NULL;
9199 return 0;
9200}
9201
Matt Carlson679563f2009-09-01 12:55:46 +00009202static bool tg3_enable_msix(struct tg3 *tp)
9203{
9204 int i, rc, cpus = num_online_cpus();
9205 struct msix_entry msix_ent[tp->irq_max];
9206
9207 if (cpus == 1)
9208 /* Just fallback to the simpler MSI mode. */
9209 return false;
9210
9211 /*
9212 * We want as many rx rings enabled as there are cpus.
9213 * The first MSIX vector only deals with link interrupts, etc,
9214 * so we add one to the number of vectors we are requesting.
9215 */
9216 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9217
9218 for (i = 0; i < tp->irq_max; i++) {
9219 msix_ent[i].entry = i;
9220 msix_ent[i].vector = 0;
9221 }
9222
9223 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009224 if (rc < 0) {
9225 return false;
9226 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009227 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9228 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009229 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9230 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009231 tp->irq_cnt = rc;
9232 }
9233
9234 for (i = 0; i < tp->irq_max; i++)
9235 tp->napi[i].irq_vec = msix_ent[i].vector;
9236
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009237 netif_set_real_num_tx_queues(tp->dev, 1);
9238 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9239 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9240 pci_disable_msix(tp->pdev);
9241 return false;
9242 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009243
9244 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +00009245 tg3_flag_set(tp, ENABLE_RSS);
Matt Carlsond78b59f2011-04-05 14:22:46 +00009246
9247 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9248 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Joe Perches63c3a662011-04-26 08:12:10 +00009249 tg3_flag_set(tp, ENABLE_TSS);
Matt Carlsonb92b9042010-11-24 08:31:51 +00009250 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9251 }
9252 }
Matt Carlson2430b032010-06-05 17:24:34 +00009253
Matt Carlson679563f2009-09-01 12:55:46 +00009254 return true;
9255}
9256
Matt Carlson07b01732009-08-28 14:01:15 +00009257static void tg3_ints_init(struct tg3 *tp)
9258{
Joe Perches63c3a662011-04-26 08:12:10 +00009259 if ((tg3_flag(tp, SUPPORT_MSI) || tg3_flag(tp, SUPPORT_MSIX)) &&
9260 !tg3_flag(tp, TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009261 /* All MSI supporting chips should support tagged
9262 * status. Assert that this is the case.
9263 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009264 netdev_warn(tp->dev,
9265 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009266 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009267 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009268
Joe Perches63c3a662011-04-26 08:12:10 +00009269 if (tg3_flag(tp, SUPPORT_MSIX) && tg3_enable_msix(tp))
9270 tg3_flag_set(tp, USING_MSIX);
9271 else if (tg3_flag(tp, SUPPORT_MSI) && pci_enable_msi(tp->pdev) == 0)
9272 tg3_flag_set(tp, USING_MSI);
Matt Carlson679563f2009-09-01 12:55:46 +00009273
Joe Perches63c3a662011-04-26 08:12:10 +00009274 if (tg3_flag(tp, USING_MSI) || tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009275 u32 msi_mode = tr32(MSGINT_MODE);
Joe Perches63c3a662011-04-26 08:12:10 +00009276 if (tg3_flag(tp, USING_MSIX) && tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009277 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009278 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9279 }
9280defcfg:
Joe Perches63c3a662011-04-26 08:12:10 +00009281 if (!tg3_flag(tp, USING_MSIX)) {
Matt Carlson679563f2009-09-01 12:55:46 +00009282 tp->irq_cnt = 1;
9283 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009284 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009285 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009286 }
Matt Carlson07b01732009-08-28 14:01:15 +00009287}
9288
9289static void tg3_ints_fini(struct tg3 *tp)
9290{
Joe Perches63c3a662011-04-26 08:12:10 +00009291 if (tg3_flag(tp, USING_MSIX))
Matt Carlson679563f2009-09-01 12:55:46 +00009292 pci_disable_msix(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009293 else if (tg3_flag(tp, USING_MSI))
Matt Carlson679563f2009-09-01 12:55:46 +00009294 pci_disable_msi(tp->pdev);
Joe Perches63c3a662011-04-26 08:12:10 +00009295 tg3_flag_clear(tp, USING_MSI);
9296 tg3_flag_clear(tp, USING_MSIX);
9297 tg3_flag_clear(tp, ENABLE_RSS);
9298 tg3_flag_clear(tp, ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009299}
9300
Linus Torvalds1da177e2005-04-16 15:20:36 -07009301static int tg3_open(struct net_device *dev)
9302{
9303 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009304 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009305
Matt Carlson9e9fd122009-01-19 16:57:45 -08009306 if (tp->fw_needed) {
9307 err = tg3_request_firmware(tp);
9308 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9309 if (err)
9310 return err;
9311 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009312 netdev_warn(tp->dev, "TSO capability disabled\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009313 tg3_flag_clear(tp, TSO_CAPABLE);
9314 } else if (!tg3_flag(tp, TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009315 netdev_notice(tp->dev, "TSO capability restored\n");
Joe Perches63c3a662011-04-26 08:12:10 +00009316 tg3_flag_set(tp, TSO_CAPABLE);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009317 }
9318 }
9319
Michael Chanc49a1562006-12-17 17:07:29 -08009320 netif_carrier_off(tp->dev);
9321
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009322 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009323 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009324 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009325
9326 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009327
Linus Torvalds1da177e2005-04-16 15:20:36 -07009328 tg3_disable_ints(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009329 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009330
David S. Millerf47c11e2005-06-24 20:18:35 -07009331 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009332
Matt Carlson679563f2009-09-01 12:55:46 +00009333 /*
9334 * Setup interrupts first so we know how
9335 * many NAPI resources to allocate
9336 */
9337 tg3_ints_init(tp);
9338
Linus Torvalds1da177e2005-04-16 15:20:36 -07009339 /* The placement of this call is tied
9340 * to the setup and use of Host TX descriptors.
9341 */
9342 err = tg3_alloc_consistent(tp);
9343 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009344 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009345
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009346 tg3_napi_init(tp);
9347
Matt Carlsonfed97812009-09-01 13:10:19 +00009348 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009349
Matt Carlson4f125f42009-09-01 12:55:02 +00009350 for (i = 0; i < tp->irq_cnt; i++) {
9351 struct tg3_napi *tnapi = &tp->napi[i];
9352 err = tg3_request_irq(tp, i);
9353 if (err) {
9354 for (i--; i >= 0; i--)
9355 free_irq(tnapi->irq_vec, tnapi);
9356 break;
9357 }
9358 }
Matt Carlson07b01732009-08-28 14:01:15 +00009359
9360 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009361 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009362
David S. Millerf47c11e2005-06-24 20:18:35 -07009363 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009364
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009365 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009366 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009367 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009368 tg3_free_rings(tp);
9369 } else {
Matt Carlson0e6cf6a2011-06-13 13:38:55 +00009370 if (tg3_flag(tp, TAGGED_STATUS) &&
9371 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717 &&
9372 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57765)
David S. Millerfac9b832005-05-18 22:46:34 -07009373 tp->timer_offset = HZ;
9374 else
9375 tp->timer_offset = HZ / 10;
9376
9377 BUG_ON(tp->timer_offset > HZ);
9378 tp->timer_counter = tp->timer_multiplier =
9379 (HZ / tp->timer_offset);
9380 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009381 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009382
9383 init_timer(&tp->timer);
9384 tp->timer.expires = jiffies + tp->timer_offset;
9385 tp->timer.data = (unsigned long) tp;
9386 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387 }
9388
David S. Millerf47c11e2005-06-24 20:18:35 -07009389 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009390
Matt Carlson07b01732009-08-28 14:01:15 +00009391 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009392 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009393
Joe Perches63c3a662011-04-26 08:12:10 +00009394 if (tg3_flag(tp, USING_MSI)) {
Michael Chan79381092005-04-21 17:13:59 -07009395 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009396
Michael Chan79381092005-04-21 17:13:59 -07009397 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009398 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009399 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009400 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009401 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009402
Matt Carlson679563f2009-09-01 12:55:46 +00009403 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009404 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009405
Joe Perches63c3a662011-04-26 08:12:10 +00009406 if (!tg3_flag(tp, 57765_PLUS) && tg3_flag(tp, USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009407 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009408
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009409 tw32(PCIE_TRANSACTION_CFG,
9410 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009411 }
Michael Chan79381092005-04-21 17:13:59 -07009412 }
9413
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009414 tg3_phy_start(tp);
9415
David S. Millerf47c11e2005-06-24 20:18:35 -07009416 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009417
Michael Chan79381092005-04-21 17:13:59 -07009418 add_timer(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +00009419 tg3_flag_set(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009420 tg3_enable_ints(tp);
9421
David S. Millerf47c11e2005-06-24 20:18:35 -07009422 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009423
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009424 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009425
Mahesh Bandewar06c03c02011-05-08 06:51:48 +00009426 /*
9427 * Reset loopback feature if it was turned on while the device was down
9428 * make sure that it's installed properly now.
9429 */
9430 if (dev->features & NETIF_F_LOOPBACK)
9431 tg3_set_loopback(dev, dev->features);
9432
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009434
Matt Carlson679563f2009-09-01 12:55:46 +00009435err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009436 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9437 struct tg3_napi *tnapi = &tp->napi[i];
9438 free_irq(tnapi->irq_vec, tnapi);
9439 }
Matt Carlson07b01732009-08-28 14:01:15 +00009440
Matt Carlson679563f2009-09-01 12:55:46 +00009441err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009442 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009443 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009444 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009445
9446err_out1:
9447 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009448 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009449}
9450
Eric Dumazet511d2222010-07-07 20:44:24 +00009451static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9452 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009453static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9454
9455static int tg3_close(struct net_device *dev)
9456{
Matt Carlson4f125f42009-09-01 12:55:02 +00009457 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009458 struct tg3 *tp = netdev_priv(dev);
9459
Matt Carlsonfed97812009-09-01 13:10:19 +00009460 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009461 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009462
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009463 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009464
9465 del_timer_sync(&tp->timer);
9466
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009467 tg3_phy_stop(tp);
9468
David S. Millerf47c11e2005-06-24 20:18:35 -07009469 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009470
9471 tg3_disable_ints(tp);
9472
Michael Chan944d9802005-05-29 14:57:48 -07009473 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009474 tg3_free_rings(tp);
Joe Perches63c3a662011-04-26 08:12:10 +00009475 tg3_flag_clear(tp, INIT_COMPLETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009476
David S. Millerf47c11e2005-06-24 20:18:35 -07009477 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009478
Matt Carlson4f125f42009-09-01 12:55:02 +00009479 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9480 struct tg3_napi *tnapi = &tp->napi[i];
9481 free_irq(tnapi->irq_vec, tnapi);
9482 }
Matt Carlson07b01732009-08-28 14:01:15 +00009483
9484 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009485
Eric Dumazet511d2222010-07-07 20:44:24 +00009486 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9487
Linus Torvalds1da177e2005-04-16 15:20:36 -07009488 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9489 sizeof(tp->estats_prev));
9490
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009491 tg3_napi_fini(tp);
9492
Linus Torvalds1da177e2005-04-16 15:20:36 -07009493 tg3_free_consistent(tp);
9494
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009495 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009496
9497 netif_carrier_off(tp->dev);
9498
Linus Torvalds1da177e2005-04-16 15:20:36 -07009499 return 0;
9500}
9501
Eric Dumazet511d2222010-07-07 20:44:24 +00009502static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009503{
9504 return ((u64)val->high << 32) | ((u64)val->low);
9505}
9506
Eric Dumazet511d2222010-07-07 20:44:24 +00009507static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009508{
9509 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9510
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009511 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009512 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9513 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009514 u32 val;
9515
David S. Millerf47c11e2005-06-24 20:18:35 -07009516 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009517 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9518 tg3_writephy(tp, MII_TG3_TEST1,
9519 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009520 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009521 } else
9522 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009523 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009524
9525 tp->phy_crc_errors += val;
9526
9527 return tp->phy_crc_errors;
9528 }
9529
9530 return get_stat64(&hw_stats->rx_fcs_errors);
9531}
9532
9533#define ESTAT_ADD(member) \
9534 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009535 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009536
9537static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9538{
9539 struct tg3_ethtool_stats *estats = &tp->estats;
9540 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9541 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9542
9543 if (!hw_stats)
9544 return old_estats;
9545
9546 ESTAT_ADD(rx_octets);
9547 ESTAT_ADD(rx_fragments);
9548 ESTAT_ADD(rx_ucast_packets);
9549 ESTAT_ADD(rx_mcast_packets);
9550 ESTAT_ADD(rx_bcast_packets);
9551 ESTAT_ADD(rx_fcs_errors);
9552 ESTAT_ADD(rx_align_errors);
9553 ESTAT_ADD(rx_xon_pause_rcvd);
9554 ESTAT_ADD(rx_xoff_pause_rcvd);
9555 ESTAT_ADD(rx_mac_ctrl_rcvd);
9556 ESTAT_ADD(rx_xoff_entered);
9557 ESTAT_ADD(rx_frame_too_long_errors);
9558 ESTAT_ADD(rx_jabbers);
9559 ESTAT_ADD(rx_undersize_packets);
9560 ESTAT_ADD(rx_in_length_errors);
9561 ESTAT_ADD(rx_out_length_errors);
9562 ESTAT_ADD(rx_64_or_less_octet_packets);
9563 ESTAT_ADD(rx_65_to_127_octet_packets);
9564 ESTAT_ADD(rx_128_to_255_octet_packets);
9565 ESTAT_ADD(rx_256_to_511_octet_packets);
9566 ESTAT_ADD(rx_512_to_1023_octet_packets);
9567 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9568 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9569 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9570 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9571 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9572
9573 ESTAT_ADD(tx_octets);
9574 ESTAT_ADD(tx_collisions);
9575 ESTAT_ADD(tx_xon_sent);
9576 ESTAT_ADD(tx_xoff_sent);
9577 ESTAT_ADD(tx_flow_control);
9578 ESTAT_ADD(tx_mac_errors);
9579 ESTAT_ADD(tx_single_collisions);
9580 ESTAT_ADD(tx_mult_collisions);
9581 ESTAT_ADD(tx_deferred);
9582 ESTAT_ADD(tx_excessive_collisions);
9583 ESTAT_ADD(tx_late_collisions);
9584 ESTAT_ADD(tx_collide_2times);
9585 ESTAT_ADD(tx_collide_3times);
9586 ESTAT_ADD(tx_collide_4times);
9587 ESTAT_ADD(tx_collide_5times);
9588 ESTAT_ADD(tx_collide_6times);
9589 ESTAT_ADD(tx_collide_7times);
9590 ESTAT_ADD(tx_collide_8times);
9591 ESTAT_ADD(tx_collide_9times);
9592 ESTAT_ADD(tx_collide_10times);
9593 ESTAT_ADD(tx_collide_11times);
9594 ESTAT_ADD(tx_collide_12times);
9595 ESTAT_ADD(tx_collide_13times);
9596 ESTAT_ADD(tx_collide_14times);
9597 ESTAT_ADD(tx_collide_15times);
9598 ESTAT_ADD(tx_ucast_packets);
9599 ESTAT_ADD(tx_mcast_packets);
9600 ESTAT_ADD(tx_bcast_packets);
9601 ESTAT_ADD(tx_carrier_sense_errors);
9602 ESTAT_ADD(tx_discards);
9603 ESTAT_ADD(tx_errors);
9604
9605 ESTAT_ADD(dma_writeq_full);
9606 ESTAT_ADD(dma_write_prioq_full);
9607 ESTAT_ADD(rxbds_empty);
9608 ESTAT_ADD(rx_discards);
9609 ESTAT_ADD(rx_errors);
9610 ESTAT_ADD(rx_threshold_hit);
9611
9612 ESTAT_ADD(dma_readq_full);
9613 ESTAT_ADD(dma_read_prioq_full);
9614 ESTAT_ADD(tx_comp_queue_full);
9615
9616 ESTAT_ADD(ring_set_send_prod_index);
9617 ESTAT_ADD(ring_status_update);
9618 ESTAT_ADD(nic_irqs);
9619 ESTAT_ADD(nic_avoided_irqs);
9620 ESTAT_ADD(nic_tx_threshold_hit);
9621
Matt Carlson4452d092011-05-19 12:12:51 +00009622 ESTAT_ADD(mbuf_lwm_thresh_hit);
9623
Linus Torvalds1da177e2005-04-16 15:20:36 -07009624 return estats;
9625}
9626
Eric Dumazet511d2222010-07-07 20:44:24 +00009627static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9628 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009629{
9630 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009631 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009632 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9633
9634 if (!hw_stats)
9635 return old_stats;
9636
9637 stats->rx_packets = old_stats->rx_packets +
9638 get_stat64(&hw_stats->rx_ucast_packets) +
9639 get_stat64(&hw_stats->rx_mcast_packets) +
9640 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009641
Linus Torvalds1da177e2005-04-16 15:20:36 -07009642 stats->tx_packets = old_stats->tx_packets +
9643 get_stat64(&hw_stats->tx_ucast_packets) +
9644 get_stat64(&hw_stats->tx_mcast_packets) +
9645 get_stat64(&hw_stats->tx_bcast_packets);
9646
9647 stats->rx_bytes = old_stats->rx_bytes +
9648 get_stat64(&hw_stats->rx_octets);
9649 stats->tx_bytes = old_stats->tx_bytes +
9650 get_stat64(&hw_stats->tx_octets);
9651
9652 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009653 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009654 stats->tx_errors = old_stats->tx_errors +
9655 get_stat64(&hw_stats->tx_errors) +
9656 get_stat64(&hw_stats->tx_mac_errors) +
9657 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9658 get_stat64(&hw_stats->tx_discards);
9659
9660 stats->multicast = old_stats->multicast +
9661 get_stat64(&hw_stats->rx_mcast_packets);
9662 stats->collisions = old_stats->collisions +
9663 get_stat64(&hw_stats->tx_collisions);
9664
9665 stats->rx_length_errors = old_stats->rx_length_errors +
9666 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9667 get_stat64(&hw_stats->rx_undersize_packets);
9668
9669 stats->rx_over_errors = old_stats->rx_over_errors +
9670 get_stat64(&hw_stats->rxbds_empty);
9671 stats->rx_frame_errors = old_stats->rx_frame_errors +
9672 get_stat64(&hw_stats->rx_align_errors);
9673 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9674 get_stat64(&hw_stats->tx_discards);
9675 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9676 get_stat64(&hw_stats->tx_carrier_sense_errors);
9677
9678 stats->rx_crc_errors = old_stats->rx_crc_errors +
9679 calc_crc_errors(tp);
9680
John W. Linville4f63b872005-09-12 14:43:18 -07009681 stats->rx_missed_errors = old_stats->rx_missed_errors +
9682 get_stat64(&hw_stats->rx_discards);
9683
Eric Dumazetb0057c52010-10-10 19:55:52 +00009684 stats->rx_dropped = tp->rx_dropped;
9685
Linus Torvalds1da177e2005-04-16 15:20:36 -07009686 return stats;
9687}
9688
9689static inline u32 calc_crc(unsigned char *buf, int len)
9690{
9691 u32 reg;
9692 u32 tmp;
9693 int j, k;
9694
9695 reg = 0xffffffff;
9696
9697 for (j = 0; j < len; j++) {
9698 reg ^= buf[j];
9699
9700 for (k = 0; k < 8; k++) {
9701 tmp = reg & 0x01;
9702
9703 reg >>= 1;
9704
Matt Carlson859a588792010-04-05 10:19:28 +00009705 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009706 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009707 }
9708 }
9709
9710 return ~reg;
9711}
9712
9713static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9714{
9715 /* accept or reject all multicast frames */
9716 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9717 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9718 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9719 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9720}
9721
9722static void __tg3_set_rx_mode(struct net_device *dev)
9723{
9724 struct tg3 *tp = netdev_priv(dev);
9725 u32 rx_mode;
9726
9727 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9728 RX_MODE_KEEP_VLAN_TAG);
9729
Matt Carlsonbf933c82011-01-25 15:58:49 +00009730#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009731 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9732 * flag clear.
9733 */
Joe Perches63c3a662011-04-26 08:12:10 +00009734 if (!tg3_flag(tp, ENABLE_ASF))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9736#endif
9737
9738 if (dev->flags & IFF_PROMISC) {
9739 /* Promiscuous mode. */
9740 rx_mode |= RX_MODE_PROMISC;
9741 } else if (dev->flags & IFF_ALLMULTI) {
9742 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009743 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009744 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009745 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009746 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009747 } else {
9748 /* Accept one or more multicast(s). */
Jiri Pirko22bedad32010-04-01 21:22:57 +00009749 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009750 u32 mc_filter[4] = { 0, };
9751 u32 regidx;
9752 u32 bit;
9753 u32 crc;
9754
Jiri Pirko22bedad32010-04-01 21:22:57 +00009755 netdev_for_each_mc_addr(ha, dev) {
9756 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009757 bit = ~crc & 0x7f;
9758 regidx = (bit & 0x60) >> 5;
9759 bit &= 0x1f;
9760 mc_filter[regidx] |= (1 << bit);
9761 }
9762
9763 tw32(MAC_HASH_REG_0, mc_filter[0]);
9764 tw32(MAC_HASH_REG_1, mc_filter[1]);
9765 tw32(MAC_HASH_REG_2, mc_filter[2]);
9766 tw32(MAC_HASH_REG_3, mc_filter[3]);
9767 }
9768
9769 if (rx_mode != tp->rx_mode) {
9770 tp->rx_mode = rx_mode;
9771 tw32_f(MAC_RX_MODE, rx_mode);
9772 udelay(10);
9773 }
9774}
9775
9776static void tg3_set_rx_mode(struct net_device *dev)
9777{
9778 struct tg3 *tp = netdev_priv(dev);
9779
Michael Chane75f7c92006-03-20 21:33:26 -08009780 if (!netif_running(dev))
9781 return;
9782
David S. Millerf47c11e2005-06-24 20:18:35 -07009783 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009784 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009785 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009786}
9787
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788static int tg3_get_regs_len(struct net_device *dev)
9789{
Matt Carlson97bd8e42011-04-13 11:05:04 +00009790 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009791}
9792
9793static void tg3_get_regs(struct net_device *dev,
9794 struct ethtool_regs *regs, void *_p)
9795{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009796 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009797
9798 regs->version = 0;
9799
Matt Carlson97bd8e42011-04-13 11:05:04 +00009800 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009801
Matt Carlson800960682010-08-02 11:26:06 +00009802 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009803 return;
9804
David S. Millerf47c11e2005-06-24 20:18:35 -07009805 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009806
Matt Carlson97bd8e42011-04-13 11:05:04 +00009807 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009808
David S. Millerf47c11e2005-06-24 20:18:35 -07009809 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810}
9811
9812static int tg3_get_eeprom_len(struct net_device *dev)
9813{
9814 struct tg3 *tp = netdev_priv(dev);
9815
9816 return tp->nvram_size;
9817}
9818
Linus Torvalds1da177e2005-04-16 15:20:36 -07009819static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9820{
9821 struct tg3 *tp = netdev_priv(dev);
9822 int ret;
9823 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009824 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009825 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009826
Joe Perches63c3a662011-04-26 08:12:10 +00009827 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +00009828 return -EINVAL;
9829
Matt Carlson800960682010-08-02 11:26:06 +00009830 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009831 return -EAGAIN;
9832
Linus Torvalds1da177e2005-04-16 15:20:36 -07009833 offset = eeprom->offset;
9834 len = eeprom->len;
9835 eeprom->len = 0;
9836
9837 eeprom->magic = TG3_EEPROM_MAGIC;
9838
9839 if (offset & 3) {
9840 /* adjustments to start on required 4 byte boundary */
9841 b_offset = offset & 3;
9842 b_count = 4 - b_offset;
9843 if (b_count > len) {
9844 /* i.e. offset=1 len=2 */
9845 b_count = len;
9846 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009847 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009848 if (ret)
9849 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009850 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009851 len -= b_count;
9852 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009853 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009854 }
9855
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009856 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009857 pd = &data[eeprom->len];
9858 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009859 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009860 if (ret) {
9861 eeprom->len += i;
9862 return ret;
9863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009864 memcpy(pd + i, &val, 4);
9865 }
9866 eeprom->len += i;
9867
9868 if (len & 3) {
9869 /* read last bytes not ending on 4 byte boundary */
9870 pd = &data[eeprom->len];
9871 b_count = len & 3;
9872 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009873 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009874 if (ret)
9875 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009876 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009877 eeprom->len += b_count;
9878 }
9879 return 0;
9880}
9881
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009882static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009883
9884static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9885{
9886 struct tg3 *tp = netdev_priv(dev);
9887 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009888 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009889 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009890 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009891
Matt Carlson800960682010-08-02 11:26:06 +00009892 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009893 return -EAGAIN;
9894
Joe Perches63c3a662011-04-26 08:12:10 +00009895 if (tg3_flag(tp, NO_NVRAM) ||
Matt Carlsondf259d82009-04-20 06:57:14 +00009896 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009897 return -EINVAL;
9898
9899 offset = eeprom->offset;
9900 len = eeprom->len;
9901
9902 if ((b_offset = (offset & 3))) {
9903 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009904 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009905 if (ret)
9906 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009907 len += b_offset;
9908 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009909 if (len < 4)
9910 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009911 }
9912
9913 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009914 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009915 /* adjustments to end on required 4 byte boundary */
9916 odd_len = 1;
9917 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009918 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009919 if (ret)
9920 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009921 }
9922
9923 buf = data;
9924 if (b_offset || odd_len) {
9925 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009926 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009927 return -ENOMEM;
9928 if (b_offset)
9929 memcpy(buf, &start, 4);
9930 if (odd_len)
9931 memcpy(buf+len-4, &end, 4);
9932 memcpy(buf + b_offset, data, eeprom->len);
9933 }
9934
9935 ret = tg3_nvram_write_block(tp, offset, len, buf);
9936
9937 if (buf != data)
9938 kfree(buf);
9939
9940 return ret;
9941}
9942
9943static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9944{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009945 struct tg3 *tp = netdev_priv(dev);
9946
Joe Perches63c3a662011-04-26 08:12:10 +00009947 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009948 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009949 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009950 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009951 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9952 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009953 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009954
Linus Torvalds1da177e2005-04-16 15:20:36 -07009955 cmd->supported = (SUPPORTED_Autoneg);
9956
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009957 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009958 cmd->supported |= (SUPPORTED_1000baseT_Half |
9959 SUPPORTED_1000baseT_Full);
9960
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009961 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009962 cmd->supported |= (SUPPORTED_100baseT_Half |
9963 SUPPORTED_100baseT_Full |
9964 SUPPORTED_10baseT_Half |
9965 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009966 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009967 cmd->port = PORT_TP;
9968 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009969 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009970 cmd->port = PORT_FIBRE;
9971 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009972
Linus Torvalds1da177e2005-04-16 15:20:36 -07009973 cmd->advertising = tp->link_config.advertising;
Matt Carlson5bb09772011-06-13 13:39:00 +00009974 if (tg3_flag(tp, PAUSE_AUTONEG)) {
9975 if (tp->link_config.flowctrl & FLOW_CTRL_RX) {
9976 if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
9977 cmd->advertising |= ADVERTISED_Pause;
9978 } else {
9979 cmd->advertising |= ADVERTISED_Pause |
9980 ADVERTISED_Asym_Pause;
9981 }
9982 } else if (tp->link_config.flowctrl & FLOW_CTRL_TX) {
9983 cmd->advertising |= ADVERTISED_Asym_Pause;
9984 }
9985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009986 if (netif_running(dev)) {
David Decotigny70739492011-04-27 18:32:40 +00009987 ethtool_cmd_speed_set(cmd, tp->link_config.active_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009988 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +00009989 } else {
David Decotigny70739492011-04-27 18:32:40 +00009990 ethtool_cmd_speed_set(cmd, SPEED_INVALID);
Matt Carlson64c22182010-10-14 10:37:44 +00009991 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009992 }
Matt Carlson882e9792009-09-01 13:21:36 +00009993 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009994 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009995 cmd->autoneg = tp->link_config.autoneg;
9996 cmd->maxtxpkt = 0;
9997 cmd->maxrxpkt = 0;
9998 return 0;
9999}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010000
Linus Torvalds1da177e2005-04-16 15:20:36 -070010001static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
10002{
10003 struct tg3 *tp = netdev_priv(dev);
David Decotigny25db0332011-04-27 18:32:39 +000010004 u32 speed = ethtool_cmd_speed(cmd);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010005
Joe Perches63c3a662011-04-26 08:12:10 +000010006 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010007 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010008 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010009 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010010 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10011 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010012 }
10013
Matt Carlson7e5856b2009-02-25 14:23:01 +000010014 if (cmd->autoneg != AUTONEG_ENABLE &&
10015 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010016 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010017
10018 if (cmd->autoneg == AUTONEG_DISABLE &&
10019 cmd->duplex != DUPLEX_FULL &&
10020 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010021 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010022
Matt Carlson7e5856b2009-02-25 14:23:01 +000010023 if (cmd->autoneg == AUTONEG_ENABLE) {
10024 u32 mask = ADVERTISED_Autoneg |
10025 ADVERTISED_Pause |
10026 ADVERTISED_Asym_Pause;
10027
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010028 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010029 mask |= ADVERTISED_1000baseT_Half |
10030 ADVERTISED_1000baseT_Full;
10031
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010032 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010033 mask |= ADVERTISED_100baseT_Half |
10034 ADVERTISED_100baseT_Full |
10035 ADVERTISED_10baseT_Half |
10036 ADVERTISED_10baseT_Full |
10037 ADVERTISED_TP;
10038 else
10039 mask |= ADVERTISED_FIBRE;
10040
10041 if (cmd->advertising & ~mask)
10042 return -EINVAL;
10043
10044 mask &= (ADVERTISED_1000baseT_Half |
10045 ADVERTISED_1000baseT_Full |
10046 ADVERTISED_100baseT_Half |
10047 ADVERTISED_100baseT_Full |
10048 ADVERTISED_10baseT_Half |
10049 ADVERTISED_10baseT_Full);
10050
10051 cmd->advertising &= mask;
10052 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010053 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
David Decotigny25db0332011-04-27 18:32:39 +000010054 if (speed != SPEED_1000)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010055 return -EINVAL;
10056
10057 if (cmd->duplex != DUPLEX_FULL)
10058 return -EINVAL;
10059 } else {
David Decotigny25db0332011-04-27 18:32:39 +000010060 if (speed != SPEED_100 &&
10061 speed != SPEED_10)
Matt Carlson7e5856b2009-02-25 14:23:01 +000010062 return -EINVAL;
10063 }
10064 }
10065
David S. Millerf47c11e2005-06-24 20:18:35 -070010066 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010067
10068 tp->link_config.autoneg = cmd->autoneg;
10069 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010070 tp->link_config.advertising = (cmd->advertising |
10071 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010072 tp->link_config.speed = SPEED_INVALID;
10073 tp->link_config.duplex = DUPLEX_INVALID;
10074 } else {
10075 tp->link_config.advertising = 0;
David Decotigny25db0332011-04-27 18:32:39 +000010076 tp->link_config.speed = speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010077 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010078 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010079
Michael Chan24fcad62006-12-17 17:06:46 -080010080 tp->link_config.orig_speed = tp->link_config.speed;
10081 tp->link_config.orig_duplex = tp->link_config.duplex;
10082 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10083
Linus Torvalds1da177e2005-04-16 15:20:36 -070010084 if (netif_running(dev))
10085 tg3_setup_phy(tp, 1);
10086
David S. Millerf47c11e2005-06-24 20:18:35 -070010087 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010088
Linus Torvalds1da177e2005-04-16 15:20:36 -070010089 return 0;
10090}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010091
Linus Torvalds1da177e2005-04-16 15:20:36 -070010092static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10093{
10094 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010095
Linus Torvalds1da177e2005-04-16 15:20:36 -070010096 strcpy(info->driver, DRV_MODULE_NAME);
10097 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010098 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010099 strcpy(info->bus_info, pci_name(tp->pdev));
10100}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010101
Linus Torvalds1da177e2005-04-16 15:20:36 -070010102static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10103{
10104 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010105
Joe Perches63c3a662011-04-26 08:12:10 +000010106 if (tg3_flag(tp, WOL_CAP) && device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010107 wol->supported = WAKE_MAGIC;
10108 else
10109 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010110 wol->wolopts = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010111 if (tg3_flag(tp, WOL_ENABLE) && device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010112 wol->wolopts = WAKE_MAGIC;
10113 memset(&wol->sopass, 0, sizeof(wol->sopass));
10114}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010115
Linus Torvalds1da177e2005-04-16 15:20:36 -070010116static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10117{
10118 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010119 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010120
Linus Torvalds1da177e2005-04-16 15:20:36 -070010121 if (wol->wolopts & ~WAKE_MAGIC)
10122 return -EINVAL;
10123 if ((wol->wolopts & WAKE_MAGIC) &&
Joe Perches63c3a662011-04-26 08:12:10 +000010124 !(tg3_flag(tp, WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010125 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010126
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010127 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10128
David S. Millerf47c11e2005-06-24 20:18:35 -070010129 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010130 if (device_may_wakeup(dp))
Joe Perches63c3a662011-04-26 08:12:10 +000010131 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010132 else
Joe Perches63c3a662011-04-26 08:12:10 +000010133 tg3_flag_clear(tp, WOL_ENABLE);
David S. Millerf47c11e2005-06-24 20:18:35 -070010134 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010135
Linus Torvalds1da177e2005-04-16 15:20:36 -070010136 return 0;
10137}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010138
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139static u32 tg3_get_msglevel(struct net_device *dev)
10140{
10141 struct tg3 *tp = netdev_priv(dev);
10142 return tp->msg_enable;
10143}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010144
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145static void tg3_set_msglevel(struct net_device *dev, u32 value)
10146{
10147 struct tg3 *tp = netdev_priv(dev);
10148 tp->msg_enable = value;
10149}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010150
Linus Torvalds1da177e2005-04-16 15:20:36 -070010151static int tg3_nway_reset(struct net_device *dev)
10152{
10153 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010154 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010155
Linus Torvalds1da177e2005-04-16 15:20:36 -070010156 if (!netif_running(dev))
10157 return -EAGAIN;
10158
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010159 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010160 return -EINVAL;
10161
Joe Perches63c3a662011-04-26 08:12:10 +000010162 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010163 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010164 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010165 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010166 } else {
10167 u32 bmcr;
10168
10169 spin_lock_bh(&tp->lock);
10170 r = -EINVAL;
10171 tg3_readphy(tp, MII_BMCR, &bmcr);
10172 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10173 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010174 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010175 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10176 BMCR_ANENABLE);
10177 r = 0;
10178 }
10179 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010180 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010181
Linus Torvalds1da177e2005-04-16 15:20:36 -070010182 return r;
10183}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010184
Linus Torvalds1da177e2005-04-16 15:20:36 -070010185static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10186{
10187 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010188
Matt Carlson2c49a442010-09-30 10:34:35 +000010189 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010190 ering->rx_mini_max_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010191 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Matt Carlson2c49a442010-09-30 10:34:35 +000010192 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010193 else
10194 ering->rx_jumbo_max_pending = 0;
10195
10196 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010197
10198 ering->rx_pending = tp->rx_pending;
10199 ering->rx_mini_pending = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010200 if (tg3_flag(tp, JUMBO_RING_ENABLE))
Michael Chan4f81c322006-03-20 21:33:42 -080010201 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10202 else
10203 ering->rx_jumbo_pending = 0;
10204
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010205 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010206}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010207
Linus Torvalds1da177e2005-04-16 15:20:36 -070010208static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10209{
10210 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010211 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010212
Matt Carlson2c49a442010-09-30 10:34:35 +000010213 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10214 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010215 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10216 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Joe Perches63c3a662011-04-26 08:12:10 +000010217 (tg3_flag(tp, TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010218 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010219 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010220
Michael Chanbbe832c2005-06-24 20:20:04 -070010221 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010222 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010223 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010224 irq_sync = 1;
10225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010226
Michael Chanbbe832c2005-06-24 20:20:04 -070010227 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010228
Linus Torvalds1da177e2005-04-16 15:20:36 -070010229 tp->rx_pending = ering->rx_pending;
10230
Joe Perches63c3a662011-04-26 08:12:10 +000010231 if (tg3_flag(tp, MAX_RXPEND_64) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070010232 tp->rx_pending > 63)
10233 tp->rx_pending = 63;
10234 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010235
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010236 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010237 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010238
10239 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010240 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010241 err = tg3_restart_hw(tp, 1);
10242 if (!err)
10243 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010244 }
10245
David S. Millerf47c11e2005-06-24 20:18:35 -070010246 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010247
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010248 if (irq_sync && !err)
10249 tg3_phy_start(tp);
10250
Michael Chanb9ec6c12006-07-25 16:37:27 -070010251 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010252}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010253
Linus Torvalds1da177e2005-04-16 15:20:36 -070010254static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10255{
10256 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010257
Joe Perches63c3a662011-04-26 08:12:10 +000010258 epause->autoneg = !!tg3_flag(tp, PAUSE_AUTONEG);
Matt Carlson8d018622007-12-20 20:05:44 -080010259
Steve Glendinninge18ce342008-12-16 02:00:00 -080010260 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010261 epause->rx_pause = 1;
10262 else
10263 epause->rx_pause = 0;
10264
Steve Glendinninge18ce342008-12-16 02:00:00 -080010265 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010266 epause->tx_pause = 1;
10267 else
10268 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010269}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010270
Linus Torvalds1da177e2005-04-16 15:20:36 -070010271static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10272{
10273 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010274 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010275
Joe Perches63c3a662011-04-26 08:12:10 +000010276 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson27121682010-02-17 15:16:57 +000010277 u32 newadv;
10278 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010279
Matt Carlson27121682010-02-17 15:16:57 +000010280 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010281
Matt Carlson27121682010-02-17 15:16:57 +000010282 if (!(phydev->supported & SUPPORTED_Pause) ||
10283 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010284 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010285 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010286
Matt Carlson27121682010-02-17 15:16:57 +000010287 tp->link_config.flowctrl = 0;
10288 if (epause->rx_pause) {
10289 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010290
Matt Carlson27121682010-02-17 15:16:57 +000010291 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010292 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010293 newadv = ADVERTISED_Pause;
10294 } else
10295 newadv = ADVERTISED_Pause |
10296 ADVERTISED_Asym_Pause;
10297 } else if (epause->tx_pause) {
10298 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10299 newadv = ADVERTISED_Asym_Pause;
10300 } else
10301 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010302
Matt Carlson27121682010-02-17 15:16:57 +000010303 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010304 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010305 else
Joe Perches63c3a662011-04-26 08:12:10 +000010306 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlson27121682010-02-17 15:16:57 +000010307
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010308 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010309 u32 oldadv = phydev->advertising &
10310 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10311 if (oldadv != newadv) {
10312 phydev->advertising &=
10313 ~(ADVERTISED_Pause |
10314 ADVERTISED_Asym_Pause);
10315 phydev->advertising |= newadv;
10316 if (phydev->autoneg) {
10317 /*
10318 * Always renegotiate the link to
10319 * inform our link partner of our
10320 * flow control settings, even if the
10321 * flow control is forced. Let
10322 * tg3_adjust_link() do the final
10323 * flow control setup.
10324 */
10325 return phy_start_aneg(phydev);
10326 }
10327 }
10328
10329 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010330 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010331 } else {
10332 tp->link_config.orig_advertising &=
10333 ~(ADVERTISED_Pause |
10334 ADVERTISED_Asym_Pause);
10335 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010336 }
10337 } else {
10338 int irq_sync = 0;
10339
10340 if (netif_running(dev)) {
10341 tg3_netif_stop(tp);
10342 irq_sync = 1;
10343 }
10344
10345 tg3_full_lock(tp, irq_sync);
10346
10347 if (epause->autoneg)
Joe Perches63c3a662011-04-26 08:12:10 +000010348 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010349 else
Joe Perches63c3a662011-04-26 08:12:10 +000010350 tg3_flag_clear(tp, PAUSE_AUTONEG);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010351 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010352 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010353 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010354 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010355 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010356 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010357 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010358 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010359
10360 if (netif_running(dev)) {
10361 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10362 err = tg3_restart_hw(tp, 1);
10363 if (!err)
10364 tg3_netif_start(tp);
10365 }
10366
10367 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010369
Michael Chanb9ec6c12006-07-25 16:37:27 -070010370 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010371}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010372
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010373static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010374{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010375 switch (sset) {
10376 case ETH_SS_TEST:
10377 return TG3_NUM_TEST;
10378 case ETH_SS_STATS:
10379 return TG3_NUM_STATS;
10380 default:
10381 return -EOPNOTSUPP;
10382 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010383}
10384
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010385static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010386{
10387 switch (stringset) {
10388 case ETH_SS_STATS:
10389 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10390 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010391 case ETH_SS_TEST:
10392 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10393 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010394 default:
10395 WARN_ON(1); /* we need a WARN() */
10396 break;
10397 }
10398}
10399
stephen hemminger81b87092011-04-04 08:43:50 +000010400static int tg3_set_phys_id(struct net_device *dev,
10401 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010402{
10403 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010404
10405 if (!netif_running(tp->dev))
10406 return -EAGAIN;
10407
stephen hemminger81b87092011-04-04 08:43:50 +000010408 switch (state) {
10409 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010410 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010411
stephen hemminger81b87092011-04-04 08:43:50 +000010412 case ETHTOOL_ID_ON:
10413 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10414 LED_CTRL_1000MBPS_ON |
10415 LED_CTRL_100MBPS_ON |
10416 LED_CTRL_10MBPS_ON |
10417 LED_CTRL_TRAFFIC_OVERRIDE |
10418 LED_CTRL_TRAFFIC_BLINK |
10419 LED_CTRL_TRAFFIC_LED);
10420 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010421
stephen hemminger81b87092011-04-04 08:43:50 +000010422 case ETHTOOL_ID_OFF:
10423 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10424 LED_CTRL_TRAFFIC_OVERRIDE);
10425 break;
Michael Chan4009a932005-09-05 17:52:54 -070010426
stephen hemminger81b87092011-04-04 08:43:50 +000010427 case ETHTOOL_ID_INACTIVE:
10428 tw32(MAC_LED_CTRL, tp->led_ctrl);
10429 break;
Michael Chan4009a932005-09-05 17:52:54 -070010430 }
stephen hemminger81b87092011-04-04 08:43:50 +000010431
Michael Chan4009a932005-09-05 17:52:54 -070010432 return 0;
10433}
10434
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010435static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010436 struct ethtool_stats *estats, u64 *tmp_stats)
10437{
10438 struct tg3 *tp = netdev_priv(dev);
10439 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10440}
10441
Matt Carlsonc3e94502011-04-13 11:05:08 +000010442static __be32 * tg3_vpd_readblock(struct tg3 *tp)
10443{
10444 int i;
10445 __be32 *buf;
10446 u32 offset = 0, len = 0;
10447 u32 magic, val;
10448
Joe Perches63c3a662011-04-26 08:12:10 +000010449 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &magic))
Matt Carlsonc3e94502011-04-13 11:05:08 +000010450 return NULL;
10451
10452 if (magic == TG3_EEPROM_MAGIC) {
10453 for (offset = TG3_NVM_DIR_START;
10454 offset < TG3_NVM_DIR_END;
10455 offset += TG3_NVM_DIRENT_SIZE) {
10456 if (tg3_nvram_read(tp, offset, &val))
10457 return NULL;
10458
10459 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10460 TG3_NVM_DIRTYPE_EXTVPD)
10461 break;
10462 }
10463
10464 if (offset != TG3_NVM_DIR_END) {
10465 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10466 if (tg3_nvram_read(tp, offset + 4, &offset))
10467 return NULL;
10468
10469 offset = tg3_nvram_logical_addr(tp, offset);
10470 }
10471 }
10472
10473 if (!offset || !len) {
10474 offset = TG3_NVM_VPD_OFF;
10475 len = TG3_NVM_VPD_LEN;
10476 }
10477
10478 buf = kmalloc(len, GFP_KERNEL);
10479 if (buf == NULL)
10480 return NULL;
10481
10482 if (magic == TG3_EEPROM_MAGIC) {
10483 for (i = 0; i < len; i += 4) {
10484 /* The data is in little-endian format in NVRAM.
10485 * Use the big-endian read routines to preserve
10486 * the byte order as it exists in NVRAM.
10487 */
10488 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10489 goto error;
10490 }
10491 } else {
10492 u8 *ptr;
10493 ssize_t cnt;
10494 unsigned int pos = 0;
10495
10496 ptr = (u8 *)&buf[0];
10497 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10498 cnt = pci_read_vpd(tp->pdev, pos,
10499 len - pos, ptr);
10500 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10501 cnt = 0;
10502 else if (cnt < 0)
10503 goto error;
10504 }
10505 if (pos != len)
10506 goto error;
10507 }
10508
10509 return buf;
10510
10511error:
10512 kfree(buf);
10513 return NULL;
10514}
10515
Michael Chan566f86a2005-05-29 14:56:58 -070010516#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010517#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10518#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10519#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Matt Carlson727a6d92011-06-13 13:38:58 +000010520#define NVRAM_SELFBOOT_FORMAT1_4_SIZE 0x20
10521#define NVRAM_SELFBOOT_FORMAT1_5_SIZE 0x24
10522#define NVRAM_SELFBOOT_FORMAT1_6_SIZE 0x4c
Michael Chanb16250e2006-09-27 16:10:14 -070010523#define NVRAM_SELFBOOT_HW_SIZE 0x20
10524#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010525
10526static int tg3_test_nvram(struct tg3 *tp)
10527{
Al Virob9fc7dc2007-12-17 22:59:57 -080010528 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010529 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010530 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010531
Joe Perches63c3a662011-04-26 08:12:10 +000010532 if (tg3_flag(tp, NO_NVRAM))
Matt Carlsondf259d82009-04-20 06:57:14 +000010533 return 0;
10534
Matt Carlsone4f34112009-02-25 14:25:00 +000010535 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010536 return -EIO;
10537
Michael Chan1b277772006-03-20 22:27:48 -080010538 if (magic == TG3_EEPROM_MAGIC)
10539 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010540 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010541 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10542 TG3_EEPROM_SB_FORMAT_1) {
10543 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10544 case TG3_EEPROM_SB_REVISION_0:
10545 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10546 break;
10547 case TG3_EEPROM_SB_REVISION_2:
10548 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10549 break;
10550 case TG3_EEPROM_SB_REVISION_3:
10551 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10552 break;
Matt Carlson727a6d92011-06-13 13:38:58 +000010553 case TG3_EEPROM_SB_REVISION_4:
10554 size = NVRAM_SELFBOOT_FORMAT1_4_SIZE;
10555 break;
10556 case TG3_EEPROM_SB_REVISION_5:
10557 size = NVRAM_SELFBOOT_FORMAT1_5_SIZE;
10558 break;
10559 case TG3_EEPROM_SB_REVISION_6:
10560 size = NVRAM_SELFBOOT_FORMAT1_6_SIZE;
10561 break;
Matt Carlsona5767de2007-11-12 21:10:58 -080010562 default:
Matt Carlson727a6d92011-06-13 13:38:58 +000010563 return -EIO;
Matt Carlsona5767de2007-11-12 21:10:58 -080010564 }
10565 } else
Michael Chan1b277772006-03-20 22:27:48 -080010566 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010567 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10568 size = NVRAM_SELFBOOT_HW_SIZE;
10569 else
Michael Chan1b277772006-03-20 22:27:48 -080010570 return -EIO;
10571
10572 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010573 if (buf == NULL)
10574 return -ENOMEM;
10575
Michael Chan1b277772006-03-20 22:27:48 -080010576 err = -EIO;
10577 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010578 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10579 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010580 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010581 }
Michael Chan1b277772006-03-20 22:27:48 -080010582 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010583 goto out;
10584
Michael Chan1b277772006-03-20 22:27:48 -080010585 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010586 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010587 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010588 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010589 u8 *buf8 = (u8 *) buf, csum8 = 0;
10590
Al Virob9fc7dc2007-12-17 22:59:57 -080010591 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010592 TG3_EEPROM_SB_REVISION_2) {
10593 /* For rev 2, the csum doesn't include the MBA. */
10594 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10595 csum8 += buf8[i];
10596 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10597 csum8 += buf8[i];
10598 } else {
10599 for (i = 0; i < size; i++)
10600 csum8 += buf8[i];
10601 }
Michael Chan1b277772006-03-20 22:27:48 -080010602
Adrian Bunkad96b482006-04-05 22:21:04 -070010603 if (csum8 == 0) {
10604 err = 0;
10605 goto out;
10606 }
10607
10608 err = -EIO;
10609 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010610 }
Michael Chan566f86a2005-05-29 14:56:58 -070010611
Al Virob9fc7dc2007-12-17 22:59:57 -080010612 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010613 TG3_EEPROM_MAGIC_HW) {
10614 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010615 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010616 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010617
10618 /* Separate the parity bits and the data bytes. */
10619 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10620 if ((i == 0) || (i == 8)) {
10621 int l;
10622 u8 msk;
10623
10624 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10625 parity[k++] = buf8[i] & msk;
10626 i++;
Matt Carlson859a588792010-04-05 10:19:28 +000010627 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010628 int l;
10629 u8 msk;
10630
10631 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10632 parity[k++] = buf8[i] & msk;
10633 i++;
10634
10635 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10636 parity[k++] = buf8[i] & msk;
10637 i++;
10638 }
10639 data[j++] = buf8[i];
10640 }
10641
10642 err = -EIO;
10643 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10644 u8 hw8 = hweight8(data[i]);
10645
10646 if ((hw8 & 0x1) && parity[i])
10647 goto out;
10648 else if (!(hw8 & 0x1) && !parity[i])
10649 goto out;
10650 }
10651 err = 0;
10652 goto out;
10653 }
10654
Matt Carlson01c3a392011-03-09 16:58:20 +000010655 err = -EIO;
10656
Michael Chan566f86a2005-05-29 14:56:58 -070010657 /* Bootstrap checksum at offset 0x10 */
10658 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010659 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010660 goto out;
10661
10662 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10663 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010664 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010665 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010666
Matt Carlsonc3e94502011-04-13 11:05:08 +000010667 kfree(buf);
10668
10669 buf = tg3_vpd_readblock(tp);
10670 if (!buf)
10671 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010672
10673 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10674 PCI_VPD_LRDT_RO_DATA);
10675 if (i > 0) {
10676 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10677 if (j < 0)
10678 goto out;
10679
10680 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10681 goto out;
10682
10683 i += PCI_VPD_LRDT_TAG_SIZE;
10684 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10685 PCI_VPD_RO_KEYWORD_CHKSUM);
10686 if (j > 0) {
10687 u8 csum8 = 0;
10688
10689 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10690
10691 for (i = 0; i <= j; i++)
10692 csum8 += ((u8 *)buf)[i];
10693
10694 if (csum8)
10695 goto out;
10696 }
10697 }
10698
Michael Chan566f86a2005-05-29 14:56:58 -070010699 err = 0;
10700
10701out:
10702 kfree(buf);
10703 return err;
10704}
10705
Michael Chanca430072005-05-29 14:57:23 -070010706#define TG3_SERDES_TIMEOUT_SEC 2
10707#define TG3_COPPER_TIMEOUT_SEC 6
10708
10709static int tg3_test_link(struct tg3 *tp)
10710{
10711 int i, max;
10712
10713 if (!netif_running(tp->dev))
10714 return -ENODEV;
10715
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010716 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010717 max = TG3_SERDES_TIMEOUT_SEC;
10718 else
10719 max = TG3_COPPER_TIMEOUT_SEC;
10720
10721 for (i = 0; i < max; i++) {
10722 if (netif_carrier_ok(tp->dev))
10723 return 0;
10724
10725 if (msleep_interruptible(1000))
10726 break;
10727 }
10728
10729 return -EIO;
10730}
10731
Michael Chana71116d2005-05-29 14:58:11 -070010732/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010733static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010734{
Michael Chanb16250e2006-09-27 16:10:14 -070010735 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010736 u32 offset, read_mask, write_mask, val, save_val, read_val;
10737 static struct {
10738 u16 offset;
10739 u16 flags;
10740#define TG3_FL_5705 0x1
10741#define TG3_FL_NOT_5705 0x2
10742#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010743#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010744 u32 read_mask;
10745 u32 write_mask;
10746 } reg_tbl[] = {
10747 /* MAC Control Registers */
10748 { MAC_MODE, TG3_FL_NOT_5705,
10749 0x00000000, 0x00ef6f8c },
10750 { MAC_MODE, TG3_FL_5705,
10751 0x00000000, 0x01ef6b8c },
10752 { MAC_STATUS, TG3_FL_NOT_5705,
10753 0x03800107, 0x00000000 },
10754 { MAC_STATUS, TG3_FL_5705,
10755 0x03800100, 0x00000000 },
10756 { MAC_ADDR_0_HIGH, 0x0000,
10757 0x00000000, 0x0000ffff },
10758 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010759 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010760 { MAC_RX_MTU_SIZE, 0x0000,
10761 0x00000000, 0x0000ffff },
10762 { MAC_TX_MODE, 0x0000,
10763 0x00000000, 0x00000070 },
10764 { MAC_TX_LENGTHS, 0x0000,
10765 0x00000000, 0x00003fff },
10766 { MAC_RX_MODE, TG3_FL_NOT_5705,
10767 0x00000000, 0x000007fc },
10768 { MAC_RX_MODE, TG3_FL_5705,
10769 0x00000000, 0x000007dc },
10770 { MAC_HASH_REG_0, 0x0000,
10771 0x00000000, 0xffffffff },
10772 { MAC_HASH_REG_1, 0x0000,
10773 0x00000000, 0xffffffff },
10774 { MAC_HASH_REG_2, 0x0000,
10775 0x00000000, 0xffffffff },
10776 { MAC_HASH_REG_3, 0x0000,
10777 0x00000000, 0xffffffff },
10778
10779 /* Receive Data and Receive BD Initiator Control Registers. */
10780 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10781 0x00000000, 0xffffffff },
10782 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10783 0x00000000, 0xffffffff },
10784 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10785 0x00000000, 0x00000003 },
10786 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10787 0x00000000, 0xffffffff },
10788 { RCVDBDI_STD_BD+0, 0x0000,
10789 0x00000000, 0xffffffff },
10790 { RCVDBDI_STD_BD+4, 0x0000,
10791 0x00000000, 0xffffffff },
10792 { RCVDBDI_STD_BD+8, 0x0000,
10793 0x00000000, 0xffff0002 },
10794 { RCVDBDI_STD_BD+0xc, 0x0000,
10795 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010796
Michael Chana71116d2005-05-29 14:58:11 -070010797 /* Receive BD Initiator Control Registers. */
10798 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10799 0x00000000, 0xffffffff },
10800 { RCVBDI_STD_THRESH, TG3_FL_5705,
10801 0x00000000, 0x000003ff },
10802 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10803 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010804
Michael Chana71116d2005-05-29 14:58:11 -070010805 /* Host Coalescing Control Registers. */
10806 { HOSTCC_MODE, TG3_FL_NOT_5705,
10807 0x00000000, 0x00000004 },
10808 { HOSTCC_MODE, TG3_FL_5705,
10809 0x00000000, 0x000000f6 },
10810 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10811 0x00000000, 0xffffffff },
10812 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10813 0x00000000, 0x000003ff },
10814 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10815 0x00000000, 0xffffffff },
10816 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10817 0x00000000, 0x000003ff },
10818 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10819 0x00000000, 0xffffffff },
10820 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10821 0x00000000, 0x000000ff },
10822 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10823 0x00000000, 0xffffffff },
10824 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10825 0x00000000, 0x000000ff },
10826 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10827 0x00000000, 0xffffffff },
10828 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10829 0x00000000, 0xffffffff },
10830 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10831 0x00000000, 0xffffffff },
10832 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10833 0x00000000, 0x000000ff },
10834 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10835 0x00000000, 0xffffffff },
10836 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10837 0x00000000, 0x000000ff },
10838 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10839 0x00000000, 0xffffffff },
10840 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10841 0x00000000, 0xffffffff },
10842 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10843 0x00000000, 0xffffffff },
10844 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10845 0x00000000, 0xffffffff },
10846 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10847 0x00000000, 0xffffffff },
10848 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10849 0xffffffff, 0x00000000 },
10850 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10851 0xffffffff, 0x00000000 },
10852
10853 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010854 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010855 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010856 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010857 0x00000000, 0x007fffff },
10858 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10859 0x00000000, 0x0000003f },
10860 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10861 0x00000000, 0x000001ff },
10862 { BUFMGR_MB_HIGH_WATER, 0x0000,
10863 0x00000000, 0x000001ff },
10864 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10865 0xffffffff, 0x00000000 },
10866 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10867 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010868
Michael Chana71116d2005-05-29 14:58:11 -070010869 /* Mailbox Registers */
10870 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10871 0x00000000, 0x000001ff },
10872 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10873 0x00000000, 0x000001ff },
10874 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10875 0x00000000, 0x000007ff },
10876 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10877 0x00000000, 0x000001ff },
10878
10879 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10880 };
10881
Michael Chanb16250e2006-09-27 16:10:14 -070010882 is_5705 = is_5750 = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000010883 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chana71116d2005-05-29 14:58:11 -070010884 is_5705 = 1;
Joe Perches63c3a662011-04-26 08:12:10 +000010885 if (tg3_flag(tp, 5750_PLUS))
Michael Chanb16250e2006-09-27 16:10:14 -070010886 is_5750 = 1;
10887 }
Michael Chana71116d2005-05-29 14:58:11 -070010888
10889 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10890 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10891 continue;
10892
10893 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10894 continue;
10895
Joe Perches63c3a662011-04-26 08:12:10 +000010896 if (tg3_flag(tp, IS_5788) &&
Michael Chana71116d2005-05-29 14:58:11 -070010897 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10898 continue;
10899
Michael Chanb16250e2006-09-27 16:10:14 -070010900 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10901 continue;
10902
Michael Chana71116d2005-05-29 14:58:11 -070010903 offset = (u32) reg_tbl[i].offset;
10904 read_mask = reg_tbl[i].read_mask;
10905 write_mask = reg_tbl[i].write_mask;
10906
10907 /* Save the original register content */
10908 save_val = tr32(offset);
10909
10910 /* Determine the read-only value. */
10911 read_val = save_val & read_mask;
10912
10913 /* Write zero to the register, then make sure the read-only bits
10914 * are not changed and the read/write bits are all zeros.
10915 */
10916 tw32(offset, 0);
10917
10918 val = tr32(offset);
10919
10920 /* Test the read-only and read/write bits. */
10921 if (((val & read_mask) != read_val) || (val & write_mask))
10922 goto out;
10923
10924 /* Write ones to all the bits defined by RdMask and WrMask, then
10925 * make sure the read-only bits are not changed and the
10926 * read/write bits are all ones.
10927 */
10928 tw32(offset, read_mask | write_mask);
10929
10930 val = tr32(offset);
10931
10932 /* Test the read-only bits. */
10933 if ((val & read_mask) != read_val)
10934 goto out;
10935
10936 /* Test the read/write bits. */
10937 if ((val & write_mask) != write_mask)
10938 goto out;
10939
10940 tw32(offset, save_val);
10941 }
10942
10943 return 0;
10944
10945out:
Michael Chan9f88f292006-12-07 00:22:54 -080010946 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010947 netdev_err(tp->dev,
10948 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010949 tw32(offset, save_val);
10950 return -EIO;
10951}
10952
Michael Chan7942e1d2005-05-29 14:58:36 -070010953static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10954{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010955 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010956 int i;
10957 u32 j;
10958
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010959 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010960 for (j = 0; j < len; j += 4) {
10961 u32 val;
10962
10963 tg3_write_mem(tp, offset + j, test_pattern[i]);
10964 tg3_read_mem(tp, offset + j, &val);
10965 if (val != test_pattern[i])
10966 return -EIO;
10967 }
10968 }
10969 return 0;
10970}
10971
10972static int tg3_test_memory(struct tg3 *tp)
10973{
10974 static struct mem_entry {
10975 u32 offset;
10976 u32 len;
10977 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010978 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010979 { 0x00002000, 0x1c000},
10980 { 0xffffffff, 0x00000}
10981 }, mem_tbl_5705[] = {
10982 { 0x00000100, 0x0000c},
10983 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010984 { 0x00004000, 0x00800},
10985 { 0x00006000, 0x01000},
10986 { 0x00008000, 0x02000},
10987 { 0x00010000, 0x0e000},
10988 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010989 }, mem_tbl_5755[] = {
10990 { 0x00000200, 0x00008},
10991 { 0x00004000, 0x00800},
10992 { 0x00006000, 0x00800},
10993 { 0x00008000, 0x02000},
10994 { 0x00010000, 0x0c000},
10995 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010996 }, mem_tbl_5906[] = {
10997 { 0x00000200, 0x00008},
10998 { 0x00004000, 0x00400},
10999 { 0x00006000, 0x00400},
11000 { 0x00008000, 0x01000},
11001 { 0x00010000, 0x01000},
11002 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011003 }, mem_tbl_5717[] = {
11004 { 0x00000200, 0x00008},
11005 { 0x00010000, 0x0a000},
11006 { 0x00020000, 0x13c00},
11007 { 0xffffffff, 0x00000}
11008 }, mem_tbl_57765[] = {
11009 { 0x00000200, 0x00008},
11010 { 0x00004000, 0x00800},
11011 { 0x00006000, 0x09800},
11012 { 0x00010000, 0x0a000},
11013 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070011014 };
11015 struct mem_entry *mem_tbl;
11016 int err = 0;
11017 int i;
11018
Joe Perches63c3a662011-04-26 08:12:10 +000011019 if (tg3_flag(tp, 5717_PLUS))
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011020 mem_tbl = mem_tbl_5717;
11021 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11022 mem_tbl = mem_tbl_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000011023 else if (tg3_flag(tp, 5755_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011024 mem_tbl = mem_tbl_5755;
11025 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11026 mem_tbl = mem_tbl_5906;
Joe Perches63c3a662011-04-26 08:12:10 +000011027 else if (tg3_flag(tp, 5705_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080011028 mem_tbl = mem_tbl_5705;
11029 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011030 mem_tbl = mem_tbl_570x;
11031
11032 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011033 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11034 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011035 break;
11036 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011037
Michael Chan7942e1d2005-05-29 14:58:36 -070011038 return err;
11039}
11040
Michael Chan9f40dea2005-09-05 17:53:06 -070011041#define TG3_MAC_LOOPBACK 0
11042#define TG3_PHY_LOOPBACK 1
Matt Carlsonbb158d62011-04-25 12:42:47 +000011043#define TG3_TSO_LOOPBACK 2
11044
11045#define TG3_TSO_MSS 500
11046
11047#define TG3_TSO_IP_HDR_LEN 20
11048#define TG3_TSO_TCP_HDR_LEN 20
11049#define TG3_TSO_TCP_OPT_LEN 12
11050
11051static const u8 tg3_tso_header[] = {
110520x08, 0x00,
110530x45, 0x00, 0x00, 0x00,
110540x00, 0x00, 0x40, 0x00,
110550x40, 0x06, 0x00, 0x00,
110560x0a, 0x00, 0x00, 0x01,
110570x0a, 0x00, 0x00, 0x02,
110580x0d, 0x00, 0xe0, 0x00,
110590x00, 0x00, 0x01, 0x00,
110600x00, 0x00, 0x02, 0x00,
110610x80, 0x10, 0x10, 0x00,
110620x14, 0x09, 0x00, 0x00,
110630x01, 0x01, 0x08, 0x0a,
110640x11, 0x11, 0x11, 0x11,
110650x11, 0x11, 0x11, 0x11,
11066};
Michael Chan9f40dea2005-09-05 17:53:06 -070011067
Matt Carlson4852a862011-04-13 11:05:07 +000011068static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070011069{
Michael Chan9f40dea2005-09-05 17:53:06 -070011070 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonbb158d62011-04-25 12:42:47 +000011071 u32 base_flags = 0, mss = 0, desc_idx, coal_now, data_off, val;
Michael Chanc76949a2005-05-29 14:58:59 -070011072 struct sk_buff *skb, *rx_skb;
11073 u8 *tx_data;
11074 dma_addr_t map;
11075 int num_pkts, tx_len, rx_len, i, err;
11076 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011077 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011078 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011079
Matt Carlsonc8873402010-02-12 14:47:11 +000011080 tnapi = &tp->napi[0];
11081 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011082 if (tp->irq_cnt > 1) {
Joe Perches63c3a662011-04-26 08:12:10 +000011083 if (tg3_flag(tp, ENABLE_RSS))
Matt Carlson1da85aa2010-09-30 10:34:34 +000011084 rnapi = &tp->napi[1];
Joe Perches63c3a662011-04-26 08:12:10 +000011085 if (tg3_flag(tp, ENABLE_TSS))
Matt Carlsonc8873402010-02-12 14:47:11 +000011086 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011087 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011088 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011089
Michael Chan9f40dea2005-09-05 17:53:06 -070011090 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070011091 /* HW errata - mac loopback fails in some cases on 5780.
11092 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000011093 * errata. Also, the MAC loopback test is deprecated for
11094 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070011095 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000011096 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011097 tg3_flag(tp, CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070011098 return 0;
11099
Matt Carlson49692ca2011-01-25 15:58:52 +000011100 mac_mode = tp->mac_mode &
11101 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
11102 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Joe Perches63c3a662011-04-26 08:12:10 +000011103 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011104 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011105 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070011106 mac_mode |= MAC_MODE_PORT_MODE_MII;
11107 else
11108 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070011109 tw32(MAC_MODE, mac_mode);
Matt Carlsonbb158d62011-04-25 12:42:47 +000011110 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011111 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000011112 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080011113 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
11114 } else
11115 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070011116
Matt Carlson9ef8ca92007-07-11 19:48:29 -070011117 tg3_phy_toggle_automdix(tp, 0);
11118
Michael Chan3f7045c2006-09-27 16:02:29 -070011119 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070011120 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080011121
Matt Carlson49692ca2011-01-25 15:58:52 +000011122 mac_mode = tp->mac_mode &
11123 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011124 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000011125 tg3_writephy(tp, MII_TG3_FET_PTEST,
11126 MII_TG3_FET_PTEST_FRC_TX_LINK |
11127 MII_TG3_FET_PTEST_FRC_TX_LOCK);
11128 /* The write needs to be flushed for the AC131 */
11129 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11130 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080011131 mac_mode |= MAC_MODE_PORT_MODE_MII;
11132 } else
11133 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070011134
Michael Chanc94e3942005-09-27 12:12:42 -070011135 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011136 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070011137 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
11138 udelay(10);
11139 tw32_f(MAC_RX_MODE, tp->rx_mode);
11140 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011141 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000011142 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
11143 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011144 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000011145 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011146 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080011147 tg3_writephy(tp, MII_TG3_EXT_CTRL,
11148 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
11149 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011150 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000011151
11152 /* Wait for link */
11153 for (i = 0; i < 100; i++) {
11154 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11155 break;
11156 mdelay(1);
11157 }
Matt Carlson859a588792010-04-05 10:19:28 +000011158 }
Michael Chanc76949a2005-05-29 14:58:59 -070011159
11160 err = -EIO;
11161
Matt Carlson4852a862011-04-13 11:05:07 +000011162 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011163 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011164 if (!skb)
11165 return -ENOMEM;
11166
Michael Chanc76949a2005-05-29 14:58:59 -070011167 tx_data = skb_put(skb, tx_len);
11168 memcpy(tx_data, tp->dev->dev_addr, 6);
11169 memset(tx_data + 6, 0x0, 8);
11170
Matt Carlson4852a862011-04-13 11:05:07 +000011171 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011172
Matt Carlsonbb158d62011-04-25 12:42:47 +000011173 if (loopback_mode == TG3_TSO_LOOPBACK) {
11174 struct iphdr *iph = (struct iphdr *)&tx_data[ETH_HLEN];
11175
11176 u32 hdr_len = TG3_TSO_IP_HDR_LEN + TG3_TSO_TCP_HDR_LEN +
11177 TG3_TSO_TCP_OPT_LEN;
11178
11179 memcpy(tx_data + ETH_ALEN * 2, tg3_tso_header,
11180 sizeof(tg3_tso_header));
11181 mss = TG3_TSO_MSS;
11182
11183 val = tx_len - ETH_ALEN * 2 - sizeof(tg3_tso_header);
11184 num_pkts = DIV_ROUND_UP(val, TG3_TSO_MSS);
11185
11186 /* Set the total length field in the IP header */
11187 iph->tot_len = htons((u16)(mss + hdr_len));
11188
11189 base_flags = (TXD_FLAG_CPU_PRE_DMA |
11190 TXD_FLAG_CPU_POST_DMA);
11191
Joe Perches63c3a662011-04-26 08:12:10 +000011192 if (tg3_flag(tp, HW_TSO_1) ||
11193 tg3_flag(tp, HW_TSO_2) ||
11194 tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011195 struct tcphdr *th;
11196 val = ETH_HLEN + TG3_TSO_IP_HDR_LEN;
11197 th = (struct tcphdr *)&tx_data[val];
11198 th->check = 0;
11199 } else
11200 base_flags |= TXD_FLAG_TCPUDP_CSUM;
11201
Joe Perches63c3a662011-04-26 08:12:10 +000011202 if (tg3_flag(tp, HW_TSO_3)) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011203 mss |= (hdr_len & 0xc) << 12;
11204 if (hdr_len & 0x10)
11205 base_flags |= 0x00000010;
11206 base_flags |= (hdr_len & 0x3e0) << 5;
Joe Perches63c3a662011-04-26 08:12:10 +000011207 } else if (tg3_flag(tp, HW_TSO_2))
Matt Carlsonbb158d62011-04-25 12:42:47 +000011208 mss |= hdr_len << 9;
Joe Perches63c3a662011-04-26 08:12:10 +000011209 else if (tg3_flag(tp, HW_TSO_1) ||
Matt Carlsonbb158d62011-04-25 12:42:47 +000011210 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
11211 mss |= (TG3_TSO_TCP_OPT_LEN << 9);
11212 } else {
11213 base_flags |= (TG3_TSO_TCP_OPT_LEN << 10);
11214 }
11215
11216 data_off = ETH_ALEN * 2 + sizeof(tg3_tso_header);
11217 } else {
11218 num_pkts = 1;
11219 data_off = ETH_HLEN;
11220 }
11221
11222 for (i = data_off; i < tx_len; i++)
Michael Chanc76949a2005-05-29 14:58:59 -070011223 tx_data[i] = (u8) (i & 0xff);
11224
Alexander Duyckf4188d82009-12-02 16:48:38 +000011225 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11226 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011227 dev_kfree_skb(skb);
11228 return -EIO;
11229 }
Michael Chanc76949a2005-05-29 14:58:59 -070011230
11231 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011232 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011233
11234 udelay(10);
11235
Matt Carlson898a56f2009-08-28 14:02:40 +000011236 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011237
Matt Carlsonbb158d62011-04-25 12:42:47 +000011238 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len,
11239 base_flags, (mss << 1) | 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011240
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011241 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011242
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011243 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11244 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011245
11246 udelay(10);
11247
Matt Carlson303fc922009-11-02 14:27:34 +000011248 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11249 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011250 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011251 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011252
11253 udelay(10);
11254
Matt Carlson898a56f2009-08-28 14:02:40 +000011255 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11256 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011257 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011258 (rx_idx == (rx_start_idx + num_pkts)))
11259 break;
11260 }
11261
Alexander Duyckf4188d82009-12-02 16:48:38 +000011262 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011263 dev_kfree_skb(skb);
11264
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011265 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011266 goto out;
11267
11268 if (rx_idx != rx_start_idx + num_pkts)
11269 goto out;
11270
Matt Carlsonbb158d62011-04-25 12:42:47 +000011271 val = data_off;
11272 while (rx_idx != rx_start_idx) {
11273 desc = &rnapi->rx_rcb[rx_start_idx++];
11274 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11275 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011276
Matt Carlsonbb158d62011-04-25 12:42:47 +000011277 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11278 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
Matt Carlson4852a862011-04-13 11:05:07 +000011279 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011280
Matt Carlsonbb158d62011-04-25 12:42:47 +000011281 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT)
11282 - ETH_FCS_LEN;
11283
11284 if (loopback_mode != TG3_TSO_LOOPBACK) {
11285 if (rx_len != tx_len)
11286 goto out;
11287
11288 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11289 if (opaque_key != RXD_OPAQUE_RING_STD)
11290 goto out;
11291 } else {
11292 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11293 goto out;
11294 }
11295 } else if ((desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
11296 (desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
Matt Carlson54e0a672011-05-19 12:12:50 +000011297 >> RXD_TCPCSUM_SHIFT != 0xffff) {
Matt Carlsonbb158d62011-04-25 12:42:47 +000011298 goto out;
11299 }
11300
11301 if (opaque_key == RXD_OPAQUE_RING_STD) {
11302 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11303 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx],
11304 mapping);
11305 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
11306 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11307 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx],
11308 mapping);
11309 } else
Matt Carlson4852a862011-04-13 11:05:07 +000011310 goto out;
11311
Matt Carlsonbb158d62011-04-25 12:42:47 +000011312 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len,
11313 PCI_DMA_FROMDEVICE);
11314
11315 for (i = data_off; i < rx_len; i++, val++) {
11316 if (*(rx_skb->data + i) != (u8) (val & 0xff))
11317 goto out;
11318 }
Matt Carlson4852a862011-04-13 11:05:07 +000011319 }
11320
Michael Chanc76949a2005-05-29 14:58:59 -070011321 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011322
Michael Chanc76949a2005-05-29 14:58:59 -070011323 /* tg3_free_rings will unmap and free the rx_skb */
11324out:
11325 return err;
11326}
11327
Matt Carlson00c266b2011-04-25 12:42:46 +000011328#define TG3_STD_LOOPBACK_FAILED 1
11329#define TG3_JMB_LOOPBACK_FAILED 2
Matt Carlsonbb158d62011-04-25 12:42:47 +000011330#define TG3_TSO_LOOPBACK_FAILED 4
Matt Carlson00c266b2011-04-25 12:42:46 +000011331
11332#define TG3_MAC_LOOPBACK_SHIFT 0
11333#define TG3_PHY_LOOPBACK_SHIFT 4
Matt Carlsonbb158d62011-04-25 12:42:47 +000011334#define TG3_LOOPBACK_FAILED 0x00000077
Michael Chan9f40dea2005-09-05 17:53:06 -070011335
11336static int tg3_test_loopback(struct tg3 *tp)
11337{
11338 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011339 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011340
11341 if (!netif_running(tp->dev))
11342 return TG3_LOOPBACK_FAILED;
11343
Matt Carlsonab789042011-01-25 15:58:54 +000011344 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11345 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11346
Michael Chanb9ec6c12006-07-25 16:37:27 -070011347 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011348 if (err) {
11349 err = TG3_LOOPBACK_FAILED;
11350 goto done;
11351 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011352
Joe Perches63c3a662011-04-26 08:12:10 +000011353 if (tg3_flag(tp, ENABLE_RSS)) {
Matt Carlson4a85f092011-04-20 07:57:37 +000011354 int i;
11355
11356 /* Reroute all rx packets to the 1st queue */
11357 for (i = MAC_RSS_INDIR_TBL_0;
11358 i < MAC_RSS_INDIR_TBL_0 + TG3_RSS_INDIR_TBL_SIZE; i += 4)
11359 tw32(i, 0x0);
11360 }
11361
Matt Carlson6833c042008-11-21 17:18:59 -080011362 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011363 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011364 tg3_phy_toggle_apd(tp, false);
11365
Joe Perches63c3a662011-04-26 08:12:10 +000011366 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011367 int i;
11368 u32 status;
11369
11370 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11371
11372 /* Wait for up to 40 microseconds to acquire lock. */
11373 for (i = 0; i < 4; i++) {
11374 status = tr32(TG3_CPMU_MUTEX_GNT);
11375 if (status == CPMU_MUTEX_GNT_DRIVER)
11376 break;
11377 udelay(10);
11378 }
11379
Matt Carlsonab789042011-01-25 15:58:54 +000011380 if (status != CPMU_MUTEX_GNT_DRIVER) {
11381 err = TG3_LOOPBACK_FAILED;
11382 goto done;
11383 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011384
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011385 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011386 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011387 tw32(TG3_CPMU_CTRL,
11388 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11389 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011390 }
11391
Matt Carlson4852a862011-04-13 11:05:07 +000011392 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011393 err |= TG3_STD_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011394
Joe Perches63c3a662011-04-26 08:12:10 +000011395 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011396 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_MAC_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011397 err |= TG3_JMB_LOOPBACK_FAILED << TG3_MAC_LOOPBACK_SHIFT;
Matt Carlson4852a862011-04-13 11:05:07 +000011398
Joe Perches63c3a662011-04-26 08:12:10 +000011399 if (tg3_flag(tp, CPMU_PRESENT)) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011400 tw32(TG3_CPMU_CTRL, cpmuctrl);
11401
11402 /* Release the mutex */
11403 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11404 }
11405
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011406 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000011407 !tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson4852a862011-04-13 11:05:07 +000011408 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011409 err |= TG3_STD_LOOPBACK_FAILED <<
11410 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011411 if (tg3_flag(tp, TSO_CAPABLE) &&
Matt Carlsonbb158d62011-04-25 12:42:47 +000011412 tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_TSO_LOOPBACK))
11413 err |= TG3_TSO_LOOPBACK_FAILED <<
11414 TG3_PHY_LOOPBACK_SHIFT;
Joe Perches63c3a662011-04-26 08:12:10 +000011415 if (tg3_flag(tp, JUMBO_RING_ENABLE) &&
Matt Carlson4852a862011-04-13 11:05:07 +000011416 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_PHY_LOOPBACK))
Matt Carlson00c266b2011-04-25 12:42:46 +000011417 err |= TG3_JMB_LOOPBACK_FAILED <<
11418 TG3_PHY_LOOPBACK_SHIFT;
Michael Chan9f40dea2005-09-05 17:53:06 -070011419 }
11420
Matt Carlson6833c042008-11-21 17:18:59 -080011421 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011422 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011423 tg3_phy_toggle_apd(tp, true);
11424
Matt Carlsonab789042011-01-25 15:58:54 +000011425done:
11426 tp->phy_flags |= eee_cap;
11427
Michael Chan9f40dea2005-09-05 17:53:06 -070011428 return err;
11429}
11430
Michael Chan4cafd3f2005-05-29 14:56:34 -070011431static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11432 u64 *data)
11433{
Michael Chan566f86a2005-05-29 14:56:58 -070011434 struct tg3 *tp = netdev_priv(dev);
11435
Matt Carlson800960682010-08-02 11:26:06 +000011436 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011437 tg3_power_up(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011438
Michael Chan566f86a2005-05-29 14:56:58 -070011439 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11440
11441 if (tg3_test_nvram(tp) != 0) {
11442 etest->flags |= ETH_TEST_FL_FAILED;
11443 data[0] = 1;
11444 }
Michael Chanca430072005-05-29 14:57:23 -070011445 if (tg3_test_link(tp) != 0) {
11446 etest->flags |= ETH_TEST_FL_FAILED;
11447 data[1] = 1;
11448 }
Michael Chana71116d2005-05-29 14:58:11 -070011449 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011450 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011451
Michael Chanbbe832c2005-06-24 20:20:04 -070011452 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011453 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011454 tg3_netif_stop(tp);
11455 irq_sync = 1;
11456 }
11457
11458 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011459
11460 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011461 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011462 tg3_halt_cpu(tp, RX_CPU_BASE);
Joe Perches63c3a662011-04-26 08:12:10 +000011463 if (!tg3_flag(tp, 5705_PLUS))
Michael Chana71116d2005-05-29 14:58:11 -070011464 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011465 if (!err)
11466 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011467
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011468 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011469 tg3_phy_reset(tp);
11470
Michael Chana71116d2005-05-29 14:58:11 -070011471 if (tg3_test_registers(tp) != 0) {
11472 etest->flags |= ETH_TEST_FL_FAILED;
11473 data[2] = 1;
11474 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011475 if (tg3_test_memory(tp) != 0) {
11476 etest->flags |= ETH_TEST_FL_FAILED;
11477 data[3] = 1;
11478 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011479 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011480 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011481
David S. Millerf47c11e2005-06-24 20:18:35 -070011482 tg3_full_unlock(tp);
11483
Michael Chand4bc3922005-05-29 14:59:20 -070011484 if (tg3_test_interrupt(tp) != 0) {
11485 etest->flags |= ETH_TEST_FL_FAILED;
11486 data[5] = 1;
11487 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011488
11489 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011490
Michael Chana71116d2005-05-29 14:58:11 -070011491 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11492 if (netif_running(dev)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011493 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011494 err2 = tg3_restart_hw(tp, 1);
11495 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011496 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011497 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011498
11499 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011500
11501 if (irq_sync && !err2)
11502 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011503 }
Matt Carlson800960682010-08-02 11:26:06 +000011504 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011505 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011506
Michael Chan4cafd3f2005-05-29 14:56:34 -070011507}
11508
Linus Torvalds1da177e2005-04-16 15:20:36 -070011509static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11510{
11511 struct mii_ioctl_data *data = if_mii(ifr);
11512 struct tg3 *tp = netdev_priv(dev);
11513 int err;
11514
Joe Perches63c3a662011-04-26 08:12:10 +000011515 if (tg3_flag(tp, USE_PHYLIB)) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011516 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011517 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011518 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011519 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011520 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011521 }
11522
Matt Carlson33f401a2010-04-05 10:19:27 +000011523 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011524 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011525 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011526
11527 /* fallthru */
11528 case SIOCGMIIREG: {
11529 u32 mii_regval;
11530
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011531 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011532 break; /* We have no PHY */
11533
Matt Carlson34eea5a2011-04-20 07:57:38 +000011534 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011535 return -EAGAIN;
11536
David S. Millerf47c11e2005-06-24 20:18:35 -070011537 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011538 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011539 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011540
11541 data->val_out = mii_regval;
11542
11543 return err;
11544 }
11545
11546 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011547 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011548 break; /* We have no PHY */
11549
Matt Carlson34eea5a2011-04-20 07:57:38 +000011550 if (!netif_running(dev))
Michael Chanbc1c7562006-03-20 17:48:03 -080011551 return -EAGAIN;
11552
David S. Millerf47c11e2005-06-24 20:18:35 -070011553 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011554 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011555 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011556
11557 return err;
11558
11559 default:
11560 /* do nothing */
11561 break;
11562 }
11563 return -EOPNOTSUPP;
11564}
11565
David S. Miller15f98502005-05-18 22:49:26 -070011566static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11567{
11568 struct tg3 *tp = netdev_priv(dev);
11569
11570 memcpy(ec, &tp->coal, sizeof(*ec));
11571 return 0;
11572}
11573
Michael Chand244c892005-07-05 14:42:33 -070011574static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11575{
11576 struct tg3 *tp = netdev_priv(dev);
11577 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11578 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11579
Joe Perches63c3a662011-04-26 08:12:10 +000011580 if (!tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070011581 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11582 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11583 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11584 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11585 }
11586
11587 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11588 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11589 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11590 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11591 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11592 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11593 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11594 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11595 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11596 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11597 return -EINVAL;
11598
11599 /* No rx interrupts will be generated if both are zero */
11600 if ((ec->rx_coalesce_usecs == 0) &&
11601 (ec->rx_max_coalesced_frames == 0))
11602 return -EINVAL;
11603
11604 /* No tx interrupts will be generated if both are zero */
11605 if ((ec->tx_coalesce_usecs == 0) &&
11606 (ec->tx_max_coalesced_frames == 0))
11607 return -EINVAL;
11608
11609 /* Only copy relevant parameters, ignore all others. */
11610 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11611 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11612 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11613 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11614 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11615 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11616 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11617 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11618 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11619
11620 if (netif_running(dev)) {
11621 tg3_full_lock(tp, 0);
11622 __tg3_set_coalesce(tp, &tp->coal);
11623 tg3_full_unlock(tp);
11624 }
11625 return 0;
11626}
11627
Jeff Garzik7282d492006-09-13 14:30:00 -040011628static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011629 .get_settings = tg3_get_settings,
11630 .set_settings = tg3_set_settings,
11631 .get_drvinfo = tg3_get_drvinfo,
11632 .get_regs_len = tg3_get_regs_len,
11633 .get_regs = tg3_get_regs,
11634 .get_wol = tg3_get_wol,
11635 .set_wol = tg3_set_wol,
11636 .get_msglevel = tg3_get_msglevel,
11637 .set_msglevel = tg3_set_msglevel,
11638 .nway_reset = tg3_nway_reset,
11639 .get_link = ethtool_op_get_link,
11640 .get_eeprom_len = tg3_get_eeprom_len,
11641 .get_eeprom = tg3_get_eeprom,
11642 .set_eeprom = tg3_set_eeprom,
11643 .get_ringparam = tg3_get_ringparam,
11644 .set_ringparam = tg3_set_ringparam,
11645 .get_pauseparam = tg3_get_pauseparam,
11646 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011647 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011648 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011649 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011650 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011651 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011652 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011653 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011654};
11655
11656static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11657{
Michael Chan1b277772006-03-20 22:27:48 -080011658 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011659
11660 tp->nvram_size = EEPROM_CHIP_SIZE;
11661
Matt Carlsone4f34112009-02-25 14:25:00 +000011662 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011663 return;
11664
Michael Chanb16250e2006-09-27 16:10:14 -070011665 if ((magic != TG3_EEPROM_MAGIC) &&
11666 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11667 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011668 return;
11669
11670 /*
11671 * Size the chip by reading offsets at increasing powers of two.
11672 * When we encounter our validation signature, we know the addressing
11673 * has wrapped around, and thus have our chip size.
11674 */
Michael Chan1b277772006-03-20 22:27:48 -080011675 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011676
11677 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011678 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011679 return;
11680
Michael Chan18201802006-03-20 22:29:15 -080011681 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011682 break;
11683
11684 cursize <<= 1;
11685 }
11686
11687 tp->nvram_size = cursize;
11688}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011689
Linus Torvalds1da177e2005-04-16 15:20:36 -070011690static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11691{
11692 u32 val;
11693
Joe Perches63c3a662011-04-26 08:12:10 +000011694 if (tg3_flag(tp, NO_NVRAM) || tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011695 return;
11696
11697 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011698 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011699 tg3_get_eeprom_size(tp);
11700 return;
11701 }
11702
Matt Carlson6d348f22009-02-25 14:25:52 +000011703 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011704 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011705 /* This is confusing. We want to operate on the
11706 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11707 * call will read from NVRAM and byteswap the data
11708 * according to the byteswapping settings for all
11709 * other register accesses. This ensures the data we
11710 * want will always reside in the lower 16-bits.
11711 * However, the data in NVRAM is in LE format, which
11712 * means the data from the NVRAM read will always be
11713 * opposite the endianness of the CPU. The 16-bit
11714 * byteswap then brings the data to CPU endianness.
11715 */
11716 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011717 return;
11718 }
11719 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011720 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011721}
11722
11723static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11724{
11725 u32 nvcfg1;
11726
11727 nvcfg1 = tr32(NVRAM_CFG1);
11728 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
Joe Perches63c3a662011-04-26 08:12:10 +000011729 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011730 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011731 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11732 tw32(NVRAM_CFG1, nvcfg1);
11733 }
11734
Matt Carlson6ff6f812011-05-19 12:12:54 +000011735 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
Joe Perches63c3a662011-04-26 08:12:10 +000011736 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011737 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011738 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11739 tp->nvram_jedecnum = JEDEC_ATMEL;
11740 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011741 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011742 break;
11743 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11744 tp->nvram_jedecnum = JEDEC_ATMEL;
11745 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11746 break;
11747 case FLASH_VENDOR_ATMEL_EEPROM:
11748 tp->nvram_jedecnum = JEDEC_ATMEL;
11749 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011750 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011751 break;
11752 case FLASH_VENDOR_ST:
11753 tp->nvram_jedecnum = JEDEC_ST;
11754 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011755 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011756 break;
11757 case FLASH_VENDOR_SAIFUN:
11758 tp->nvram_jedecnum = JEDEC_SAIFUN;
11759 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11760 break;
11761 case FLASH_VENDOR_SST_SMALL:
11762 case FLASH_VENDOR_SST_LARGE:
11763 tp->nvram_jedecnum = JEDEC_SST;
11764 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11765 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011766 }
Matt Carlson8590a602009-08-28 12:29:16 +000011767 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011768 tp->nvram_jedecnum = JEDEC_ATMEL;
11769 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
Joe Perches63c3a662011-04-26 08:12:10 +000011770 tg3_flag_set(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011771 }
11772}
11773
Matt Carlsona1b950d2009-09-01 13:20:17 +000011774static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11775{
11776 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11777 case FLASH_5752PAGE_SIZE_256:
11778 tp->nvram_pagesize = 256;
11779 break;
11780 case FLASH_5752PAGE_SIZE_512:
11781 tp->nvram_pagesize = 512;
11782 break;
11783 case FLASH_5752PAGE_SIZE_1K:
11784 tp->nvram_pagesize = 1024;
11785 break;
11786 case FLASH_5752PAGE_SIZE_2K:
11787 tp->nvram_pagesize = 2048;
11788 break;
11789 case FLASH_5752PAGE_SIZE_4K:
11790 tp->nvram_pagesize = 4096;
11791 break;
11792 case FLASH_5752PAGE_SIZE_264:
11793 tp->nvram_pagesize = 264;
11794 break;
11795 case FLASH_5752PAGE_SIZE_528:
11796 tp->nvram_pagesize = 528;
11797 break;
11798 }
11799}
11800
Michael Chan361b4ac2005-04-21 17:11:21 -070011801static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11802{
11803 u32 nvcfg1;
11804
11805 nvcfg1 = tr32(NVRAM_CFG1);
11806
Michael Chane6af3012005-04-21 17:12:05 -070011807 /* NVRAM protection for TPM */
11808 if (nvcfg1 & (1 << 27))
Joe Perches63c3a662011-04-26 08:12:10 +000011809 tg3_flag_set(tp, PROTECTED_NVRAM);
Michael Chane6af3012005-04-21 17:12:05 -070011810
Michael Chan361b4ac2005-04-21 17:11:21 -070011811 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011812 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11813 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11814 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011815 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011816 break;
11817 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11818 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011819 tg3_flag_set(tp, NVRAM_BUFFERED);
11820 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011821 break;
11822 case FLASH_5752VENDOR_ST_M45PE10:
11823 case FLASH_5752VENDOR_ST_M45PE20:
11824 case FLASH_5752VENDOR_ST_M45PE40:
11825 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011826 tg3_flag_set(tp, NVRAM_BUFFERED);
11827 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011828 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011829 }
11830
Joe Perches63c3a662011-04-26 08:12:10 +000011831 if (tg3_flag(tp, FLASH)) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011832 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011833 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011834 /* For eeprom, set pagesize to maximum eeprom size */
11835 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11836
11837 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11838 tw32(NVRAM_CFG1, nvcfg1);
11839 }
11840}
11841
Michael Chand3c7b882006-03-23 01:28:25 -080011842static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11843{
Matt Carlson989a9d22007-05-05 11:51:05 -070011844 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011845
11846 nvcfg1 = tr32(NVRAM_CFG1);
11847
11848 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011849 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011850 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson989a9d22007-05-05 11:51:05 -070011851 protect = 1;
11852 }
Michael Chand3c7b882006-03-23 01:28:25 -080011853
Matt Carlson989a9d22007-05-05 11:51:05 -070011854 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11855 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011856 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11857 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11858 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11859 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11860 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011861 tg3_flag_set(tp, NVRAM_BUFFERED);
11862 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011863 tp->nvram_pagesize = 264;
11864 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11865 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11866 tp->nvram_size = (protect ? 0x3e200 :
11867 TG3_NVRAM_SIZE_512KB);
11868 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11869 tp->nvram_size = (protect ? 0x1f200 :
11870 TG3_NVRAM_SIZE_256KB);
11871 else
11872 tp->nvram_size = (protect ? 0x1f200 :
11873 TG3_NVRAM_SIZE_128KB);
11874 break;
11875 case FLASH_5752VENDOR_ST_M45PE10:
11876 case FLASH_5752VENDOR_ST_M45PE20:
11877 case FLASH_5752VENDOR_ST_M45PE40:
11878 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011879 tg3_flag_set(tp, NVRAM_BUFFERED);
11880 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011881 tp->nvram_pagesize = 256;
11882 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11883 tp->nvram_size = (protect ?
11884 TG3_NVRAM_SIZE_64KB :
11885 TG3_NVRAM_SIZE_128KB);
11886 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11887 tp->nvram_size = (protect ?
11888 TG3_NVRAM_SIZE_64KB :
11889 TG3_NVRAM_SIZE_256KB);
11890 else
11891 tp->nvram_size = (protect ?
11892 TG3_NVRAM_SIZE_128KB :
11893 TG3_NVRAM_SIZE_512KB);
11894 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011895 }
11896}
11897
Michael Chan1b277772006-03-20 22:27:48 -080011898static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11899{
11900 u32 nvcfg1;
11901
11902 nvcfg1 = tr32(NVRAM_CFG1);
11903
11904 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011905 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11906 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11907 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11908 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11909 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011910 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson8590a602009-08-28 12:29:16 +000011911 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011912
Matt Carlson8590a602009-08-28 12:29:16 +000011913 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11914 tw32(NVRAM_CFG1, nvcfg1);
11915 break;
11916 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11917 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11918 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11919 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11920 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011921 tg3_flag_set(tp, NVRAM_BUFFERED);
11922 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011923 tp->nvram_pagesize = 264;
11924 break;
11925 case FLASH_5752VENDOR_ST_M45PE10:
11926 case FLASH_5752VENDOR_ST_M45PE20:
11927 case FLASH_5752VENDOR_ST_M45PE40:
11928 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011929 tg3_flag_set(tp, NVRAM_BUFFERED);
11930 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011931 tp->nvram_pagesize = 256;
11932 break;
Michael Chan1b277772006-03-20 22:27:48 -080011933 }
11934}
11935
Matt Carlson6b91fa02007-10-10 18:01:09 -070011936static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11937{
11938 u32 nvcfg1, protect = 0;
11939
11940 nvcfg1 = tr32(NVRAM_CFG1);
11941
11942 /* NVRAM protection for TPM */
11943 if (nvcfg1 & (1 << 27)) {
Joe Perches63c3a662011-04-26 08:12:10 +000011944 tg3_flag_set(tp, PROTECTED_NVRAM);
Matt Carlson6b91fa02007-10-10 18:01:09 -070011945 protect = 1;
11946 }
11947
11948 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11949 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011950 case FLASH_5761VENDOR_ATMEL_ADB021D:
11951 case FLASH_5761VENDOR_ATMEL_ADB041D:
11952 case FLASH_5761VENDOR_ATMEL_ADB081D:
11953 case FLASH_5761VENDOR_ATMEL_ADB161D:
11954 case FLASH_5761VENDOR_ATMEL_MDB021D:
11955 case FLASH_5761VENDOR_ATMEL_MDB041D:
11956 case FLASH_5761VENDOR_ATMEL_MDB081D:
11957 case FLASH_5761VENDOR_ATMEL_MDB161D:
11958 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000011959 tg3_flag_set(tp, NVRAM_BUFFERED);
11960 tg3_flag_set(tp, FLASH);
11961 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson8590a602009-08-28 12:29:16 +000011962 tp->nvram_pagesize = 256;
11963 break;
11964 case FLASH_5761VENDOR_ST_A_M45PE20:
11965 case FLASH_5761VENDOR_ST_A_M45PE40:
11966 case FLASH_5761VENDOR_ST_A_M45PE80:
11967 case FLASH_5761VENDOR_ST_A_M45PE16:
11968 case FLASH_5761VENDOR_ST_M_M45PE20:
11969 case FLASH_5761VENDOR_ST_M_M45PE40:
11970 case FLASH_5761VENDOR_ST_M_M45PE80:
11971 case FLASH_5761VENDOR_ST_M_M45PE16:
11972 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000011973 tg3_flag_set(tp, NVRAM_BUFFERED);
11974 tg3_flag_set(tp, FLASH);
Matt Carlson8590a602009-08-28 12:29:16 +000011975 tp->nvram_pagesize = 256;
11976 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011977 }
11978
11979 if (protect) {
11980 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11981 } else {
11982 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011983 case FLASH_5761VENDOR_ATMEL_ADB161D:
11984 case FLASH_5761VENDOR_ATMEL_MDB161D:
11985 case FLASH_5761VENDOR_ST_A_M45PE16:
11986 case FLASH_5761VENDOR_ST_M_M45PE16:
11987 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11988 break;
11989 case FLASH_5761VENDOR_ATMEL_ADB081D:
11990 case FLASH_5761VENDOR_ATMEL_MDB081D:
11991 case FLASH_5761VENDOR_ST_A_M45PE80:
11992 case FLASH_5761VENDOR_ST_M_M45PE80:
11993 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11994 break;
11995 case FLASH_5761VENDOR_ATMEL_ADB041D:
11996 case FLASH_5761VENDOR_ATMEL_MDB041D:
11997 case FLASH_5761VENDOR_ST_A_M45PE40:
11998 case FLASH_5761VENDOR_ST_M_M45PE40:
11999 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12000 break;
12001 case FLASH_5761VENDOR_ATMEL_ADB021D:
12002 case FLASH_5761VENDOR_ATMEL_MDB021D:
12003 case FLASH_5761VENDOR_ST_A_M45PE20:
12004 case FLASH_5761VENDOR_ST_M_M45PE20:
12005 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12006 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070012007 }
12008 }
12009}
12010
Michael Chanb5d37722006-09-27 16:06:21 -070012011static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
12012{
12013 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012014 tg3_flag_set(tp, NVRAM_BUFFERED);
Michael Chanb5d37722006-09-27 16:06:21 -070012015 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12016}
12017
Matt Carlson321d32a2008-11-21 17:22:19 -080012018static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
12019{
12020 u32 nvcfg1;
12021
12022 nvcfg1 = tr32(NVRAM_CFG1);
12023
12024 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12025 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
12026 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
12027 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012028 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson321d32a2008-11-21 17:22:19 -080012029 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12030
12031 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12032 tw32(NVRAM_CFG1, nvcfg1);
12033 return;
12034 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12035 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12036 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12037 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12038 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12039 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12040 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12041 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012042 tg3_flag_set(tp, NVRAM_BUFFERED);
12043 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012044
12045 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12046 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
12047 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
12048 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
12049 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12050 break;
12051 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
12052 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
12053 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12054 break;
12055 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
12056 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
12057 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12058 break;
12059 }
12060 break;
12061 case FLASH_5752VENDOR_ST_M45PE10:
12062 case FLASH_5752VENDOR_ST_M45PE20:
12063 case FLASH_5752VENDOR_ST_M45PE40:
12064 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012065 tg3_flag_set(tp, NVRAM_BUFFERED);
12066 tg3_flag_set(tp, FLASH);
Matt Carlson321d32a2008-11-21 17:22:19 -080012067
12068 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12069 case FLASH_5752VENDOR_ST_M45PE10:
12070 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12071 break;
12072 case FLASH_5752VENDOR_ST_M45PE20:
12073 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12074 break;
12075 case FLASH_5752VENDOR_ST_M45PE40:
12076 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12077 break;
12078 }
12079 break;
12080 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012081 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson321d32a2008-11-21 17:22:19 -080012082 return;
12083 }
12084
Matt Carlsona1b950d2009-09-01 13:20:17 +000012085 tg3_nvram_get_pagesize(tp, nvcfg1);
12086 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012087 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012088}
12089
12090
12091static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
12092{
12093 u32 nvcfg1;
12094
12095 nvcfg1 = tr32(NVRAM_CFG1);
12096
12097 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12098 case FLASH_5717VENDOR_ATMEL_EEPROM:
12099 case FLASH_5717VENDOR_MICRO_EEPROM:
12100 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012101 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012102 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12103
12104 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12105 tw32(NVRAM_CFG1, nvcfg1);
12106 return;
12107 case FLASH_5717VENDOR_ATMEL_MDB011D:
12108 case FLASH_5717VENDOR_ATMEL_ADB011B:
12109 case FLASH_5717VENDOR_ATMEL_ADB011D:
12110 case FLASH_5717VENDOR_ATMEL_MDB021D:
12111 case FLASH_5717VENDOR_ATMEL_ADB021B:
12112 case FLASH_5717VENDOR_ATMEL_ADB021D:
12113 case FLASH_5717VENDOR_ATMEL_45USPT:
12114 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012115 tg3_flag_set(tp, NVRAM_BUFFERED);
12116 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012117
12118 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12119 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012120 /* Detect size with tg3_nvram_get_size() */
12121 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012122 case FLASH_5717VENDOR_ATMEL_ADB021B:
12123 case FLASH_5717VENDOR_ATMEL_ADB021D:
12124 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12125 break;
12126 default:
12127 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12128 break;
12129 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012130 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012131 case FLASH_5717VENDOR_ST_M_M25PE10:
12132 case FLASH_5717VENDOR_ST_A_M25PE10:
12133 case FLASH_5717VENDOR_ST_M_M45PE10:
12134 case FLASH_5717VENDOR_ST_A_M45PE10:
12135 case FLASH_5717VENDOR_ST_M_M25PE20:
12136 case FLASH_5717VENDOR_ST_A_M25PE20:
12137 case FLASH_5717VENDOR_ST_M_M45PE20:
12138 case FLASH_5717VENDOR_ST_A_M45PE20:
12139 case FLASH_5717VENDOR_ST_25USPT:
12140 case FLASH_5717VENDOR_ST_45USPT:
12141 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012142 tg3_flag_set(tp, NVRAM_BUFFERED);
12143 tg3_flag_set(tp, FLASH);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012144
12145 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12146 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012147 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012148 /* Detect size with tg3_nvram_get_size() */
12149 break;
12150 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012151 case FLASH_5717VENDOR_ST_A_M45PE20:
12152 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12153 break;
12154 default:
12155 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12156 break;
12157 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012158 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012159 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012160 tg3_flag_set(tp, NO_NVRAM);
Matt Carlsona1b950d2009-09-01 13:20:17 +000012161 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012162 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012163
12164 tg3_nvram_get_pagesize(tp, nvcfg1);
12165 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012166 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson321d32a2008-11-21 17:22:19 -080012167}
12168
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012169static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12170{
12171 u32 nvcfg1, nvmpinstrp;
12172
12173 nvcfg1 = tr32(NVRAM_CFG1);
12174 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12175
12176 switch (nvmpinstrp) {
12177 case FLASH_5720_EEPROM_HD:
12178 case FLASH_5720_EEPROM_LD:
12179 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012180 tg3_flag_set(tp, NVRAM_BUFFERED);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012181
12182 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12183 tw32(NVRAM_CFG1, nvcfg1);
12184 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12185 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12186 else
12187 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12188 return;
12189 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12190 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12191 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12192 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12193 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12194 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12195 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12196 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12197 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12198 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12199 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12200 case FLASH_5720VENDOR_ATMEL_45USPT:
12201 tp->nvram_jedecnum = JEDEC_ATMEL;
Joe Perches63c3a662011-04-26 08:12:10 +000012202 tg3_flag_set(tp, NVRAM_BUFFERED);
12203 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012204
12205 switch (nvmpinstrp) {
12206 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12207 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12208 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12209 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12210 break;
12211 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12212 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12213 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12214 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12215 break;
12216 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12217 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12218 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12219 break;
12220 default:
12221 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12222 break;
12223 }
12224 break;
12225 case FLASH_5720VENDOR_M_ST_M25PE10:
12226 case FLASH_5720VENDOR_M_ST_M45PE10:
12227 case FLASH_5720VENDOR_A_ST_M25PE10:
12228 case FLASH_5720VENDOR_A_ST_M45PE10:
12229 case FLASH_5720VENDOR_M_ST_M25PE20:
12230 case FLASH_5720VENDOR_M_ST_M45PE20:
12231 case FLASH_5720VENDOR_A_ST_M25PE20:
12232 case FLASH_5720VENDOR_A_ST_M45PE20:
12233 case FLASH_5720VENDOR_M_ST_M25PE40:
12234 case FLASH_5720VENDOR_M_ST_M45PE40:
12235 case FLASH_5720VENDOR_A_ST_M25PE40:
12236 case FLASH_5720VENDOR_A_ST_M45PE40:
12237 case FLASH_5720VENDOR_M_ST_M25PE80:
12238 case FLASH_5720VENDOR_M_ST_M45PE80:
12239 case FLASH_5720VENDOR_A_ST_M25PE80:
12240 case FLASH_5720VENDOR_A_ST_M45PE80:
12241 case FLASH_5720VENDOR_ST_25USPT:
12242 case FLASH_5720VENDOR_ST_45USPT:
12243 tp->nvram_jedecnum = JEDEC_ST;
Joe Perches63c3a662011-04-26 08:12:10 +000012244 tg3_flag_set(tp, NVRAM_BUFFERED);
12245 tg3_flag_set(tp, FLASH);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012246
12247 switch (nvmpinstrp) {
12248 case FLASH_5720VENDOR_M_ST_M25PE20:
12249 case FLASH_5720VENDOR_M_ST_M45PE20:
12250 case FLASH_5720VENDOR_A_ST_M25PE20:
12251 case FLASH_5720VENDOR_A_ST_M45PE20:
12252 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12253 break;
12254 case FLASH_5720VENDOR_M_ST_M25PE40:
12255 case FLASH_5720VENDOR_M_ST_M45PE40:
12256 case FLASH_5720VENDOR_A_ST_M25PE40:
12257 case FLASH_5720VENDOR_A_ST_M45PE40:
12258 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12259 break;
12260 case FLASH_5720VENDOR_M_ST_M25PE80:
12261 case FLASH_5720VENDOR_M_ST_M45PE80:
12262 case FLASH_5720VENDOR_A_ST_M25PE80:
12263 case FLASH_5720VENDOR_A_ST_M45PE80:
12264 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12265 break;
12266 default:
12267 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12268 break;
12269 }
12270 break;
12271 default:
Joe Perches63c3a662011-04-26 08:12:10 +000012272 tg3_flag_set(tp, NO_NVRAM);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012273 return;
12274 }
12275
12276 tg3_nvram_get_pagesize(tp, nvcfg1);
12277 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Joe Perches63c3a662011-04-26 08:12:10 +000012278 tg3_flag_set(tp, NO_NVRAM_ADDR_TRANS);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012279}
12280
Linus Torvalds1da177e2005-04-16 15:20:36 -070012281/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12282static void __devinit tg3_nvram_init(struct tg3 *tp)
12283{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012284 tw32_f(GRC_EEPROM_ADDR,
12285 (EEPROM_ADDR_FSM_RESET |
12286 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12287 EEPROM_ADDR_CLKPERD_SHIFT)));
12288
Michael Chan9d57f012006-12-07 00:23:25 -080012289 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012290
12291 /* Enable seeprom accesses. */
12292 tw32_f(GRC_LOCAL_CTRL,
12293 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12294 udelay(100);
12295
12296 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12297 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Joe Perches63c3a662011-04-26 08:12:10 +000012298 tg3_flag_set(tp, NVRAM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012299
Michael Chanec41c7d2006-01-17 02:40:55 -080012300 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012301 netdev_warn(tp->dev,
12302 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012303 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012304 return;
12305 }
Michael Chane6af3012005-04-21 17:12:05 -070012306 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012307
Matt Carlson989a9d22007-05-05 11:51:05 -070012308 tp->nvram_size = 0;
12309
Michael Chan361b4ac2005-04-21 17:11:21 -070012310 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12311 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012312 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12313 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012314 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012315 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12316 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012317 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012318 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12319 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012320 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12321 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012322 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12323 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012324 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012325 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12326 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012327 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012328 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12329 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012330 else
12331 tg3_get_nvram_info(tp);
12332
Matt Carlson989a9d22007-05-05 11:51:05 -070012333 if (tp->nvram_size == 0)
12334 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012335
Michael Chane6af3012005-04-21 17:12:05 -070012336 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012337 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012338
12339 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012340 tg3_flag_clear(tp, NVRAM);
12341 tg3_flag_clear(tp, NVRAM_BUFFERED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012342
12343 tg3_get_eeprom_size(tp);
12344 }
12345}
12346
Linus Torvalds1da177e2005-04-16 15:20:36 -070012347static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12348 u32 offset, u32 len, u8 *buf)
12349{
12350 int i, j, rc = 0;
12351 u32 val;
12352
12353 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012354 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012355 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012356
12357 addr = offset + i;
12358
12359 memcpy(&data, buf + i, 4);
12360
Matt Carlson62cedd12009-04-20 14:52:29 -070012361 /*
12362 * The SEEPROM interface expects the data to always be opposite
12363 * the native endian format. We accomplish this by reversing
12364 * all the operations that would have been performed on the
12365 * data from a call to tg3_nvram_read_be32().
12366 */
12367 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012368
12369 val = tr32(GRC_EEPROM_ADDR);
12370 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12371
12372 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12373 EEPROM_ADDR_READ);
12374 tw32(GRC_EEPROM_ADDR, val |
12375 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12376 (addr & EEPROM_ADDR_ADDR_MASK) |
12377 EEPROM_ADDR_START |
12378 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012379
Michael Chan9d57f012006-12-07 00:23:25 -080012380 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012381 val = tr32(GRC_EEPROM_ADDR);
12382
12383 if (val & EEPROM_ADDR_COMPLETE)
12384 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012385 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012386 }
12387 if (!(val & EEPROM_ADDR_COMPLETE)) {
12388 rc = -EBUSY;
12389 break;
12390 }
12391 }
12392
12393 return rc;
12394}
12395
12396/* offset and length are dword aligned */
12397static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12398 u8 *buf)
12399{
12400 int ret = 0;
12401 u32 pagesize = tp->nvram_pagesize;
12402 u32 pagemask = pagesize - 1;
12403 u32 nvram_cmd;
12404 u8 *tmp;
12405
12406 tmp = kmalloc(pagesize, GFP_KERNEL);
12407 if (tmp == NULL)
12408 return -ENOMEM;
12409
12410 while (len) {
12411 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012412 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012413
12414 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012415
Linus Torvalds1da177e2005-04-16 15:20:36 -070012416 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012417 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12418 (__be32 *) (tmp + j));
12419 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012420 break;
12421 }
12422 if (ret)
12423 break;
12424
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012425 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012426 size = pagesize;
12427 if (len < size)
12428 size = len;
12429
12430 len -= size;
12431
12432 memcpy(tmp + page_off, buf, size);
12433
12434 offset = offset + (pagesize - page_off);
12435
Michael Chane6af3012005-04-21 17:12:05 -070012436 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012437
12438 /*
12439 * Before we can erase the flash page, we need
12440 * to issue a special "write enable" command.
12441 */
12442 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12443
12444 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12445 break;
12446
12447 /* Erase the target page */
12448 tw32(NVRAM_ADDR, phy_addr);
12449
12450 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12451 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12452
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012453 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012454 break;
12455
12456 /* Issue another write enable to start the write. */
12457 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12458
12459 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12460 break;
12461
12462 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012463 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012464
Al Virob9fc7dc2007-12-17 22:59:57 -080012465 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012466
Al Virob9fc7dc2007-12-17 22:59:57 -080012467 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012468
12469 tw32(NVRAM_ADDR, phy_addr + j);
12470
12471 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12472 NVRAM_CMD_WR;
12473
12474 if (j == 0)
12475 nvram_cmd |= NVRAM_CMD_FIRST;
12476 else if (j == (pagesize - 4))
12477 nvram_cmd |= NVRAM_CMD_LAST;
12478
12479 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12480 break;
12481 }
12482 if (ret)
12483 break;
12484 }
12485
12486 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12487 tg3_nvram_exec_cmd(tp, nvram_cmd);
12488
12489 kfree(tmp);
12490
12491 return ret;
12492}
12493
12494/* offset and length are dword aligned */
12495static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12496 u8 *buf)
12497{
12498 int i, ret = 0;
12499
12500 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012501 u32 page_off, phy_addr, nvram_cmd;
12502 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012503
12504 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012505 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012506
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012507 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012508
Michael Chan18201802006-03-20 22:29:15 -080012509 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012510
12511 tw32(NVRAM_ADDR, phy_addr);
12512
12513 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12514
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012515 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012516 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012517 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012518 nvram_cmd |= NVRAM_CMD_LAST;
12519
12520 if (i == (len - 4))
12521 nvram_cmd |= NVRAM_CMD_LAST;
12522
Matt Carlson321d32a2008-11-21 17:22:19 -080012523 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012524 !tg3_flag(tp, 5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012525 (tp->nvram_jedecnum == JEDEC_ST) &&
12526 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012527
12528 if ((ret = tg3_nvram_exec_cmd(tp,
12529 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12530 NVRAM_CMD_DONE)))
12531
12532 break;
12533 }
Joe Perches63c3a662011-04-26 08:12:10 +000012534 if (!tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012535 /* We always do complete word writes to eeprom. */
12536 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12537 }
12538
12539 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12540 break;
12541 }
12542 return ret;
12543}
12544
12545/* offset and length are dword aligned */
12546static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12547{
12548 int ret;
12549
Joe Perches63c3a662011-04-26 08:12:10 +000012550 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012551 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12552 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012553 udelay(40);
12554 }
12555
Joe Perches63c3a662011-04-26 08:12:10 +000012556 if (!tg3_flag(tp, NVRAM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012557 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012558 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012559 u32 grc_mode;
12560
Michael Chanec41c7d2006-01-17 02:40:55 -080012561 ret = tg3_nvram_lock(tp);
12562 if (ret)
12563 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012564
Michael Chane6af3012005-04-21 17:12:05 -070012565 tg3_enable_nvram_access(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000012566 if (tg3_flag(tp, 5750_PLUS) && !tg3_flag(tp, PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012567 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012568
12569 grc_mode = tr32(GRC_MODE);
12570 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12571
Joe Perches63c3a662011-04-26 08:12:10 +000012572 if (tg3_flag(tp, NVRAM_BUFFERED) || !tg3_flag(tp, FLASH)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012573 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12574 buf);
Matt Carlson859a588792010-04-05 10:19:28 +000012575 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012576 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12577 buf);
12578 }
12579
12580 grc_mode = tr32(GRC_MODE);
12581 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12582
Michael Chane6af3012005-04-21 17:12:05 -070012583 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012584 tg3_nvram_unlock(tp);
12585 }
12586
Joe Perches63c3a662011-04-26 08:12:10 +000012587 if (tg3_flag(tp, EEPROM_WRITE_PROT)) {
Michael Chan314fba32005-04-21 17:07:04 -070012588 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012589 udelay(40);
12590 }
12591
12592 return ret;
12593}
12594
12595struct subsys_tbl_ent {
12596 u16 subsys_vendor, subsys_devid;
12597 u32 phy_id;
12598};
12599
Matt Carlson24daf2b2010-02-17 15:17:02 +000012600static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012601 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012602 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012603 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012604 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012605 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012606 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012607 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012608 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12609 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12610 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012611 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012612 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012613 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012614 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12615 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12616 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012617 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012618 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012619 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012620 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012621 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012622 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012623 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012624
12625 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012626 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012627 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012628 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012629 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012630 { TG3PCI_SUBVENDOR_ID_3COM,
12631 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12632 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012633 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012634 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012635 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012636
12637 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012638 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012639 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012640 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012641 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012642 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012643 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012644 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012645 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012646
12647 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012648 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012649 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012650 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012651 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012652 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12653 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12654 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012655 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012656 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012657 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012658
12659 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012660 { TG3PCI_SUBVENDOR_ID_IBM,
12661 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012662};
12663
Matt Carlson24daf2b2010-02-17 15:17:02 +000012664static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012665{
12666 int i;
12667
12668 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12669 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12670 tp->pdev->subsystem_vendor) &&
12671 (subsys_id_to_phy_id[i].subsys_devid ==
12672 tp->pdev->subsystem_device))
12673 return &subsys_id_to_phy_id[i];
12674 }
12675 return NULL;
12676}
12677
Michael Chan7d0c41e2005-04-21 17:06:20 -070012678static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012679{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012680 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012681 u16 pmcsr;
12682
12683 /* On some early chips the SRAM cannot be accessed in D3hot state,
12684 * so need make sure we're in D0.
12685 */
12686 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12687 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12688 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12689 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012690
12691 /* Make sure register accesses (indirect or otherwise)
12692 * will function correctly.
12693 */
12694 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12695 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012696
David S. Millerf49639e2006-06-09 11:58:36 -070012697 /* The memory arbiter has to be enabled in order for SRAM accesses
12698 * to succeed. Normally on powerup the tg3 chip firmware will make
12699 * sure it is enabled, but other entities such as system netboot
12700 * code might disable it.
12701 */
12702 val = tr32(MEMARB_MODE);
12703 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12704
Matt Carlson79eb6902010-02-17 15:17:03 +000012705 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012706 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12707
Gary Zambranoa85feb82007-05-05 11:52:19 -070012708 /* Assume an onboard device and WOL capable by default. */
Joe Perches63c3a662011-04-26 08:12:10 +000012709 tg3_flag_set(tp, EEPROM_WRITE_PROT);
12710 tg3_flag_set(tp, WOL_CAP);
David S. Miller72b845e2006-03-14 14:11:48 -080012711
Michael Chanb5d37722006-09-27 16:06:21 -070012712 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012713 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012714 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12715 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012716 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012717 val = tr32(VCPU_CFGSHDW);
12718 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Joe Perches63c3a662011-04-26 08:12:10 +000012719 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson0527ba32007-10-10 18:03:30 -070012720 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012721 (val & VCPU_CFGSHDW_WOL_MAGPKT)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012722 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012723 device_set_wakeup_enable(&tp->pdev->dev, true);
12724 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012725 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012726 }
12727
Linus Torvalds1da177e2005-04-16 15:20:36 -070012728 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12729 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12730 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012731 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012732 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012733
12734 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12735 tp->nic_sram_data_cfg = nic_cfg;
12736
12737 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12738 ver >>= NIC_SRAM_DATA_VER_SHIFT;
Matt Carlson6ff6f812011-05-19 12:12:54 +000012739 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12740 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
12741 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012742 (ver > 0) && (ver < 0x100))
12743 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12744
Matt Carlsona9daf362008-05-25 23:49:44 -070012745 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12746 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12747
Linus Torvalds1da177e2005-04-16 15:20:36 -070012748 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12749 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12750 eeprom_phy_serdes = 1;
12751
12752 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12753 if (nic_phy_id != 0) {
12754 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12755 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12756
12757 eeprom_phy_id = (id1 >> 16) << 10;
12758 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12759 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12760 } else
12761 eeprom_phy_id = 0;
12762
Michael Chan7d0c41e2005-04-21 17:06:20 -070012763 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012764 if (eeprom_phy_serdes) {
Joe Perches63c3a662011-04-26 08:12:10 +000012765 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012766 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012767 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012768 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012769 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012770
Joe Perches63c3a662011-04-26 08:12:10 +000012771 if (tg3_flag(tp, 5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012772 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12773 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012774 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012775 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12776
12777 switch (led_cfg) {
12778 default:
12779 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12780 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12781 break;
12782
12783 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12784 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12785 break;
12786
12787 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12788 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012789
12790 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12791 * read on some older 5700/5701 bootcode.
12792 */
12793 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12794 ASIC_REV_5700 ||
12795 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12796 ASIC_REV_5701)
12797 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12798
Linus Torvalds1da177e2005-04-16 15:20:36 -070012799 break;
12800
12801 case SHASTA_EXT_LED_SHARED:
12802 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12803 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12804 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12805 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12806 LED_CTRL_MODE_PHY_2);
12807 break;
12808
12809 case SHASTA_EXT_LED_MAC:
12810 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12811 break;
12812
12813 case SHASTA_EXT_LED_COMBO:
12814 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12815 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12816 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12817 LED_CTRL_MODE_PHY_2);
12818 break;
12819
Stephen Hemminger855e1112008-04-16 16:37:28 -070012820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012821
12822 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12823 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12824 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12825 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12826
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012827 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12828 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012829
Michael Chan9d26e212006-12-07 00:21:14 -080012830 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Joe Perches63c3a662011-04-26 08:12:10 +000012831 tg3_flag_set(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012832 if ((tp->pdev->subsystem_vendor ==
12833 PCI_VENDOR_ID_ARIMA) &&
12834 (tp->pdev->subsystem_device == 0x205a ||
12835 tp->pdev->subsystem_device == 0x2063))
Joe Perches63c3a662011-04-26 08:12:10 +000012836 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
Michael Chan9d26e212006-12-07 00:21:14 -080012837 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000012838 tg3_flag_clear(tp, EEPROM_WRITE_PROT);
12839 tg3_flag_set(tp, IS_NIC);
Michael Chan9d26e212006-12-07 00:21:14 -080012840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012841
12842 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
Joe Perches63c3a662011-04-26 08:12:10 +000012843 tg3_flag_set(tp, ENABLE_ASF);
12844 if (tg3_flag(tp, 5750_PLUS))
12845 tg3_flag_set(tp, ASF_NEW_HANDSHAKE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012846 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012847
12848 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
Joe Perches63c3a662011-04-26 08:12:10 +000012849 tg3_flag(tp, 5750_PLUS))
12850 tg3_flag_set(tp, ENABLE_APE);
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012851
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012852 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012853 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
Joe Perches63c3a662011-04-26 08:12:10 +000012854 tg3_flag_clear(tp, WOL_CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012855
Joe Perches63c3a662011-04-26 08:12:10 +000012856 if (tg3_flag(tp, WOL_CAP) &&
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012857 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE)) {
Joe Perches63c3a662011-04-26 08:12:10 +000012858 tg3_flag_set(tp, WOL_ENABLE);
Rafael J. Wysocki6fdbab92011-04-28 11:02:15 +000012859 device_set_wakeup_enable(&tp->pdev->dev, true);
12860 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012861
Linus Torvalds1da177e2005-04-16 15:20:36 -070012862 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012863 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012864
12865 /* serdes signal pre-emphasis in register 0x590 set by */
12866 /* bootcode if bit 18 is set */
12867 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012868 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012869
Joe Perches63c3a662011-04-26 08:12:10 +000012870 if ((tg3_flag(tp, 57765_PLUS) ||
12871 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12872 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX)) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012873 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012874 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012875
Joe Perches63c3a662011-04-26 08:12:10 +000012876 if (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012877 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Joe Perches63c3a662011-04-26 08:12:10 +000012878 !tg3_flag(tp, 57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012879 u32 cfg3;
12880
12881 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12882 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
Joe Perches63c3a662011-04-26 08:12:10 +000012883 tg3_flag_set(tp, ASPM_WORKAROUND);
Matt Carlson8ed5d972007-05-07 00:25:49 -070012884 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012885
Matt Carlson14417062010-02-17 15:16:59 +000012886 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
Joe Perches63c3a662011-04-26 08:12:10 +000012887 tg3_flag_set(tp, RGMII_INBAND_DISABLE);
Matt Carlsona9daf362008-05-25 23:49:44 -070012888 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012889 tg3_flag_set(tp, RGMII_EXT_IBND_RX_EN);
Matt Carlsona9daf362008-05-25 23:49:44 -070012890 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
Joe Perches63c3a662011-04-26 08:12:10 +000012891 tg3_flag_set(tp, RGMII_EXT_IBND_TX_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012892 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012893done:
Joe Perches63c3a662011-04-26 08:12:10 +000012894 if (tg3_flag(tp, WOL_CAP))
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012895 device_set_wakeup_enable(&tp->pdev->dev,
Joe Perches63c3a662011-04-26 08:12:10 +000012896 tg3_flag(tp, WOL_ENABLE));
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012897 else
12898 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012899}
12900
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012901static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12902{
12903 int i;
12904 u32 val;
12905
12906 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12907 tw32(OTP_CTRL, cmd);
12908
12909 /* Wait for up to 1 ms for command to execute. */
12910 for (i = 0; i < 100; i++) {
12911 val = tr32(OTP_STATUS);
12912 if (val & OTP_STATUS_CMD_DONE)
12913 break;
12914 udelay(10);
12915 }
12916
12917 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12918}
12919
12920/* Read the gphy configuration from the OTP region of the chip. The gphy
12921 * configuration is a 32-bit value that straddles the alignment boundary.
12922 * We do two 32-bit reads and then shift and merge the results.
12923 */
12924static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12925{
12926 u32 bhalf_otp, thalf_otp;
12927
12928 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12929
12930 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12931 return 0;
12932
12933 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12934
12935 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12936 return 0;
12937
12938 thalf_otp = tr32(OTP_READ_DATA);
12939
12940 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12941
12942 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12943 return 0;
12944
12945 bhalf_otp = tr32(OTP_READ_DATA);
12946
12947 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12948}
12949
Matt Carlsone256f8a2011-03-09 16:58:24 +000012950static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12951{
12952 u32 adv = ADVERTISED_Autoneg |
12953 ADVERTISED_Pause;
12954
12955 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12956 adv |= ADVERTISED_1000baseT_Half |
12957 ADVERTISED_1000baseT_Full;
12958
12959 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12960 adv |= ADVERTISED_100baseT_Half |
12961 ADVERTISED_100baseT_Full |
12962 ADVERTISED_10baseT_Half |
12963 ADVERTISED_10baseT_Full |
12964 ADVERTISED_TP;
12965 else
12966 adv |= ADVERTISED_FIBRE;
12967
12968 tp->link_config.advertising = adv;
12969 tp->link_config.speed = SPEED_INVALID;
12970 tp->link_config.duplex = DUPLEX_INVALID;
12971 tp->link_config.autoneg = AUTONEG_ENABLE;
12972 tp->link_config.active_speed = SPEED_INVALID;
12973 tp->link_config.active_duplex = DUPLEX_INVALID;
12974 tp->link_config.orig_speed = SPEED_INVALID;
12975 tp->link_config.orig_duplex = DUPLEX_INVALID;
12976 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12977}
12978
Michael Chan7d0c41e2005-04-21 17:06:20 -070012979static int __devinit tg3_phy_probe(struct tg3 *tp)
12980{
12981 u32 hw_phy_id_1, hw_phy_id_2;
12982 u32 hw_phy_id, hw_phy_id_masked;
12983 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012984
Matt Carlsone256f8a2011-03-09 16:58:24 +000012985 /* flow control autonegotiation is default behavior */
Joe Perches63c3a662011-04-26 08:12:10 +000012986 tg3_flag_set(tp, PAUSE_AUTONEG);
Matt Carlsone256f8a2011-03-09 16:58:24 +000012987 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
12988
Joe Perches63c3a662011-04-26 08:12:10 +000012989 if (tg3_flag(tp, USE_PHYLIB))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012990 return tg3_phy_init(tp);
12991
Linus Torvalds1da177e2005-04-16 15:20:36 -070012992 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012993 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012994 */
12995 err = 0;
Joe Perches63c3a662011-04-26 08:12:10 +000012996 if (tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012997 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012998 } else {
12999 /* Now read the physical PHY_ID from the chip and verify
13000 * that it is sane. If it doesn't look good, we fall back
13001 * to either the hard-coded table based PHY_ID and failing
13002 * that the value found in the eeprom area.
13003 */
13004 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
13005 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
13006
13007 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
13008 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
13009 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
13010
Matt Carlson79eb6902010-02-17 15:17:03 +000013011 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013012 }
13013
Matt Carlson79eb6902010-02-17 15:17:03 +000013014 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013015 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000013016 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013017 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070013018 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013019 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013020 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000013021 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070013022 /* Do nothing, phy ID already set up in
13023 * tg3_get_eeprom_hw_cfg().
13024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013025 } else {
13026 struct subsys_tbl_ent *p;
13027
13028 /* No eeprom signature? Try the hardcoded
13029 * subsys device table.
13030 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000013031 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013032 if (!p)
13033 return -ENODEV;
13034
13035 tp->phy_id = p->phy_id;
13036 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000013037 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013038 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013039 }
13040 }
13041
Matt Carlsona6b68da2010-12-06 08:28:52 +000013042 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
13043 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
13044 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
13045 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
13046 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000013047 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
13048
Matt Carlsone256f8a2011-03-09 16:58:24 +000013049 tg3_phy_init_link_config(tp);
13050
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013051 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013052 !tg3_flag(tp, ENABLE_APE) &&
13053 !tg3_flag(tp, ENABLE_ASF)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013054 u32 bmsr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013055
13056 tg3_readphy(tp, MII_BMSR, &bmsr);
13057 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
13058 (bmsr & BMSR_LSTATUS))
13059 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013060
Linus Torvalds1da177e2005-04-16 15:20:36 -070013061 err = tg3_phy_reset(tp);
13062 if (err)
13063 return err;
13064
Matt Carlson42b64a42011-05-19 12:12:49 +000013065 tg3_phy_set_wirespeed(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013066
Michael Chan3600d912006-12-07 00:21:48 -080013067 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
13068 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
13069 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
13070 if (!tg3_copper_is_advertising_all(tp, mask)) {
Matt Carlson42b64a42011-05-19 12:12:49 +000013071 tg3_phy_autoneg_cfg(tp, tp->link_config.advertising,
13072 tp->link_config.flowctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013073
13074 tg3_writephy(tp, MII_BMCR,
13075 BMCR_ANENABLE | BMCR_ANRESTART);
13076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013077 }
13078
13079skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000013080 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013081 err = tg3_init_5401phy_dsp(tp);
13082 if (err)
13083 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013084
Linus Torvalds1da177e2005-04-16 15:20:36 -070013085 err = tg3_init_5401phy_dsp(tp);
13086 }
13087
Linus Torvalds1da177e2005-04-16 15:20:36 -070013088 return err;
13089}
13090
Matt Carlson184b8902010-04-05 10:19:25 +000013091static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013092{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013093 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013094 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000013095 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013096
Matt Carlsonc3e94502011-04-13 11:05:08 +000013097 vpd_data = (u8 *)tg3_vpd_readblock(tp);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013098 if (!vpd_data)
13099 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013100
Matt Carlson4181b2c2010-02-26 14:04:45 +000013101 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
13102 PCI_VPD_LRDT_RO_DATA);
13103 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013104 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013105
13106 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13107 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13108 i += PCI_VPD_LRDT_TAG_SIZE;
13109
13110 if (block_end > TG3_NVM_VPD_LEN)
13111 goto out_not_found;
13112
Matt Carlson184b8902010-04-05 10:19:25 +000013113 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13114 PCI_VPD_RO_KEYWORD_MFR_ID);
13115 if (j > 0) {
13116 len = pci_vpd_info_field_size(&vpd_data[j]);
13117
13118 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13119 if (j + len > block_end || len != 4 ||
13120 memcmp(&vpd_data[j], "1028", 4))
13121 goto partno;
13122
13123 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13124 PCI_VPD_RO_KEYWORD_VENDOR0);
13125 if (j < 0)
13126 goto partno;
13127
13128 len = pci_vpd_info_field_size(&vpd_data[j]);
13129
13130 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13131 if (j + len > block_end)
13132 goto partno;
13133
13134 memcpy(tp->fw_ver, &vpd_data[j], len);
13135 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
13136 }
13137
13138partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013139 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13140 PCI_VPD_RO_KEYWORD_PARTNO);
13141 if (i < 0)
13142 goto out_not_found;
13143
13144 len = pci_vpd_info_field_size(&vpd_data[i]);
13145
13146 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13147 if (len > TG3_BPN_SIZE ||
13148 (len + i) > TG3_NVM_VPD_LEN)
13149 goto out_not_found;
13150
13151 memcpy(tp->board_part_number, &vpd_data[i], len);
13152
Linus Torvalds1da177e2005-04-16 15:20:36 -070013153out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013154 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013155 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013156 return;
13157
13158out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013159 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13160 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13161 strcpy(tp->board_part_number, "BCM5717");
13162 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13163 strcpy(tp->board_part_number, "BCM5718");
13164 else
13165 goto nomatch;
13166 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13167 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13168 strcpy(tp->board_part_number, "BCM57780");
13169 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13170 strcpy(tp->board_part_number, "BCM57760");
13171 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13172 strcpy(tp->board_part_number, "BCM57790");
13173 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13174 strcpy(tp->board_part_number, "BCM57788");
13175 else
13176 goto nomatch;
13177 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13178 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13179 strcpy(tp->board_part_number, "BCM57761");
13180 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13181 strcpy(tp->board_part_number, "BCM57765");
13182 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13183 strcpy(tp->board_part_number, "BCM57781");
13184 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13185 strcpy(tp->board_part_number, "BCM57785");
13186 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13187 strcpy(tp->board_part_number, "BCM57791");
13188 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13189 strcpy(tp->board_part_number, "BCM57795");
13190 else
13191 goto nomatch;
13192 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013193 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013194 } else {
13195nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013196 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013198}
13199
Matt Carlson9c8a6202007-10-21 16:16:08 -070013200static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13201{
13202 u32 val;
13203
Matt Carlsone4f34112009-02-25 14:25:00 +000013204 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013205 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013206 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013207 val != 0)
13208 return 0;
13209
13210 return 1;
13211}
13212
Matt Carlsonacd9c112009-02-25 14:26:33 +000013213static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13214{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013215 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013216 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013217 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013218
13219 if (tg3_nvram_read(tp, 0xc, &offset) ||
13220 tg3_nvram_read(tp, 0x4, &start))
13221 return;
13222
13223 offset = tg3_nvram_logical_addr(tp, offset);
13224
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013225 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013226 return;
13227
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013228 if ((val & 0xfc000000) == 0x0c000000) {
13229 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013230 return;
13231
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013232 if (val == 0)
13233 newver = true;
13234 }
13235
Matt Carlson75f99362010-04-05 10:19:24 +000013236 dst_off = strlen(tp->fw_ver);
13237
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013238 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013239 if (TG3_VER_SIZE - dst_off < 16 ||
13240 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013241 return;
13242
13243 offset = offset + ver_offset - start;
13244 for (i = 0; i < 16; i += 4) {
13245 __be32 v;
13246 if (tg3_nvram_read_be32(tp, offset + i, &v))
13247 return;
13248
Matt Carlson75f99362010-04-05 10:19:24 +000013249 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013250 }
13251 } else {
13252 u32 major, minor;
13253
13254 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13255 return;
13256
13257 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13258 TG3_NVM_BCVER_MAJSFT;
13259 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013260 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13261 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013262 }
13263}
13264
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013265static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13266{
13267 u32 val, major, minor;
13268
13269 /* Use native endian representation */
13270 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13271 return;
13272
13273 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13274 TG3_NVM_HWSB_CFG1_MAJSFT;
13275 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13276 TG3_NVM_HWSB_CFG1_MINSFT;
13277
13278 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13279}
13280
Matt Carlsondfe00d72008-11-21 17:19:41 -080013281static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13282{
13283 u32 offset, major, minor, build;
13284
Matt Carlson75f99362010-04-05 10:19:24 +000013285 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013286
13287 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13288 return;
13289
13290 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13291 case TG3_EEPROM_SB_REVISION_0:
13292 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13293 break;
13294 case TG3_EEPROM_SB_REVISION_2:
13295 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13296 break;
13297 case TG3_EEPROM_SB_REVISION_3:
13298 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13299 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013300 case TG3_EEPROM_SB_REVISION_4:
13301 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13302 break;
13303 case TG3_EEPROM_SB_REVISION_5:
13304 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13305 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013306 case TG3_EEPROM_SB_REVISION_6:
13307 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13308 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013309 default:
13310 return;
13311 }
13312
Matt Carlsone4f34112009-02-25 14:25:00 +000013313 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013314 return;
13315
13316 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13317 TG3_EEPROM_SB_EDH_BLD_SHFT;
13318 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13319 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13320 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13321
13322 if (minor > 99 || build > 26)
13323 return;
13324
Matt Carlson75f99362010-04-05 10:19:24 +000013325 offset = strlen(tp->fw_ver);
13326 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13327 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013328
13329 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013330 offset = strlen(tp->fw_ver);
13331 if (offset < TG3_VER_SIZE - 1)
13332 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013333 }
13334}
13335
Matt Carlsonacd9c112009-02-25 14:26:33 +000013336static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013337{
13338 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013339 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013340
13341 for (offset = TG3_NVM_DIR_START;
13342 offset < TG3_NVM_DIR_END;
13343 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013344 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013345 return;
13346
13347 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13348 break;
13349 }
13350
13351 if (offset == TG3_NVM_DIR_END)
13352 return;
13353
Joe Perches63c3a662011-04-26 08:12:10 +000013354 if (!tg3_flag(tp, 5705_PLUS))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013355 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013356 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013357 return;
13358
Matt Carlsone4f34112009-02-25 14:25:00 +000013359 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013360 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013361 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013362 return;
13363
13364 offset += val - start;
13365
Matt Carlsonacd9c112009-02-25 14:26:33 +000013366 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013367
Matt Carlsonacd9c112009-02-25 14:26:33 +000013368 tp->fw_ver[vlen++] = ',';
13369 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013370
13371 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013372 __be32 v;
13373 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013374 return;
13375
Al Virob9fc7dc2007-12-17 22:59:57 -080013376 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013377
Matt Carlsonacd9c112009-02-25 14:26:33 +000013378 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13379 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013380 break;
13381 }
13382
Matt Carlsonacd9c112009-02-25 14:26:33 +000013383 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13384 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013385 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013386}
13387
Matt Carlson7fd76442009-02-25 14:27:20 +000013388static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13389{
13390 int vlen;
13391 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013392 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013393
Joe Perches63c3a662011-04-26 08:12:10 +000013394 if (!tg3_flag(tp, ENABLE_APE) || !tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000013395 return;
13396
13397 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13398 if (apedata != APE_SEG_SIG_MAGIC)
13399 return;
13400
13401 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13402 if (!(apedata & APE_FW_STATUS_READY))
13403 return;
13404
13405 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13406
Matt Carlsondc6d0742010-09-15 08:59:55 +000013407 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
Joe Perches63c3a662011-04-26 08:12:10 +000013408 tg3_flag_set(tp, APE_HAS_NCSI);
Matt Carlsonecc79642010-08-02 11:26:01 +000013409 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013410 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013411 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013412 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013413
Matt Carlson7fd76442009-02-25 14:27:20 +000013414 vlen = strlen(tp->fw_ver);
13415
Matt Carlsonecc79642010-08-02 11:26:01 +000013416 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13417 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013418 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13419 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13420 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13421 (apedata & APE_FW_VERSION_BLDMSK));
13422}
13423
Matt Carlsonacd9c112009-02-25 14:26:33 +000013424static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13425{
13426 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013427 bool vpd_vers = false;
13428
13429 if (tp->fw_ver[0] != 0)
13430 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013431
Joe Perches63c3a662011-04-26 08:12:10 +000013432 if (tg3_flag(tp, NO_NVRAM)) {
Matt Carlson75f99362010-04-05 10:19:24 +000013433 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013434 return;
13435 }
13436
Matt Carlsonacd9c112009-02-25 14:26:33 +000013437 if (tg3_nvram_read(tp, 0, &val))
13438 return;
13439
13440 if (val == TG3_EEPROM_MAGIC)
13441 tg3_read_bc_ver(tp);
13442 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13443 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013444 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13445 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013446 else
13447 return;
13448
Joe Perches63c3a662011-04-26 08:12:10 +000013449 if (!tg3_flag(tp, ENABLE_ASF) || tg3_flag(tp, ENABLE_APE) || vpd_vers)
Matt Carlson75f99362010-04-05 10:19:24 +000013450 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013451
13452 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013453
Matt Carlson75f99362010-04-05 10:19:24 +000013454done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013455 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013456}
13457
Michael Chan7544b092007-05-05 13:08:32 -070013458static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13459
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013460static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13461{
Joe Perches63c3a662011-04-26 08:12:10 +000013462 if (tg3_flag(tp, LRG_PROD_RING_CAP))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013463 return TG3_RX_RET_MAX_SIZE_5717;
Joe Perches63c3a662011-04-26 08:12:10 +000013464 else if (tg3_flag(tp, JUMBO_CAPABLE) && !tg3_flag(tp, 5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013465 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013466 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013467 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013468}
13469
Matt Carlson41434702011-03-09 16:58:22 +000013470static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013471 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13472 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13473 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13474 { },
13475};
13476
Linus Torvalds1da177e2005-04-16 15:20:36 -070013477static int __devinit tg3_get_invariants(struct tg3 *tp)
13478{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013479 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013480 u32 pci_state_reg, grc_misc_cfg;
13481 u32 val;
13482 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013483 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013484
Linus Torvalds1da177e2005-04-16 15:20:36 -070013485 /* Force memory write invalidate off. If we leave it on,
13486 * then on 5700_BX chips we have to enable a workaround.
13487 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13488 * to match the cacheline size. The Broadcom driver have this
13489 * workaround but turns MWI off all the times so never uses
13490 * it. This seems to suggest that the workaround is insufficient.
13491 */
13492 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13493 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13494 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13495
13496 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13497 * has the register indirect write enable bit set before
13498 * we try to access any of the MMIO registers. It is also
13499 * critical that the PCI-X hw workaround situation is decided
13500 * before that as well.
13501 */
13502 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13503 &misc_ctrl_reg);
13504
13505 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13506 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013507 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13508 u32 prod_id_asic_rev;
13509
Matt Carlson5001e2f2009-11-13 13:03:51 +000013510 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13511 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013512 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13513 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013514 pci_read_config_dword(tp->pdev,
13515 TG3PCI_GEN2_PRODID_ASICREV,
13516 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013517 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13518 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13519 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13520 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13521 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13522 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13523 pci_read_config_dword(tp->pdev,
13524 TG3PCI_GEN15_PRODID_ASICREV,
13525 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013526 else
13527 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13528 &prod_id_asic_rev);
13529
Matt Carlson321d32a2008-11-21 17:22:19 -080013530 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013532
Michael Chanff645be2005-04-21 17:09:53 -070013533 /* Wrong chip ID in 5752 A0. This code can be removed later
13534 * as A0 is not in production.
13535 */
13536 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13537 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13538
Michael Chan68929142005-08-09 20:17:14 -070013539 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13540 * we need to disable memory and use config. cycles
13541 * only to access all registers. The 5702/03 chips
13542 * can mistakenly decode the special cycles from the
13543 * ICH chipsets as memory write cycles, causing corruption
13544 * of register and memory space. Only certain ICH bridges
13545 * will drive special cycles with non-zero data during the
13546 * address phase which can fall within the 5703's address
13547 * range. This is not an ICH bug as the PCI spec allows
13548 * non-zero address during special cycles. However, only
13549 * these ICH bridges are known to drive non-zero addresses
13550 * during special cycles.
13551 *
13552 * Since special cycles do not cross PCI bridges, we only
13553 * enable this workaround if the 5703 is on the secondary
13554 * bus of these ICH bridges.
13555 */
13556 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13557 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13558 static struct tg3_dev_id {
13559 u32 vendor;
13560 u32 device;
13561 u32 rev;
13562 } ich_chipsets[] = {
13563 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13564 PCI_ANY_ID },
13565 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13566 PCI_ANY_ID },
13567 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13568 0xa },
13569 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13570 PCI_ANY_ID },
13571 { },
13572 };
13573 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13574 struct pci_dev *bridge = NULL;
13575
13576 while (pci_id->vendor != 0) {
13577 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13578 bridge);
13579 if (!bridge) {
13580 pci_id++;
13581 continue;
13582 }
13583 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013584 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013585 continue;
13586 }
13587 if (bridge->subordinate &&
13588 (bridge->subordinate->number ==
13589 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013590 tg3_flag_set(tp, ICH_WORKAROUND);
Michael Chan68929142005-08-09 20:17:14 -070013591 pci_dev_put(bridge);
13592 break;
13593 }
13594 }
13595 }
13596
Matt Carlson6ff6f812011-05-19 12:12:54 +000013597 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Matt Carlson41588ba2008-04-19 18:12:33 -070013598 static struct tg3_dev_id {
13599 u32 vendor;
13600 u32 device;
13601 } bridge_chipsets[] = {
13602 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13603 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13604 { },
13605 };
13606 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13607 struct pci_dev *bridge = NULL;
13608
13609 while (pci_id->vendor != 0) {
13610 bridge = pci_get_device(pci_id->vendor,
13611 pci_id->device,
13612 bridge);
13613 if (!bridge) {
13614 pci_id++;
13615 continue;
13616 }
13617 if (bridge->subordinate &&
13618 (bridge->subordinate->number <=
13619 tp->pdev->bus->number) &&
13620 (bridge->subordinate->subordinate >=
13621 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013622 tg3_flag_set(tp, 5701_DMA_BUG);
Matt Carlson41588ba2008-04-19 18:12:33 -070013623 pci_dev_put(bridge);
13624 break;
13625 }
13626 }
13627 }
13628
Michael Chan4a29cc22006-03-19 13:21:12 -080013629 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13630 * DMA addresses > 40-bit. This bridge may have other additional
13631 * 57xx devices behind it in some 4-port NIC designs for example.
13632 * Any tg3 device found behind the bridge will also need the 40-bit
13633 * DMA workaround.
13634 */
Michael Chana4e2b342005-10-26 15:46:52 -070013635 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13636 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
Joe Perches63c3a662011-04-26 08:12:10 +000013637 tg3_flag_set(tp, 5780_CLASS);
13638 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4cf78e42005-07-25 12:29:19 -070013639 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a588792010-04-05 10:19:28 +000013640 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013641 struct pci_dev *bridge = NULL;
13642
13643 do {
13644 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13645 PCI_DEVICE_ID_SERVERWORKS_EPB,
13646 bridge);
13647 if (bridge && bridge->subordinate &&
13648 (bridge->subordinate->number <=
13649 tp->pdev->bus->number) &&
13650 (bridge->subordinate->subordinate >=
13651 tp->pdev->bus->number)) {
Joe Perches63c3a662011-04-26 08:12:10 +000013652 tg3_flag_set(tp, 40BIT_DMA_BUG);
Michael Chan4a29cc22006-03-19 13:21:12 -080013653 pci_dev_put(bridge);
13654 break;
13655 }
13656 } while (bridge);
13657 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013658
Linus Torvalds1da177e2005-04-16 15:20:36 -070013659 /* Initialize misc host control in PCI block. */
13660 tp->misc_host_ctrl |= (misc_ctrl_reg &
13661 MISC_HOST_CTRL_CHIPREV);
13662 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13663 tp->misc_host_ctrl);
13664
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013665 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13666 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013667 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13668 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013669 tp->pdev_peer = tg3_find_peer(tp);
13670
Matt Carlsonc885e822010-08-02 11:25:57 +000013671 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013672 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13673 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Joe Perches63c3a662011-04-26 08:12:10 +000013674 tg3_flag_set(tp, 5717_PLUS);
Matt Carlson0a58d662011-04-05 14:22:45 +000013675
13676 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013677 tg3_flag(tp, 5717_PLUS))
13678 tg3_flag_set(tp, 57765_PLUS);
Matt Carlsonc885e822010-08-02 11:25:57 +000013679
Matt Carlson321d32a2008-11-21 17:22:19 -080013680 /* Intentionally exclude ASIC_REV_5906 */
13681 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013682 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013683 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013684 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013685 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013686 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013687 tg3_flag(tp, 57765_PLUS))
13688 tg3_flag_set(tp, 5755_PLUS);
Matt Carlson321d32a2008-11-21 17:22:19 -080013689
13690 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13691 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013692 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013693 tg3_flag(tp, 5755_PLUS) ||
13694 tg3_flag(tp, 5780_CLASS))
13695 tg3_flag_set(tp, 5750_PLUS);
John W. Linville6708e5c2005-04-21 17:00:52 -070013696
Matt Carlson6ff6f812011-05-19 12:12:54 +000013697 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013698 tg3_flag(tp, 5750_PLUS))
13699 tg3_flag_set(tp, 5705_PLUS);
John W. Linville1b440c562005-04-21 17:03:18 -070013700
Matt Carlson507399f2009-11-13 13:03:37 +000013701 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013702 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013703 ; /* Do nothing. HW bug. */
Joe Perches63c3a662011-04-26 08:12:10 +000013704 else if (tg3_flag(tp, 57765_PLUS))
13705 tg3_flag_set(tp, HW_TSO_3);
13706 else if (tg3_flag(tp, 5755_PLUS) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000013707 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Joe Perches63c3a662011-04-26 08:12:10 +000013708 tg3_flag_set(tp, HW_TSO_2);
13709 else if (tg3_flag(tp, 5750_PLUS)) {
13710 tg3_flag_set(tp, HW_TSO_1);
13711 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013712 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13713 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
Joe Perches63c3a662011-04-26 08:12:10 +000013714 tg3_flag_clear(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013715 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13716 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13717 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013718 tg3_flag_set(tp, TSO_BUG);
Matt Carlson507399f2009-11-13 13:03:37 +000013719 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13720 tp->fw_needed = FIRMWARE_TG3TSO5;
13721 else
13722 tp->fw_needed = FIRMWARE_TG3TSO;
13723 }
13724
Matt Carlsondabc5c62011-05-19 12:12:52 +000013725 /* Selectively allow TSO based on operating conditions */
Matt Carlson6ff6f812011-05-19 12:12:54 +000013726 if (tg3_flag(tp, HW_TSO_1) ||
13727 tg3_flag(tp, HW_TSO_2) ||
13728 tg3_flag(tp, HW_TSO_3) ||
Matt Carlsondabc5c62011-05-19 12:12:52 +000013729 (tp->fw_needed && !tg3_flag(tp, ENABLE_ASF)))
13730 tg3_flag_set(tp, TSO_CAPABLE);
13731 else {
13732 tg3_flag_clear(tp, TSO_CAPABLE);
13733 tg3_flag_clear(tp, TSO_BUG);
13734 tp->fw_needed = NULL;
13735 }
13736
13737 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
13738 tp->fw_needed = FIRMWARE_TG3;
13739
Matt Carlson507399f2009-11-13 13:03:37 +000013740 tp->irq_max = 1;
13741
Joe Perches63c3a662011-04-26 08:12:10 +000013742 if (tg3_flag(tp, 5750_PLUS)) {
13743 tg3_flag_set(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013744 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13745 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13746 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13747 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13748 tp->pdev_peer == tp->pdev))
Joe Perches63c3a662011-04-26 08:12:10 +000013749 tg3_flag_clear(tp, SUPPORT_MSI);
Michael Chan7544b092007-05-05 13:08:32 -070013750
Joe Perches63c3a662011-04-26 08:12:10 +000013751 if (tg3_flag(tp, 5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013752 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013753 tg3_flag_set(tp, 1SHOT_MSI);
Michael Chan52c0fd82006-06-29 20:15:54 -070013754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013755
Joe Perches63c3a662011-04-26 08:12:10 +000013756 if (tg3_flag(tp, 57765_PLUS)) {
13757 tg3_flag_set(tp, SUPPORT_MSIX);
Matt Carlson507399f2009-11-13 13:03:37 +000013758 tp->irq_max = TG3_IRQ_MAX_VECS;
13759 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013760 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013761
Matt Carlson2ffcc982011-05-19 12:12:44 +000013762 if (tg3_flag(tp, 5755_PLUS))
Joe Perches63c3a662011-04-26 08:12:10 +000013763 tg3_flag_set(tp, SHORT_DMA_BUG);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013764
Joe Perches63c3a662011-04-26 08:12:10 +000013765 if (tg3_flag(tp, 5717_PLUS))
13766 tg3_flag_set(tp, LRG_PROD_RING_CAP);
Matt Carlsonde9f5232011-04-05 14:22:43 +000013767
Joe Perches63c3a662011-04-26 08:12:10 +000013768 if (tg3_flag(tp, 57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013769 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Joe Perches63c3a662011-04-26 08:12:10 +000013770 tg3_flag_set(tp, USE_JUMBO_BDFLAG);
Matt Carlsonb703df62009-12-03 08:36:21 +000013771
Joe Perches63c3a662011-04-26 08:12:10 +000013772 if (!tg3_flag(tp, 5705_PLUS) ||
13773 tg3_flag(tp, 5780_CLASS) ||
13774 tg3_flag(tp, USE_JUMBO_BDFLAG))
13775 tg3_flag_set(tp, JUMBO_CAPABLE);
Michael Chan0f893dc2005-07-25 12:30:38 -070013776
Matt Carlson52f44902008-11-21 17:17:04 -080013777 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13778 &pci_state_reg);
13779
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013780 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13781 if (tp->pcie_cap != 0) {
13782 u16 lnkctl;
13783
Joe Perches63c3a662011-04-26 08:12:10 +000013784 tg3_flag_set(tp, PCI_EXPRESS);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013785
Matt Carlsoncf790032010-11-24 08:31:48 +000013786 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013787 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13788 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013789 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013790
13791 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013792
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013793 pci_read_config_word(tp->pdev,
13794 tp->pcie_cap + PCI_EXP_LNKCTL,
13795 &lnkctl);
13796 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
Matt Carlson7196cd62011-05-19 16:02:44 +000013797 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
13798 ASIC_REV_5906) {
Joe Perches63c3a662011-04-26 08:12:10 +000013799 tg3_flag_clear(tp, HW_TSO_2);
Matt Carlsondabc5c62011-05-19 12:12:52 +000013800 tg3_flag_clear(tp, TSO_CAPABLE);
Matt Carlson7196cd62011-05-19 16:02:44 +000013801 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013802 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013803 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013804 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13805 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Joe Perches63c3a662011-04-26 08:12:10 +000013806 tg3_flag_set(tp, CLKREQ_BUG);
Matt Carlson614b0592010-01-20 16:58:02 +000013807 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
Joe Perches63c3a662011-04-26 08:12:10 +000013808 tg3_flag_set(tp, L1PLLPD_EN);
Michael Chanc7835a72006-11-15 21:14:42 -080013809 }
Matt Carlson52f44902008-11-21 17:17:04 -080013810 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Joe Perches63c3a662011-04-26 08:12:10 +000013811 tg3_flag_set(tp, PCI_EXPRESS);
13812 } else if (!tg3_flag(tp, 5705_PLUS) ||
13813 tg3_flag(tp, 5780_CLASS)) {
Matt Carlson52f44902008-11-21 17:17:04 -080013814 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13815 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013816 dev_err(&tp->pdev->dev,
13817 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013818 return -EIO;
13819 }
13820
13821 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
Joe Perches63c3a662011-04-26 08:12:10 +000013822 tg3_flag_set(tp, PCIX_MODE);
Matt Carlson52f44902008-11-21 17:17:04 -080013823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013824
Michael Chan399de502005-10-03 14:02:39 -070013825 /* If we have an AMD 762 or VIA K8T800 chipset, write
13826 * reordering to the mailbox registers done by the host
13827 * controller can cause major troubles. We read back from
13828 * every mailbox register write to force the writes to be
13829 * posted to the chip in order.
13830 */
Matt Carlson41434702011-03-09 16:58:22 +000013831 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Joe Perches63c3a662011-04-26 08:12:10 +000013832 !tg3_flag(tp, PCI_EXPRESS))
13833 tg3_flag_set(tp, MBOX_WRITE_REORDER);
Michael Chan399de502005-10-03 14:02:39 -070013834
Matt Carlson69fc4052008-12-21 20:19:57 -080013835 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13836 &tp->pci_cacheline_sz);
13837 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13838 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013839 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13840 tp->pci_lat_timer < 64) {
13841 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013842 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13843 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013844 }
13845
Matt Carlson52f44902008-11-21 17:17:04 -080013846 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13847 /* 5700 BX chips need to have their TX producer index
13848 * mailboxes written twice to workaround a bug.
13849 */
Joe Perches63c3a662011-04-26 08:12:10 +000013850 tg3_flag_set(tp, TXD_MBOX_HWBUG);
Matt Carlson9974a352007-10-07 23:27:28 -070013851
Matt Carlson52f44902008-11-21 17:17:04 -080013852 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013853 *
13854 * The workaround is to use indirect register accesses
13855 * for all chip writes not to mailbox registers.
13856 */
Joe Perches63c3a662011-04-26 08:12:10 +000013857 if (tg3_flag(tp, PCIX_MODE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013858 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013859
Joe Perches63c3a662011-04-26 08:12:10 +000013860 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013861
13862 /* The chip can have it's power management PCI config
13863 * space registers clobbered due to this bug.
13864 * So explicitly force the chip into D0 here.
13865 */
Matt Carlson9974a352007-10-07 23:27:28 -070013866 pci_read_config_dword(tp->pdev,
13867 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013868 &pm_reg);
13869 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13870 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013871 pci_write_config_dword(tp->pdev,
13872 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013873 pm_reg);
13874
13875 /* Also, force SERR#/PERR# in PCI command. */
13876 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13877 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13878 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13879 }
13880 }
13881
Linus Torvalds1da177e2005-04-16 15:20:36 -070013882 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013883 tg3_flag_set(tp, PCI_HIGH_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013884 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
Joe Perches63c3a662011-04-26 08:12:10 +000013885 tg3_flag_set(tp, PCI_32BIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013886
13887 /* Chip-specific fixup from Broadcom driver */
13888 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13889 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13890 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13891 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13892 }
13893
Michael Chan1ee582d2005-08-09 20:16:46 -070013894 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013895 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013896 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013897 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013898 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013899 tp->write32_tx_mbox = tg3_write32;
13900 tp->write32_rx_mbox = tg3_write32;
13901
13902 /* Various workaround register access methods */
Joe Perches63c3a662011-04-26 08:12:10 +000013903 if (tg3_flag(tp, PCIX_TARGET_HWBUG))
Michael Chan1ee582d2005-08-09 20:16:46 -070013904 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013905 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013906 (tg3_flag(tp, PCI_EXPRESS) &&
Matt Carlson98efd8a2007-05-05 12:47:25 -070013907 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13908 /*
13909 * Back to back register writes can cause problems on these
13910 * chips, the workaround is to read back all reg writes
13911 * except those to mailbox regs.
13912 *
13913 * See tg3_write_indirect_reg32().
13914 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013915 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013916 }
13917
Joe Perches63c3a662011-04-26 08:12:10 +000013918 if (tg3_flag(tp, TXD_MBOX_HWBUG) || tg3_flag(tp, MBOX_WRITE_REORDER)) {
Michael Chan1ee582d2005-08-09 20:16:46 -070013919 tp->write32_tx_mbox = tg3_write32_tx_mbox;
Joe Perches63c3a662011-04-26 08:12:10 +000013920 if (tg3_flag(tp, MBOX_WRITE_REORDER))
Michael Chan1ee582d2005-08-09 20:16:46 -070013921 tp->write32_rx_mbox = tg3_write_flush_reg32;
13922 }
Michael Chan20094932005-08-09 20:16:32 -070013923
Joe Perches63c3a662011-04-26 08:12:10 +000013924 if (tg3_flag(tp, ICH_WORKAROUND)) {
Michael Chan68929142005-08-09 20:17:14 -070013925 tp->read32 = tg3_read_indirect_reg32;
13926 tp->write32 = tg3_write_indirect_reg32;
13927 tp->read32_mbox = tg3_read_indirect_mbox;
13928 tp->write32_mbox = tg3_write_indirect_mbox;
13929 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13930 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13931
13932 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013933 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013934
13935 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13936 pci_cmd &= ~PCI_COMMAND_MEMORY;
13937 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13938 }
Michael Chanb5d37722006-09-27 16:06:21 -070013939 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13940 tp->read32_mbox = tg3_read32_mbox_5906;
13941 tp->write32_mbox = tg3_write32_mbox_5906;
13942 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13943 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13944 }
Michael Chan68929142005-08-09 20:17:14 -070013945
Michael Chanbbadf502006-04-06 21:46:34 -070013946 if (tp->write32 == tg3_write_indirect_reg32 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013947 (tg3_flag(tp, PCIX_MODE) &&
Michael Chanbbadf502006-04-06 21:46:34 -070013948 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013949 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Joe Perches63c3a662011-04-26 08:12:10 +000013950 tg3_flag_set(tp, SRAM_USE_CONFIG);
Michael Chanbbadf502006-04-06 21:46:34 -070013951
Michael Chan7d0c41e2005-04-21 17:06:20 -070013952 /* Get eeprom hw config before calling tg3_set_power_state().
Joe Perches63c3a662011-04-26 08:12:10 +000013953 * In particular, the TG3_FLAG_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013954 * determined before calling tg3_set_power_state() so that
13955 * we know whether or not to switch out of Vaux power.
13956 * When the flag is set, it means that GPIO1 is used for eeprom
13957 * write protect and also implies that it is a LOM where GPIOs
13958 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013959 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013960 tg3_get_eeprom_hw_cfg(tp);
13961
Joe Perches63c3a662011-04-26 08:12:10 +000013962 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson0d3031d2007-10-10 18:02:43 -070013963 /* Allow reads and writes to the
13964 * APE register and memory space.
13965 */
13966 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc12010-06-05 17:24:30 +000013967 PCISTATE_ALLOW_APE_SHMEM_WR |
13968 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013969 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13970 pci_state_reg);
13971 }
13972
Matt Carlson9936bcf2007-10-10 18:03:07 -070013973 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013974 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013975 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013976 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013977 tg3_flag(tp, 57765_PLUS))
13978 tg3_flag_set(tp, CPMU_PRESENT);
Matt Carlsond30cdd22007-10-07 23:28:35 -070013979
Matt Carlsonbea8a632011-04-25 12:42:49 +000013980 /* Set up tp->grc_local_ctrl before calling tg3_power_up().
Michael Chan314fba32005-04-21 17:07:04 -070013981 * GPIO1 driven high will bring 5700's external PHY out of reset.
13982 * It is also used as eeprom write protect on LOMs.
13983 */
13984 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
Matt Carlson6ff6f812011-05-19 12:12:54 +000013985 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
Joe Perches63c3a662011-04-26 08:12:10 +000013986 tg3_flag(tp, EEPROM_WRITE_PROT))
Michael Chan314fba32005-04-21 17:07:04 -070013987 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13988 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013989 /* Unused GPIO3 must be driven as output on 5752 because there
13990 * are no pull-up resistors on unused GPIO pins.
13991 */
13992 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13993 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013994
Matt Carlson321d32a2008-11-21 17:22:19 -080013995 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013996 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13997 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013998 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13999
Matt Carlson8d519ab2009-04-20 06:58:01 +000014000 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
14001 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014002 /* Turn off the debug UART. */
14003 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
Joe Perches63c3a662011-04-26 08:12:10 +000014004 if (tg3_flag(tp, IS_NIC))
Matt Carlson5f0c4a32008-06-09 15:41:12 -070014005 /* Keep VMain power. */
14006 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
14007 GRC_LCLCTRL_GPIO_OUTPUT0;
14008 }
14009
Linus Torvalds1da177e2005-04-16 15:20:36 -070014010 /* Force the chip into D0. */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000014011 err = tg3_power_up(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014012 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014013 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014014 return err;
14015 }
14016
Linus Torvalds1da177e2005-04-16 15:20:36 -070014017 /* Derive initial jumbo mode from MTU assigned in
14018 * ether_setup() via the alloc_etherdev() call
14019 */
Joe Perches63c3a662011-04-26 08:12:10 +000014020 if (tp->dev->mtu > ETH_DATA_LEN && !tg3_flag(tp, 5780_CLASS))
14021 tg3_flag_set(tp, JUMBO_RING_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014022
14023 /* Determine WakeOnLan speed to use. */
14024 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14025 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
14026 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
14027 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
Joe Perches63c3a662011-04-26 08:12:10 +000014028 tg3_flag_clear(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014029 } else {
Joe Perches63c3a662011-04-26 08:12:10 +000014030 tg3_flag_set(tp, WOL_SPEED_100MB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014031 }
14032
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014033 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014034 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000014035
Linus Torvalds1da177e2005-04-16 15:20:36 -070014036 /* A few boards don't want Ethernet@WireSpeed phy feature */
Matt Carlson6ff6f812011-05-19 12:12:54 +000014037 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14038 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070014039 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070014040 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014041 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
14042 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
14043 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014044
14045 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
14046 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014047 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014048 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014049 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014050
Joe Perches63c3a662011-04-26 08:12:10 +000014051 if (tg3_flag(tp, 5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014052 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080014053 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000014054 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014055 !tg3_flag(tp, 57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070014056 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070014057 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070014058 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
14059 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080014060 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
14061 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014062 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080014063 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014064 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080014065 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014066 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070014067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014068
Matt Carlsonb2a5c192008-04-03 21:44:44 -070014069 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
14070 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
14071 tp->phy_otp = tg3_read_otp_phycfg(tp);
14072 if (tp->phy_otp == 0)
14073 tp->phy_otp = TG3_OTP_DEFAULT;
14074 }
14075
Joe Perches63c3a662011-04-26 08:12:10 +000014076 if (tg3_flag(tp, CPMU_PRESENT))
Matt Carlson8ef21422008-05-02 16:47:53 -070014077 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
14078 else
14079 tp->mi_mode = MAC_MI_MODE_BASE;
14080
Linus Torvalds1da177e2005-04-16 15:20:36 -070014081 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014082 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
14083 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
14084 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
14085
Matt Carlson4d958472011-04-20 07:57:35 +000014086 /* Set these bits to enable statistics workaround. */
14087 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
14088 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
14089 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
14090 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
14091 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
14092 }
14093
Matt Carlson321d32a2008-11-21 17:22:19 -080014094 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14095 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Joe Perches63c3a662011-04-26 08:12:10 +000014096 tg3_flag_set(tp, USE_PHYLIB);
Matt Carlson57e69832008-05-25 23:48:31 -070014097
Matt Carlson158d7ab2008-05-29 01:37:54 -070014098 err = tg3_mdio_init(tp);
14099 if (err)
14100 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014101
14102 /* Initialize data/descriptor byte/word swapping. */
14103 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014104 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14105 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14106 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14107 GRC_MODE_B2HRX_ENABLE |
14108 GRC_MODE_HTX2B_ENABLE |
14109 GRC_MODE_HOST_STACKUP);
14110 else
14111 val &= GRC_MODE_HOST_STACKUP;
14112
Linus Torvalds1da177e2005-04-16 15:20:36 -070014113 tw32(GRC_MODE, val | tp->grc_mode);
14114
14115 tg3_switch_clocks(tp);
14116
14117 /* Clear this out for sanity. */
14118 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14119
14120 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14121 &pci_state_reg);
14122 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014123 !tg3_flag(tp, PCIX_TARGET_HWBUG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014124 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14125
14126 if (chiprevid == CHIPREV_ID_5701_A0 ||
14127 chiprevid == CHIPREV_ID_5701_B0 ||
14128 chiprevid == CHIPREV_ID_5701_B2 ||
14129 chiprevid == CHIPREV_ID_5701_B5) {
14130 void __iomem *sram_base;
14131
14132 /* Write some dummy words into the SRAM status block
14133 * area, see if it reads back correctly. If the return
14134 * value is bad, force enable the PCIX workaround.
14135 */
14136 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14137
14138 writel(0x00000000, sram_base);
14139 writel(0x00000000, sram_base + 4);
14140 writel(0xffffffff, sram_base + 4);
14141 if (readl(sram_base) != 0x00000000)
Joe Perches63c3a662011-04-26 08:12:10 +000014142 tg3_flag_set(tp, PCIX_TARGET_HWBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014143 }
14144 }
14145
14146 udelay(50);
14147 tg3_nvram_init(tp);
14148
14149 grc_misc_cfg = tr32(GRC_MISC_CFG);
14150 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14151
Linus Torvalds1da177e2005-04-16 15:20:36 -070014152 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14153 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14154 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
Joe Perches63c3a662011-04-26 08:12:10 +000014155 tg3_flag_set(tp, IS_5788);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014156
Joe Perches63c3a662011-04-26 08:12:10 +000014157 if (!tg3_flag(tp, IS_5788) &&
Matt Carlson6ff6f812011-05-19 12:12:54 +000014158 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014159 tg3_flag_set(tp, TAGGED_STATUS);
14160 if (tg3_flag(tp, TAGGED_STATUS)) {
David S. Millerfac9b832005-05-18 22:46:34 -070014161 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14162 HOSTCC_MODE_CLRTICK_TXBD);
14163
14164 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14165 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14166 tp->misc_host_ctrl);
14167 }
14168
Matt Carlson3bda1252008-08-15 14:08:22 -070014169 /* Preserve the APE MAC_MODE bits */
Joe Perches63c3a662011-04-26 08:12:10 +000014170 if (tg3_flag(tp, ENABLE_APE))
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014171 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014172 else
14173 tp->mac_mode = TG3_DEF_MAC_MODE;
14174
Linus Torvalds1da177e2005-04-16 15:20:36 -070014175 /* these are limited to 10/100 only */
14176 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14177 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14178 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14179 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14180 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14181 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14182 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14183 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14184 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014185 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14186 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014187 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014188 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14189 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014190 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14191 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014192
14193 err = tg3_phy_probe(tp);
14194 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014195 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014196 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014197 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014198 }
14199
Matt Carlson184b8902010-04-05 10:19:25 +000014200 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014201 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014202
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014203 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14204 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014205 } else {
14206 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014207 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014208 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014209 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014210 }
14211
14212 /* 5700 {AX,BX} chips have a broken status block link
14213 * change bit implementation, so we must use the
14214 * status register in those cases.
14215 */
14216 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Joe Perches63c3a662011-04-26 08:12:10 +000014217 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014218 else
Joe Perches63c3a662011-04-26 08:12:10 +000014219 tg3_flag_clear(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014220
14221 /* The led_ctrl is set during tg3_phy_probe, here we might
14222 * have to force the link status polling mechanism based
14223 * upon subsystem IDs.
14224 */
14225 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014226 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014227 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14228 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Joe Perches63c3a662011-04-26 08:12:10 +000014229 tg3_flag_set(tp, USE_LINKCHG_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014230 }
14231
14232 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014233 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Joe Perches63c3a662011-04-26 08:12:10 +000014234 tg3_flag_set(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014235 else
Joe Perches63c3a662011-04-26 08:12:10 +000014236 tg3_flag_clear(tp, POLL_SERDES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014237
Matt Carlsonbf933c82011-01-25 15:58:49 +000014238 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014239 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014240 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014241 tg3_flag(tp, PCIX_MODE)) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014242 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014243#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014244 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014245#endif
14246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014247
Matt Carlson2c49a442010-09-30 10:34:35 +000014248 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14249 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014250 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14251
Matt Carlson2c49a442010-09-30 10:34:35 +000014252 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014253
14254 /* Increment the rx prod index on the rx std ring by at most
14255 * 8 for these chips to workaround hw errata.
14256 */
14257 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14258 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14259 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14260 tp->rx_std_max_post = 8;
14261
Joe Perches63c3a662011-04-26 08:12:10 +000014262 if (tg3_flag(tp, ASPM_WORKAROUND))
Matt Carlson8ed5d972007-05-07 00:25:49 -070014263 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14264 PCIE_PWR_MGMT_L1_THRESH_MSK;
14265
Linus Torvalds1da177e2005-04-16 15:20:36 -070014266 return err;
14267}
14268
David S. Miller49b6e95f2007-03-29 01:38:42 -070014269#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014270static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14271{
14272 struct net_device *dev = tp->dev;
14273 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014274 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014275 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014276 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014277
David S. Miller49b6e95f2007-03-29 01:38:42 -070014278 addr = of_get_property(dp, "local-mac-address", &len);
14279 if (addr && len == 6) {
14280 memcpy(dev->dev_addr, addr, 6);
14281 memcpy(dev->perm_addr, dev->dev_addr, 6);
14282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014283 }
14284 return -ENODEV;
14285}
14286
14287static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14288{
14289 struct net_device *dev = tp->dev;
14290
14291 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014292 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014293 return 0;
14294}
14295#endif
14296
14297static int __devinit tg3_get_device_address(struct tg3 *tp)
14298{
14299 struct net_device *dev = tp->dev;
14300 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014301 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014302
David S. Miller49b6e95f2007-03-29 01:38:42 -070014303#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014304 if (!tg3_get_macaddr_sparc(tp))
14305 return 0;
14306#endif
14307
14308 mac_offset = 0x7c;
Matt Carlson6ff6f812011-05-19 12:12:54 +000014309 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
Joe Perches63c3a662011-04-26 08:12:10 +000014310 tg3_flag(tp, 5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014311 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14312 mac_offset = 0xcc;
14313 if (tg3_nvram_lock(tp))
14314 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14315 else
14316 tg3_nvram_unlock(tp);
Joe Perches63c3a662011-04-26 08:12:10 +000014317 } else if (tg3_flag(tp, 5717_PLUS)) {
Matt Carlsona50d0792010-06-05 17:24:37 +000014318 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014319 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000014320 if (PCI_FUNC(tp->pdev->devfn) > 1)
14321 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014322 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014323 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014324
14325 /* First try to get it from MAC address mailbox. */
14326 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14327 if ((hi >> 16) == 0x484b) {
14328 dev->dev_addr[0] = (hi >> 8) & 0xff;
14329 dev->dev_addr[1] = (hi >> 0) & 0xff;
14330
14331 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14332 dev->dev_addr[2] = (lo >> 24) & 0xff;
14333 dev->dev_addr[3] = (lo >> 16) & 0xff;
14334 dev->dev_addr[4] = (lo >> 8) & 0xff;
14335 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014336
Michael Chan008652b2006-03-27 23:14:53 -080014337 /* Some old bootcode may report a 0 MAC address in SRAM */
14338 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14339 }
14340 if (!addr_ok) {
14341 /* Next, try NVRAM. */
Joe Perches63c3a662011-04-26 08:12:10 +000014342 if (!tg3_flag(tp, NO_NVRAM) &&
Matt Carlsondf259d82009-04-20 06:57:14 +000014343 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014344 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014345 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14346 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014347 }
14348 /* Finally just fetch it out of the MAC control regs. */
14349 else {
14350 hi = tr32(MAC_ADDR_0_HIGH);
14351 lo = tr32(MAC_ADDR_0_LOW);
14352
14353 dev->dev_addr[5] = lo & 0xff;
14354 dev->dev_addr[4] = (lo >> 8) & 0xff;
14355 dev->dev_addr[3] = (lo >> 16) & 0xff;
14356 dev->dev_addr[2] = (lo >> 24) & 0xff;
14357 dev->dev_addr[1] = hi & 0xff;
14358 dev->dev_addr[0] = (hi >> 8) & 0xff;
14359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014360 }
14361
14362 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014363#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014364 if (!tg3_get_default_macaddr_sparc(tp))
14365 return 0;
14366#endif
14367 return -EINVAL;
14368 }
John W. Linville2ff43692005-09-12 14:44:20 -070014369 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014370 return 0;
14371}
14372
David S. Miller59e6b432005-05-18 22:50:10 -070014373#define BOUNDARY_SINGLE_CACHELINE 1
14374#define BOUNDARY_MULTI_CACHELINE 2
14375
14376static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14377{
14378 int cacheline_size;
14379 u8 byte;
14380 int goal;
14381
14382 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14383 if (byte == 0)
14384 cacheline_size = 1024;
14385 else
14386 cacheline_size = (int) byte * 4;
14387
14388 /* On 5703 and later chips, the boundary bits have no
14389 * effect.
14390 */
14391 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14392 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
Joe Perches63c3a662011-04-26 08:12:10 +000014393 !tg3_flag(tp, PCI_EXPRESS))
David S. Miller59e6b432005-05-18 22:50:10 -070014394 goto out;
14395
14396#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14397 goal = BOUNDARY_MULTI_CACHELINE;
14398#else
14399#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14400 goal = BOUNDARY_SINGLE_CACHELINE;
14401#else
14402 goal = 0;
14403#endif
14404#endif
14405
Joe Perches63c3a662011-04-26 08:12:10 +000014406 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014407 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14408 goto out;
14409 }
14410
David S. Miller59e6b432005-05-18 22:50:10 -070014411 if (!goal)
14412 goto out;
14413
14414 /* PCI controllers on most RISC systems tend to disconnect
14415 * when a device tries to burst across a cache-line boundary.
14416 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14417 *
14418 * Unfortunately, for PCI-E there are only limited
14419 * write-side controls for this, and thus for reads
14420 * we will still get the disconnects. We'll also waste
14421 * these PCI cycles for both read and write for chips
14422 * other than 5700 and 5701 which do not implement the
14423 * boundary bits.
14424 */
Joe Perches63c3a662011-04-26 08:12:10 +000014425 if (tg3_flag(tp, PCIX_MODE) && !tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014426 switch (cacheline_size) {
14427 case 16:
14428 case 32:
14429 case 64:
14430 case 128:
14431 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14432 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14433 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14434 } else {
14435 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14436 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14437 }
14438 break;
14439
14440 case 256:
14441 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14442 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14443 break;
14444
14445 default:
14446 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14447 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14448 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014449 }
Joe Perches63c3a662011-04-26 08:12:10 +000014450 } else if (tg3_flag(tp, PCI_EXPRESS)) {
David S. Miller59e6b432005-05-18 22:50:10 -070014451 switch (cacheline_size) {
14452 case 16:
14453 case 32:
14454 case 64:
14455 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14456 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14457 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14458 break;
14459 }
14460 /* fallthrough */
14461 case 128:
14462 default:
14463 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14464 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14465 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014466 }
David S. Miller59e6b432005-05-18 22:50:10 -070014467 } else {
14468 switch (cacheline_size) {
14469 case 16:
14470 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14471 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14472 DMA_RWCTRL_WRITE_BNDRY_16);
14473 break;
14474 }
14475 /* fallthrough */
14476 case 32:
14477 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14478 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14479 DMA_RWCTRL_WRITE_BNDRY_32);
14480 break;
14481 }
14482 /* fallthrough */
14483 case 64:
14484 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14485 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14486 DMA_RWCTRL_WRITE_BNDRY_64);
14487 break;
14488 }
14489 /* fallthrough */
14490 case 128:
14491 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14492 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14493 DMA_RWCTRL_WRITE_BNDRY_128);
14494 break;
14495 }
14496 /* fallthrough */
14497 case 256:
14498 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14499 DMA_RWCTRL_WRITE_BNDRY_256);
14500 break;
14501 case 512:
14502 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14503 DMA_RWCTRL_WRITE_BNDRY_512);
14504 break;
14505 case 1024:
14506 default:
14507 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14508 DMA_RWCTRL_WRITE_BNDRY_1024);
14509 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014510 }
David S. Miller59e6b432005-05-18 22:50:10 -070014511 }
14512
14513out:
14514 return val;
14515}
14516
Linus Torvalds1da177e2005-04-16 15:20:36 -070014517static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14518{
14519 struct tg3_internal_buffer_desc test_desc;
14520 u32 sram_dma_descs;
14521 int i, ret;
14522
14523 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14524
14525 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14526 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14527 tw32(RDMAC_STATUS, 0);
14528 tw32(WDMAC_STATUS, 0);
14529
14530 tw32(BUFMGR_MODE, 0);
14531 tw32(FTQ_RESET, 0);
14532
14533 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14534 test_desc.addr_lo = buf_dma & 0xffffffff;
14535 test_desc.nic_mbuf = 0x00002100;
14536 test_desc.len = size;
14537
14538 /*
14539 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14540 * the *second* time the tg3 driver was getting loaded after an
14541 * initial scan.
14542 *
14543 * Broadcom tells me:
14544 * ...the DMA engine is connected to the GRC block and a DMA
14545 * reset may affect the GRC block in some unpredictable way...
14546 * The behavior of resets to individual blocks has not been tested.
14547 *
14548 * Broadcom noted the GRC reset will also reset all sub-components.
14549 */
14550 if (to_device) {
14551 test_desc.cqid_sqid = (13 << 8) | 2;
14552
14553 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14554 udelay(40);
14555 } else {
14556 test_desc.cqid_sqid = (16 << 8) | 7;
14557
14558 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14559 udelay(40);
14560 }
14561 test_desc.flags = 0x00000005;
14562
14563 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14564 u32 val;
14565
14566 val = *(((u32 *)&test_desc) + i);
14567 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14568 sram_dma_descs + (i * sizeof(u32)));
14569 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14570 }
14571 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14572
Matt Carlson859a588792010-04-05 10:19:28 +000014573 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014574 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a588792010-04-05 10:19:28 +000014575 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014576 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014577
14578 ret = -ENODEV;
14579 for (i = 0; i < 40; i++) {
14580 u32 val;
14581
14582 if (to_device)
14583 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14584 else
14585 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14586 if ((val & 0xffff) == sram_dma_descs) {
14587 ret = 0;
14588 break;
14589 }
14590
14591 udelay(100);
14592 }
14593
14594 return ret;
14595}
14596
David S. Millerded73402005-05-23 13:59:47 -070014597#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014598
Matt Carlson41434702011-03-09 16:58:22 +000014599static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014600 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14601 { },
14602};
14603
Linus Torvalds1da177e2005-04-16 15:20:36 -070014604static int __devinit tg3_test_dma(struct tg3 *tp)
14605{
14606 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014607 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014608 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014609
Matt Carlson4bae65c2010-11-24 08:31:52 +000014610 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14611 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014612 if (!buf) {
14613 ret = -ENOMEM;
14614 goto out_nofree;
14615 }
14616
14617 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14618 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14619
David S. Miller59e6b432005-05-18 22:50:10 -070014620 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014621
Joe Perches63c3a662011-04-26 08:12:10 +000014622 if (tg3_flag(tp, 57765_PLUS))
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014623 goto out;
14624
Joe Perches63c3a662011-04-26 08:12:10 +000014625 if (tg3_flag(tp, PCI_EXPRESS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014626 /* DMA read watermark not used on PCIE */
14627 tp->dma_rwctrl |= 0x00180000;
Joe Perches63c3a662011-04-26 08:12:10 +000014628 } else if (!tg3_flag(tp, PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014629 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14630 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014631 tp->dma_rwctrl |= 0x003f0000;
14632 else
14633 tp->dma_rwctrl |= 0x003f000f;
14634 } else {
14635 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14636 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14637 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014638 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014639
Michael Chan4a29cc22006-03-19 13:21:12 -080014640 /* If the 5704 is behind the EPB bridge, we can
14641 * do the less restrictive ONE_DMA workaround for
14642 * better performance.
14643 */
Joe Perches63c3a662011-04-26 08:12:10 +000014644 if (tg3_flag(tp, 40BIT_DMA_BUG) &&
Michael Chan4a29cc22006-03-19 13:21:12 -080014645 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14646 tp->dma_rwctrl |= 0x8000;
14647 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014648 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14649
Michael Chan49afdeb2007-02-13 12:17:03 -080014650 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14651 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014652 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014653 tp->dma_rwctrl |=
14654 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14655 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14656 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014657 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14658 /* 5780 always in PCIX mode */
14659 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014660 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14661 /* 5714 always in PCIX mode */
14662 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014663 } else {
14664 tp->dma_rwctrl |= 0x001b000f;
14665 }
14666 }
14667
14668 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14669 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14670 tp->dma_rwctrl &= 0xfffffff0;
14671
14672 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14673 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14674 /* Remove this if it causes problems for some boards. */
14675 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14676
14677 /* On 5700/5701 chips, we need to set this bit.
14678 * Otherwise the chip will issue cacheline transactions
14679 * to streamable DMA memory with not all the byte
14680 * enables turned on. This is an error on several
14681 * RISC PCI controllers, in particular sparc64.
14682 *
14683 * On 5703/5704 chips, this bit has been reassigned
14684 * a different meaning. In particular, it is used
14685 * on those chips to enable a PCI-X workaround.
14686 */
14687 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14688 }
14689
14690 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14691
14692#if 0
14693 /* Unneeded, already done by tg3_get_invariants. */
14694 tg3_switch_clocks(tp);
14695#endif
14696
Linus Torvalds1da177e2005-04-16 15:20:36 -070014697 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14698 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14699 goto out;
14700
David S. Miller59e6b432005-05-18 22:50:10 -070014701 /* It is best to perform DMA test with maximum write burst size
14702 * to expose the 5700/5701 write DMA bug.
14703 */
14704 saved_dma_rwctrl = tp->dma_rwctrl;
14705 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14706 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14707
Linus Torvalds1da177e2005-04-16 15:20:36 -070014708 while (1) {
14709 u32 *p = buf, i;
14710
14711 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14712 p[i] = i;
14713
14714 /* Send the buffer to the chip. */
14715 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14716 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014717 dev_err(&tp->pdev->dev,
14718 "%s: Buffer write failed. err = %d\n",
14719 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014720 break;
14721 }
14722
14723#if 0
14724 /* validate data reached card RAM correctly. */
14725 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14726 u32 val;
14727 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14728 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014729 dev_err(&tp->pdev->dev,
14730 "%s: Buffer corrupted on device! "
14731 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014732 /* ret = -ENODEV here? */
14733 }
14734 p[i] = 0;
14735 }
14736#endif
14737 /* Now read it back. */
14738 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14739 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014740 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14741 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014742 break;
14743 }
14744
14745 /* Verify it. */
14746 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14747 if (p[i] == i)
14748 continue;
14749
David S. Miller59e6b432005-05-18 22:50:10 -070014750 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14751 DMA_RWCTRL_WRITE_BNDRY_16) {
14752 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014753 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14754 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14755 break;
14756 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014757 dev_err(&tp->pdev->dev,
14758 "%s: Buffer corrupted on read back! "
14759 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014760 ret = -ENODEV;
14761 goto out;
14762 }
14763 }
14764
14765 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14766 /* Success. */
14767 ret = 0;
14768 break;
14769 }
14770 }
David S. Miller59e6b432005-05-18 22:50:10 -070014771 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14772 DMA_RWCTRL_WRITE_BNDRY_16) {
14773 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014774 * now look for chipsets that are known to expose the
14775 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014776 */
Matt Carlson41434702011-03-09 16:58:22 +000014777 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014778 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14779 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a588792010-04-05 10:19:28 +000014780 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014781 /* Safe to use the calculated DMA boundary. */
14782 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a588792010-04-05 10:19:28 +000014783 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014784
David S. Miller59e6b432005-05-18 22:50:10 -070014785 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014787
14788out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014789 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014790out_nofree:
14791 return ret;
14792}
14793
Linus Torvalds1da177e2005-04-16 15:20:36 -070014794static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14795{
Joe Perches63c3a662011-04-26 08:12:10 +000014796 if (tg3_flag(tp, 57765_PLUS)) {
Matt Carlson666bc832010-01-20 16:58:03 +000014797 tp->bufmgr_config.mbuf_read_dma_low_water =
14798 DEFAULT_MB_RDMA_LOW_WATER_5705;
14799 tp->bufmgr_config.mbuf_mac_rx_low_water =
14800 DEFAULT_MB_MACRX_LOW_WATER_57765;
14801 tp->bufmgr_config.mbuf_high_water =
14802 DEFAULT_MB_HIGH_WATER_57765;
14803
14804 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14805 DEFAULT_MB_RDMA_LOW_WATER_5705;
14806 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14807 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14808 tp->bufmgr_config.mbuf_high_water_jumbo =
14809 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
Joe Perches63c3a662011-04-26 08:12:10 +000014810 } else if (tg3_flag(tp, 5705_PLUS)) {
Michael Chanfdfec1722005-07-25 12:31:48 -070014811 tp->bufmgr_config.mbuf_read_dma_low_water =
14812 DEFAULT_MB_RDMA_LOW_WATER_5705;
14813 tp->bufmgr_config.mbuf_mac_rx_low_water =
14814 DEFAULT_MB_MACRX_LOW_WATER_5705;
14815 tp->bufmgr_config.mbuf_high_water =
14816 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014817 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14818 tp->bufmgr_config.mbuf_mac_rx_low_water =
14819 DEFAULT_MB_MACRX_LOW_WATER_5906;
14820 tp->bufmgr_config.mbuf_high_water =
14821 DEFAULT_MB_HIGH_WATER_5906;
14822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014823
Michael Chanfdfec1722005-07-25 12:31:48 -070014824 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14825 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14826 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14827 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14828 tp->bufmgr_config.mbuf_high_water_jumbo =
14829 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14830 } else {
14831 tp->bufmgr_config.mbuf_read_dma_low_water =
14832 DEFAULT_MB_RDMA_LOW_WATER;
14833 tp->bufmgr_config.mbuf_mac_rx_low_water =
14834 DEFAULT_MB_MACRX_LOW_WATER;
14835 tp->bufmgr_config.mbuf_high_water =
14836 DEFAULT_MB_HIGH_WATER;
14837
14838 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14839 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14840 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14841 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14842 tp->bufmgr_config.mbuf_high_water_jumbo =
14843 DEFAULT_MB_HIGH_WATER_JUMBO;
14844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014845
14846 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14847 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14848}
14849
14850static char * __devinit tg3_phy_string(struct tg3 *tp)
14851{
Matt Carlson79eb6902010-02-17 15:17:03 +000014852 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14853 case TG3_PHY_ID_BCM5400: return "5400";
14854 case TG3_PHY_ID_BCM5401: return "5401";
14855 case TG3_PHY_ID_BCM5411: return "5411";
14856 case TG3_PHY_ID_BCM5701: return "5701";
14857 case TG3_PHY_ID_BCM5703: return "5703";
14858 case TG3_PHY_ID_BCM5704: return "5704";
14859 case TG3_PHY_ID_BCM5705: return "5705";
14860 case TG3_PHY_ID_BCM5750: return "5750";
14861 case TG3_PHY_ID_BCM5752: return "5752";
14862 case TG3_PHY_ID_BCM5714: return "5714";
14863 case TG3_PHY_ID_BCM5780: return "5780";
14864 case TG3_PHY_ID_BCM5755: return "5755";
14865 case TG3_PHY_ID_BCM5787: return "5787";
14866 case TG3_PHY_ID_BCM5784: return "5784";
14867 case TG3_PHY_ID_BCM5756: return "5722/5756";
14868 case TG3_PHY_ID_BCM5906: return "5906";
14869 case TG3_PHY_ID_BCM5761: return "5761";
14870 case TG3_PHY_ID_BCM5718C: return "5718C";
14871 case TG3_PHY_ID_BCM5718S: return "5718S";
14872 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014873 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000014874 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014875 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014876 case 0: return "serdes";
14877 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014879}
14880
Michael Chanf9804dd2005-09-27 12:13:10 -070014881static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14882{
Joe Perches63c3a662011-04-26 08:12:10 +000014883 if (tg3_flag(tp, PCI_EXPRESS)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014884 strcpy(str, "PCI Express");
14885 return str;
Joe Perches63c3a662011-04-26 08:12:10 +000014886 } else if (tg3_flag(tp, PCIX_MODE)) {
Michael Chanf9804dd2005-09-27 12:13:10 -070014887 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14888
14889 strcpy(str, "PCIX:");
14890
14891 if ((clock_ctrl == 7) ||
14892 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14893 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14894 strcat(str, "133MHz");
14895 else if (clock_ctrl == 0)
14896 strcat(str, "33MHz");
14897 else if (clock_ctrl == 2)
14898 strcat(str, "50MHz");
14899 else if (clock_ctrl == 4)
14900 strcat(str, "66MHz");
14901 else if (clock_ctrl == 6)
14902 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014903 } else {
14904 strcpy(str, "PCI:");
Joe Perches63c3a662011-04-26 08:12:10 +000014905 if (tg3_flag(tp, PCI_HIGH_SPEED))
Michael Chanf9804dd2005-09-27 12:13:10 -070014906 strcat(str, "66MHz");
14907 else
14908 strcat(str, "33MHz");
14909 }
Joe Perches63c3a662011-04-26 08:12:10 +000014910 if (tg3_flag(tp, PCI_32BIT))
Michael Chanf9804dd2005-09-27 12:13:10 -070014911 strcat(str, ":32-bit");
14912 else
14913 strcat(str, ":64-bit");
14914 return str;
14915}
14916
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014917static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014918{
14919 struct pci_dev *peer;
14920 unsigned int func, devnr = tp->pdev->devfn & ~7;
14921
14922 for (func = 0; func < 8; func++) {
14923 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14924 if (peer && peer != tp->pdev)
14925 break;
14926 pci_dev_put(peer);
14927 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014928 /* 5704 can be configured in single-port mode, set peer to
14929 * tp->pdev in that case.
14930 */
14931 if (!peer) {
14932 peer = tp->pdev;
14933 return peer;
14934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014935
14936 /*
14937 * We don't need to keep the refcount elevated; there's no way
14938 * to remove one half of this device without removing the other
14939 */
14940 pci_dev_put(peer);
14941
14942 return peer;
14943}
14944
David S. Miller15f98502005-05-18 22:49:26 -070014945static void __devinit tg3_init_coal(struct tg3 *tp)
14946{
14947 struct ethtool_coalesce *ec = &tp->coal;
14948
14949 memset(ec, 0, sizeof(*ec));
14950 ec->cmd = ETHTOOL_GCOALESCE;
14951 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14952 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14953 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14954 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14955 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14956 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14957 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14958 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14959 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14960
14961 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14962 HOSTCC_MODE_CLRTICK_TXBD)) {
14963 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14964 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14965 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14966 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14967 }
Michael Chand244c892005-07-05 14:42:33 -070014968
Joe Perches63c3a662011-04-26 08:12:10 +000014969 if (tg3_flag(tp, 5705_PLUS)) {
Michael Chand244c892005-07-05 14:42:33 -070014970 ec->rx_coalesce_usecs_irq = 0;
14971 ec->tx_coalesce_usecs_irq = 0;
14972 ec->stats_block_coalesce_usecs = 0;
14973 }
David S. Miller15f98502005-05-18 22:49:26 -070014974}
14975
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014976static const struct net_device_ops tg3_netdev_ops = {
14977 .ndo_open = tg3_open,
14978 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014979 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014980 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014981 .ndo_validate_addr = eth_validate_addr,
14982 .ndo_set_multicast_list = tg3_set_rx_mode,
14983 .ndo_set_mac_address = tg3_set_mac_addr,
14984 .ndo_do_ioctl = tg3_ioctl,
14985 .ndo_tx_timeout = tg3_tx_timeout,
14986 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000014987 .ndo_fix_features = tg3_fix_features,
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000014988 .ndo_set_features = tg3_set_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080014989#ifdef CONFIG_NET_POLL_CONTROLLER
14990 .ndo_poll_controller = tg3_poll_controller,
14991#endif
14992};
14993
Linus Torvalds1da177e2005-04-16 15:20:36 -070014994static int __devinit tg3_init_one(struct pci_dev *pdev,
14995 const struct pci_device_id *ent)
14996{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014997 struct net_device *dev;
14998 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014999 int i, err, pm_cap;
15000 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070015001 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080015002 u64 dma_mask, persist_dma_mask;
Matt Carlson0da06062011-05-19 12:12:53 +000015003 u32 features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015004
Joe Perches05dbe002010-02-17 19:44:19 +000015005 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015006
15007 err = pci_enable_device(pdev);
15008 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015009 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015010 return err;
15011 }
15012
Linus Torvalds1da177e2005-04-16 15:20:36 -070015013 err = pci_request_regions(pdev, DRV_MODULE_NAME);
15014 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000015015 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015016 goto err_out_disable_pdev;
15017 }
15018
15019 pci_set_master(pdev);
15020
15021 /* Find power-management capability. */
15022 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
15023 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000015024 dev_err(&pdev->dev,
15025 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015026 err = -EIO;
15027 goto err_out_free_res;
15028 }
15029
Matt Carlsonfe5f5782009-09-01 13:09:39 +000015030 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015031 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000015032 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015033 err = -ENOMEM;
15034 goto err_out_free_res;
15035 }
15036
Linus Torvalds1da177e2005-04-16 15:20:36 -070015037 SET_NETDEV_DEV(dev, &pdev->dev);
15038
Linus Torvalds1da177e2005-04-16 15:20:36 -070015039 tp = netdev_priv(dev);
15040 tp->pdev = pdev;
15041 tp->dev = dev;
15042 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015043 tp->rx_mode = TG3_DEF_RX_MODE;
15044 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070015045
Linus Torvalds1da177e2005-04-16 15:20:36 -070015046 if (tg3_debug > 0)
15047 tp->msg_enable = tg3_debug;
15048 else
15049 tp->msg_enable = TG3_DEF_MSG_ENABLE;
15050
15051 /* The word/byte swap controls here control register access byte
15052 * swapping. DMA data byte swapping is controlled in the GRC_MODE
15053 * setting below.
15054 */
15055 tp->misc_host_ctrl =
15056 MISC_HOST_CTRL_MASK_PCI_INT |
15057 MISC_HOST_CTRL_WORD_SWAP |
15058 MISC_HOST_CTRL_INDIR_ACCESS |
15059 MISC_HOST_CTRL_PCISTATE_RW;
15060
15061 /* The NONFRM (non-frame) byte/word swap controls take effect
15062 * on descriptor entries, anything which isn't packet data.
15063 *
15064 * The StrongARM chips on the board (one for tx, one for rx)
15065 * are running in big-endian mode.
15066 */
15067 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
15068 GRC_MODE_WSWAP_NONFRM_DATA);
15069#ifdef __BIG_ENDIAN
15070 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
15071#endif
15072 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015073 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015074 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015075
Matt Carlsond5fe4882008-11-21 17:20:32 -080015076 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015077 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015078 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015079 err = -ENOMEM;
15080 goto err_out_free_dev;
15081 }
15082
Linus Torvalds1da177e2005-04-16 15:20:36 -070015083 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15084 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015085
Linus Torvalds1da177e2005-04-16 15:20:36 -070015086 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015087 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Matt Carlson2ffcc982011-05-19 12:12:44 +000015088 dev->netdev_ops = &tg3_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015089 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015090
15091 err = tg3_get_invariants(tp);
15092 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015093 dev_err(&pdev->dev,
15094 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015095 goto err_out_iounmap;
15096 }
15097
Michael Chan4a29cc22006-03-19 13:21:12 -080015098 /* The EPB bridge inside 5714, 5715, and 5780 and any
15099 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015100 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15101 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15102 * do DMA address check in tg3_start_xmit().
15103 */
Joe Perches63c3a662011-04-26 08:12:10 +000015104 if (tg3_flag(tp, IS_5788))
Yang Hongyang284901a2009-04-06 19:01:15 -070015105 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Joe Perches63c3a662011-04-26 08:12:10 +000015106 else if (tg3_flag(tp, 40BIT_DMA_BUG)) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015107 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015108#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015109 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015110#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015111 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015112 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015113
15114 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015115 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015116 err = pci_set_dma_mask(pdev, dma_mask);
15117 if (!err) {
Matt Carlson0da06062011-05-19 12:12:53 +000015118 features |= NETIF_F_HIGHDMA;
Michael Chan72f2afb2006-03-06 19:28:35 -080015119 err = pci_set_consistent_dma_mask(pdev,
15120 persist_dma_mask);
15121 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015122 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15123 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015124 goto err_out_iounmap;
15125 }
15126 }
15127 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015128 if (err || dma_mask == DMA_BIT_MASK(32)) {
15129 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015130 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015131 dev_err(&pdev->dev,
15132 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015133 goto err_out_iounmap;
15134 }
15135 }
15136
Michael Chanfdfec1722005-07-25 12:31:48 -070015137 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015138
Matt Carlson0da06062011-05-19 12:12:53 +000015139 features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
15140
15141 /* 5700 B0 chips do not support checksumming correctly due
15142 * to hardware bugs.
15143 */
15144 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
15145 features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
15146
15147 if (tg3_flag(tp, 5755_PLUS))
15148 features |= NETIF_F_IPV6_CSUM;
15149 }
15150
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015151 /* TSO is on by default on chips that support hardware TSO.
15152 * Firmware TSO on older chips gives lower performance, so it
15153 * is off by default, but can be enabled using ethtool.
15154 */
Joe Perches63c3a662011-04-26 08:12:10 +000015155 if ((tg3_flag(tp, HW_TSO_1) ||
15156 tg3_flag(tp, HW_TSO_2) ||
15157 tg3_flag(tp, HW_TSO_3)) &&
Matt Carlson0da06062011-05-19 12:12:53 +000015158 (features & NETIF_F_IP_CSUM))
15159 features |= NETIF_F_TSO;
Joe Perches63c3a662011-04-26 08:12:10 +000015160 if (tg3_flag(tp, HW_TSO_2) || tg3_flag(tp, HW_TSO_3)) {
Matt Carlson0da06062011-05-19 12:12:53 +000015161 if (features & NETIF_F_IPV6_CSUM)
15162 features |= NETIF_F_TSO6;
Joe Perches63c3a662011-04-26 08:12:10 +000015163 if (tg3_flag(tp, HW_TSO_3) ||
Matt Carlsone849cdc2009-11-13 13:03:38 +000015164 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015165 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15166 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Joe Perches63c3a662011-04-26 08:12:10 +000015167 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015168 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson0da06062011-05-19 12:12:53 +000015169 features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015171
Matt Carlsond542fe22011-05-19 16:02:43 +000015172 dev->features |= features;
15173 dev->vlan_features |= features;
15174
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015175 /*
15176 * Add loopback capability only for a subset of devices that support
15177 * MAC-LOOPBACK. Eventually this need to be enhanced to allow INT-PHY
15178 * loopback for the remaining devices.
15179 */
15180 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5780 &&
15181 !tg3_flag(tp, CPMU_PRESENT))
15182 /* Add the loopback capability */
Matt Carlson0da06062011-05-19 12:12:53 +000015183 features |= NETIF_F_LOOPBACK;
15184
Matt Carlson0da06062011-05-19 12:12:53 +000015185 dev->hw_features |= features;
Mahesh Bandewar06c03c02011-05-08 06:51:48 +000015186
Linus Torvalds1da177e2005-04-16 15:20:36 -070015187 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
Joe Perches63c3a662011-04-26 08:12:10 +000015188 !tg3_flag(tp, TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070015189 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
Joe Perches63c3a662011-04-26 08:12:10 +000015190 tg3_flag_set(tp, MAX_RXPEND_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015191 tp->rx_pending = 63;
15192 }
15193
Linus Torvalds1da177e2005-04-16 15:20:36 -070015194 err = tg3_get_device_address(tp);
15195 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015196 dev_err(&pdev->dev,
15197 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000015198 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015199 }
15200
Joe Perches63c3a662011-04-26 08:12:10 +000015201 if (tg3_flag(tp, ENABLE_APE)) {
Matt Carlson63532392008-11-03 16:49:57 -080015202 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080015203 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015204 dev_err(&pdev->dev,
15205 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015206 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000015207 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015208 }
15209
15210 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000015211
Joe Perches63c3a662011-04-26 08:12:10 +000015212 if (tg3_flag(tp, ENABLE_ASF))
Matt Carlson7fd76442009-02-25 14:27:20 +000015213 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015214 }
15215
Matt Carlsonc88864d2007-11-12 21:07:01 -080015216 /*
15217 * Reset chip in case UNDI or EFI driver did not shutdown
15218 * DMA self test will enable WDMAC and we'll see (spurious)
15219 * pending DMA on the PCI bus at that point.
15220 */
15221 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15222 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15223 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15224 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15225 }
15226
15227 err = tg3_test_dma(tp);
15228 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015229 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015230 goto err_out_apeunmap;
15231 }
15232
Matt Carlson78f90dc2009-11-13 13:03:42 +000015233 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15234 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15235 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015236 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015237 struct tg3_napi *tnapi = &tp->napi[i];
15238
15239 tnapi->tp = tp;
15240 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15241
15242 tnapi->int_mbox = intmbx;
15243 if (i < 4)
15244 intmbx += 0x8;
15245 else
15246 intmbx += 0x4;
15247
15248 tnapi->consmbox = rcvmbx;
15249 tnapi->prodmbox = sndmbx;
15250
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015251 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015252 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015253 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015254 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015255
Joe Perches63c3a662011-04-26 08:12:10 +000015256 if (!tg3_flag(tp, SUPPORT_MSIX))
Matt Carlson78f90dc2009-11-13 13:03:42 +000015257 break;
15258
15259 /*
15260 * If we support MSIX, we'll be using RSS. If we're using
15261 * RSS, the first vector only handles link interrupts and the
15262 * remaining vectors handle rx and tx interrupts. Reuse the
15263 * mailbox values for the next iteration. The values we setup
15264 * above are still useful for the single vectored mode.
15265 */
15266 if (!i)
15267 continue;
15268
15269 rcvmbx += 0x8;
15270
15271 if (sndmbx & 0x4)
15272 sndmbx -= 0x4;
15273 else
15274 sndmbx += 0xc;
15275 }
15276
Matt Carlsonc88864d2007-11-12 21:07:01 -080015277 tg3_init_coal(tp);
15278
Michael Chanc49a1562006-12-17 17:07:29 -080015279 pci_set_drvdata(pdev, dev);
15280
Linus Torvalds1da177e2005-04-16 15:20:36 -070015281 err = register_netdev(dev);
15282 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015283 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015284 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015285 }
15286
Joe Perches05dbe002010-02-17 19:44:19 +000015287 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15288 tp->board_part_number,
15289 tp->pci_chip_rev_id,
15290 tg3_bus_string(tp, str),
15291 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015292
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015293 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015294 struct phy_device *phydev;
15295 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015296 netdev_info(dev,
15297 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015298 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015299 } else {
15300 char *ethtype;
15301
15302 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15303 ethtype = "10/100Base-TX";
15304 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15305 ethtype = "1000Base-SX";
15306 else
15307 ethtype = "10/100/1000Base-T";
15308
Matt Carlson5129c3a2010-04-05 10:19:23 +000015309 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlson47007832011-04-20 07:57:43 +000015310 "(WireSpeed[%d], EEE[%d])\n",
15311 tg3_phy_string(tp), ethtype,
15312 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0,
15313 (tp->phy_flags & TG3_PHYFLG_EEE_CAP) != 0);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015314 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015315
Joe Perches05dbe002010-02-17 19:44:19 +000015316 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015317 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015318 tg3_flag(tp, USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015319 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches63c3a662011-04-26 08:12:10 +000015320 tg3_flag(tp, ENABLE_ASF) != 0,
15321 tg3_flag(tp, TSO_CAPABLE) != 0);
Joe Perches05dbe002010-02-17 19:44:19 +000015322 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15323 tp->dma_rwctrl,
15324 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15325 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015326
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015327 pci_save_state(pdev);
15328
Linus Torvalds1da177e2005-04-16 15:20:36 -070015329 return 0;
15330
Matt Carlson0d3031d2007-10-10 18:02:43 -070015331err_out_apeunmap:
15332 if (tp->aperegs) {
15333 iounmap(tp->aperegs);
15334 tp->aperegs = NULL;
15335 }
15336
Linus Torvalds1da177e2005-04-16 15:20:36 -070015337err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015338 if (tp->regs) {
15339 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015340 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015342
15343err_out_free_dev:
15344 free_netdev(dev);
15345
15346err_out_free_res:
15347 pci_release_regions(pdev);
15348
15349err_out_disable_pdev:
15350 pci_disable_device(pdev);
15351 pci_set_drvdata(pdev, NULL);
15352 return err;
15353}
15354
15355static void __devexit tg3_remove_one(struct pci_dev *pdev)
15356{
15357 struct net_device *dev = pci_get_drvdata(pdev);
15358
15359 if (dev) {
15360 struct tg3 *tp = netdev_priv(dev);
15361
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015362 if (tp->fw)
15363 release_firmware(tp->fw);
15364
Tejun Heo23f333a2010-12-12 16:45:14 +010015365 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015366
Joe Perches63c3a662011-04-26 08:12:10 +000015367 if (!tg3_flag(tp, USE_PHYLIB)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015368 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015369 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015370 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015371
Linus Torvalds1da177e2005-04-16 15:20:36 -070015372 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015373 if (tp->aperegs) {
15374 iounmap(tp->aperegs);
15375 tp->aperegs = NULL;
15376 }
Michael Chan68929142005-08-09 20:17:14 -070015377 if (tp->regs) {
15378 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015379 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015381 free_netdev(dev);
15382 pci_release_regions(pdev);
15383 pci_disable_device(pdev);
15384 pci_set_drvdata(pdev, NULL);
15385 }
15386}
15387
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015388#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015389static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015390{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015391 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015392 struct net_device *dev = pci_get_drvdata(pdev);
15393 struct tg3 *tp = netdev_priv(dev);
15394 int err;
15395
15396 if (!netif_running(dev))
15397 return 0;
15398
Tejun Heo23f333a2010-12-12 16:45:14 +010015399 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015400 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015401 tg3_netif_stop(tp);
15402
15403 del_timer_sync(&tp->timer);
15404
David S. Millerf47c11e2005-06-24 20:18:35 -070015405 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015406 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015407 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015408
15409 netif_device_detach(dev);
15410
David S. Millerf47c11e2005-06-24 20:18:35 -070015411 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015412 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Joe Perches63c3a662011-04-26 08:12:10 +000015413 tg3_flag_clear(tp, INIT_COMPLETE);
David S. Millerf47c11e2005-06-24 20:18:35 -070015414 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015415
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015416 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015417 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015418 int err2;
15419
David S. Millerf47c11e2005-06-24 20:18:35 -070015420 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015421
Joe Perches63c3a662011-04-26 08:12:10 +000015422 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015423 err2 = tg3_restart_hw(tp, 1);
15424 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015425 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015426
15427 tp->timer.expires = jiffies + tp->timer_offset;
15428 add_timer(&tp->timer);
15429
15430 netif_device_attach(dev);
15431 tg3_netif_start(tp);
15432
Michael Chanb9ec6c12006-07-25 16:37:27 -070015433out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015434 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015435
15436 if (!err2)
15437 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015438 }
15439
15440 return err;
15441}
15442
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015443static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015444{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015445 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015446 struct net_device *dev = pci_get_drvdata(pdev);
15447 struct tg3 *tp = netdev_priv(dev);
15448 int err;
15449
15450 if (!netif_running(dev))
15451 return 0;
15452
Linus Torvalds1da177e2005-04-16 15:20:36 -070015453 netif_device_attach(dev);
15454
David S. Millerf47c11e2005-06-24 20:18:35 -070015455 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015456
Joe Perches63c3a662011-04-26 08:12:10 +000015457 tg3_flag_set(tp, INIT_COMPLETE);
Michael Chanb9ec6c12006-07-25 16:37:27 -070015458 err = tg3_restart_hw(tp, 1);
15459 if (err)
15460 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015461
15462 tp->timer.expires = jiffies + tp->timer_offset;
15463 add_timer(&tp->timer);
15464
Linus Torvalds1da177e2005-04-16 15:20:36 -070015465 tg3_netif_start(tp);
15466
Michael Chanb9ec6c12006-07-25 16:37:27 -070015467out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015468 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015469
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015470 if (!err)
15471 tg3_phy_start(tp);
15472
Michael Chanb9ec6c12006-07-25 16:37:27 -070015473 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015474}
15475
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015476static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015477#define TG3_PM_OPS (&tg3_pm_ops)
15478
15479#else
15480
15481#define TG3_PM_OPS NULL
15482
15483#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015484
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015485/**
15486 * tg3_io_error_detected - called when PCI error is detected
15487 * @pdev: Pointer to PCI device
15488 * @state: The current pci connection state
15489 *
15490 * This function is called after a PCI bus error affecting
15491 * this device has been detected.
15492 */
15493static pci_ers_result_t tg3_io_error_detected(struct pci_dev *pdev,
15494 pci_channel_state_t state)
15495{
15496 struct net_device *netdev = pci_get_drvdata(pdev);
15497 struct tg3 *tp = netdev_priv(netdev);
15498 pci_ers_result_t err = PCI_ERS_RESULT_NEED_RESET;
15499
15500 netdev_info(netdev, "PCI I/O error detected\n");
15501
15502 rtnl_lock();
15503
15504 if (!netif_running(netdev))
15505 goto done;
15506
15507 tg3_phy_stop(tp);
15508
15509 tg3_netif_stop(tp);
15510
15511 del_timer_sync(&tp->timer);
Joe Perches63c3a662011-04-26 08:12:10 +000015512 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015513
15514 /* Want to make sure that the reset task doesn't run */
15515 cancel_work_sync(&tp->reset_task);
Joe Perches63c3a662011-04-26 08:12:10 +000015516 tg3_flag_clear(tp, TX_RECOVERY_PENDING);
15517 tg3_flag_clear(tp, RESTART_TIMER);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015518
15519 netif_device_detach(netdev);
15520
15521 /* Clean up software state, even if MMIO is blocked */
15522 tg3_full_lock(tp, 0);
15523 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
15524 tg3_full_unlock(tp);
15525
15526done:
15527 if (state == pci_channel_io_perm_failure)
15528 err = PCI_ERS_RESULT_DISCONNECT;
15529 else
15530 pci_disable_device(pdev);
15531
15532 rtnl_unlock();
15533
15534 return err;
15535}
15536
15537/**
15538 * tg3_io_slot_reset - called after the pci bus has been reset.
15539 * @pdev: Pointer to PCI device
15540 *
15541 * Restart the card from scratch, as if from a cold-boot.
15542 * At this point, the card has exprienced a hard reset,
15543 * followed by fixups by BIOS, and has its config space
15544 * set up identically to what it was at cold boot.
15545 */
15546static pci_ers_result_t tg3_io_slot_reset(struct pci_dev *pdev)
15547{
15548 struct net_device *netdev = pci_get_drvdata(pdev);
15549 struct tg3 *tp = netdev_priv(netdev);
15550 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
15551 int err;
15552
15553 rtnl_lock();
15554
15555 if (pci_enable_device(pdev)) {
15556 netdev_err(netdev, "Cannot re-enable PCI device after reset.\n");
15557 goto done;
15558 }
15559
15560 pci_set_master(pdev);
15561 pci_restore_state(pdev);
15562 pci_save_state(pdev);
15563
15564 if (!netif_running(netdev)) {
15565 rc = PCI_ERS_RESULT_RECOVERED;
15566 goto done;
15567 }
15568
15569 err = tg3_power_up(tp);
15570 if (err) {
15571 netdev_err(netdev, "Failed to restore register access.\n");
15572 goto done;
15573 }
15574
15575 rc = PCI_ERS_RESULT_RECOVERED;
15576
15577done:
15578 rtnl_unlock();
15579
15580 return rc;
15581}
15582
15583/**
15584 * tg3_io_resume - called when traffic can start flowing again.
15585 * @pdev: Pointer to PCI device
15586 *
15587 * This callback is called when the error recovery driver tells
15588 * us that its OK to resume normal operation.
15589 */
15590static void tg3_io_resume(struct pci_dev *pdev)
15591{
15592 struct net_device *netdev = pci_get_drvdata(pdev);
15593 struct tg3 *tp = netdev_priv(netdev);
15594 int err;
15595
15596 rtnl_lock();
15597
15598 if (!netif_running(netdev))
15599 goto done;
15600
15601 tg3_full_lock(tp, 0);
Joe Perches63c3a662011-04-26 08:12:10 +000015602 tg3_flag_set(tp, INIT_COMPLETE);
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015603 err = tg3_restart_hw(tp, 1);
15604 tg3_full_unlock(tp);
15605 if (err) {
15606 netdev_err(netdev, "Cannot restart hardware after reset.\n");
15607 goto done;
15608 }
15609
15610 netif_device_attach(netdev);
15611
15612 tp->timer.expires = jiffies + tp->timer_offset;
15613 add_timer(&tp->timer);
15614
15615 tg3_netif_start(tp);
15616
15617 tg3_phy_start(tp);
15618
15619done:
15620 rtnl_unlock();
15621}
15622
15623static struct pci_error_handlers tg3_err_handler = {
15624 .error_detected = tg3_io_error_detected,
15625 .slot_reset = tg3_io_slot_reset,
15626 .resume = tg3_io_resume
15627};
15628
Linus Torvalds1da177e2005-04-16 15:20:36 -070015629static struct pci_driver tg3_driver = {
15630 .name = DRV_MODULE_NAME,
15631 .id_table = tg3_pci_tbl,
15632 .probe = tg3_init_one,
15633 .remove = __devexit_p(tg3_remove_one),
Matt Carlsonb45aa2f2011-04-25 12:42:48 +000015634 .err_handler = &tg3_err_handler,
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015635 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015636};
15637
15638static int __init tg3_init(void)
15639{
Jeff Garzik29917622006-08-19 17:48:59 -040015640 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015641}
15642
15643static void __exit tg3_cleanup(void)
15644{
15645 pci_unregister_driver(&tg3_driver);
15646}
15647
15648module_init(tg3_init);
15649module_exit(tg3_cleanup);