blob: 58787ea8b7a4062d4707e205051e098a65e419b8 [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>
29#include <linux/ioport.h>
30#include <linux/pci.h>
31#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/ethtool.h>
Matt Carlson3110f5f52010-12-06 08:28:50 +000035#include <linux/mdio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/mii.h>
Matt Carlson158d7ab2008-05-29 01:37:54 -070037#include <linux/phy.h>
Matt Carlsona9daf362008-05-25 23:49:44 -070038#include <linux/brcmphy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/if_vlan.h>
40#include <linux/ip.h>
41#include <linux/tcp.h>
42#include <linux/workqueue.h>
Michael Chan61487482005-09-05 17:53:19 -070043#include <linux/prefetch.h>
Tobias Klauserf9a5f7d2005-10-29 15:09:26 +020044#include <linux/dma-mapping.h>
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080045#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <net/checksum.h>
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -030048#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include <asm/system.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000051#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/byteorder.h>
Javier Martinez Canillas27fd9de2011-03-26 16:42:31 +000053#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David S. Miller49b6e95f2007-03-29 01:38:42 -070055#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/idprom.h>
David S. Miller49b6e95f2007-03-29 01:38:42 -070057#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
Matt Carlson63532392008-11-03 16:49:57 -080060#define BAR_0 0
61#define BAR_2 2
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include "tg3.h"
64
65#define DRV_MODULE_NAME "tg3"
Matt Carlson6867c842010-07-11 09:31:44 +000066#define TG3_MAJ_NUM 3
Matt Carlsonb86fb2c2011-01-25 15:58:57 +000067#define TG3_MIN_NUM 117
Matt Carlson6867c842010-07-11 09:31:44 +000068#define DRV_MODULE_VERSION \
69 __stringify(TG3_MAJ_NUM) "." __stringify(TG3_MIN_NUM)
Matt Carlsonb86fb2c2011-01-25 15:58:57 +000070#define DRV_MODULE_RELDATE "January 25, 2011"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#define TG3_DEF_MAC_MODE 0
73#define TG3_DEF_RX_MODE 0
74#define TG3_DEF_TX_MODE 0
75#define TG3_DEF_MSG_ENABLE \
76 (NETIF_MSG_DRV | \
77 NETIF_MSG_PROBE | \
78 NETIF_MSG_LINK | \
79 NETIF_MSG_TIMER | \
80 NETIF_MSG_IFDOWN | \
81 NETIF_MSG_IFUP | \
82 NETIF_MSG_RX_ERR | \
83 NETIF_MSG_TX_ERR)
84
85/* length of time before we decide the hardware is borked,
86 * and dev->tx_timeout() should be called to fix the problem
87 */
88#define TG3_TX_TIMEOUT (5 * HZ)
89
90/* hardware minimum and maximum for a single frame's data payload */
91#define TG3_MIN_MTU 60
92#define TG3_MAX_MTU(tp) \
Matt Carlson8f666b02009-08-28 13:58:24 +000093 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ? 9000 : 1500)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/* These numbers seem to be hard coded in the NIC firmware somehow.
96 * You can't change the ring sizes, but you can change where you place
97 * them in the NIC onboard memory.
98 */
Matt Carlson7cb32cf2010-09-30 10:34:36 +000099#define TG3_RX_STD_RING_SIZE(tp) \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000100 ((tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP) ? \
101 TG3_RX_STD_MAX_SIZE_5717 : TG3_RX_STD_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#define TG3_DEF_RX_RING_PENDING 200
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000103#define TG3_RX_JMB_RING_SIZE(tp) \
Matt Carlsonde9f5232011-04-05 14:22:43 +0000104 ((tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP) ? \
105 TG3_RX_JMB_MAX_SIZE_5717 : TG3_RX_JMB_MAX_SIZE_5700)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#define TG3_DEF_RX_JUMBO_RING_PENDING 100
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000107#define TG3_RSS_INDIR_TBL_SIZE 128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/* Do not place this n-ring entries value into the tp struct itself,
110 * we really want to expose these constants to GCC so that modulo et
111 * al. operations are done with shifts and masks instead of with
112 * hw multiply/modulo instructions. Another solution would be to
113 * replace things like '% foo' with '& (foo - 1)'.
114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116#define TG3_TX_RING_SIZE 512
117#define TG3_DEF_TX_RING_PENDING (TG3_TX_RING_SIZE - 1)
118
Matt Carlson2c49a442010-09-30 10:34:35 +0000119#define TG3_RX_STD_RING_BYTES(tp) \
120 (sizeof(struct tg3_rx_buffer_desc) * TG3_RX_STD_RING_SIZE(tp))
121#define TG3_RX_JMB_RING_BYTES(tp) \
122 (sizeof(struct tg3_ext_rx_buffer_desc) * TG3_RX_JMB_RING_SIZE(tp))
123#define TG3_RX_RCB_RING_BYTES(tp) \
Matt Carlson7cb32cf2010-09-30 10:34:36 +0000124 (sizeof(struct tg3_rx_buffer_desc) * (tp->rx_ret_ring_mask + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#define TG3_TX_RING_BYTES (sizeof(struct tg3_tx_buffer_desc) * \
126 TG3_TX_RING_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#define NEXT_TX(N) (((N) + 1) & (TG3_TX_RING_SIZE - 1))
128
Matt Carlson287be122009-08-28 13:58:46 +0000129#define TG3_DMA_BYTE_ENAB 64
130
131#define TG3_RX_STD_DMA_SZ 1536
132#define TG3_RX_JMB_DMA_SZ 9046
133
134#define TG3_RX_DMA_TO_MAP_SZ(x) ((x) + TG3_DMA_BYTE_ENAB)
135
136#define TG3_RX_STD_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_STD_DMA_SZ)
137#define TG3_RX_JMB_MAP_SZ TG3_RX_DMA_TO_MAP_SZ(TG3_RX_JMB_DMA_SZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Matt Carlson2c49a442010-09-30 10:34:35 +0000139#define TG3_RX_STD_BUFF_RING_SIZE(tp) \
140 (sizeof(struct ring_info) * TG3_RX_STD_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000141
Matt Carlson2c49a442010-09-30 10:34:35 +0000142#define TG3_RX_JMB_BUFF_RING_SIZE(tp) \
143 (sizeof(struct ring_info) * TG3_RX_JMB_RING_SIZE(tp))
Matt Carlson2b2cdb62009-11-13 13:03:48 +0000144
Matt Carlsond2757fc2010-04-12 06:58:27 +0000145/* Due to a hardware bug, the 5701 can only DMA to memory addresses
146 * that are at least dword aligned when used in PCIX mode. The driver
147 * works around this bug by double copying the packet. This workaround
148 * is built into the normal double copy length check for efficiency.
149 *
150 * However, the double copy is only necessary on those architectures
151 * where unaligned memory accesses are inefficient. For those architectures
152 * where unaligned memory accesses incur little penalty, we can reintegrate
153 * the 5701 in the normal rx path. Doing so saves a device structure
154 * dereference by hardcoding the double copy threshold in place.
155 */
156#define TG3_RX_COPY_THRESHOLD 256
157#if NET_IP_ALIGN == 0 || defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
158 #define TG3_RX_COPY_THRESH(tp) TG3_RX_COPY_THRESHOLD
159#else
160 #define TG3_RX_COPY_THRESH(tp) ((tp)->rx_copy_thresh)
161#endif
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* minimum number of free TX descriptors required to wake up TX process */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000164#define TG3_TX_WAKEUP_THRESH(tnapi) ((tnapi)->tx_pending / 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Matt Carlsonad829262008-11-21 17:16:16 -0800166#define TG3_RAW_IP_ALIGN 2
167
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000168#define TG3_FW_UPDATE_TIMEOUT_SEC 5
169
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800170#define FIRMWARE_TG3 "tigon/tg3.bin"
171#define FIRMWARE_TG3TSO "tigon/tg3_tso.bin"
172#define FIRMWARE_TG3TSO5 "tigon/tg3_tso5.bin"
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static char version[] __devinitdata =
Joe Perches05dbe002010-02-17 19:44:19 +0000175 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177MODULE_AUTHOR("David S. Miller (davem@redhat.com) and Jeff Garzik (jgarzik@pobox.com)");
178MODULE_DESCRIPTION("Broadcom Tigon3 ethernet driver");
179MODULE_LICENSE("GPL");
180MODULE_VERSION(DRV_MODULE_VERSION);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -0800181MODULE_FIRMWARE(FIRMWARE_TG3);
182MODULE_FIRMWARE(FIRMWARE_TG3TSO);
183MODULE_FIRMWARE(FIRMWARE_TG3TSO5);
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static int tg3_debug = -1; /* -1 == use TG3_DEF_MSG_ENABLE as value */
186module_param(tg3_debug, int, 0);
187MODULE_PARM_DESC(tg3_debug, "Tigon3 bitmapped debugging message enable value");
188
Alexey Dobriyana3aa1882010-01-07 11:58:11 +0000189static DEFINE_PCI_DEVICE_TABLE(tg3_pci_tbl) = {
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700190 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5700)},
191 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5701)},
192 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702)},
193 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703)},
194 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704)},
195 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702FE)},
196 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705)},
197 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705_2)},
198 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M)},
199 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705M_2)},
200 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702X)},
201 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703X)},
202 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S)},
203 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5702A3)},
204 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5703A3)},
205 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5782)},
206 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5788)},
207 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5789)},
208 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901)},
209 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5901_2)},
210 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5704S_2)},
211 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5705F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700212 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5721)},
Michael Chan126a3362006-09-27 16:03:07 -0700213 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5722)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700214 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700215 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751M)},
216 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5751F)},
217 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752)},
218 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5752M)},
219 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753)},
220 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753M)},
221 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5753F)},
222 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754)},
223 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5754M)},
224 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755)},
225 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5755M)},
Michael Chan126a3362006-09-27 16:03:07 -0700226 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5756)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700227 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5786)},
228 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787)},
229 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787M)},
Michael Chan676917d2006-12-07 00:20:22 -0800230 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5787F)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700231 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714)},
232 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5714S)},
233 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715)},
234 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5715S)},
235 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780)},
236 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5780S)},
237 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5781)},
Michael Chanb5d37722006-09-27 16:06:21 -0700238 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906)},
239 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5906M)},
Matt Carlsond30cdd22007-10-07 23:28:35 -0700240 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5784)},
241 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5764)},
Matt Carlson6c7af272007-10-21 16:12:02 -0700242 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5723)},
Matt Carlson9936bcf2007-10-10 18:03:07 -0700243 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761)},
244 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, PCI_DEVICE_ID_TIGON3_5761E)},
Matt Carlsonc88e6682008-11-03 16:49:18 -0800245 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761S)},
246 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5761SE)},
Matt Carlson2befdce2009-08-28 12:28:45 +0000247 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_G)},
248 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5785_F)},
Matt Carlson321d32a2008-11-21 17:22:19 -0800249 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57780)},
250 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57760)},
251 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57790)},
Matt Carlson5e7ccf22009-08-25 10:08:42 +0000252 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57788)},
Matt Carlson5001e2f2009-11-13 13:03:51 +0000253 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5717)},
254 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5718)},
Matt Carlsonb0f75222010-01-20 16:58:11 +0000255 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57781)},
256 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57785)},
257 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57761)},
258 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57765)},
259 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57791)},
260 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_57795)},
Matt Carlson302b5002010-06-05 17:24:38 +0000261 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5719)},
Matt Carlsonba1f3c72011-04-05 14:22:50 +0000262 {PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, TG3PCI_DEVICE_TIGON3_5720)},
Henrik Kretzschmar13185212006-08-22 00:28:33 -0700263 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9DXX)},
264 {PCI_DEVICE(PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_9MXX)},
265 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1000)},
266 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1001)},
267 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC1003)},
268 {PCI_DEVICE(PCI_VENDOR_ID_ALTIMA, PCI_DEVICE_ID_ALTIMA_AC9100)},
269 {PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_TIGON3)},
270 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
272
273MODULE_DEVICE_TABLE(pci, tg3_pci_tbl);
274
Andreas Mohr50da8592006-08-14 23:54:30 -0700275static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000277} ethtool_stats_keys[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 { "rx_octets" },
279 { "rx_fragments" },
280 { "rx_ucast_packets" },
281 { "rx_mcast_packets" },
282 { "rx_bcast_packets" },
283 { "rx_fcs_errors" },
284 { "rx_align_errors" },
285 { "rx_xon_pause_rcvd" },
286 { "rx_xoff_pause_rcvd" },
287 { "rx_mac_ctrl_rcvd" },
288 { "rx_xoff_entered" },
289 { "rx_frame_too_long_errors" },
290 { "rx_jabbers" },
291 { "rx_undersize_packets" },
292 { "rx_in_length_errors" },
293 { "rx_out_length_errors" },
294 { "rx_64_or_less_octet_packets" },
295 { "rx_65_to_127_octet_packets" },
296 { "rx_128_to_255_octet_packets" },
297 { "rx_256_to_511_octet_packets" },
298 { "rx_512_to_1023_octet_packets" },
299 { "rx_1024_to_1522_octet_packets" },
300 { "rx_1523_to_2047_octet_packets" },
301 { "rx_2048_to_4095_octet_packets" },
302 { "rx_4096_to_8191_octet_packets" },
303 { "rx_8192_to_9022_octet_packets" },
304
305 { "tx_octets" },
306 { "tx_collisions" },
307
308 { "tx_xon_sent" },
309 { "tx_xoff_sent" },
310 { "tx_flow_control" },
311 { "tx_mac_errors" },
312 { "tx_single_collisions" },
313 { "tx_mult_collisions" },
314 { "tx_deferred" },
315 { "tx_excessive_collisions" },
316 { "tx_late_collisions" },
317 { "tx_collide_2times" },
318 { "tx_collide_3times" },
319 { "tx_collide_4times" },
320 { "tx_collide_5times" },
321 { "tx_collide_6times" },
322 { "tx_collide_7times" },
323 { "tx_collide_8times" },
324 { "tx_collide_9times" },
325 { "tx_collide_10times" },
326 { "tx_collide_11times" },
327 { "tx_collide_12times" },
328 { "tx_collide_13times" },
329 { "tx_collide_14times" },
330 { "tx_collide_15times" },
331 { "tx_ucast_packets" },
332 { "tx_mcast_packets" },
333 { "tx_bcast_packets" },
334 { "tx_carrier_sense_errors" },
335 { "tx_discards" },
336 { "tx_errors" },
337
338 { "dma_writeq_full" },
339 { "dma_write_prioq_full" },
340 { "rxbds_empty" },
341 { "rx_discards" },
Matt Carlson4d958472011-04-20 07:57:35 +0000342 { "mbuf_lwm_thresh_hit" },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 { "rx_errors" },
344 { "rx_threshold_hit" },
345
346 { "dma_readq_full" },
347 { "dma_read_prioq_full" },
348 { "tx_comp_queue_full" },
349
350 { "ring_set_send_prod_index" },
351 { "ring_status_update" },
352 { "nic_irqs" },
353 { "nic_avoided_irqs" },
354 { "nic_tx_threshold_hit" }
355};
356
Matt Carlson48fa55a2011-04-13 11:05:06 +0000357#define TG3_NUM_STATS ARRAY_SIZE(ethtool_stats_keys)
358
359
Andreas Mohr50da8592006-08-14 23:54:30 -0700360static const struct {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700361 const char string[ETH_GSTRING_LEN];
Matt Carlson48fa55a2011-04-13 11:05:06 +0000362} ethtool_test_keys[] = {
Michael Chan4cafd3f2005-05-29 14:56:34 -0700363 { "nvram test (online) " },
364 { "link test (online) " },
365 { "register test (offline)" },
366 { "memory test (offline)" },
367 { "loopback test (offline)" },
368 { "interrupt test (offline)" },
369};
370
Matt Carlson48fa55a2011-04-13 11:05:06 +0000371#define TG3_NUM_TEST ARRAY_SIZE(ethtool_test_keys)
372
373
Michael Chanb401e9e2005-12-19 16:27:04 -0800374static void tg3_write32(struct tg3 *tp, u32 off, u32 val)
375{
376 writel(val, tp->regs + off);
377}
378
379static u32 tg3_read32(struct tg3 *tp, u32 off)
380{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000381 return readl(tp->regs + off);
Michael Chanb401e9e2005-12-19 16:27:04 -0800382}
383
Matt Carlson0d3031d2007-10-10 18:02:43 -0700384static void tg3_ape_write32(struct tg3 *tp, u32 off, u32 val)
385{
386 writel(val, tp->aperegs + off);
387}
388
389static u32 tg3_ape_read32(struct tg3 *tp, u32 off)
390{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000391 return readl(tp->aperegs + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394static void tg3_write_indirect_reg32(struct tg3 *tp, u32 off, u32 val)
395{
Michael Chan68929142005-08-09 20:17:14 -0700396 unsigned long flags;
397
398 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700399 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
400 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
Michael Chan68929142005-08-09 20:17:14 -0700401 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Michael Chan1ee582d2005-08-09 20:16:46 -0700402}
403
404static void tg3_write_flush_reg32(struct tg3 *tp, u32 off, u32 val)
405{
406 writel(val, tp->regs + off);
407 readl(tp->regs + off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Michael Chan68929142005-08-09 20:17:14 -0700410static u32 tg3_read_indirect_reg32(struct tg3 *tp, u32 off)
411{
412 unsigned long flags;
413 u32 val;
414
415 spin_lock_irqsave(&tp->indirect_lock, flags);
416 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off);
417 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
418 spin_unlock_irqrestore(&tp->indirect_lock, flags);
419 return val;
420}
421
422static void tg3_write_indirect_mbox(struct tg3 *tp, u32 off, u32 val)
423{
424 unsigned long flags;
425
426 if (off == (MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW)) {
427 pci_write_config_dword(tp->pdev, TG3PCI_RCV_RET_RING_CON_IDX +
428 TG3_64BIT_REG_LOW, val);
429 return;
430 }
Matt Carlson66711e62009-11-13 13:03:49 +0000431 if (off == TG3_RX_STD_PROD_IDX_REG) {
Michael Chan68929142005-08-09 20:17:14 -0700432 pci_write_config_dword(tp->pdev, TG3PCI_STD_RING_PROD_IDX +
433 TG3_64BIT_REG_LOW, val);
434 return;
435 }
436
437 spin_lock_irqsave(&tp->indirect_lock, flags);
438 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
439 pci_write_config_dword(tp->pdev, TG3PCI_REG_DATA, val);
440 spin_unlock_irqrestore(&tp->indirect_lock, flags);
441
442 /* In indirect mode when disabling interrupts, we also need
443 * to clear the interrupt bit in the GRC local ctrl register.
444 */
445 if ((off == (MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW)) &&
446 (val == 0x1)) {
447 pci_write_config_dword(tp->pdev, TG3PCI_MISC_LOCAL_CTRL,
448 tp->grc_local_ctrl|GRC_LCLCTRL_CLEARINT);
449 }
450}
451
452static u32 tg3_read_indirect_mbox(struct tg3 *tp, u32 off)
453{
454 unsigned long flags;
455 u32 val;
456
457 spin_lock_irqsave(&tp->indirect_lock, flags);
458 pci_write_config_dword(tp->pdev, TG3PCI_REG_BASE_ADDR, off + 0x5600);
459 pci_read_config_dword(tp->pdev, TG3PCI_REG_DATA, &val);
460 spin_unlock_irqrestore(&tp->indirect_lock, flags);
461 return val;
462}
463
Michael Chanb401e9e2005-12-19 16:27:04 -0800464/* usec_wait specifies the wait time in usec when writing to certain registers
465 * where it is unsafe to read back the register without some delay.
466 * GRC_LOCAL_CTRL is one example if the GPIOs are toggled to switch power.
467 * TG3PCI_CLOCK_CTRL is another example if the clock frequencies are changed.
468 */
469static void _tw32_flush(struct tg3 *tp, u32 off, u32 val, u32 usec_wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Michael Chanb401e9e2005-12-19 16:27:04 -0800471 if ((tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) ||
472 (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
473 /* Non-posted methods */
474 tp->write32(tp, off, val);
475 else {
476 /* Posted method */
477 tg3_write32(tp, off, val);
478 if (usec_wait)
479 udelay(usec_wait);
480 tp->read32(tp, off);
481 }
482 /* Wait again after the read for the posted method to guarantee that
483 * the wait time is met.
484 */
485 if (usec_wait)
486 udelay(usec_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Michael Chan09ee9292005-08-09 20:17:00 -0700489static inline void tw32_mailbox_flush(struct tg3 *tp, u32 off, u32 val)
490{
491 tp->write32_mbox(tp, off, val);
Michael Chan68929142005-08-09 20:17:14 -0700492 if (!(tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) &&
493 !(tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND))
494 tp->read32_mbox(tp, off);
Michael Chan09ee9292005-08-09 20:17:00 -0700495}
496
Michael Chan20094932005-08-09 20:16:32 -0700497static void tg3_write32_tx_mbox(struct tg3 *tp, u32 off, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 void __iomem *mbox = tp->regs + off;
500 writel(val, mbox);
501 if (tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG)
502 writel(val, mbox);
503 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
504 readl(mbox);
505}
506
Michael Chanb5d37722006-09-27 16:06:21 -0700507static u32 tg3_read32_mbox_5906(struct tg3 *tp, u32 off)
508{
Matt Carlsonde6f31e2010-04-12 06:58:30 +0000509 return readl(tp->regs + off + GRCMBOX_BASE);
Michael Chanb5d37722006-09-27 16:06:21 -0700510}
511
512static void tg3_write32_mbox_5906(struct tg3 *tp, u32 off, u32 val)
513{
514 writel(val, tp->regs + off + GRCMBOX_BASE);
515}
516
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000517#define tw32_mailbox(reg, val) tp->write32_mbox(tp, reg, val)
Michael Chan09ee9292005-08-09 20:17:00 -0700518#define tw32_mailbox_f(reg, val) tw32_mailbox_flush(tp, (reg), (val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000519#define tw32_rx_mbox(reg, val) tp->write32_rx_mbox(tp, reg, val)
520#define tw32_tx_mbox(reg, val) tp->write32_tx_mbox(tp, reg, val)
521#define tr32_mailbox(reg) tp->read32_mbox(tp, reg)
Michael Chan20094932005-08-09 20:16:32 -0700522
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000523#define tw32(reg, val) tp->write32(tp, reg, val)
524#define tw32_f(reg, val) _tw32_flush(tp, (reg), (val), 0)
525#define tw32_wait_f(reg, val, us) _tw32_flush(tp, (reg), (val), (us))
526#define tr32(reg) tp->read32(tp, reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528static void tg3_write_mem(struct tg3 *tp, u32 off, u32 val)
529{
Michael Chan68929142005-08-09 20:17:14 -0700530 unsigned long flags;
531
Michael Chanb5d37722006-09-27 16:06:21 -0700532 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
533 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC))
534 return;
535
Michael Chan68929142005-08-09 20:17:14 -0700536 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700537 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
538 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
539 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Michael Chanbbadf502006-04-06 21:46:34 -0700541 /* Always leave this as zero. */
542 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
543 } else {
544 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
545 tw32_f(TG3PCI_MEM_WIN_DATA, val);
546
547 /* Always leave this as zero. */
548 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
549 }
Michael Chan68929142005-08-09 20:17:14 -0700550 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553static void tg3_read_mem(struct tg3 *tp, u32 off, u32 *val)
554{
Michael Chan68929142005-08-09 20:17:14 -0700555 unsigned long flags;
556
Michael Chanb5d37722006-09-27 16:06:21 -0700557 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) &&
558 (off >= NIC_SRAM_STATS_BLK) && (off < NIC_SRAM_TX_BUFFER_DESC)) {
559 *val = 0;
560 return;
561 }
562
Michael Chan68929142005-08-09 20:17:14 -0700563 spin_lock_irqsave(&tp->indirect_lock, flags);
Michael Chanbbadf502006-04-06 21:46:34 -0700564 if (tp->tg3_flags & TG3_FLAG_SRAM_USE_CONFIG) {
565 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, off);
566 pci_read_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Michael Chanbbadf502006-04-06 21:46:34 -0700568 /* Always leave this as zero. */
569 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
570 } else {
571 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, off);
572 *val = tr32(TG3PCI_MEM_WIN_DATA);
573
574 /* Always leave this as zero. */
575 tw32_f(TG3PCI_MEM_WIN_BASE_ADDR, 0);
576 }
Michael Chan68929142005-08-09 20:17:14 -0700577 spin_unlock_irqrestore(&tp->indirect_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Matt Carlson0d3031d2007-10-10 18:02:43 -0700580static void tg3_ape_lock_init(struct tg3 *tp)
581{
582 int i;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000583 u32 regbase;
584
585 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
586 regbase = TG3_APE_LOCK_GRANT;
587 else
588 regbase = TG3_APE_PER_LOCK_GRANT;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700589
590 /* Make sure the driver hasn't any stale locks. */
591 for (i = 0; i < 8; i++)
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000592 tg3_ape_write32(tp, regbase + 4 * i, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700593}
594
595static int tg3_ape_lock(struct tg3 *tp, int locknum)
596{
597 int i, off;
598 int ret = 0;
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000599 u32 status, req, gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700600
601 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
602 return 0;
603
604 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000605 case TG3_APE_LOCK_GRC:
606 case TG3_APE_LOCK_MEM:
607 break;
608 default:
609 return -EINVAL;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700610 }
611
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000612 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
613 req = TG3_APE_LOCK_REQ;
614 gnt = TG3_APE_LOCK_GRANT;
615 } else {
616 req = TG3_APE_PER_LOCK_REQ;
617 gnt = TG3_APE_PER_LOCK_GRANT;
618 }
619
Matt Carlson0d3031d2007-10-10 18:02:43 -0700620 off = 4 * locknum;
621
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000622 tg3_ape_write32(tp, req + off, APE_LOCK_REQ_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700623
624 /* Wait for up to 1 millisecond to acquire lock. */
625 for (i = 0; i < 100; i++) {
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000626 status = tg3_ape_read32(tp, gnt + off);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700627 if (status == APE_LOCK_GRANT_DRIVER)
628 break;
629 udelay(10);
630 }
631
632 if (status != APE_LOCK_GRANT_DRIVER) {
633 /* Revoke the lock request. */
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000634 tg3_ape_write32(tp, gnt + off,
Matt Carlson0d3031d2007-10-10 18:02:43 -0700635 APE_LOCK_GRANT_DRIVER);
636
637 ret = -EBUSY;
638 }
639
640 return ret;
641}
642
643static void tg3_ape_unlock(struct tg3 *tp, int locknum)
644{
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000645 u32 gnt;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700646
647 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
648 return;
649
650 switch (locknum) {
Matt Carlson33f401a2010-04-05 10:19:27 +0000651 case TG3_APE_LOCK_GRC:
652 case TG3_APE_LOCK_MEM:
653 break;
654 default:
655 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -0700656 }
657
Matt Carlsonf92d9dc2010-06-05 17:24:30 +0000658 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
659 gnt = TG3_APE_LOCK_GRANT;
660 else
661 gnt = TG3_APE_PER_LOCK_GRANT;
662
663 tg3_ape_write32(tp, gnt + 4 * locknum, APE_LOCK_GRANT_DRIVER);
Matt Carlson0d3031d2007-10-10 18:02:43 -0700664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666static void tg3_disable_ints(struct tg3 *tp)
667{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000668 int i;
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 tw32(TG3PCI_MISC_HOST_CTRL,
671 (tp->misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000672 for (i = 0; i < tp->irq_max; i++)
673 tw32_mailbox_f(tp->napi[i].int_mbox, 0x00000001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676static void tg3_enable_ints(struct tg3 *tp)
677{
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000678 int i;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000679
Michael Chanbbe832c2005-06-24 20:20:04 -0700680 tp->irq_sync = 0;
681 wmb();
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 tw32(TG3PCI_MISC_HOST_CTRL,
684 (tp->misc_host_ctrl & ~MISC_HOST_CTRL_MASK_PCI_INT));
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000685
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000686 tp->coal_now = tp->coalesce_mode | HOSTCC_MODE_ENABLE;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000687 for (i = 0; i < tp->irq_cnt; i++) {
688 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsonc6cdf432010-04-05 10:19:26 +0000689
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000690 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
691 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
692 tw32_mailbox_f(tnapi->int_mbox, tnapi->last_tag << 24);
693
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000694 tp->coal_now |= tnapi->coal_now;
Matt Carlson89aeb3b2009-09-01 13:08:58 +0000695 }
Matt Carlsonf19af9c2009-09-01 12:47:49 +0000696
697 /* Force an initial interrupt */
698 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
699 (tp->napi[0].hw_status->status & SD_STATUS_UPDATED))
700 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
701 else
Matt Carlsonf89f38b2010-02-12 14:47:07 +0000702 tw32(HOSTCC_MODE, tp->coal_now);
703
704 tp->coal_now &= ~(tp->napi[0].coal_now | tp->napi[1].coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Matt Carlson17375d22009-08-28 14:02:18 +0000707static inline unsigned int tg3_has_work(struct tg3_napi *tnapi)
Michael Chan04237dd2005-04-25 15:17:17 -0700708{
Matt Carlson17375d22009-08-28 14:02:18 +0000709 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +0000710 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan04237dd2005-04-25 15:17:17 -0700711 unsigned int work_exists = 0;
712
713 /* check for phy events */
714 if (!(tp->tg3_flags &
715 (TG3_FLAG_USE_LINKCHG_REG |
716 TG3_FLAG_POLL_SERDES))) {
717 if (sblk->status & SD_STATUS_LINK_CHG)
718 work_exists = 1;
719 }
720 /* check for RX/TX work to do */
Matt Carlsonf3f3f272009-08-28 14:03:21 +0000721 if (sblk->idx[0].tx_consumer != tnapi->tx_cons ||
Matt Carlson8d9d7cf2009-09-01 13:19:05 +0000722 *(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Michael Chan04237dd2005-04-25 15:17:17 -0700723 work_exists = 1;
724
725 return work_exists;
726}
727
Matt Carlson17375d22009-08-28 14:02:18 +0000728/* tg3_int_reenable
Michael Chan04237dd2005-04-25 15:17:17 -0700729 * similar to tg3_enable_ints, but it accurately determines whether there
730 * is new work pending and can return without flushing the PIO write
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400731 * which reenables interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
Matt Carlson17375d22009-08-28 14:02:18 +0000733static void tg3_int_reenable(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Matt Carlson17375d22009-08-28 14:02:18 +0000735 struct tg3 *tp = tnapi->tp;
736
Matt Carlson898a56f2009-08-28 14:02:40 +0000737 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 mmiowb();
739
David S. Millerfac9b832005-05-18 22:46:34 -0700740 /* When doing tagged status, this work check is unnecessary.
741 * The last_tag we write above tells the chip which piece of
742 * work we've completed.
743 */
744 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) &&
Matt Carlson17375d22009-08-28 14:02:18 +0000745 tg3_has_work(tnapi))
Michael Chan04237dd2005-04-25 15:17:17 -0700746 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +0000747 HOSTCC_MODE_ENABLE | tnapi->coal_now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750static void tg3_switch_clocks(struct tg3 *tp)
751{
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000752 u32 clock_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 u32 orig_clock_ctrl;
754
Matt Carlson795d01c2007-10-07 23:28:17 -0700755 if ((tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
756 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan4cf78e42005-07-25 12:29:19 -0700757 return;
758
Matt Carlsonf6eb9b12009-09-01 13:19:53 +0000759 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL);
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 orig_clock_ctrl = clock_ctrl;
762 clock_ctrl &= (CLOCK_CTRL_FORCE_CLKRUN |
763 CLOCK_CTRL_CLKRUN_OENABLE |
764 0x1f);
765 tp->pci_clock_ctrl = clock_ctrl;
766
767 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
768 if (orig_clock_ctrl & CLOCK_CTRL_625_CORE) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800769 tw32_wait_f(TG3PCI_CLOCK_CTRL,
770 clock_ctrl | CLOCK_CTRL_625_CORE, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772 } else if ((orig_clock_ctrl & CLOCK_CTRL_44MHZ_CORE) != 0) {
Michael Chanb401e9e2005-12-19 16:27:04 -0800773 tw32_wait_f(TG3PCI_CLOCK_CTRL,
774 clock_ctrl |
775 (CLOCK_CTRL_44MHZ_CORE | CLOCK_CTRL_ALTCLK),
776 40);
777 tw32_wait_f(TG3PCI_CLOCK_CTRL,
778 clock_ctrl | (CLOCK_CTRL_ALTCLK),
779 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
Michael Chanb401e9e2005-12-19 16:27:04 -0800781 tw32_wait_f(TG3PCI_CLOCK_CTRL, clock_ctrl, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784#define PHY_BUSY_LOOPS 5000
785
786static int tg3_readphy(struct tg3 *tp, int reg, u32 *val)
787{
788 u32 frame_val;
789 unsigned int loops;
790 int ret;
791
792 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
793 tw32_f(MAC_MI_MODE,
794 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
795 udelay(80);
796 }
797
798 *val = 0x0;
799
Matt Carlson882e9792009-09-01 13:21:36 +0000800 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 MI_COM_PHY_ADDR_MASK);
802 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
803 MI_COM_REG_ADDR_MASK);
804 frame_val |= (MI_COM_CMD_READ | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 tw32_f(MAC_MI_COM, frame_val);
807
808 loops = PHY_BUSY_LOOPS;
809 while (loops != 0) {
810 udelay(10);
811 frame_val = tr32(MAC_MI_COM);
812
813 if ((frame_val & MI_COM_BUSY) == 0) {
814 udelay(5);
815 frame_val = tr32(MAC_MI_COM);
816 break;
817 }
818 loops -= 1;
819 }
820
821 ret = -EBUSY;
822 if (loops != 0) {
823 *val = frame_val & MI_COM_DATA_MASK;
824 ret = 0;
825 }
826
827 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
828 tw32_f(MAC_MI_MODE, tp->mi_mode);
829 udelay(80);
830 }
831
832 return ret;
833}
834
835static int tg3_writephy(struct tg3 *tp, int reg, u32 val)
836{
837 u32 frame_val;
838 unsigned int loops;
839 int ret;
840
Matt Carlsonf07e9af2010-08-02 11:26:07 +0000841 if ((tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Michael Chanb5d37722006-09-27 16:06:21 -0700842 (reg == MII_TG3_CTRL || reg == MII_TG3_AUX_CTRL))
843 return 0;
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
846 tw32_f(MAC_MI_MODE,
847 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
848 udelay(80);
849 }
850
Matt Carlson882e9792009-09-01 13:21:36 +0000851 frame_val = ((tp->phy_addr << MI_COM_PHY_ADDR_SHIFT) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 MI_COM_PHY_ADDR_MASK);
853 frame_val |= ((reg << MI_COM_REG_ADDR_SHIFT) &
854 MI_COM_REG_ADDR_MASK);
855 frame_val |= (val & MI_COM_DATA_MASK);
856 frame_val |= (MI_COM_CMD_WRITE | MI_COM_START);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 tw32_f(MAC_MI_COM, frame_val);
859
860 loops = PHY_BUSY_LOOPS;
861 while (loops != 0) {
862 udelay(10);
863 frame_val = tr32(MAC_MI_COM);
864 if ((frame_val & MI_COM_BUSY) == 0) {
865 udelay(5);
866 frame_val = tr32(MAC_MI_COM);
867 break;
868 }
869 loops -= 1;
870 }
871
872 ret = -EBUSY;
873 if (loops != 0)
874 ret = 0;
875
876 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
877 tw32_f(MAC_MI_MODE, tp->mi_mode);
878 udelay(80);
879 }
880
881 return ret;
882}
883
Matt Carlson95e28692008-05-25 23:44:14 -0700884static int tg3_bmcr_reset(struct tg3 *tp)
885{
886 u32 phy_control;
887 int limit, err;
888
889 /* OK, reset it, and poll the BMCR_RESET bit until it
890 * clears or we time out.
891 */
892 phy_control = BMCR_RESET;
893 err = tg3_writephy(tp, MII_BMCR, phy_control);
894 if (err != 0)
895 return -EBUSY;
896
897 limit = 5000;
898 while (limit--) {
899 err = tg3_readphy(tp, MII_BMCR, &phy_control);
900 if (err != 0)
901 return -EBUSY;
902
903 if ((phy_control & BMCR_RESET) == 0) {
904 udelay(40);
905 break;
906 }
907 udelay(10);
908 }
Roel Kluind4675b52009-02-12 16:33:27 -0800909 if (limit < 0)
Matt Carlson95e28692008-05-25 23:44:14 -0700910 return -EBUSY;
911
912 return 0;
913}
914
Matt Carlson158d7ab2008-05-29 01:37:54 -0700915static int tg3_mdio_read(struct mii_bus *bp, int mii_id, int reg)
916{
Francois Romieu3d165432009-01-19 16:56:50 -0800917 struct tg3 *tp = bp->priv;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700918 u32 val;
919
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000920 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700921
922 if (tg3_readphy(tp, reg, &val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000923 val = -EIO;
924
925 spin_unlock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700926
927 return val;
928}
929
930static int tg3_mdio_write(struct mii_bus *bp, int mii_id, int reg, u16 val)
931{
Francois Romieu3d165432009-01-19 16:56:50 -0800932 struct tg3 *tp = bp->priv;
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000933 u32 ret = 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700934
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000935 spin_lock_bh(&tp->lock);
Matt Carlson158d7ab2008-05-29 01:37:54 -0700936
937 if (tg3_writephy(tp, reg, val))
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000938 ret = -EIO;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700939
Matt Carlson24bb4fb2009-10-05 17:55:29 +0000940 spin_unlock_bh(&tp->lock);
941
942 return ret;
Matt Carlson158d7ab2008-05-29 01:37:54 -0700943}
944
945static int tg3_mdio_reset(struct mii_bus *bp)
946{
947 return 0;
948}
949
Matt Carlson9c61d6b2008-11-03 16:54:56 -0800950static void tg3_mdio_config_5785(struct tg3 *tp)
Matt Carlsona9daf362008-05-25 23:49:44 -0700951{
952 u32 val;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800953 struct phy_device *phydev;
Matt Carlsona9daf362008-05-25 23:49:44 -0700954
Matt Carlson3f0e3ad2009-11-02 14:24:36 +0000955 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800956 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +0000957 case PHY_ID_BCM50610:
958 case PHY_ID_BCM50610M:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800959 val = MAC_PHYCFG2_50610_LED_MODES;
960 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000961 case PHY_ID_BCMAC131:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800962 val = MAC_PHYCFG2_AC131_LED_MODES;
963 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000964 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800965 val = MAC_PHYCFG2_RTL8211C_LED_MODES;
966 break;
Matt Carlson6a443a02010-02-17 15:17:04 +0000967 case PHY_ID_RTL8201E:
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800968 val = MAC_PHYCFG2_RTL8201E_LED_MODES;
969 break;
970 default:
Matt Carlsona9daf362008-05-25 23:49:44 -0700971 return;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800972 }
973
974 if (phydev->interface != PHY_INTERFACE_MODE_RGMII) {
975 tw32(MAC_PHYCFG2, val);
976
977 val = tr32(MAC_PHYCFG1);
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000978 val &= ~(MAC_PHYCFG1_RGMII_INT |
979 MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK);
980 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT;
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800981 tw32(MAC_PHYCFG1, val);
982
983 return;
984 }
985
Matt Carlson14417062010-02-17 15:16:59 +0000986 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE))
Matt Carlsonfcb389d2008-11-03 16:55:44 -0800987 val |= MAC_PHYCFG2_EMODE_MASK_MASK |
988 MAC_PHYCFG2_FMODE_MASK_MASK |
989 MAC_PHYCFG2_GMODE_MASK_MASK |
990 MAC_PHYCFG2_ACT_MASK_MASK |
991 MAC_PHYCFG2_QUAL_MASK_MASK |
992 MAC_PHYCFG2_INBAND_ENABLE;
993
994 tw32(MAC_PHYCFG2, val);
Matt Carlsona9daf362008-05-25 23:49:44 -0700995
Matt Carlsonbb85fbb2009-08-25 10:09:07 +0000996 val = tr32(MAC_PHYCFG1);
997 val &= ~(MAC_PHYCFG1_RXCLK_TO_MASK | MAC_PHYCFG1_TXCLK_TO_MASK |
998 MAC_PHYCFG1_RGMII_EXT_RX_DEC | MAC_PHYCFG1_RGMII_SND_STAT_EN);
Matt Carlson14417062010-02-17 15:16:59 +0000999 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001000 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1001 val |= MAC_PHYCFG1_RGMII_EXT_RX_DEC;
1002 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1003 val |= MAC_PHYCFG1_RGMII_SND_STAT_EN;
1004 }
Matt Carlsonbb85fbb2009-08-25 10:09:07 +00001005 val |= MAC_PHYCFG1_RXCLK_TIMEOUT | MAC_PHYCFG1_TXCLK_TIMEOUT |
1006 MAC_PHYCFG1_RGMII_INT | MAC_PHYCFG1_TXC_DRV;
1007 tw32(MAC_PHYCFG1, val);
Matt Carlsona9daf362008-05-25 23:49:44 -07001008
Matt Carlsona9daf362008-05-25 23:49:44 -07001009 val = tr32(MAC_EXT_RGMII_MODE);
1010 val &= ~(MAC_RGMII_MODE_RX_INT_B |
1011 MAC_RGMII_MODE_RX_QUALITY |
1012 MAC_RGMII_MODE_RX_ACTIVITY |
1013 MAC_RGMII_MODE_RX_ENG_DET |
1014 MAC_RGMII_MODE_TX_ENABLE |
1015 MAC_RGMII_MODE_TX_LOWPWR |
1016 MAC_RGMII_MODE_TX_RESET);
Matt Carlson14417062010-02-17 15:16:59 +00001017 if (!(tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)) {
Matt Carlsona9daf362008-05-25 23:49:44 -07001018 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1019 val |= MAC_RGMII_MODE_RX_INT_B |
1020 MAC_RGMII_MODE_RX_QUALITY |
1021 MAC_RGMII_MODE_RX_ACTIVITY |
1022 MAC_RGMII_MODE_RX_ENG_DET;
1023 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1024 val |= MAC_RGMII_MODE_TX_ENABLE |
1025 MAC_RGMII_MODE_TX_LOWPWR |
1026 MAC_RGMII_MODE_TX_RESET;
1027 }
1028 tw32(MAC_EXT_RGMII_MODE, val);
1029}
1030
Matt Carlson158d7ab2008-05-29 01:37:54 -07001031static void tg3_mdio_start(struct tg3 *tp)
1032{
Matt Carlson158d7ab2008-05-29 01:37:54 -07001033 tp->mi_mode &= ~MAC_MI_MODE_AUTO_POLL;
1034 tw32_f(MAC_MI_MODE, tp->mi_mode);
1035 udelay(80);
Matt Carlsona9daf362008-05-25 23:49:44 -07001036
Matt Carlson9ea48182010-02-17 15:17:01 +00001037 if ((tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) &&
1038 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1039 tg3_mdio_config_5785(tp);
1040}
1041
1042static int tg3_mdio_init(struct tg3 *tp)
1043{
1044 int i;
1045 u32 reg;
1046 struct phy_device *phydev;
1047
Matt Carlson0a58d662011-04-05 14:22:45 +00001048 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlson9c7df912010-06-05 17:24:36 +00001049 u32 is_serdes;
Matt Carlson882e9792009-09-01 13:21:36 +00001050
Matt Carlson9c7df912010-06-05 17:24:36 +00001051 tp->phy_addr = PCI_FUNC(tp->pdev->devfn) + 1;
Matt Carlson882e9792009-09-01 13:21:36 +00001052
Matt Carlsond1ec96a2010-01-12 10:11:38 +00001053 if (tp->pci_chip_rev_id != CHIPREV_ID_5717_A0)
1054 is_serdes = tr32(SG_DIG_STATUS) & SG_DIG_IS_SERDES;
1055 else
1056 is_serdes = tr32(TG3_CPMU_PHY_STRAP) &
1057 TG3_CPMU_PHY_STRAP_IS_SERDES;
Matt Carlson882e9792009-09-01 13:21:36 +00001058 if (is_serdes)
1059 tp->phy_addr += 7;
1060 } else
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001061 tp->phy_addr = TG3_PHY_MII_ADDR;
Matt Carlson882e9792009-09-01 13:21:36 +00001062
Matt Carlson158d7ab2008-05-29 01:37:54 -07001063 tg3_mdio_start(tp);
1064
1065 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) ||
1066 (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED))
1067 return 0;
1068
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001069 tp->mdio_bus = mdiobus_alloc();
1070 if (tp->mdio_bus == NULL)
1071 return -ENOMEM;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001072
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001073 tp->mdio_bus->name = "tg3 mdio bus";
1074 snprintf(tp->mdio_bus->id, MII_BUS_ID_SIZE, "%x",
Matt Carlson158d7ab2008-05-29 01:37:54 -07001075 (tp->pdev->bus->number << 8) | tp->pdev->devfn);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001076 tp->mdio_bus->priv = tp;
1077 tp->mdio_bus->parent = &tp->pdev->dev;
1078 tp->mdio_bus->read = &tg3_mdio_read;
1079 tp->mdio_bus->write = &tg3_mdio_write;
1080 tp->mdio_bus->reset = &tg3_mdio_reset;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001081 tp->mdio_bus->phy_mask = ~(1 << TG3_PHY_MII_ADDR);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001082 tp->mdio_bus->irq = &tp->mdio_irq[0];
Matt Carlson158d7ab2008-05-29 01:37:54 -07001083
1084 for (i = 0; i < PHY_MAX_ADDR; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001085 tp->mdio_bus->irq[i] = PHY_POLL;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001086
1087 /* The bus registration will look for all the PHYs on the mdio bus.
1088 * Unfortunately, it does not ensure the PHY is powered up before
1089 * accessing the PHY ID registers. A chip reset is the
1090 * quickest way to bring the device back to an operational state..
1091 */
1092 if (tg3_readphy(tp, MII_BMCR, &reg) || (reg & BMCR_PDOWN))
1093 tg3_bmcr_reset(tp);
1094
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001095 i = mdiobus_register(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001096 if (i) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001097 dev_warn(&tp->pdev->dev, "mdiobus_reg failed (0x%x)\n", i);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001098 mdiobus_free(tp->mdio_bus);
Matt Carlsona9daf362008-05-25 23:49:44 -07001099 return i;
1100 }
Matt Carlson158d7ab2008-05-29 01:37:54 -07001101
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001102 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsona9daf362008-05-25 23:49:44 -07001103
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001104 if (!phydev || !phydev->drv) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001105 dev_warn(&tp->pdev->dev, "No PHY devices\n");
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001106 mdiobus_unregister(tp->mdio_bus);
1107 mdiobus_free(tp->mdio_bus);
1108 return -ENODEV;
1109 }
1110
1111 switch (phydev->drv->phy_id & phydev->drv->phy_id_mask) {
Matt Carlson6a443a02010-02-17 15:17:04 +00001112 case PHY_ID_BCM57780:
Matt Carlson321d32a2008-11-21 17:22:19 -08001113 phydev->interface = PHY_INTERFACE_MODE_GMII;
Matt Carlsonc704dc22009-11-02 14:32:12 +00001114 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08001115 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001116 case PHY_ID_BCM50610:
1117 case PHY_ID_BCM50610M:
Matt Carlson32e5a8d2009-11-02 14:31:39 +00001118 phydev->dev_flags |= PHY_BRCM_CLEAR_RGMII_MODE |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001119 PHY_BRCM_RX_REFCLK_UNUSED |
Matt Carlson52fae082009-11-02 14:32:38 +00001120 PHY_BRCM_DIS_TXCRXC_NOENRGY |
Matt Carlsonc704dc22009-11-02 14:32:12 +00001121 PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlson14417062010-02-17 15:16:59 +00001122 if (tp->tg3_flags3 & TG3_FLG3_RGMII_INBAND_DISABLE)
Matt Carlsona9daf362008-05-25 23:49:44 -07001123 phydev->dev_flags |= PHY_BRCM_STD_IBND_DISABLE;
1124 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_RX_EN)
1125 phydev->dev_flags |= PHY_BRCM_EXT_IBND_RX_ENABLE;
1126 if (tp->tg3_flags3 & TG3_FLG3_RGMII_EXT_IBND_TX_EN)
1127 phydev->dev_flags |= PHY_BRCM_EXT_IBND_TX_ENABLE;
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001128 /* fallthru */
Matt Carlson6a443a02010-02-17 15:17:04 +00001129 case PHY_ID_RTL8211C:
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001130 phydev->interface = PHY_INTERFACE_MODE_RGMII;
Matt Carlsona9daf362008-05-25 23:49:44 -07001131 break;
Matt Carlson6a443a02010-02-17 15:17:04 +00001132 case PHY_ID_RTL8201E:
1133 case PHY_ID_BCMAC131:
Matt Carlsona9daf362008-05-25 23:49:44 -07001134 phydev->interface = PHY_INTERFACE_MODE_MII;
Matt Carlsoncdd4e092009-11-02 14:31:11 +00001135 phydev->dev_flags |= PHY_BRCM_AUTO_PWRDWN_ENABLE;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001136 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlsona9daf362008-05-25 23:49:44 -07001137 break;
1138 }
1139
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001140 tp->tg3_flags3 |= TG3_FLG3_MDIOBUS_INITED;
1141
1142 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
1143 tg3_mdio_config_5785(tp);
Matt Carlsona9daf362008-05-25 23:49:44 -07001144
1145 return 0;
Matt Carlson158d7ab2008-05-29 01:37:54 -07001146}
1147
1148static void tg3_mdio_fini(struct tg3 *tp)
1149{
1150 if (tp->tg3_flags3 & TG3_FLG3_MDIOBUS_INITED) {
1151 tp->tg3_flags3 &= ~TG3_FLG3_MDIOBUS_INITED;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001152 mdiobus_unregister(tp->mdio_bus);
1153 mdiobus_free(tp->mdio_bus);
Matt Carlson158d7ab2008-05-29 01:37:54 -07001154 }
1155}
1156
Matt Carlsonddfc87b2010-10-14 10:37:40 +00001157static int tg3_phy_cl45_write(struct tg3 *tp, u32 devad, u32 addr, u32 val)
1158{
1159 int err;
1160
1161 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1162 if (err)
1163 goto done;
1164
1165 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1166 if (err)
1167 goto done;
1168
1169 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1170 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1171 if (err)
1172 goto done;
1173
1174 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, val);
1175
1176done:
1177 return err;
1178}
1179
1180static int tg3_phy_cl45_read(struct tg3 *tp, u32 devad, u32 addr, u32 *val)
1181{
1182 int err;
1183
1184 err = tg3_writephy(tp, MII_TG3_MMD_CTRL, devad);
1185 if (err)
1186 goto done;
1187
1188 err = tg3_writephy(tp, MII_TG3_MMD_ADDRESS, addr);
1189 if (err)
1190 goto done;
1191
1192 err = tg3_writephy(tp, MII_TG3_MMD_CTRL,
1193 MII_TG3_MMD_CTRL_DATA_NOINC | devad);
1194 if (err)
1195 goto done;
1196
1197 err = tg3_readphy(tp, MII_TG3_MMD_ADDRESS, val);
1198
1199done:
1200 return err;
1201}
1202
Matt Carlson95e28692008-05-25 23:44:14 -07001203/* tp->lock is held. */
Matt Carlson4ba526c2008-08-15 14:10:04 -07001204static inline void tg3_generate_fw_event(struct tg3 *tp)
1205{
1206 u32 val;
1207
1208 val = tr32(GRC_RX_CPU_EVENT);
1209 val |= GRC_RX_CPU_DRIVER_EVENT;
1210 tw32_f(GRC_RX_CPU_EVENT, val);
1211
1212 tp->last_event_jiffies = jiffies;
1213}
1214
1215#define TG3_FW_EVENT_TIMEOUT_USEC 2500
1216
1217/* tp->lock is held. */
Matt Carlson95e28692008-05-25 23:44:14 -07001218static void tg3_wait_for_event_ack(struct tg3 *tp)
1219{
1220 int i;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001221 unsigned int delay_cnt;
1222 long time_remain;
Matt Carlson95e28692008-05-25 23:44:14 -07001223
Matt Carlson4ba526c2008-08-15 14:10:04 -07001224 /* If enough time has passed, no wait is necessary. */
1225 time_remain = (long)(tp->last_event_jiffies + 1 +
1226 usecs_to_jiffies(TG3_FW_EVENT_TIMEOUT_USEC)) -
1227 (long)jiffies;
1228 if (time_remain < 0)
1229 return;
1230
1231 /* Check if we can shorten the wait time. */
1232 delay_cnt = jiffies_to_usecs(time_remain);
1233 if (delay_cnt > TG3_FW_EVENT_TIMEOUT_USEC)
1234 delay_cnt = TG3_FW_EVENT_TIMEOUT_USEC;
1235 delay_cnt = (delay_cnt >> 3) + 1;
1236
1237 for (i = 0; i < delay_cnt; i++) {
Matt Carlson95e28692008-05-25 23:44:14 -07001238 if (!(tr32(GRC_RX_CPU_EVENT) & GRC_RX_CPU_DRIVER_EVENT))
1239 break;
Matt Carlson4ba526c2008-08-15 14:10:04 -07001240 udelay(8);
Matt Carlson95e28692008-05-25 23:44:14 -07001241 }
1242}
1243
1244/* tp->lock is held. */
1245static void tg3_ump_link_report(struct tg3 *tp)
1246{
1247 u32 reg;
1248 u32 val;
1249
1250 if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
1251 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
1252 return;
1253
1254 tg3_wait_for_event_ack(tp);
1255
1256 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_LINK_UPDATE);
1257
1258 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 14);
1259
1260 val = 0;
1261 if (!tg3_readphy(tp, MII_BMCR, &reg))
1262 val = reg << 16;
1263 if (!tg3_readphy(tp, MII_BMSR, &reg))
1264 val |= (reg & 0xffff);
1265 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX, val);
1266
1267 val = 0;
1268 if (!tg3_readphy(tp, MII_ADVERTISE, &reg))
1269 val = reg << 16;
1270 if (!tg3_readphy(tp, MII_LPA, &reg))
1271 val |= (reg & 0xffff);
1272 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 4, val);
1273
1274 val = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001275 if (!(tp->phy_flags & TG3_PHYFLG_MII_SERDES)) {
Matt Carlson95e28692008-05-25 23:44:14 -07001276 if (!tg3_readphy(tp, MII_CTRL1000, &reg))
1277 val = reg << 16;
1278 if (!tg3_readphy(tp, MII_STAT1000, &reg))
1279 val |= (reg & 0xffff);
1280 }
1281 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 8, val);
1282
1283 if (!tg3_readphy(tp, MII_PHYADDR, &reg))
1284 val = reg << 16;
1285 else
1286 val = 0;
1287 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX + 12, val);
1288
Matt Carlson4ba526c2008-08-15 14:10:04 -07001289 tg3_generate_fw_event(tp);
Matt Carlson95e28692008-05-25 23:44:14 -07001290}
1291
1292static void tg3_link_report(struct tg3 *tp)
1293{
1294 if (!netif_carrier_ok(tp->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001295 netif_info(tp, link, tp->dev, "Link is down\n");
Matt Carlson95e28692008-05-25 23:44:14 -07001296 tg3_ump_link_report(tp);
1297 } else if (netif_msg_link(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00001298 netdev_info(tp->dev, "Link is up at %d Mbps, %s duplex\n",
1299 (tp->link_config.active_speed == SPEED_1000 ?
1300 1000 :
1301 (tp->link_config.active_speed == SPEED_100 ?
1302 100 : 10)),
1303 (tp->link_config.active_duplex == DUPLEX_FULL ?
1304 "full" : "half"));
Matt Carlson95e28692008-05-25 23:44:14 -07001305
Joe Perches05dbe002010-02-17 19:44:19 +00001306 netdev_info(tp->dev, "Flow control is %s for TX and %s for RX\n",
1307 (tp->link_config.active_flowctrl & FLOW_CTRL_TX) ?
1308 "on" : "off",
1309 (tp->link_config.active_flowctrl & FLOW_CTRL_RX) ?
1310 "on" : "off");
Matt Carlson95e28692008-05-25 23:44:14 -07001311 tg3_ump_link_report(tp);
1312 }
1313}
1314
1315static u16 tg3_advert_flowctrl_1000T(u8 flow_ctrl)
1316{
1317 u16 miireg;
1318
Steve Glendinninge18ce342008-12-16 02:00:00 -08001319 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001320 miireg = ADVERTISE_PAUSE_CAP;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001321 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001322 miireg = ADVERTISE_PAUSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001323 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001324 miireg = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
1325 else
1326 miireg = 0;
1327
1328 return miireg;
1329}
1330
1331static u16 tg3_advert_flowctrl_1000X(u8 flow_ctrl)
1332{
1333 u16 miireg;
1334
Steve Glendinninge18ce342008-12-16 02:00:00 -08001335 if ((flow_ctrl & FLOW_CTRL_TX) && (flow_ctrl & FLOW_CTRL_RX))
Matt Carlson95e28692008-05-25 23:44:14 -07001336 miireg = ADVERTISE_1000XPAUSE;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001337 else if (flow_ctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001338 miireg = ADVERTISE_1000XPSE_ASYM;
Steve Glendinninge18ce342008-12-16 02:00:00 -08001339 else if (flow_ctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001340 miireg = ADVERTISE_1000XPAUSE | ADVERTISE_1000XPSE_ASYM;
1341 else
1342 miireg = 0;
1343
1344 return miireg;
1345}
1346
Matt Carlson95e28692008-05-25 23:44:14 -07001347static u8 tg3_resolve_flowctrl_1000X(u16 lcladv, u16 rmtadv)
1348{
1349 u8 cap = 0;
1350
1351 if (lcladv & ADVERTISE_1000XPAUSE) {
1352 if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1353 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001354 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001355 else if (rmtadv & LPA_1000XPAUSE_ASYM)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001356 cap = FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001357 } else {
1358 if (rmtadv & LPA_1000XPAUSE)
Steve Glendinninge18ce342008-12-16 02:00:00 -08001359 cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
Matt Carlson95e28692008-05-25 23:44:14 -07001360 }
1361 } else if (lcladv & ADVERTISE_1000XPSE_ASYM) {
1362 if ((rmtadv & LPA_1000XPAUSE) && (rmtadv & LPA_1000XPAUSE_ASYM))
Steve Glendinninge18ce342008-12-16 02:00:00 -08001363 cap = FLOW_CTRL_TX;
Matt Carlson95e28692008-05-25 23:44:14 -07001364 }
1365
1366 return cap;
1367}
1368
Matt Carlsonf51f3562008-05-25 23:45:08 -07001369static void tg3_setup_flow_control(struct tg3 *tp, u32 lcladv, u32 rmtadv)
Matt Carlson95e28692008-05-25 23:44:14 -07001370{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001371 u8 autoneg;
Matt Carlsonf51f3562008-05-25 23:45:08 -07001372 u8 flowctrl = 0;
Matt Carlson95e28692008-05-25 23:44:14 -07001373 u32 old_rx_mode = tp->rx_mode;
1374 u32 old_tx_mode = tp->tx_mode;
1375
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001376 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001377 autoneg = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]->autoneg;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001378 else
1379 autoneg = tp->link_config.autoneg;
1380
1381 if (autoneg == AUTONEG_ENABLE &&
Matt Carlson95e28692008-05-25 23:44:14 -07001382 (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001383 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Matt Carlsonf51f3562008-05-25 23:45:08 -07001384 flowctrl = tg3_resolve_flowctrl_1000X(lcladv, rmtadv);
Matt Carlson95e28692008-05-25 23:44:14 -07001385 else
Steve Glendinningbc02ff92008-12-16 02:00:48 -08001386 flowctrl = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Matt Carlsonf51f3562008-05-25 23:45:08 -07001387 } else
1388 flowctrl = tp->link_config.flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001389
Matt Carlsonf51f3562008-05-25 23:45:08 -07001390 tp->link_config.active_flowctrl = flowctrl;
Matt Carlson95e28692008-05-25 23:44:14 -07001391
Steve Glendinninge18ce342008-12-16 02:00:00 -08001392 if (flowctrl & FLOW_CTRL_RX)
Matt Carlson95e28692008-05-25 23:44:14 -07001393 tp->rx_mode |= RX_MODE_FLOW_CTRL_ENABLE;
1394 else
1395 tp->rx_mode &= ~RX_MODE_FLOW_CTRL_ENABLE;
1396
Matt Carlsonf51f3562008-05-25 23:45:08 -07001397 if (old_rx_mode != tp->rx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001398 tw32_f(MAC_RX_MODE, tp->rx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001399
Steve Glendinninge18ce342008-12-16 02:00:00 -08001400 if (flowctrl & FLOW_CTRL_TX)
Matt Carlson95e28692008-05-25 23:44:14 -07001401 tp->tx_mode |= TX_MODE_FLOW_CTRL_ENABLE;
1402 else
1403 tp->tx_mode &= ~TX_MODE_FLOW_CTRL_ENABLE;
1404
Matt Carlsonf51f3562008-05-25 23:45:08 -07001405 if (old_tx_mode != tp->tx_mode)
Matt Carlson95e28692008-05-25 23:44:14 -07001406 tw32_f(MAC_TX_MODE, tp->tx_mode);
Matt Carlson95e28692008-05-25 23:44:14 -07001407}
1408
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001409static void tg3_adjust_link(struct net_device *dev)
1410{
1411 u8 oldflowctrl, linkmesg = 0;
1412 u32 mac_mode, lcl_adv, rmt_adv;
1413 struct tg3 *tp = netdev_priv(dev);
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001414 struct phy_device *phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001415
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001416 spin_lock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001417
1418 mac_mode = tp->mac_mode & ~(MAC_MODE_PORT_MODE_MASK |
1419 MAC_MODE_HALF_DUPLEX);
1420
1421 oldflowctrl = tp->link_config.active_flowctrl;
1422
1423 if (phydev->link) {
1424 lcl_adv = 0;
1425 rmt_adv = 0;
1426
1427 if (phydev->speed == SPEED_100 || phydev->speed == SPEED_10)
1428 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001429 else if (phydev->speed == SPEED_1000 ||
1430 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001431 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonc3df0742009-11-02 14:27:02 +00001432 else
1433 mac_mode |= MAC_MODE_PORT_MODE_MII;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001434
1435 if (phydev->duplex == DUPLEX_HALF)
1436 mac_mode |= MAC_MODE_HALF_DUPLEX;
1437 else {
1438 lcl_adv = tg3_advert_flowctrl_1000T(
1439 tp->link_config.flowctrl);
1440
1441 if (phydev->pause)
1442 rmt_adv = LPA_PAUSE_CAP;
1443 if (phydev->asym_pause)
1444 rmt_adv |= LPA_PAUSE_ASYM;
1445 }
1446
1447 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
1448 } else
1449 mac_mode |= MAC_MODE_PORT_MODE_GMII;
1450
1451 if (mac_mode != tp->mac_mode) {
1452 tp->mac_mode = mac_mode;
1453 tw32_f(MAC_MODE, tp->mac_mode);
1454 udelay(40);
1455 }
1456
Matt Carlsonfcb389d2008-11-03 16:55:44 -08001457 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
1458 if (phydev->speed == SPEED_10)
1459 tw32(MAC_MI_STAT,
1460 MAC_MI_STAT_10MBPS_MODE |
1461 MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1462 else
1463 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
1464 }
1465
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001466 if (phydev->speed == SPEED_1000 && phydev->duplex == DUPLEX_HALF)
1467 tw32(MAC_TX_LENGTHS,
1468 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1469 (6 << TX_LENGTHS_IPG_SHIFT) |
1470 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT)));
1471 else
1472 tw32(MAC_TX_LENGTHS,
1473 ((2 << TX_LENGTHS_IPG_CRS_SHIFT) |
1474 (6 << TX_LENGTHS_IPG_SHIFT) |
1475 (32 << TX_LENGTHS_SLOT_TIME_SHIFT)));
1476
1477 if ((phydev->link && tp->link_config.active_speed == SPEED_INVALID) ||
1478 (!phydev->link && tp->link_config.active_speed != SPEED_INVALID) ||
1479 phydev->speed != tp->link_config.active_speed ||
1480 phydev->duplex != tp->link_config.active_duplex ||
1481 oldflowctrl != tp->link_config.active_flowctrl)
Matt Carlsonc6cdf432010-04-05 10:19:26 +00001482 linkmesg = 1;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001483
1484 tp->link_config.active_speed = phydev->speed;
1485 tp->link_config.active_duplex = phydev->duplex;
1486
Matt Carlson24bb4fb2009-10-05 17:55:29 +00001487 spin_unlock_bh(&tp->lock);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001488
1489 if (linkmesg)
1490 tg3_link_report(tp);
1491}
1492
1493static int tg3_phy_init(struct tg3 *tp)
1494{
1495 struct phy_device *phydev;
1496
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001497 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001498 return 0;
1499
1500 /* Bring the PHY back to a known state. */
1501 tg3_bmcr_reset(tp);
1502
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001503 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001504
1505 /* Attach the MAC to the PHY. */
Kay Sieversfb28ad32008-11-10 13:55:14 -08001506 phydev = phy_connect(tp->dev, dev_name(&phydev->dev), tg3_adjust_link,
Matt Carlsona9daf362008-05-25 23:49:44 -07001507 phydev->dev_flags, phydev->interface);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001508 if (IS_ERR(phydev)) {
Matt Carlsonab96b242010-04-05 10:19:22 +00001509 dev_err(&tp->pdev->dev, "Could not attach to PHY\n");
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001510 return PTR_ERR(phydev);
1511 }
1512
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001513 /* Mask with MAC supported features. */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001514 switch (phydev->interface) {
1515 case PHY_INTERFACE_MODE_GMII:
1516 case PHY_INTERFACE_MODE_RGMII:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001517 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Matt Carlson321d32a2008-11-21 17:22:19 -08001518 phydev->supported &= (PHY_GBIT_FEATURES |
1519 SUPPORTED_Pause |
1520 SUPPORTED_Asym_Pause);
1521 break;
1522 }
1523 /* fallthru */
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001524 case PHY_INTERFACE_MODE_MII:
1525 phydev->supported &= (PHY_BASIC_FEATURES |
1526 SUPPORTED_Pause |
1527 SUPPORTED_Asym_Pause);
1528 break;
1529 default:
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001530 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlson9c61d6b2008-11-03 16:54:56 -08001531 return -EINVAL;
1532 }
1533
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001534 tp->phy_flags |= TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001535
1536 phydev->advertising = phydev->supported;
1537
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001538 return 0;
1539}
1540
1541static void tg3_phy_start(struct tg3 *tp)
1542{
1543 struct phy_device *phydev;
1544
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001545 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001546 return;
1547
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001548 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001549
Matt Carlson80096062010-08-02 11:26:06 +00001550 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
1551 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001552 phydev->speed = tp->link_config.orig_speed;
1553 phydev->duplex = tp->link_config.orig_duplex;
1554 phydev->autoneg = tp->link_config.orig_autoneg;
1555 phydev->advertising = tp->link_config.orig_advertising;
1556 }
1557
1558 phy_start(phydev);
1559
1560 phy_start_aneg(phydev);
1561}
1562
1563static void tg3_phy_stop(struct tg3 *tp)
1564{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001565 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001566 return;
1567
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001568 phy_stop(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001569}
1570
1571static void tg3_phy_fini(struct tg3 *tp)
1572{
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001573 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00001574 phy_disconnect(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001575 tp->phy_flags &= ~TG3_PHYFLG_IS_CONNECTED;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07001576 }
1577}
1578
Matt Carlson52b02d02010-10-14 10:37:41 +00001579static int tg3_phydsp_read(struct tg3 *tp, u32 reg, u32 *val)
1580{
1581 int err;
1582
1583 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1584 if (!err)
1585 err = tg3_readphy(tp, MII_TG3_DSP_RW_PORT, val);
1586
1587 return err;
1588}
1589
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001590static int tg3_phydsp_write(struct tg3 *tp, u32 reg, u32 val)
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001591{
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001592 int err;
1593
1594 err = tg3_writephy(tp, MII_TG3_DSP_ADDRESS, reg);
1595 if (!err)
1596 err = tg3_writephy(tp, MII_TG3_DSP_RW_PORT, val);
1597
1598 return err;
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001599}
1600
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001601static void tg3_phy_fet_toggle_apd(struct tg3 *tp, bool enable)
1602{
1603 u32 phytest;
1604
1605 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
1606 u32 phy;
1607
1608 tg3_writephy(tp, MII_TG3_FET_TEST,
1609 phytest | MII_TG3_FET_SHADOW_EN);
1610 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXSTAT2, &phy)) {
1611 if (enable)
1612 phy |= MII_TG3_FET_SHDW_AUXSTAT2_APD;
1613 else
1614 phy &= ~MII_TG3_FET_SHDW_AUXSTAT2_APD;
1615 tg3_writephy(tp, MII_TG3_FET_SHDW_AUXSTAT2, phy);
1616 }
1617 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
1618 }
1619}
1620
Matt Carlson6833c042008-11-21 17:18:59 -08001621static void tg3_phy_toggle_apd(struct tg3 *tp, bool enable)
1622{
1623 u32 reg;
1624
Matt Carlsonecf14102010-01-20 16:58:05 +00001625 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlson0a58d662011-04-05 14:22:45 +00001626 ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001627 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Matt Carlson6833c042008-11-21 17:18:59 -08001628 return;
1629
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001630 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +00001631 tg3_phy_fet_toggle_apd(tp, enable);
1632 return;
1633 }
1634
Matt Carlson6833c042008-11-21 17:18:59 -08001635 reg = MII_TG3_MISC_SHDW_WREN |
1636 MII_TG3_MISC_SHDW_SCR5_SEL |
1637 MII_TG3_MISC_SHDW_SCR5_LPED |
1638 MII_TG3_MISC_SHDW_SCR5_DLPTLM |
1639 MII_TG3_MISC_SHDW_SCR5_SDTL |
1640 MII_TG3_MISC_SHDW_SCR5_C125OE;
1641 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 || !enable)
1642 reg |= MII_TG3_MISC_SHDW_SCR5_DLLAPD;
1643
1644 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1645
1646
1647 reg = MII_TG3_MISC_SHDW_WREN |
1648 MII_TG3_MISC_SHDW_APD_SEL |
1649 MII_TG3_MISC_SHDW_APD_WKTM_84MS;
1650 if (enable)
1651 reg |= MII_TG3_MISC_SHDW_APD_ENABLE;
1652
1653 tg3_writephy(tp, MII_TG3_MISC_SHDW, reg);
1654}
1655
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001656static void tg3_phy_toggle_automdix(struct tg3 *tp, int enable)
1657{
1658 u32 phy;
1659
1660 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001661 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001662 return;
1663
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001664 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001665 u32 ephy;
1666
Matt Carlson535ef6e2009-08-25 10:09:36 +00001667 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &ephy)) {
1668 u32 reg = MII_TG3_FET_SHDW_MISCCTRL;
1669
1670 tg3_writephy(tp, MII_TG3_FET_TEST,
1671 ephy | MII_TG3_FET_SHADOW_EN);
1672 if (!tg3_readphy(tp, reg, &phy)) {
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001673 if (enable)
Matt Carlson535ef6e2009-08-25 10:09:36 +00001674 phy |= MII_TG3_FET_SHDW_MISCCTRL_MDIX;
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001675 else
Matt Carlson535ef6e2009-08-25 10:09:36 +00001676 phy &= ~MII_TG3_FET_SHDW_MISCCTRL_MDIX;
1677 tg3_writephy(tp, reg, phy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001678 }
Matt Carlson535ef6e2009-08-25 10:09:36 +00001679 tg3_writephy(tp, MII_TG3_FET_TEST, ephy);
Matt Carlson9ef8ca92007-07-11 19:48:29 -07001680 }
1681 } else {
1682 phy = MII_TG3_AUXCTL_MISC_RDSEL_MISC |
1683 MII_TG3_AUXCTL_SHDWSEL_MISC;
1684 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, phy) &&
1685 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &phy)) {
1686 if (enable)
1687 phy |= MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1688 else
1689 phy &= ~MII_TG3_AUXCTL_MISC_FORCE_AMDIX;
1690 phy |= MII_TG3_AUXCTL_MISC_WREN;
1691 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1692 }
1693 }
1694}
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696static void tg3_phy_set_wirespeed(struct tg3 *tp)
1697{
1698 u32 val;
1699
Matt Carlsonf07e9af2010-08-02 11:26:07 +00001700 if (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return;
1702
1703 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x7007) &&
1704 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
1705 tg3_writephy(tp, MII_TG3_AUX_CTRL,
1706 (val | (1 << 15) | (1 << 4)));
1707}
1708
Matt Carlsonb2a5c192008-04-03 21:44:44 -07001709static void tg3_phy_apply_otp(struct tg3 *tp)
1710{
1711 u32 otp, phy;
1712
1713 if (!tp->phy_otp)
1714 return;
1715
1716 otp = tp->phy_otp;
1717
1718 /* Enable SM_DSP clock and tx 6dB coding. */
1719 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1720 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1721 MII_TG3_AUXCTL_ACTL_TX_6DB;
1722 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1723
1724 phy = ((otp & TG3_OTP_AGCTGT_MASK) >> TG3_OTP_AGCTGT_SHIFT);
1725 phy |= MII_TG3_DSP_TAP1_AGCTGT_DFLT;
1726 tg3_phydsp_write(tp, MII_TG3_DSP_TAP1, phy);
1727
1728 phy = ((otp & TG3_OTP_HPFFLTR_MASK) >> TG3_OTP_HPFFLTR_SHIFT) |
1729 ((otp & TG3_OTP_HPFOVER_MASK) >> TG3_OTP_HPFOVER_SHIFT);
1730 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH0, phy);
1731
1732 phy = ((otp & TG3_OTP_LPFDIS_MASK) >> TG3_OTP_LPFDIS_SHIFT);
1733 phy |= MII_TG3_DSP_AADJ1CH3_ADCCKADJ;
1734 tg3_phydsp_write(tp, MII_TG3_DSP_AADJ1CH3, phy);
1735
1736 phy = ((otp & TG3_OTP_VDAC_MASK) >> TG3_OTP_VDAC_SHIFT);
1737 tg3_phydsp_write(tp, MII_TG3_DSP_EXP75, phy);
1738
1739 phy = ((otp & TG3_OTP_10BTAMP_MASK) >> TG3_OTP_10BTAMP_SHIFT);
1740 tg3_phydsp_write(tp, MII_TG3_DSP_EXP96, phy);
1741
1742 phy = ((otp & TG3_OTP_ROFF_MASK) >> TG3_OTP_ROFF_SHIFT) |
1743 ((otp & TG3_OTP_RCOFF_MASK) >> TG3_OTP_RCOFF_SHIFT);
1744 tg3_phydsp_write(tp, MII_TG3_DSP_EXP97, phy);
1745
1746 /* Turn off SM_DSP clock. */
1747 phy = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1748 MII_TG3_AUXCTL_ACTL_TX_6DB;
1749 tg3_writephy(tp, MII_TG3_AUX_CTRL, phy);
1750}
1751
Matt Carlson52b02d02010-10-14 10:37:41 +00001752static void tg3_phy_eee_adjust(struct tg3 *tp, u32 current_link_up)
1753{
1754 u32 val;
1755
1756 if (!(tp->phy_flags & TG3_PHYFLG_EEE_CAP))
1757 return;
1758
1759 tp->setlpicnt = 0;
1760
1761 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
1762 current_link_up == 1 &&
Matt Carlsona6b68da2010-12-06 08:28:52 +00001763 tp->link_config.active_duplex == DUPLEX_FULL &&
1764 (tp->link_config.active_speed == SPEED_100 ||
1765 tp->link_config.active_speed == SPEED_1000)) {
Matt Carlson52b02d02010-10-14 10:37:41 +00001766 u32 eeectl;
1767
1768 if (tp->link_config.active_speed == SPEED_1000)
1769 eeectl = TG3_CPMU_EEE_CTRL_EXIT_16_5_US;
1770 else
1771 eeectl = TG3_CPMU_EEE_CTRL_EXIT_36_US;
1772
1773 tw32(TG3_CPMU_EEE_CTRL, eeectl);
1774
Matt Carlson3110f5f52010-12-06 08:28:50 +00001775 tg3_phy_cl45_read(tp, MDIO_MMD_AN,
1776 TG3_CL45_D7_EEERES_STAT, &val);
Matt Carlson52b02d02010-10-14 10:37:41 +00001777
Matt Carlson21a00ab2011-01-25 15:58:55 +00001778 switch (val) {
1779 case TG3_CL45_D7_EEERES_STAT_LP_1000T:
1780 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
1781 case ASIC_REV_5717:
1782 case ASIC_REV_5719:
1783 case ASIC_REV_57765:
1784 /* Enable SM_DSP clock and tx 6dB coding. */
1785 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1786 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
1787 MII_TG3_AUXCTL_ACTL_TX_6DB;
1788 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
1789
1790 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, 0x0000);
1791
1792 /* Turn off SM_DSP clock. */
1793 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
1794 MII_TG3_AUXCTL_ACTL_TX_6DB;
1795 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
1796 }
1797 /* Fallthrough */
1798 case TG3_CL45_D7_EEERES_STAT_LP_100TX:
Matt Carlson52b02d02010-10-14 10:37:41 +00001799 tp->setlpicnt = 2;
Matt Carlson21a00ab2011-01-25 15:58:55 +00001800 }
Matt Carlson52b02d02010-10-14 10:37:41 +00001801 }
1802
1803 if (!tp->setlpicnt) {
1804 val = tr32(TG3_CPMU_EEE_MODE);
1805 tw32(TG3_CPMU_EEE_MODE, val & ~TG3_CPMU_EEEMD_LPI_ENABLE);
1806 }
1807}
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809static int tg3_wait_macro_done(struct tg3 *tp)
1810{
1811 int limit = 100;
1812
1813 while (limit--) {
1814 u32 tmp32;
1815
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001816 if (!tg3_readphy(tp, MII_TG3_DSP_CONTROL, &tmp32)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if ((tmp32 & 0x1000) == 0)
1818 break;
1819 }
1820 }
Roel Kluind4675b52009-02-12 16:33:27 -08001821 if (limit < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return -EBUSY;
1823
1824 return 0;
1825}
1826
1827static int tg3_phy_write_and_check_testpat(struct tg3 *tp, int *resetp)
1828{
1829 static const u32 test_pat[4][6] = {
1830 { 0x00005555, 0x00000005, 0x00002aaa, 0x0000000a, 0x00003456, 0x00000003 },
1831 { 0x00002aaa, 0x0000000a, 0x00003333, 0x00000003, 0x0000789a, 0x00000005 },
1832 { 0x00005a5a, 0x00000005, 0x00002a6a, 0x0000000a, 0x00001bcd, 0x00000003 },
1833 { 0x00002a5a, 0x0000000a, 0x000033c3, 0x00000003, 0x00002ef1, 0x00000005 }
1834 };
1835 int chan;
1836
1837 for (chan = 0; chan < 4; chan++) {
1838 int i;
1839
1840 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1841 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001842 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 for (i = 0; i < 6; i++)
1845 tg3_writephy(tp, MII_TG3_DSP_RW_PORT,
1846 test_pat[chan][i]);
1847
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001848 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 if (tg3_wait_macro_done(tp)) {
1850 *resetp = 1;
1851 return -EBUSY;
1852 }
1853
1854 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1855 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001856 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0082);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (tg3_wait_macro_done(tp)) {
1858 *resetp = 1;
1859 return -EBUSY;
1860 }
1861
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001862 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0802);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (tg3_wait_macro_done(tp)) {
1864 *resetp = 1;
1865 return -EBUSY;
1866 }
1867
1868 for (i = 0; i < 6; i += 2) {
1869 u32 low, high;
1870
1871 if (tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &low) ||
1872 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &high) ||
1873 tg3_wait_macro_done(tp)) {
1874 *resetp = 1;
1875 return -EBUSY;
1876 }
1877 low &= 0x7fff;
1878 high &= 0x000f;
1879 if (low != test_pat[chan][i] ||
1880 high != test_pat[chan][i+1]) {
1881 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000b);
1882 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4001);
1883 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x4005);
1884
1885 return -EBUSY;
1886 }
1887 }
1888 }
1889
1890 return 0;
1891}
1892
1893static int tg3_phy_reset_chanpat(struct tg3 *tp)
1894{
1895 int chan;
1896
1897 for (chan = 0; chan < 4; chan++) {
1898 int i;
1899
1900 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
1901 (chan * 0x2000) | 0x0200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001902 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0002);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 for (i = 0; i < 6; i++)
1904 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x000);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001905 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0202);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (tg3_wait_macro_done(tp))
1907 return -EBUSY;
1908 }
1909
1910 return 0;
1911}
1912
1913static int tg3_phy_reset_5703_4_5(struct tg3 *tp)
1914{
1915 u32 reg32, phy9_orig;
1916 int retries, do_phy_reset, err;
1917
1918 retries = 10;
1919 do_phy_reset = 1;
1920 do {
1921 if (do_phy_reset) {
1922 err = tg3_bmcr_reset(tp);
1923 if (err)
1924 return err;
1925 do_phy_reset = 0;
1926 }
1927
1928 /* Disable transmitter and interrupt. */
1929 if (tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32))
1930 continue;
1931
1932 reg32 |= 0x3000;
1933 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1934
1935 /* Set full-duplex, 1000 mbps. */
1936 tg3_writephy(tp, MII_BMCR,
1937 BMCR_FULLDPLX | TG3_BMCR_SPEED1000);
1938
1939 /* Set to master mode. */
1940 if (tg3_readphy(tp, MII_TG3_CTRL, &phy9_orig))
1941 continue;
1942
1943 tg3_writephy(tp, MII_TG3_CTRL,
1944 (MII_TG3_CTRL_AS_MASTER |
1945 MII_TG3_CTRL_ENABLE_AS_MASTER));
1946
1947 /* Enable SM_DSP_CLOCK and 6dB. */
1948 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
1949
1950 /* Block the PHY control access. */
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001951 tg3_phydsp_write(tp, 0x8005, 0x0800);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 err = tg3_phy_write_and_check_testpat(tp, &do_phy_reset);
1954 if (!err)
1955 break;
1956 } while (--retries);
1957
1958 err = tg3_phy_reset_chanpat(tp);
1959 if (err)
1960 return err;
1961
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00001962 tg3_phydsp_write(tp, 0x8005, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x8200);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00001965 tg3_writephy(tp, MII_TG3_DSP_CONTROL, 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
1968 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
1969 /* Set Extended packet length bit for jumbo frames */
1970 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4400);
Matt Carlson859a5882010-04-05 10:19:28 +00001971 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
1973 }
1974
1975 tg3_writephy(tp, MII_TG3_CTRL, phy9_orig);
1976
1977 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &reg32)) {
1978 reg32 &= ~0x3000;
1979 tg3_writephy(tp, MII_TG3_EXT_CTRL, reg32);
1980 } else if (!err)
1981 err = -EBUSY;
1982
1983 return err;
1984}
1985
1986/* This will reset the tigon3 PHY if there is no valid
1987 * link unless the FORCE argument is non-zero.
1988 */
1989static int tg3_phy_reset(struct tg3 *tp)
1990{
Matt Carlsonf833c4c2010-09-15 09:00:01 +00001991 u32 val, cpmuctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 int err;
1993
Michael Chan60189dd2006-12-17 17:08:07 -08001994 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08001995 val = tr32(GRC_MISC_CFG);
1996 tw32_f(GRC_MISC_CFG, val & ~GRC_MISC_CFG_EPHY_IDDQ);
1997 udelay(40);
1998 }
Matt Carlsonf833c4c2010-09-15 09:00:01 +00001999 err = tg3_readphy(tp, MII_BMSR, &val);
2000 err |= tg3_readphy(tp, MII_BMSR, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (err != 0)
2002 return -EBUSY;
2003
Michael Chanc8e1e822006-04-29 18:55:17 -07002004 if (netif_running(tp->dev) && netif_carrier_ok(tp->dev)) {
2005 netif_carrier_off(tp->dev);
2006 tg3_link_report(tp);
2007 }
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2010 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2011 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
2012 err = tg3_phy_reset_5703_4_5(tp);
2013 if (err)
2014 return err;
2015 goto out;
2016 }
2017
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002018 cpmuctrl = 0;
2019 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
2020 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
2021 cpmuctrl = tr32(TG3_CPMU_CTRL);
2022 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY)
2023 tw32(TG3_CPMU_CTRL,
2024 cpmuctrl & ~CPMU_CTRL_GPHY_10MB_RXONLY);
2025 }
2026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 err = tg3_bmcr_reset(tp);
2028 if (err)
2029 return err;
2030
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002031 if (cpmuctrl & CPMU_CTRL_GPHY_10MB_RXONLY) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002032 val = MII_TG3_DSP_EXP8_AEDW | MII_TG3_DSP_EXP8_REJ2MHz;
2033 tg3_phydsp_write(tp, MII_TG3_DSP_EXP8, val);
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002034
2035 tw32(TG3_CPMU_CTRL, cpmuctrl);
2036 }
2037
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002038 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2039 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002040 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2041 if ((val & CPMU_LSPD_1000MB_MACCLK_MASK) ==
2042 CPMU_LSPD_1000MB_MACCLK_12_5) {
2043 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2044 udelay(40);
2045 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2046 }
2047 }
2048
Matt Carlson0a58d662011-04-05 14:22:45 +00002049 if ((tp->tg3_flags3 & TG3_FLG3_5717_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002050 (tp->phy_flags & TG3_PHYFLG_MII_SERDES))
Matt Carlsonecf14102010-01-20 16:58:05 +00002051 return 0;
2052
Matt Carlsonb2a5c192008-04-03 21:44:44 -07002053 tg3_phy_apply_otp(tp);
2054
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002055 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -08002056 tg3_phy_toggle_apd(tp, true);
2057 else
2058 tg3_phy_toggle_apd(tp, false);
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060out:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002061 if (tp->phy_flags & TG3_PHYFLG_ADC_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002063 tg3_phydsp_write(tp, 0x201f, 0x2aaa);
2064 tg3_phydsp_write(tp, 0x000a, 0x0323);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2066 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002067 if (tp->phy_flags & TG3_PHYFLG_5704_A0_BUG) {
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00002068 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
2069 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002071 if (tp->phy_flags & TG3_PHYFLG_BER_BUG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00002073 tg3_phydsp_write(tp, 0x000a, 0x310b);
2074 tg3_phydsp_write(tp, 0x201f, 0x9506);
2075 tg3_phydsp_write(tp, 0x401f, 0x14e2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002077 } else if (tp->phy_flags & TG3_PHYFLG_JITTER_BUG) {
Michael Chanc424cb22006-04-29 18:56:34 -07002078 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0c00);
2079 tg3_writephy(tp, MII_TG3_DSP_ADDRESS, 0x000a);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002080 if (tp->phy_flags & TG3_PHYFLG_ADJUST_TRIM) {
Michael Chanc1d2a192007-01-08 19:57:20 -08002081 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x110b);
2082 tg3_writephy(tp, MII_TG3_TEST1,
2083 MII_TG3_TEST1_TRIM_EN | 0x4);
2084 } else
2085 tg3_writephy(tp, MII_TG3_DSP_RW_PORT, 0x010b);
Michael Chanc424cb22006-04-29 18:56:34 -07002086 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0400);
2087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /* Set Extended packet length bit (bit 14) on all chips that */
2089 /* support jumbo frames */
Matt Carlson79eb6902010-02-17 15:17:03 +00002090 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 /* Cannot do read-modify-write on 5401 */
2092 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
Matt Carlson8f666b02009-08-28 13:58:24 +00002093 } else if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /* Set bit 14 with read-modify-write to preserve other bits */
2095 if (!tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x0007) &&
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002096 !tg3_readphy(tp, MII_TG3_AUX_CTRL, &val))
2097 tg3_writephy(tp, MII_TG3_AUX_CTRL, val | 0x4000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099
2100 /* Set phy register 0x10 bit 0 to high fifo elasticity to support
2101 * jumbo frames transmission.
2102 */
Matt Carlson8f666b02009-08-28 13:58:24 +00002103 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002104 if (!tg3_readphy(tp, MII_TG3_EXT_CTRL, &val))
Matt Carlsonc6cdf432010-04-05 10:19:26 +00002105 tg3_writephy(tp, MII_TG3_EXT_CTRL,
Matt Carlsonf833c4c2010-09-15 09:00:01 +00002106 val | MII_TG3_EXT_CTRL_FIFO_ELASTIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
2108
Michael Chan715116a2006-09-27 16:09:25 -07002109 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan715116a2006-09-27 16:09:25 -07002110 /* adjust output voltage */
Matt Carlson535ef6e2009-08-25 10:09:36 +00002111 tg3_writephy(tp, MII_TG3_FET_PTEST, 0x12);
Michael Chan715116a2006-09-27 16:09:25 -07002112 }
2113
Matt Carlson9ef8ca92007-07-11 19:48:29 -07002114 tg3_phy_toggle_automdix(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 tg3_phy_set_wirespeed(tp);
2116 return 0;
2117}
2118
2119static void tg3_frob_aux_power(struct tg3 *tp)
2120{
Matt Carlson683644b2011-03-09 16:58:23 +00002121 bool need_vaux = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Matt Carlson334355a2010-01-20 16:58:10 +00002123 /* The GPIOs do something completely different on 57765. */
2124 if ((tp->tg3_flags2 & TG3_FLG2_IS_NIC) == 0 ||
Matt Carlsona50d0792010-06-05 17:24:37 +00002125 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson334355a2010-01-20 16:58:10 +00002126 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 return;
2128
Matt Carlson683644b2011-03-09 16:58:23 +00002129 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2130 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +00002131 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
2132 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) &&
Matt Carlson683644b2011-03-09 16:58:23 +00002133 tp->pdev_peer != tp->pdev) {
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002134 struct net_device *dev_peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002136 dev_peer = pci_get_drvdata(tp->pdev_peer);
Matt Carlson683644b2011-03-09 16:58:23 +00002137
Michael Chanbc1c7562006-03-20 17:48:03 -08002138 /* remove_one() may have been run on the peer. */
Matt Carlson683644b2011-03-09 16:58:23 +00002139 if (dev_peer) {
2140 struct tg3 *tp_peer = netdev_priv(dev_peer);
2141
2142 if (tp_peer->tg3_flags & TG3_FLAG_INIT_COMPLETE)
2143 return;
2144
2145 if ((tp_peer->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2146 (tp_peer->tg3_flags & TG3_FLAG_ENABLE_ASF))
2147 need_vaux = true;
2148 }
Michael Chan8c2dc7e2005-12-19 16:26:02 -08002149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Matt Carlson683644b2011-03-09 16:58:23 +00002151 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) ||
2152 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
2153 need_vaux = true;
2154
2155 if (need_vaux) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2157 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002158 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2159 (GRC_LCLCTRL_GPIO_OE0 |
2160 GRC_LCLCTRL_GPIO_OE1 |
2161 GRC_LCLCTRL_GPIO_OE2 |
2162 GRC_LCLCTRL_GPIO_OUTPUT0 |
2163 GRC_LCLCTRL_GPIO_OUTPUT1),
2164 100);
Matt Carlson8d519ab2009-04-20 06:58:01 +00002165 } else if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
2166 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -07002167 /* The 5761 non-e device swaps GPIO 0 and GPIO 2. */
2168 u32 grc_local_ctrl = GRC_LCLCTRL_GPIO_OE0 |
2169 GRC_LCLCTRL_GPIO_OE1 |
2170 GRC_LCLCTRL_GPIO_OE2 |
2171 GRC_LCLCTRL_GPIO_OUTPUT0 |
2172 GRC_LCLCTRL_GPIO_OUTPUT1 |
2173 tp->grc_local_ctrl;
2174 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2175
2176 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT2;
2177 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
2178
2179 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT0;
2180 tw32_wait_f(GRC_LOCAL_CTRL, grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 } else {
2182 u32 no_gpio2;
Michael Chandc56b7d2005-12-19 16:26:28 -08002183 u32 grc_local_ctrl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Michael Chandc56b7d2005-12-19 16:26:28 -08002185 /* Workaround to prevent overdrawing Amps. */
2186 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2187 ASIC_REV_5714) {
2188 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chanb401e9e2005-12-19 16:27:04 -08002189 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2190 grc_local_ctrl, 100);
Michael Chandc56b7d2005-12-19 16:26:28 -08002191 }
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /* On 5753 and variants, GPIO2 cannot be used. */
2194 no_gpio2 = tp->nic_sram_data_cfg &
2195 NIC_SRAM_DATA_CFG_NO_GPIO2;
2196
Michael Chandc56b7d2005-12-19 16:26:28 -08002197 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 GRC_LCLCTRL_GPIO_OE1 |
2199 GRC_LCLCTRL_GPIO_OE2 |
2200 GRC_LCLCTRL_GPIO_OUTPUT1 |
2201 GRC_LCLCTRL_GPIO_OUTPUT2;
2202 if (no_gpio2) {
2203 grc_local_ctrl &= ~(GRC_LCLCTRL_GPIO_OE2 |
2204 GRC_LCLCTRL_GPIO_OUTPUT2);
2205 }
Michael Chanb401e9e2005-12-19 16:27:04 -08002206 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2207 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 grc_local_ctrl |= GRC_LCLCTRL_GPIO_OUTPUT0;
2210
Michael Chanb401e9e2005-12-19 16:27:04 -08002211 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2212 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 if (!no_gpio2) {
2215 grc_local_ctrl &= ~GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chanb401e9e2005-12-19 16:27:04 -08002216 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2217 grc_local_ctrl, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
2219 }
2220 } else {
2221 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
2222 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
Michael Chanb401e9e2005-12-19 16:27:04 -08002223 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2224 (GRC_LCLCTRL_GPIO_OE1 |
2225 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Michael Chanb401e9e2005-12-19 16:27:04 -08002227 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2228 GRC_LCLCTRL_GPIO_OE1, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Michael Chanb401e9e2005-12-19 16:27:04 -08002230 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl |
2231 (GRC_LCLCTRL_GPIO_OE1 |
2232 GRC_LCLCTRL_GPIO_OUTPUT1), 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
2234 }
2235}
2236
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002237static int tg3_5700_link_polarity(struct tg3 *tp, u32 speed)
2238{
2239 if (tp->led_ctrl == LED_CTRL_MODE_PHY_2)
2240 return 1;
Matt Carlson79eb6902010-02-17 15:17:03 +00002241 else if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002242 if (speed != SPEED_10)
2243 return 1;
2244 } else if (speed == SPEED_10)
2245 return 1;
2246
2247 return 0;
2248}
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250static int tg3_setup_phy(struct tg3 *, int);
2251
2252#define RESET_KIND_SHUTDOWN 0
2253#define RESET_KIND_INIT 1
2254#define RESET_KIND_SUSPEND 2
2255
2256static void tg3_write_sig_post_reset(struct tg3 *, int);
2257static int tg3_halt_cpu(struct tg3 *, u32);
2258
Matt Carlson0a459aa2008-11-03 16:54:15 -08002259static void tg3_power_down_phy(struct tg3 *tp, bool do_low_power)
Michael Chan15c3b692006-03-22 01:06:52 -08002260{
Matt Carlsonce057f02007-11-12 21:08:03 -08002261 u32 val;
2262
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002263 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Michael Chan51297242007-02-13 12:17:57 -08002264 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2265 u32 sg_dig_ctrl = tr32(SG_DIG_CTRL);
2266 u32 serdes_cfg = tr32(MAC_SERDES_CFG);
2267
2268 sg_dig_ctrl |=
2269 SG_DIG_USING_HW_AUTONEG | SG_DIG_SOFT_RESET;
2270 tw32(SG_DIG_CTRL, sg_dig_ctrl);
2271 tw32(MAC_SERDES_CFG, serdes_cfg | (1 << 15));
2272 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002273 return;
Michael Chan51297242007-02-13 12:17:57 -08002274 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002275
Michael Chan60189dd2006-12-17 17:08:07 -08002276 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan60189dd2006-12-17 17:08:07 -08002277 tg3_bmcr_reset(tp);
2278 val = tr32(GRC_MISC_CFG);
2279 tw32_f(GRC_MISC_CFG, val | GRC_MISC_CFG_EPHY_IDDQ);
2280 udelay(40);
2281 return;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002282 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson0e5f7842009-11-02 14:26:38 +00002283 u32 phytest;
2284 if (!tg3_readphy(tp, MII_TG3_FET_TEST, &phytest)) {
2285 u32 phy;
2286
2287 tg3_writephy(tp, MII_ADVERTISE, 0);
2288 tg3_writephy(tp, MII_BMCR,
2289 BMCR_ANENABLE | BMCR_ANRESTART);
2290
2291 tg3_writephy(tp, MII_TG3_FET_TEST,
2292 phytest | MII_TG3_FET_SHADOW_EN);
2293 if (!tg3_readphy(tp, MII_TG3_FET_SHDW_AUXMODE4, &phy)) {
2294 phy |= MII_TG3_FET_SHDW_AUXMODE4_SBPD;
2295 tg3_writephy(tp,
2296 MII_TG3_FET_SHDW_AUXMODE4,
2297 phy);
2298 }
2299 tg3_writephy(tp, MII_TG3_FET_TEST, phytest);
2300 }
2301 return;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002302 } else if (do_low_power) {
Michael Chan715116a2006-09-27 16:09:25 -07002303 tg3_writephy(tp, MII_TG3_EXT_CTRL,
2304 MII_TG3_EXT_CTRL_FORCE_LED_OFF);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002305
2306 tg3_writephy(tp, MII_TG3_AUX_CTRL,
2307 MII_TG3_AUXCTL_SHDWSEL_PWRCTL |
2308 MII_TG3_AUXCTL_PCTL_100TX_LPWR |
2309 MII_TG3_AUXCTL_PCTL_SPR_ISOLATE |
2310 MII_TG3_AUXCTL_PCTL_VREG_11V);
Michael Chan715116a2006-09-27 16:09:25 -07002311 }
Michael Chan3f7045c2006-09-27 16:02:29 -07002312
Michael Chan15c3b692006-03-22 01:06:52 -08002313 /* The PHY should not be powered down on some chips because
2314 * of bugs.
2315 */
2316 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2317 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
2318 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002319 (tp->phy_flags & TG3_PHYFLG_MII_SERDES)))
Michael Chan15c3b692006-03-22 01:06:52 -08002320 return;
Matt Carlsonce057f02007-11-12 21:08:03 -08002321
Matt Carlsonbcb37f62008-11-03 16:52:09 -08002322 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX ||
2323 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5761_AX) {
Matt Carlsonce057f02007-11-12 21:08:03 -08002324 val = tr32(TG3_CPMU_LSPD_1000MB_CLK);
2325 val &= ~CPMU_LSPD_1000MB_MACCLK_MASK;
2326 val |= CPMU_LSPD_1000MB_MACCLK_12_5;
2327 tw32_f(TG3_CPMU_LSPD_1000MB_CLK, val);
2328 }
2329
Michael Chan15c3b692006-03-22 01:06:52 -08002330 tg3_writephy(tp, MII_BMCR, BMCR_PDOWN);
2331}
2332
Matt Carlson3f007892008-11-03 16:51:36 -08002333/* tp->lock is held. */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002334static int tg3_nvram_lock(struct tg3 *tp)
2335{
2336 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2337 int i;
2338
2339 if (tp->nvram_lock_cnt == 0) {
2340 tw32(NVRAM_SWARB, SWARB_REQ_SET1);
2341 for (i = 0; i < 8000; i++) {
2342 if (tr32(NVRAM_SWARB) & SWARB_GNT1)
2343 break;
2344 udelay(20);
2345 }
2346 if (i == 8000) {
2347 tw32(NVRAM_SWARB, SWARB_REQ_CLR1);
2348 return -ENODEV;
2349 }
2350 }
2351 tp->nvram_lock_cnt++;
2352 }
2353 return 0;
2354}
2355
2356/* tp->lock is held. */
2357static void tg3_nvram_unlock(struct tg3 *tp)
2358{
2359 if (tp->tg3_flags & TG3_FLAG_NVRAM) {
2360 if (tp->nvram_lock_cnt > 0)
2361 tp->nvram_lock_cnt--;
2362 if (tp->nvram_lock_cnt == 0)
2363 tw32_f(NVRAM_SWARB, SWARB_REQ_CLR1);
2364 }
2365}
2366
2367/* tp->lock is held. */
2368static void tg3_enable_nvram_access(struct tg3 *tp)
2369{
2370 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002371 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002372 u32 nvaccess = tr32(NVRAM_ACCESS);
2373
2374 tw32(NVRAM_ACCESS, nvaccess | ACCESS_ENABLE);
2375 }
2376}
2377
2378/* tp->lock is held. */
2379static void tg3_disable_nvram_access(struct tg3 *tp)
2380{
2381 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +00002382 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM)) {
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002383 u32 nvaccess = tr32(NVRAM_ACCESS);
2384
2385 tw32(NVRAM_ACCESS, nvaccess & ~ACCESS_ENABLE);
2386 }
2387}
2388
2389static int tg3_nvram_read_using_eeprom(struct tg3 *tp,
2390 u32 offset, u32 *val)
2391{
2392 u32 tmp;
2393 int i;
2394
2395 if (offset > EEPROM_ADDR_ADDR_MASK || (offset % 4) != 0)
2396 return -EINVAL;
2397
2398 tmp = tr32(GRC_EEPROM_ADDR) & ~(EEPROM_ADDR_ADDR_MASK |
2399 EEPROM_ADDR_DEVID_MASK |
2400 EEPROM_ADDR_READ);
2401 tw32(GRC_EEPROM_ADDR,
2402 tmp |
2403 (0 << EEPROM_ADDR_DEVID_SHIFT) |
2404 ((offset << EEPROM_ADDR_ADDR_SHIFT) &
2405 EEPROM_ADDR_ADDR_MASK) |
2406 EEPROM_ADDR_READ | EEPROM_ADDR_START);
2407
2408 for (i = 0; i < 1000; i++) {
2409 tmp = tr32(GRC_EEPROM_ADDR);
2410
2411 if (tmp & EEPROM_ADDR_COMPLETE)
2412 break;
2413 msleep(1);
2414 }
2415 if (!(tmp & EEPROM_ADDR_COMPLETE))
2416 return -EBUSY;
2417
Matt Carlson62cedd12009-04-20 14:52:29 -07002418 tmp = tr32(GRC_EEPROM_DATA);
2419
2420 /*
2421 * The data will always be opposite the native endian
2422 * format. Perform a blind byteswap to compensate.
2423 */
2424 *val = swab32(tmp);
2425
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002426 return 0;
2427}
2428
2429#define NVRAM_CMD_TIMEOUT 10000
2430
2431static int tg3_nvram_exec_cmd(struct tg3 *tp, u32 nvram_cmd)
2432{
2433 int i;
2434
2435 tw32(NVRAM_CMD, nvram_cmd);
2436 for (i = 0; i < NVRAM_CMD_TIMEOUT; i++) {
2437 udelay(10);
2438 if (tr32(NVRAM_CMD) & NVRAM_CMD_DONE) {
2439 udelay(10);
2440 break;
2441 }
2442 }
2443
2444 if (i == NVRAM_CMD_TIMEOUT)
2445 return -EBUSY;
2446
2447 return 0;
2448}
2449
2450static u32 tg3_nvram_phys_addr(struct tg3 *tp, u32 addr)
2451{
2452 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2453 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2454 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2455 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2456 (tp->nvram_jedecnum == JEDEC_ATMEL))
2457
2458 addr = ((addr / tp->nvram_pagesize) <<
2459 ATMEL_AT45DB0X1B_PAGE_POS) +
2460 (addr % tp->nvram_pagesize);
2461
2462 return addr;
2463}
2464
2465static u32 tg3_nvram_logical_addr(struct tg3 *tp, u32 addr)
2466{
2467 if ((tp->tg3_flags & TG3_FLAG_NVRAM) &&
2468 (tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) &&
2469 (tp->tg3_flags2 & TG3_FLG2_FLASH) &&
2470 !(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM_ADDR_TRANS) &&
2471 (tp->nvram_jedecnum == JEDEC_ATMEL))
2472
2473 addr = ((addr >> ATMEL_AT45DB0X1B_PAGE_POS) *
2474 tp->nvram_pagesize) +
2475 (addr & ((1 << ATMEL_AT45DB0X1B_PAGE_POS) - 1));
2476
2477 return addr;
2478}
2479
Matt Carlsone4f34112009-02-25 14:25:00 +00002480/* NOTE: Data read in from NVRAM is byteswapped according to
2481 * the byteswapping settings for all other register accesses.
2482 * tg3 devices are BE devices, so on a BE machine, the data
2483 * returned will be exactly as it is seen in NVRAM. On a LE
2484 * machine, the 32-bit value will be byteswapped.
2485 */
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002486static int tg3_nvram_read(struct tg3 *tp, u32 offset, u32 *val)
2487{
2488 int ret;
2489
2490 if (!(tp->tg3_flags & TG3_FLAG_NVRAM))
2491 return tg3_nvram_read_using_eeprom(tp, offset, val);
2492
2493 offset = tg3_nvram_phys_addr(tp, offset);
2494
2495 if (offset > NVRAM_ADDR_MSK)
2496 return -EINVAL;
2497
2498 ret = tg3_nvram_lock(tp);
2499 if (ret)
2500 return ret;
2501
2502 tg3_enable_nvram_access(tp);
2503
2504 tw32(NVRAM_ADDR, offset);
2505 ret = tg3_nvram_exec_cmd(tp, NVRAM_CMD_RD | NVRAM_CMD_GO |
2506 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_DONE);
2507
2508 if (ret == 0)
Matt Carlsone4f34112009-02-25 14:25:00 +00002509 *val = tr32(NVRAM_RDDATA);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002510
2511 tg3_disable_nvram_access(tp);
2512
2513 tg3_nvram_unlock(tp);
2514
2515 return ret;
2516}
2517
Matt Carlsona9dc5292009-02-25 14:25:30 +00002518/* Ensures NVRAM data is in bytestream format. */
2519static int tg3_nvram_read_be32(struct tg3 *tp, u32 offset, __be32 *val)
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002520{
2521 u32 v;
Matt Carlsona9dc5292009-02-25 14:25:30 +00002522 int res = tg3_nvram_read(tp, offset, &v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002523 if (!res)
Matt Carlsona9dc5292009-02-25 14:25:30 +00002524 *val = cpu_to_be32(v);
Matt Carlsonffbcfed2009-02-25 14:24:28 +00002525 return res;
2526}
2527
2528/* tp->lock is held. */
Matt Carlson3f007892008-11-03 16:51:36 -08002529static void __tg3_set_mac_addr(struct tg3 *tp, int skip_mac_1)
2530{
2531 u32 addr_high, addr_low;
2532 int i;
2533
2534 addr_high = ((tp->dev->dev_addr[0] << 8) |
2535 tp->dev->dev_addr[1]);
2536 addr_low = ((tp->dev->dev_addr[2] << 24) |
2537 (tp->dev->dev_addr[3] << 16) |
2538 (tp->dev->dev_addr[4] << 8) |
2539 (tp->dev->dev_addr[5] << 0));
2540 for (i = 0; i < 4; i++) {
2541 if (i == 1 && skip_mac_1)
2542 continue;
2543 tw32(MAC_ADDR_0_HIGH + (i * 8), addr_high);
2544 tw32(MAC_ADDR_0_LOW + (i * 8), addr_low);
2545 }
2546
2547 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
2548 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
2549 for (i = 0; i < 12; i++) {
2550 tw32(MAC_EXTADDR_0_HIGH + (i * 8), addr_high);
2551 tw32(MAC_EXTADDR_0_LOW + (i * 8), addr_low);
2552 }
2553 }
2554
2555 addr_high = (tp->dev->dev_addr[0] +
2556 tp->dev->dev_addr[1] +
2557 tp->dev->dev_addr[2] +
2558 tp->dev->dev_addr[3] +
2559 tp->dev->dev_addr[4] +
2560 tp->dev->dev_addr[5]) &
2561 TX_BACKOFF_SEED_MASK;
2562 tw32(MAC_TX_BACKOFF_SEED, addr_high);
2563}
2564
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002565static void tg3_enable_register_access(struct tg3 *tp)
2566{
2567 /*
2568 * Make sure register accesses (indirect or otherwise) will function
2569 * correctly.
2570 */
2571 pci_write_config_dword(tp->pdev,
2572 TG3PCI_MISC_HOST_CTRL, tp->misc_host_ctrl);
2573}
2574
2575static int tg3_power_up(struct tg3 *tp)
2576{
2577 tg3_enable_register_access(tp);
2578
2579 pci_set_power_state(tp->pdev, PCI_D0);
2580
2581 /* Switch out of Vaux if it is a NIC */
2582 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
2583 tw32_wait_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl, 100);
2584
2585 return 0;
2586}
2587
2588static int tg3_power_down_prepare(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
2590 u32 misc_host_ctrl;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002591 bool device_should_wake, do_low_power;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002593 tg3_enable_register_access(tp);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08002594
2595 /* Restore the CLKREQ setting. */
2596 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
2597 u16 lnkctl;
2598
2599 pci_read_config_word(tp->pdev,
2600 tp->pcie_cap + PCI_EXP_LNKCTL,
2601 &lnkctl);
2602 lnkctl |= PCI_EXP_LNKCTL_CLKREQ_EN;
2603 pci_write_config_word(tp->pdev,
2604 tp->pcie_cap + PCI_EXP_LNKCTL,
2605 lnkctl);
2606 }
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
2609 tw32(TG3PCI_MISC_HOST_CTRL,
2610 misc_host_ctrl | MISC_HOST_CTRL_MASK_PCI_INT);
2611
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002612 device_should_wake = device_may_wakeup(&tp->pdev->dev) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002613 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2614
Matt Carlsondd477002008-05-25 23:45:58 -07002615 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002616 do_low_power = false;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002617 if ((tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) &&
Matt Carlson80096062010-08-02 11:26:06 +00002618 !(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002619 struct phy_device *phydev;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002620 u32 phyid, advertising;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002621
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00002622 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002623
Matt Carlson80096062010-08-02 11:26:06 +00002624 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002625
2626 tp->link_config.orig_speed = phydev->speed;
2627 tp->link_config.orig_duplex = phydev->duplex;
2628 tp->link_config.orig_autoneg = phydev->autoneg;
2629 tp->link_config.orig_advertising = phydev->advertising;
2630
2631 advertising = ADVERTISED_TP |
2632 ADVERTISED_Pause |
2633 ADVERTISED_Autoneg |
2634 ADVERTISED_10baseT_Half;
2635
2636 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002637 device_should_wake) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002638 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2639 advertising |=
2640 ADVERTISED_100baseT_Half |
2641 ADVERTISED_100baseT_Full |
2642 ADVERTISED_10baseT_Full;
2643 else
2644 advertising |= ADVERTISED_10baseT_Full;
2645 }
2646
2647 phydev->advertising = advertising;
2648
2649 phy_start_aneg(phydev);
Matt Carlson0a459aa2008-11-03 16:54:15 -08002650
2651 phyid = phydev->drv->phy_id & phydev->drv->phy_id_mask;
Matt Carlson6a443a02010-02-17 15:17:04 +00002652 if (phyid != PHY_ID_BCMAC131) {
2653 phyid &= PHY_BCM_OUI_MASK;
2654 if (phyid == PHY_BCM_OUI_1 ||
2655 phyid == PHY_BCM_OUI_2 ||
2656 phyid == PHY_BCM_OUI_3)
Matt Carlson0a459aa2008-11-03 16:54:15 -08002657 do_low_power = true;
2658 }
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07002659 }
Matt Carlsondd477002008-05-25 23:45:58 -07002660 } else {
Matt Carlson20232762008-12-21 20:18:56 -08002661 do_low_power = true;
Matt Carlson0a459aa2008-11-03 16:54:15 -08002662
Matt Carlson80096062010-08-02 11:26:06 +00002663 if (!(tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
2664 tp->phy_flags |= TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07002665 tp->link_config.orig_speed = tp->link_config.speed;
2666 tp->link_config.orig_duplex = tp->link_config.duplex;
2667 tp->link_config.orig_autoneg = tp->link_config.autoneg;
2668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002670 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Matt Carlsondd477002008-05-25 23:45:58 -07002671 tp->link_config.speed = SPEED_10;
2672 tp->link_config.duplex = DUPLEX_HALF;
2673 tp->link_config.autoneg = AUTONEG_ENABLE;
2674 tg3_setup_phy(tp, 0);
2675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 }
2677
Michael Chanb5d37722006-09-27 16:06:21 -07002678 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
2679 u32 val;
2680
2681 val = tr32(GRC_VCPU_EXT_CTRL);
2682 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_DISABLE_WOL);
2683 } else if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan6921d202005-12-13 21:15:53 -08002684 int i;
2685 u32 val;
2686
2687 for (i = 0; i < 200; i++) {
2688 tg3_read_mem(tp, NIC_SRAM_FW_ASF_STATUS_MBOX, &val);
2689 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
2690 break;
2691 msleep(1);
2692 }
2693 }
Gary Zambranoa85feb82007-05-05 11:52:19 -07002694 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
2695 tg3_write_mem(tp, NIC_SRAM_WOL_MBOX, WOL_SIGNATURE |
2696 WOL_DRV_STATE_SHUTDOWN |
2697 WOL_DRV_WOL |
2698 WOL_SET_MAGIC_PKT);
Michael Chan6921d202005-12-13 21:15:53 -08002699
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002700 if (device_should_wake) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 u32 mac_mode;
2702
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002703 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Matt Carlson0a459aa2008-11-03 16:54:15 -08002704 if (do_low_power) {
Matt Carlsondd477002008-05-25 23:45:58 -07002705 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x5a);
2706 udelay(40);
2707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002709 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan3f7045c2006-09-27 16:02:29 -07002710 mac_mode = MAC_MODE_PORT_MODE_GMII;
2711 else
2712 mac_mode = MAC_MODE_PORT_MODE_MII;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07002714 mac_mode |= tp->mac_mode & MAC_MODE_LINK_POLARITY;
2715 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
2716 ASIC_REV_5700) {
2717 u32 speed = (tp->tg3_flags &
2718 TG3_FLAG_WOL_SPEED_100MB) ?
2719 SPEED_100 : SPEED_10;
2720 if (tg3_5700_link_polarity(tp, speed))
2721 mac_mode |= MAC_MODE_LINK_POLARITY;
2722 else
2723 mac_mode &= ~MAC_MODE_LINK_POLARITY;
2724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 } else {
2726 mac_mode = MAC_MODE_PORT_MODE_TBI;
2727 }
2728
John W. Linvillecbf46852005-04-21 17:01:29 -07002729 if (!(tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 tw32(MAC_LED_CTRL, tp->led_ctrl);
2731
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002732 mac_mode |= MAC_MODE_MAGIC_PKT_ENABLE;
2733 if (((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
2734 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) &&
2735 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
2736 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)))
2737 mac_mode |= MAC_MODE_KEEP_FRAME_IN_WOL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Matt Carlsond2394e6b2010-11-24 08:31:47 +00002739 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
2740 mac_mode |= MAC_MODE_APE_TX_EN |
2741 MAC_MODE_APE_RX_EN |
2742 MAC_MODE_TDE_ENABLE;
Matt Carlson3bda1252008-08-15 14:08:22 -07002743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 tw32_f(MAC_MODE, mac_mode);
2745 udelay(100);
2746
2747 tw32_f(MAC_RX_MODE, RX_MODE_ENABLE);
2748 udelay(10);
2749 }
2750
2751 if (!(tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB) &&
2752 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2753 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
2754 u32 base_val;
2755
2756 base_val = tp->pci_clock_ctrl;
2757 base_val |= (CLOCK_CTRL_RXCLK_DISABLE |
2758 CLOCK_CTRL_TXCLK_DISABLE);
2759
Michael Chanb401e9e2005-12-19 16:27:04 -08002760 tw32_wait_f(TG3PCI_CLOCK_CTRL, base_val | CLOCK_CTRL_ALTCLK |
2761 CLOCK_CTRL_PWRDOWN_PLL133, 40);
Michael Chand7b0a852007-02-13 12:17:38 -08002762 } else if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
Matt Carlson795d01c2007-10-07 23:28:17 -07002763 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) ||
Michael Chand7b0a852007-02-13 12:17:38 -08002764 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)) {
Michael Chan4cf78e42005-07-25 12:29:19 -07002765 /* do nothing */
Michael Chan85e94ce2005-04-21 17:05:28 -07002766 } else if (!((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF))) {
2768 u32 newbits1, newbits2;
2769
2770 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2771 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2772 newbits1 = (CLOCK_CTRL_RXCLK_DISABLE |
2773 CLOCK_CTRL_TXCLK_DISABLE |
2774 CLOCK_CTRL_ALTCLK);
2775 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2776 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
2777 newbits1 = CLOCK_CTRL_625_CORE;
2778 newbits2 = newbits1 | CLOCK_CTRL_ALTCLK;
2779 } else {
2780 newbits1 = CLOCK_CTRL_ALTCLK;
2781 newbits2 = newbits1 | CLOCK_CTRL_44MHZ_CORE;
2782 }
2783
Michael Chanb401e9e2005-12-19 16:27:04 -08002784 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits1,
2785 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Michael Chanb401e9e2005-12-19 16:27:04 -08002787 tw32_wait_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl | newbits2,
2788 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
2790 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
2791 u32 newbits3;
2792
2793 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
2794 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
2795 newbits3 = (CLOCK_CTRL_RXCLK_DISABLE |
2796 CLOCK_CTRL_TXCLK_DISABLE |
2797 CLOCK_CTRL_44MHZ_CORE);
2798 } else {
2799 newbits3 = CLOCK_CTRL_44MHZ_CORE;
2800 }
2801
Michael Chanb401e9e2005-12-19 16:27:04 -08002802 tw32_wait_f(TG3PCI_CLOCK_CTRL,
2803 tp->pci_clock_ctrl | newbits3, 40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
2805 }
2806
Matt Carlson05ac4cb2008-11-03 16:53:46 -08002807 if (!(device_should_wake) &&
Matt Carlson22435842008-11-21 17:21:13 -08002808 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
Matt Carlson0a459aa2008-11-03 16:54:15 -08002809 tg3_power_down_phy(tp, do_low_power);
Michael Chan6921d202005-12-13 21:15:53 -08002810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 tg3_frob_aux_power(tp);
2812
2813 /* Workaround for unstable PLL clock */
2814 if ((GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX) ||
2815 (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX)) {
2816 u32 val = tr32(0x7d00);
2817
2818 val &= ~((1 << 16) | (1 << 4) | (1 << 2) | (1 << 1) | 1);
2819 tw32(0x7d00, val);
Michael Chan6921d202005-12-13 21:15:53 -08002820 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chanec41c7d2006-01-17 02:40:55 -08002821 int err;
2822
2823 err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 tg3_halt_cpu(tp, RX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -08002825 if (!err)
2826 tg3_nvram_unlock(tp);
Michael Chan6921d202005-12-13 21:15:53 -08002827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 }
2829
Michael Chanbbadf502006-04-06 21:46:34 -07002830 tg3_write_sig_post_reset(tp, RESET_KIND_SHUTDOWN);
2831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return 0;
2833}
2834
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00002835static void tg3_power_down(struct tg3 *tp)
2836{
2837 tg3_power_down_prepare(tp);
2838
2839 pci_wake_from_d3(tp->pdev, tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
2840 pci_set_power_state(tp->pdev, PCI_D3hot);
2841}
2842
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843static void tg3_aux_stat_to_speed_duplex(struct tg3 *tp, u32 val, u16 *speed, u8 *duplex)
2844{
2845 switch (val & MII_TG3_AUX_STAT_SPDMASK) {
2846 case MII_TG3_AUX_STAT_10HALF:
2847 *speed = SPEED_10;
2848 *duplex = DUPLEX_HALF;
2849 break;
2850
2851 case MII_TG3_AUX_STAT_10FULL:
2852 *speed = SPEED_10;
2853 *duplex = DUPLEX_FULL;
2854 break;
2855
2856 case MII_TG3_AUX_STAT_100HALF:
2857 *speed = SPEED_100;
2858 *duplex = DUPLEX_HALF;
2859 break;
2860
2861 case MII_TG3_AUX_STAT_100FULL:
2862 *speed = SPEED_100;
2863 *duplex = DUPLEX_FULL;
2864 break;
2865
2866 case MII_TG3_AUX_STAT_1000HALF:
2867 *speed = SPEED_1000;
2868 *duplex = DUPLEX_HALF;
2869 break;
2870
2871 case MII_TG3_AUX_STAT_1000FULL:
2872 *speed = SPEED_1000;
2873 *duplex = DUPLEX_FULL;
2874 break;
2875
2876 default:
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002877 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Michael Chan715116a2006-09-27 16:09:25 -07002878 *speed = (val & MII_TG3_AUX_STAT_100) ? SPEED_100 :
2879 SPEED_10;
2880 *duplex = (val & MII_TG3_AUX_STAT_FULL) ? DUPLEX_FULL :
2881 DUPLEX_HALF;
2882 break;
2883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 *speed = SPEED_INVALID;
2885 *duplex = DUPLEX_INVALID;
2886 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07002887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888}
2889
2890static void tg3_phy_copper_begin(struct tg3 *tp)
2891{
2892 u32 new_adv;
2893 int i;
2894
Matt Carlson80096062010-08-02 11:26:06 +00002895 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 /* Entering low power mode. Disable gigabit and
2897 * 100baseT advertisements.
2898 */
2899 tg3_writephy(tp, MII_TG3_CTRL, 0);
2900
2901 new_adv = (ADVERTISE_10HALF | ADVERTISE_10FULL |
2902 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
2903 if (tp->tg3_flags & TG3_FLAG_WOL_SPEED_100MB)
2904 new_adv |= (ADVERTISE_100HALF | ADVERTISE_100FULL);
2905
2906 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2907 } else if (tp->link_config.speed == SPEED_INVALID) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002908 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 tp->link_config.advertising &=
2910 ~(ADVERTISED_1000baseT_Half |
2911 ADVERTISED_1000baseT_Full);
2912
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002913 new_adv = ADVERTISE_CSMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 if (tp->link_config.advertising & ADVERTISED_10baseT_Half)
2915 new_adv |= ADVERTISE_10HALF;
2916 if (tp->link_config.advertising & ADVERTISED_10baseT_Full)
2917 new_adv |= ADVERTISE_10FULL;
2918 if (tp->link_config.advertising & ADVERTISED_100baseT_Half)
2919 new_adv |= ADVERTISE_100HALF;
2920 if (tp->link_config.advertising & ADVERTISED_100baseT_Full)
2921 new_adv |= ADVERTISE_100FULL;
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002922
2923 new_adv |= tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2926
2927 if (tp->link_config.advertising &
2928 (ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) {
2929 new_adv = 0;
2930 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
2931 new_adv |= MII_TG3_CTRL_ADV_1000_HALF;
2932 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
2933 new_adv |= MII_TG3_CTRL_ADV_1000_FULL;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00002934 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2936 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0))
2937 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2938 MII_TG3_CTRL_ENABLE_AS_MASTER);
2939 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
2940 } else {
2941 tg3_writephy(tp, MII_TG3_CTRL, 0);
2942 }
2943 } else {
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002944 new_adv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
2945 new_adv |= ADVERTISE_CSMA;
2946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 /* Asking for a specific link mode. */
2948 if (tp->link_config.speed == SPEED_1000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 tg3_writephy(tp, MII_ADVERTISE, new_adv);
2950
2951 if (tp->link_config.duplex == DUPLEX_FULL)
2952 new_adv = MII_TG3_CTRL_ADV_1000_FULL;
2953 else
2954 new_adv = MII_TG3_CTRL_ADV_1000_HALF;
2955 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
2956 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
2957 new_adv |= (MII_TG3_CTRL_AS_MASTER |
2958 MII_TG3_CTRL_ENABLE_AS_MASTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 if (tp->link_config.speed == SPEED_100) {
2961 if (tp->link_config.duplex == DUPLEX_FULL)
2962 new_adv |= ADVERTISE_100FULL;
2963 else
2964 new_adv |= ADVERTISE_100HALF;
2965 } else {
2966 if (tp->link_config.duplex == DUPLEX_FULL)
2967 new_adv |= ADVERTISE_10FULL;
2968 else
2969 new_adv |= ADVERTISE_10HALF;
2970 }
2971 tg3_writephy(tp, MII_ADVERTISE, new_adv);
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002972
2973 new_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
Matt Carlsonba4d07a2007-12-20 20:08:00 -08002975
2976 tg3_writephy(tp, MII_TG3_CTRL, new_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 }
2978
Matt Carlson52b02d02010-10-14 10:37:41 +00002979 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
Matt Carlsona6b68da2010-12-06 08:28:52 +00002980 u32 val;
Matt Carlson52b02d02010-10-14 10:37:41 +00002981
2982 tw32(TG3_CPMU_EEE_MODE,
2983 tr32(TG3_CPMU_EEE_MODE) & ~TG3_CPMU_EEEMD_LPI_ENABLE);
2984
2985 /* Enable SM_DSP clock and tx 6dB coding. */
2986 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
2987 MII_TG3_AUXCTL_ACTL_SMDSP_ENA |
2988 MII_TG3_AUXCTL_ACTL_TX_6DB;
2989 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
2990
Matt Carlson21a00ab2011-01-25 15:58:55 +00002991 switch (GET_ASIC_REV(tp->pci_chip_rev_id)) {
2992 case ASIC_REV_5717:
2993 case ASIC_REV_57765:
2994 if (!tg3_phydsp_read(tp, MII_TG3_DSP_CH34TP2, &val))
2995 tg3_phydsp_write(tp, MII_TG3_DSP_CH34TP2, val |
2996 MII_TG3_DSP_CH34TP2_HIBW01);
2997 /* Fall through */
2998 case ASIC_REV_5719:
2999 val = MII_TG3_DSP_TAP26_ALNOKO |
3000 MII_TG3_DSP_TAP26_RMRXSTO |
3001 MII_TG3_DSP_TAP26_OPCSINPT;
3002 tg3_phydsp_write(tp, MII_TG3_DSP_TAP26, val);
3003 }
Matt Carlson52b02d02010-10-14 10:37:41 +00003004
Matt Carlsona6b68da2010-12-06 08:28:52 +00003005 val = 0;
Matt Carlson52b02d02010-10-14 10:37:41 +00003006 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3007 /* Advertise 100-BaseTX EEE ability */
3008 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003009 ADVERTISED_100baseT_Full)
3010 val |= MDIO_AN_EEE_ADV_100TX;
Matt Carlson52b02d02010-10-14 10:37:41 +00003011 /* Advertise 1000-BaseT EEE ability */
3012 if (tp->link_config.advertising &
Matt Carlson3110f5f52010-12-06 08:28:50 +00003013 ADVERTISED_1000baseT_Full)
3014 val |= MDIO_AN_EEE_ADV_1000T;
Matt Carlson52b02d02010-10-14 10:37:41 +00003015 }
Matt Carlson3110f5f52010-12-06 08:28:50 +00003016 tg3_phy_cl45_write(tp, MDIO_MMD_AN, MDIO_AN_EEE_ADV, val);
Matt Carlson52b02d02010-10-14 10:37:41 +00003017
3018 /* Turn off SM_DSP clock. */
3019 val = MII_TG3_AUXCTL_SHDWSEL_AUXCTL |
3020 MII_TG3_AUXCTL_ACTL_TX_6DB;
3021 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3022 }
3023
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 if (tp->link_config.autoneg == AUTONEG_DISABLE &&
3025 tp->link_config.speed != SPEED_INVALID) {
3026 u32 bmcr, orig_bmcr;
3027
3028 tp->link_config.active_speed = tp->link_config.speed;
3029 tp->link_config.active_duplex = tp->link_config.duplex;
3030
3031 bmcr = 0;
3032 switch (tp->link_config.speed) {
3033 default:
3034 case SPEED_10:
3035 break;
3036
3037 case SPEED_100:
3038 bmcr |= BMCR_SPEED100;
3039 break;
3040
3041 case SPEED_1000:
3042 bmcr |= TG3_BMCR_SPEED1000;
3043 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 if (tp->link_config.duplex == DUPLEX_FULL)
3047 bmcr |= BMCR_FULLDPLX;
3048
3049 if (!tg3_readphy(tp, MII_BMCR, &orig_bmcr) &&
3050 (bmcr != orig_bmcr)) {
3051 tg3_writephy(tp, MII_BMCR, BMCR_LOOPBACK);
3052 for (i = 0; i < 1500; i++) {
3053 u32 tmp;
3054
3055 udelay(10);
3056 if (tg3_readphy(tp, MII_BMSR, &tmp) ||
3057 tg3_readphy(tp, MII_BMSR, &tmp))
3058 continue;
3059 if (!(tmp & BMSR_LSTATUS)) {
3060 udelay(40);
3061 break;
3062 }
3063 }
3064 tg3_writephy(tp, MII_BMCR, bmcr);
3065 udelay(40);
3066 }
3067 } else {
3068 tg3_writephy(tp, MII_BMCR,
3069 BMCR_ANENABLE | BMCR_ANRESTART);
3070 }
3071}
3072
3073static int tg3_init_5401phy_dsp(struct tg3 *tp)
3074{
3075 int err;
3076
3077 /* Turn off tap power management. */
3078 /* Set Extended packet length bit */
3079 err = tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4c20);
3080
Matt Carlson6ee7c0a2010-08-02 11:26:04 +00003081 err |= tg3_phydsp_write(tp, 0x0012, 0x1804);
3082 err |= tg3_phydsp_write(tp, 0x0013, 0x1204);
3083 err |= tg3_phydsp_write(tp, 0x8006, 0x0132);
3084 err |= tg3_phydsp_write(tp, 0x8006, 0x0232);
3085 err |= tg3_phydsp_write(tp, 0x201f, 0x0a20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
3087 udelay(40);
3088
3089 return err;
3090}
3091
Michael Chan3600d912006-12-07 00:21:48 -08003092static int tg3_copper_is_advertising_all(struct tg3 *tp, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
Michael Chan3600d912006-12-07 00:21:48 -08003094 u32 adv_reg, all_mask = 0;
3095
3096 if (mask & ADVERTISED_10baseT_Half)
3097 all_mask |= ADVERTISE_10HALF;
3098 if (mask & ADVERTISED_10baseT_Full)
3099 all_mask |= ADVERTISE_10FULL;
3100 if (mask & ADVERTISED_100baseT_Half)
3101 all_mask |= ADVERTISE_100HALF;
3102 if (mask & ADVERTISED_100baseT_Full)
3103 all_mask |= ADVERTISE_100FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 if (tg3_readphy(tp, MII_ADVERTISE, &adv_reg))
3106 return 0;
3107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 if ((adv_reg & all_mask) != all_mask)
3109 return 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003110 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 u32 tg3_ctrl;
3112
Michael Chan3600d912006-12-07 00:21:48 -08003113 all_mask = 0;
3114 if (mask & ADVERTISED_1000baseT_Half)
3115 all_mask |= ADVERTISE_1000HALF;
3116 if (mask & ADVERTISED_1000baseT_Full)
3117 all_mask |= ADVERTISE_1000FULL;
3118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 if (tg3_readphy(tp, MII_TG3_CTRL, &tg3_ctrl))
3120 return 0;
3121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 if ((tg3_ctrl & all_mask) != all_mask)
3123 return 0;
3124 }
3125 return 1;
3126}
3127
Matt Carlsonef167e22007-12-20 20:10:01 -08003128static int tg3_adv_1000T_flowctrl_ok(struct tg3 *tp, u32 *lcladv, u32 *rmtadv)
3129{
3130 u32 curadv, reqadv;
3131
3132 if (tg3_readphy(tp, MII_ADVERTISE, lcladv))
3133 return 1;
3134
3135 curadv = *lcladv & (ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
3136 reqadv = tg3_advert_flowctrl_1000T(tp->link_config.flowctrl);
3137
3138 if (tp->link_config.active_duplex == DUPLEX_FULL) {
3139 if (curadv != reqadv)
3140 return 0;
3141
3142 if (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG)
3143 tg3_readphy(tp, MII_LPA, rmtadv);
3144 } else {
3145 /* Reprogram the advertisement register, even if it
3146 * does not affect the current link. If the link
3147 * gets renegotiated in the future, we can save an
3148 * additional renegotiation cycle by advertising
3149 * it correctly in the first place.
3150 */
3151 if (curadv != reqadv) {
3152 *lcladv &= ~(ADVERTISE_PAUSE_CAP |
3153 ADVERTISE_PAUSE_ASYM);
3154 tg3_writephy(tp, MII_ADVERTISE, *lcladv | reqadv);
3155 }
3156 }
3157
3158 return 1;
3159}
3160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161static int tg3_setup_copper_phy(struct tg3 *tp, int force_reset)
3162{
3163 int current_link_up;
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003164 u32 bmsr, val;
Matt Carlsonef167e22007-12-20 20:10:01 -08003165 u32 lcl_adv, rmt_adv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 u16 current_speed;
3167 u8 current_duplex;
3168 int i, err;
3169
3170 tw32(MAC_EVENT, 0);
3171
3172 tw32_f(MAC_STATUS,
3173 (MAC_STATUS_SYNC_CHANGED |
3174 MAC_STATUS_CFG_CHANGED |
3175 MAC_STATUS_MI_COMPLETION |
3176 MAC_STATUS_LNKSTATE_CHANGED));
3177 udelay(40);
3178
Matt Carlson8ef21422008-05-02 16:47:53 -07003179 if ((tp->mi_mode & MAC_MI_MODE_AUTO_POLL) != 0) {
3180 tw32_f(MAC_MI_MODE,
3181 (tp->mi_mode & ~MAC_MI_MODE_AUTO_POLL));
3182 udelay(80);
3183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
3185 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x02);
3186
3187 /* Some third-party PHYs need to be reset on link going
3188 * down.
3189 */
3190 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
3191 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
3192 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
3193 netif_carrier_ok(tp->dev)) {
3194 tg3_readphy(tp, MII_BMSR, &bmsr);
3195 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3196 !(bmsr & BMSR_LSTATUS))
3197 force_reset = 1;
3198 }
3199 if (force_reset)
3200 tg3_phy_reset(tp);
3201
Matt Carlson79eb6902010-02-17 15:17:03 +00003202 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 tg3_readphy(tp, MII_BMSR, &bmsr);
3204 if (tg3_readphy(tp, MII_BMSR, &bmsr) ||
3205 !(tp->tg3_flags & TG3_FLAG_INIT_COMPLETE))
3206 bmsr = 0;
3207
3208 if (!(bmsr & BMSR_LSTATUS)) {
3209 err = tg3_init_5401phy_dsp(tp);
3210 if (err)
3211 return err;
3212
3213 tg3_readphy(tp, MII_BMSR, &bmsr);
3214 for (i = 0; i < 1000; i++) {
3215 udelay(10);
3216 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3217 (bmsr & BMSR_LSTATUS)) {
3218 udelay(40);
3219 break;
3220 }
3221 }
3222
Matt Carlson79eb6902010-02-17 15:17:03 +00003223 if ((tp->phy_id & TG3_PHY_ID_REV_MASK) ==
3224 TG3_PHY_REV_BCM5401_B0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 !(bmsr & BMSR_LSTATUS) &&
3226 tp->link_config.active_speed == SPEED_1000) {
3227 err = tg3_phy_reset(tp);
3228 if (!err)
3229 err = tg3_init_5401phy_dsp(tp);
3230 if (err)
3231 return err;
3232 }
3233 }
3234 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
3235 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0) {
3236 /* 5701 {A0,B0} CRC bug workaround */
3237 tg3_writephy(tp, 0x15, 0x0a75);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00003238 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
3239 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8d68);
3240 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x8c68);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 }
3242
3243 /* Clear pending interrupts... */
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003244 tg3_readphy(tp, MII_TG3_ISTAT, &val);
3245 tg3_readphy(tp, MII_TG3_ISTAT, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003247 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 tg3_writephy(tp, MII_TG3_IMASK, ~MII_TG3_INT_LINKCHG);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003249 else if (!(tp->phy_flags & TG3_PHYFLG_IS_FET))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 tg3_writephy(tp, MII_TG3_IMASK, ~0);
3251
3252 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
3253 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
3254 if (tp->led_ctrl == LED_CTRL_MODE_PHY_1)
3255 tg3_writephy(tp, MII_TG3_EXT_CTRL,
3256 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
3257 else
3258 tg3_writephy(tp, MII_TG3_EXT_CTRL, 0);
3259 }
3260
3261 current_link_up = 0;
3262 current_speed = SPEED_INVALID;
3263 current_duplex = DUPLEX_INVALID;
3264
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003265 if (tp->phy_flags & TG3_PHYFLG_CAPACITIVE_COUPLING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 tg3_writephy(tp, MII_TG3_AUX_CTRL, 0x4007);
3267 tg3_readphy(tp, MII_TG3_AUX_CTRL, &val);
3268 if (!(val & (1 << 10))) {
3269 val |= (1 << 10);
3270 tg3_writephy(tp, MII_TG3_AUX_CTRL, val);
3271 goto relink;
3272 }
3273 }
3274
3275 bmsr = 0;
3276 for (i = 0; i < 100; i++) {
3277 tg3_readphy(tp, MII_BMSR, &bmsr);
3278 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3279 (bmsr & BMSR_LSTATUS))
3280 break;
3281 udelay(40);
3282 }
3283
3284 if (bmsr & BMSR_LSTATUS) {
3285 u32 aux_stat, bmcr;
3286
3287 tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat);
3288 for (i = 0; i < 2000; i++) {
3289 udelay(10);
3290 if (!tg3_readphy(tp, MII_TG3_AUX_STAT, &aux_stat) &&
3291 aux_stat)
3292 break;
3293 }
3294
3295 tg3_aux_stat_to_speed_duplex(tp, aux_stat,
3296 &current_speed,
3297 &current_duplex);
3298
3299 bmcr = 0;
3300 for (i = 0; i < 200; i++) {
3301 tg3_readphy(tp, MII_BMCR, &bmcr);
3302 if (tg3_readphy(tp, MII_BMCR, &bmcr))
3303 continue;
3304 if (bmcr && bmcr != 0x7fff)
3305 break;
3306 udelay(10);
3307 }
3308
Matt Carlsonef167e22007-12-20 20:10:01 -08003309 lcl_adv = 0;
3310 rmt_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Matt Carlsonef167e22007-12-20 20:10:01 -08003312 tp->link_config.active_speed = current_speed;
3313 tp->link_config.active_duplex = current_duplex;
3314
3315 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
3316 if ((bmcr & BMCR_ANENABLE) &&
3317 tg3_copper_is_advertising_all(tp,
3318 tp->link_config.advertising)) {
3319 if (tg3_adv_1000T_flowctrl_ok(tp, &lcl_adv,
3320 &rmt_adv))
3321 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 }
3323 } else {
3324 if (!(bmcr & BMCR_ANENABLE) &&
3325 tp->link_config.speed == current_speed &&
Matt Carlsonef167e22007-12-20 20:10:01 -08003326 tp->link_config.duplex == current_duplex &&
3327 tp->link_config.flowctrl ==
3328 tp->link_config.active_flowctrl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 }
3331 }
3332
Matt Carlsonef167e22007-12-20 20:10:01 -08003333 if (current_link_up == 1 &&
3334 tp->link_config.active_duplex == DUPLEX_FULL)
3335 tg3_setup_flow_control(tp, lcl_adv, rmt_adv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 }
3337
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338relink:
Matt Carlson80096062010-08-02 11:26:06 +00003339 if (current_link_up == 0 || (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 tg3_phy_copper_begin(tp);
3341
Matt Carlsonf833c4c2010-09-15 09:00:01 +00003342 tg3_readphy(tp, MII_BMSR, &bmsr);
3343 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
3344 (bmsr & BMSR_LSTATUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 current_link_up = 1;
3346 }
3347
3348 tp->mac_mode &= ~MAC_MODE_PORT_MODE_MASK;
3349 if (current_link_up == 1) {
3350 if (tp->link_config.active_speed == SPEED_100 ||
3351 tp->link_config.active_speed == SPEED_10)
3352 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3353 else
3354 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003355 } else if (tp->phy_flags & TG3_PHYFLG_IS_FET)
Matt Carlson7f97a4b2009-08-25 10:10:03 +00003356 tp->mac_mode |= MAC_MODE_PORT_MODE_MII;
3357 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
3359
3360 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
3361 if (tp->link_config.active_duplex == DUPLEX_HALF)
3362 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
3363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003365 if (current_link_up == 1 &&
3366 tg3_5700_link_polarity(tp, tp->link_config.active_speed))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07003368 else
3369 tp->mac_mode &= ~MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 }
3371
3372 /* ??? Without this setting Netgear GA302T PHY does not
3373 * ??? send/receive packets...
3374 */
Matt Carlson79eb6902010-02-17 15:17:03 +00003375 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5411 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 tp->pci_chip_rev_id == CHIPREV_ID_5700_ALTIMA) {
3377 tp->mi_mode |= MAC_MI_MODE_AUTO_POLL;
3378 tw32_f(MAC_MI_MODE, tp->mi_mode);
3379 udelay(80);
3380 }
3381
3382 tw32_f(MAC_MODE, tp->mac_mode);
3383 udelay(40);
3384
Matt Carlson52b02d02010-10-14 10:37:41 +00003385 tg3_phy_eee_adjust(tp, current_link_up);
3386
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
3388 /* Polled via timer. */
3389 tw32_f(MAC_EVENT, 0);
3390 } else {
3391 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
3392 }
3393 udelay(40);
3394
3395 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 &&
3396 current_link_up == 1 &&
3397 tp->link_config.active_speed == SPEED_1000 &&
3398 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) ||
3399 (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED))) {
3400 udelay(120);
3401 tw32_f(MAC_STATUS,
3402 (MAC_STATUS_SYNC_CHANGED |
3403 MAC_STATUS_CFG_CHANGED));
3404 udelay(40);
3405 tg3_write_mem(tp,
3406 NIC_SRAM_FIRMWARE_MBOX,
3407 NIC_SRAM_FIRMWARE_MBOX_MAGIC2);
3408 }
3409
Matt Carlson5e7dfd02008-11-21 17:18:16 -08003410 /* Prevent send BD corruption. */
3411 if (tp->tg3_flags3 & TG3_FLG3_CLKREQ_BUG) {
3412 u16 oldlnkctl, newlnkctl;
3413
3414 pci_read_config_word(tp->pdev,
3415 tp->pcie_cap + PCI_EXP_LNKCTL,
3416 &oldlnkctl);
3417 if (tp->link_config.active_speed == SPEED_100 ||
3418 tp->link_config.active_speed == SPEED_10)
3419 newlnkctl = oldlnkctl & ~PCI_EXP_LNKCTL_CLKREQ_EN;
3420 else
3421 newlnkctl = oldlnkctl | PCI_EXP_LNKCTL_CLKREQ_EN;
3422 if (newlnkctl != oldlnkctl)
3423 pci_write_config_word(tp->pdev,
3424 tp->pcie_cap + PCI_EXP_LNKCTL,
3425 newlnkctl);
3426 }
3427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 if (current_link_up != netif_carrier_ok(tp->dev)) {
3429 if (current_link_up)
3430 netif_carrier_on(tp->dev);
3431 else
3432 netif_carrier_off(tp->dev);
3433 tg3_link_report(tp);
3434 }
3435
3436 return 0;
3437}
3438
3439struct tg3_fiber_aneginfo {
3440 int state;
3441#define ANEG_STATE_UNKNOWN 0
3442#define ANEG_STATE_AN_ENABLE 1
3443#define ANEG_STATE_RESTART_INIT 2
3444#define ANEG_STATE_RESTART 3
3445#define ANEG_STATE_DISABLE_LINK_OK 4
3446#define ANEG_STATE_ABILITY_DETECT_INIT 5
3447#define ANEG_STATE_ABILITY_DETECT 6
3448#define ANEG_STATE_ACK_DETECT_INIT 7
3449#define ANEG_STATE_ACK_DETECT 8
3450#define ANEG_STATE_COMPLETE_ACK_INIT 9
3451#define ANEG_STATE_COMPLETE_ACK 10
3452#define ANEG_STATE_IDLE_DETECT_INIT 11
3453#define ANEG_STATE_IDLE_DETECT 12
3454#define ANEG_STATE_LINK_OK 13
3455#define ANEG_STATE_NEXT_PAGE_WAIT_INIT 14
3456#define ANEG_STATE_NEXT_PAGE_WAIT 15
3457
3458 u32 flags;
3459#define MR_AN_ENABLE 0x00000001
3460#define MR_RESTART_AN 0x00000002
3461#define MR_AN_COMPLETE 0x00000004
3462#define MR_PAGE_RX 0x00000008
3463#define MR_NP_LOADED 0x00000010
3464#define MR_TOGGLE_TX 0x00000020
3465#define MR_LP_ADV_FULL_DUPLEX 0x00000040
3466#define MR_LP_ADV_HALF_DUPLEX 0x00000080
3467#define MR_LP_ADV_SYM_PAUSE 0x00000100
3468#define MR_LP_ADV_ASYM_PAUSE 0x00000200
3469#define MR_LP_ADV_REMOTE_FAULT1 0x00000400
3470#define MR_LP_ADV_REMOTE_FAULT2 0x00000800
3471#define MR_LP_ADV_NEXT_PAGE 0x00001000
3472#define MR_TOGGLE_RX 0x00002000
3473#define MR_NP_RX 0x00004000
3474
3475#define MR_LINK_OK 0x80000000
3476
3477 unsigned long link_time, cur_time;
3478
3479 u32 ability_match_cfg;
3480 int ability_match_count;
3481
3482 char ability_match, idle_match, ack_match;
3483
3484 u32 txconfig, rxconfig;
3485#define ANEG_CFG_NP 0x00000080
3486#define ANEG_CFG_ACK 0x00000040
3487#define ANEG_CFG_RF2 0x00000020
3488#define ANEG_CFG_RF1 0x00000010
3489#define ANEG_CFG_PS2 0x00000001
3490#define ANEG_CFG_PS1 0x00008000
3491#define ANEG_CFG_HD 0x00004000
3492#define ANEG_CFG_FD 0x00002000
3493#define ANEG_CFG_INVAL 0x00001f06
3494
3495};
3496#define ANEG_OK 0
3497#define ANEG_DONE 1
3498#define ANEG_TIMER_ENAB 2
3499#define ANEG_FAILED -1
3500
3501#define ANEG_STATE_SETTLE_TIME 10000
3502
3503static int tg3_fiber_aneg_smachine(struct tg3 *tp,
3504 struct tg3_fiber_aneginfo *ap)
3505{
Matt Carlson5be73b42007-12-20 20:09:29 -08003506 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 unsigned long delta;
3508 u32 rx_cfg_reg;
3509 int ret;
3510
3511 if (ap->state == ANEG_STATE_UNKNOWN) {
3512 ap->rxconfig = 0;
3513 ap->link_time = 0;
3514 ap->cur_time = 0;
3515 ap->ability_match_cfg = 0;
3516 ap->ability_match_count = 0;
3517 ap->ability_match = 0;
3518 ap->idle_match = 0;
3519 ap->ack_match = 0;
3520 }
3521 ap->cur_time++;
3522
3523 if (tr32(MAC_STATUS) & MAC_STATUS_RCVD_CFG) {
3524 rx_cfg_reg = tr32(MAC_RX_AUTO_NEG);
3525
3526 if (rx_cfg_reg != ap->ability_match_cfg) {
3527 ap->ability_match_cfg = rx_cfg_reg;
3528 ap->ability_match = 0;
3529 ap->ability_match_count = 0;
3530 } else {
3531 if (++ap->ability_match_count > 1) {
3532 ap->ability_match = 1;
3533 ap->ability_match_cfg = rx_cfg_reg;
3534 }
3535 }
3536 if (rx_cfg_reg & ANEG_CFG_ACK)
3537 ap->ack_match = 1;
3538 else
3539 ap->ack_match = 0;
3540
3541 ap->idle_match = 0;
3542 } else {
3543 ap->idle_match = 1;
3544 ap->ability_match_cfg = 0;
3545 ap->ability_match_count = 0;
3546 ap->ability_match = 0;
3547 ap->ack_match = 0;
3548
3549 rx_cfg_reg = 0;
3550 }
3551
3552 ap->rxconfig = rx_cfg_reg;
3553 ret = ANEG_OK;
3554
Matt Carlson33f401a2010-04-05 10:19:27 +00003555 switch (ap->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 case ANEG_STATE_UNKNOWN:
3557 if (ap->flags & (MR_AN_ENABLE | MR_RESTART_AN))
3558 ap->state = ANEG_STATE_AN_ENABLE;
3559
3560 /* fallthru */
3561 case ANEG_STATE_AN_ENABLE:
3562 ap->flags &= ~(MR_AN_COMPLETE | MR_PAGE_RX);
3563 if (ap->flags & MR_AN_ENABLE) {
3564 ap->link_time = 0;
3565 ap->cur_time = 0;
3566 ap->ability_match_cfg = 0;
3567 ap->ability_match_count = 0;
3568 ap->ability_match = 0;
3569 ap->idle_match = 0;
3570 ap->ack_match = 0;
3571
3572 ap->state = ANEG_STATE_RESTART_INIT;
3573 } else {
3574 ap->state = ANEG_STATE_DISABLE_LINK_OK;
3575 }
3576 break;
3577
3578 case ANEG_STATE_RESTART_INIT:
3579 ap->link_time = ap->cur_time;
3580 ap->flags &= ~(MR_NP_LOADED);
3581 ap->txconfig = 0;
3582 tw32(MAC_TX_AUTO_NEG, 0);
3583 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3584 tw32_f(MAC_MODE, tp->mac_mode);
3585 udelay(40);
3586
3587 ret = ANEG_TIMER_ENAB;
3588 ap->state = ANEG_STATE_RESTART;
3589
3590 /* fallthru */
3591 case ANEG_STATE_RESTART:
3592 delta = ap->cur_time - ap->link_time;
Matt Carlson859a5882010-04-05 10:19:28 +00003593 if (delta > ANEG_STATE_SETTLE_TIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 ap->state = ANEG_STATE_ABILITY_DETECT_INIT;
Matt Carlson859a5882010-04-05 10:19:28 +00003595 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 ret = ANEG_TIMER_ENAB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 break;
3598
3599 case ANEG_STATE_DISABLE_LINK_OK:
3600 ret = ANEG_DONE;
3601 break;
3602
3603 case ANEG_STATE_ABILITY_DETECT_INIT:
3604 ap->flags &= ~(MR_TOGGLE_TX);
Matt Carlson5be73b42007-12-20 20:09:29 -08003605 ap->txconfig = ANEG_CFG_FD;
3606 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3607 if (flowctrl & ADVERTISE_1000XPAUSE)
3608 ap->txconfig |= ANEG_CFG_PS1;
3609 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3610 ap->txconfig |= ANEG_CFG_PS2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3612 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3613 tw32_f(MAC_MODE, tp->mac_mode);
3614 udelay(40);
3615
3616 ap->state = ANEG_STATE_ABILITY_DETECT;
3617 break;
3618
3619 case ANEG_STATE_ABILITY_DETECT:
Matt Carlson859a5882010-04-05 10:19:28 +00003620 if (ap->ability_match != 0 && ap->rxconfig != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 ap->state = ANEG_STATE_ACK_DETECT_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 break;
3623
3624 case ANEG_STATE_ACK_DETECT_INIT:
3625 ap->txconfig |= ANEG_CFG_ACK;
3626 tw32(MAC_TX_AUTO_NEG, ap->txconfig);
3627 tp->mac_mode |= MAC_MODE_SEND_CONFIGS;
3628 tw32_f(MAC_MODE, tp->mac_mode);
3629 udelay(40);
3630
3631 ap->state = ANEG_STATE_ACK_DETECT;
3632
3633 /* fallthru */
3634 case ANEG_STATE_ACK_DETECT:
3635 if (ap->ack_match != 0) {
3636 if ((ap->rxconfig & ~ANEG_CFG_ACK) ==
3637 (ap->ability_match_cfg & ~ANEG_CFG_ACK)) {
3638 ap->state = ANEG_STATE_COMPLETE_ACK_INIT;
3639 } else {
3640 ap->state = ANEG_STATE_AN_ENABLE;
3641 }
3642 } else if (ap->ability_match != 0 &&
3643 ap->rxconfig == 0) {
3644 ap->state = ANEG_STATE_AN_ENABLE;
3645 }
3646 break;
3647
3648 case ANEG_STATE_COMPLETE_ACK_INIT:
3649 if (ap->rxconfig & ANEG_CFG_INVAL) {
3650 ret = ANEG_FAILED;
3651 break;
3652 }
3653 ap->flags &= ~(MR_LP_ADV_FULL_DUPLEX |
3654 MR_LP_ADV_HALF_DUPLEX |
3655 MR_LP_ADV_SYM_PAUSE |
3656 MR_LP_ADV_ASYM_PAUSE |
3657 MR_LP_ADV_REMOTE_FAULT1 |
3658 MR_LP_ADV_REMOTE_FAULT2 |
3659 MR_LP_ADV_NEXT_PAGE |
3660 MR_TOGGLE_RX |
3661 MR_NP_RX);
3662 if (ap->rxconfig & ANEG_CFG_FD)
3663 ap->flags |= MR_LP_ADV_FULL_DUPLEX;
3664 if (ap->rxconfig & ANEG_CFG_HD)
3665 ap->flags |= MR_LP_ADV_HALF_DUPLEX;
3666 if (ap->rxconfig & ANEG_CFG_PS1)
3667 ap->flags |= MR_LP_ADV_SYM_PAUSE;
3668 if (ap->rxconfig & ANEG_CFG_PS2)
3669 ap->flags |= MR_LP_ADV_ASYM_PAUSE;
3670 if (ap->rxconfig & ANEG_CFG_RF1)
3671 ap->flags |= MR_LP_ADV_REMOTE_FAULT1;
3672 if (ap->rxconfig & ANEG_CFG_RF2)
3673 ap->flags |= MR_LP_ADV_REMOTE_FAULT2;
3674 if (ap->rxconfig & ANEG_CFG_NP)
3675 ap->flags |= MR_LP_ADV_NEXT_PAGE;
3676
3677 ap->link_time = ap->cur_time;
3678
3679 ap->flags ^= (MR_TOGGLE_TX);
3680 if (ap->rxconfig & 0x0008)
3681 ap->flags |= MR_TOGGLE_RX;
3682 if (ap->rxconfig & ANEG_CFG_NP)
3683 ap->flags |= MR_NP_RX;
3684 ap->flags |= MR_PAGE_RX;
3685
3686 ap->state = ANEG_STATE_COMPLETE_ACK;
3687 ret = ANEG_TIMER_ENAB;
3688 break;
3689
3690 case ANEG_STATE_COMPLETE_ACK:
3691 if (ap->ability_match != 0 &&
3692 ap->rxconfig == 0) {
3693 ap->state = ANEG_STATE_AN_ENABLE;
3694 break;
3695 }
3696 delta = ap->cur_time - ap->link_time;
3697 if (delta > ANEG_STATE_SETTLE_TIME) {
3698 if (!(ap->flags & (MR_LP_ADV_NEXT_PAGE))) {
3699 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3700 } else {
3701 if ((ap->txconfig & ANEG_CFG_NP) == 0 &&
3702 !(ap->flags & MR_NP_RX)) {
3703 ap->state = ANEG_STATE_IDLE_DETECT_INIT;
3704 } else {
3705 ret = ANEG_FAILED;
3706 }
3707 }
3708 }
3709 break;
3710
3711 case ANEG_STATE_IDLE_DETECT_INIT:
3712 ap->link_time = ap->cur_time;
3713 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3714 tw32_f(MAC_MODE, tp->mac_mode);
3715 udelay(40);
3716
3717 ap->state = ANEG_STATE_IDLE_DETECT;
3718 ret = ANEG_TIMER_ENAB;
3719 break;
3720
3721 case ANEG_STATE_IDLE_DETECT:
3722 if (ap->ability_match != 0 &&
3723 ap->rxconfig == 0) {
3724 ap->state = ANEG_STATE_AN_ENABLE;
3725 break;
3726 }
3727 delta = ap->cur_time - ap->link_time;
3728 if (delta > ANEG_STATE_SETTLE_TIME) {
3729 /* XXX another gem from the Broadcom driver :( */
3730 ap->state = ANEG_STATE_LINK_OK;
3731 }
3732 break;
3733
3734 case ANEG_STATE_LINK_OK:
3735 ap->flags |= (MR_AN_COMPLETE | MR_LINK_OK);
3736 ret = ANEG_DONE;
3737 break;
3738
3739 case ANEG_STATE_NEXT_PAGE_WAIT_INIT:
3740 /* ??? unimplemented */
3741 break;
3742
3743 case ANEG_STATE_NEXT_PAGE_WAIT:
3744 /* ??? unimplemented */
3745 break;
3746
3747 default:
3748 ret = ANEG_FAILED;
3749 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07003750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
3752 return ret;
3753}
3754
Matt Carlson5be73b42007-12-20 20:09:29 -08003755static int fiber_autoneg(struct tg3 *tp, u32 *txflags, u32 *rxflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
3757 int res = 0;
3758 struct tg3_fiber_aneginfo aninfo;
3759 int status = ANEG_FAILED;
3760 unsigned int tick;
3761 u32 tmp;
3762
3763 tw32_f(MAC_TX_AUTO_NEG, 0);
3764
3765 tmp = tp->mac_mode & ~MAC_MODE_PORT_MODE_MASK;
3766 tw32_f(MAC_MODE, tmp | MAC_MODE_PORT_MODE_GMII);
3767 udelay(40);
3768
3769 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_SEND_CONFIGS);
3770 udelay(40);
3771
3772 memset(&aninfo, 0, sizeof(aninfo));
3773 aninfo.flags |= MR_AN_ENABLE;
3774 aninfo.state = ANEG_STATE_UNKNOWN;
3775 aninfo.cur_time = 0;
3776 tick = 0;
3777 while (++tick < 195000) {
3778 status = tg3_fiber_aneg_smachine(tp, &aninfo);
3779 if (status == ANEG_DONE || status == ANEG_FAILED)
3780 break;
3781
3782 udelay(1);
3783 }
3784
3785 tp->mac_mode &= ~MAC_MODE_SEND_CONFIGS;
3786 tw32_f(MAC_MODE, tp->mac_mode);
3787 udelay(40);
3788
Matt Carlson5be73b42007-12-20 20:09:29 -08003789 *txflags = aninfo.txconfig;
3790 *rxflags = aninfo.flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
3792 if (status == ANEG_DONE &&
3793 (aninfo.flags & (MR_AN_COMPLETE | MR_LINK_OK |
3794 MR_LP_ADV_FULL_DUPLEX)))
3795 res = 1;
3796
3797 return res;
3798}
3799
3800static void tg3_init_bcm8002(struct tg3 *tp)
3801{
3802 u32 mac_status = tr32(MAC_STATUS);
3803 int i;
3804
3805 /* Reset when initting first time or we have a link. */
3806 if ((tp->tg3_flags & TG3_FLAG_INIT_COMPLETE) &&
3807 !(mac_status & MAC_STATUS_PCS_SYNCED))
3808 return;
3809
3810 /* Set PLL lock range. */
3811 tg3_writephy(tp, 0x16, 0x8007);
3812
3813 /* SW reset */
3814 tg3_writephy(tp, MII_BMCR, BMCR_RESET);
3815
3816 /* Wait for reset to complete. */
3817 /* XXX schedule_timeout() ... */
3818 for (i = 0; i < 500; i++)
3819 udelay(10);
3820
3821 /* Config mode; select PMA/Ch 1 regs. */
3822 tg3_writephy(tp, 0x10, 0x8411);
3823
3824 /* Enable auto-lock and comdet, select txclk for tx. */
3825 tg3_writephy(tp, 0x11, 0x0a10);
3826
3827 tg3_writephy(tp, 0x18, 0x00a0);
3828 tg3_writephy(tp, 0x16, 0x41ff);
3829
3830 /* Assert and deassert POR. */
3831 tg3_writephy(tp, 0x13, 0x0400);
3832 udelay(40);
3833 tg3_writephy(tp, 0x13, 0x0000);
3834
3835 tg3_writephy(tp, 0x11, 0x0a50);
3836 udelay(40);
3837 tg3_writephy(tp, 0x11, 0x0a10);
3838
3839 /* Wait for signal to stabilize */
3840 /* XXX schedule_timeout() ... */
3841 for (i = 0; i < 15000; i++)
3842 udelay(10);
3843
3844 /* Deselect the channel register so we can read the PHYID
3845 * later.
3846 */
3847 tg3_writephy(tp, 0x10, 0x8011);
3848}
3849
3850static int tg3_setup_fiber_hw_autoneg(struct tg3 *tp, u32 mac_status)
3851{
Matt Carlson82cd3d12007-12-20 20:09:00 -08003852 u16 flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 u32 sg_dig_ctrl, sg_dig_status;
3854 u32 serdes_cfg, expected_sg_dig_ctrl;
3855 int workaround, port_a;
3856 int current_link_up;
3857
3858 serdes_cfg = 0;
3859 expected_sg_dig_ctrl = 0;
3860 workaround = 0;
3861 port_a = 1;
3862 current_link_up = 0;
3863
3864 if (tp->pci_chip_rev_id != CHIPREV_ID_5704_A0 &&
3865 tp->pci_chip_rev_id != CHIPREV_ID_5704_A1) {
3866 workaround = 1;
3867 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
3868 port_a = 0;
3869
3870 /* preserve bits 0-11,13,14 for signal pre-emphasis */
3871 /* preserve bits 20-23 for voltage regulator */
3872 serdes_cfg = tr32(MAC_SERDES_CFG) & 0x00f06fff;
3873 }
3874
3875 sg_dig_ctrl = tr32(SG_DIG_CTRL);
3876
3877 if (tp->link_config.autoneg != AUTONEG_ENABLE) {
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003878 if (sg_dig_ctrl & SG_DIG_USING_HW_AUTONEG) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 if (workaround) {
3880 u32 val = serdes_cfg;
3881
3882 if (port_a)
3883 val |= 0xc010000;
3884 else
3885 val |= 0x4010000;
3886 tw32_f(MAC_SERDES_CFG, val);
3887 }
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003888
3889 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 }
3891 if (mac_status & MAC_STATUS_PCS_SYNCED) {
3892 tg3_setup_flow_control(tp, 0, 0);
3893 current_link_up = 1;
3894 }
3895 goto out;
3896 }
3897
3898 /* Want auto-negotiation. */
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003899 expected_sg_dig_ctrl = SG_DIG_USING_HW_AUTONEG | SG_DIG_COMMON_SETUP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
Matt Carlson82cd3d12007-12-20 20:09:00 -08003901 flowctrl = tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
3902 if (flowctrl & ADVERTISE_1000XPAUSE)
3903 expected_sg_dig_ctrl |= SG_DIG_PAUSE_CAP;
3904 if (flowctrl & ADVERTISE_1000XPSE_ASYM)
3905 expected_sg_dig_ctrl |= SG_DIG_ASYM_PAUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
3907 if (sg_dig_ctrl != expected_sg_dig_ctrl) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003908 if ((tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT) &&
Michael Chan3d3ebe72006-09-27 15:59:15 -07003909 tp->serdes_counter &&
3910 ((mac_status & (MAC_STATUS_PCS_SYNCED |
3911 MAC_STATUS_RCVD_CFG)) ==
3912 MAC_STATUS_PCS_SYNCED)) {
3913 tp->serdes_counter--;
3914 current_link_up = 1;
3915 goto out;
3916 }
3917restart_autoneg:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 if (workaround)
3919 tw32_f(MAC_SERDES_CFG, serdes_cfg | 0xc011000);
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003920 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl | SG_DIG_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 udelay(5);
3922 tw32_f(SG_DIG_CTRL, expected_sg_dig_ctrl);
3923
Michael Chan3d3ebe72006-09-27 15:59:15 -07003924 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003925 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 } else if (mac_status & (MAC_STATUS_PCS_SYNCED |
3927 MAC_STATUS_SIGNAL_DET)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003928 sg_dig_status = tr32(SG_DIG_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 mac_status = tr32(MAC_STATUS);
3930
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003931 if ((sg_dig_status & SG_DIG_AUTONEG_COMPLETE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 (mac_status & MAC_STATUS_PCS_SYNCED)) {
Matt Carlson82cd3d12007-12-20 20:09:00 -08003933 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
Matt Carlson82cd3d12007-12-20 20:09:00 -08003935 if (sg_dig_ctrl & SG_DIG_PAUSE_CAP)
3936 local_adv |= ADVERTISE_1000XPAUSE;
3937 if (sg_dig_ctrl & SG_DIG_ASYM_PAUSE)
3938 local_adv |= ADVERTISE_1000XPSE_ASYM;
3939
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003940 if (sg_dig_status & SG_DIG_PARTNER_PAUSE_CAPABLE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003941 remote_adv |= LPA_1000XPAUSE;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003942 if (sg_dig_status & SG_DIG_PARTNER_ASYM_PAUSE)
Matt Carlson82cd3d12007-12-20 20:09:00 -08003943 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
3945 tg3_setup_flow_control(tp, local_adv, remote_adv);
3946 current_link_up = 1;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003947 tp->serdes_counter = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003948 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003949 } else if (!(sg_dig_status & SG_DIG_AUTONEG_COMPLETE)) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07003950 if (tp->serdes_counter)
3951 tp->serdes_counter--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 else {
3953 if (workaround) {
3954 u32 val = serdes_cfg;
3955
3956 if (port_a)
3957 val |= 0xc010000;
3958 else
3959 val |= 0x4010000;
3960
3961 tw32_f(MAC_SERDES_CFG, val);
3962 }
3963
Matt Carlsonc98f6e32007-12-20 20:08:32 -08003964 tw32_f(SG_DIG_CTRL, SG_DIG_COMMON_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 udelay(40);
3966
3967 /* Link parallel detection - link is up */
3968 /* only if we have PCS_SYNC and not */
3969 /* receiving config code words */
3970 mac_status = tr32(MAC_STATUS);
3971 if ((mac_status & MAC_STATUS_PCS_SYNCED) &&
3972 !(mac_status & MAC_STATUS_RCVD_CFG)) {
3973 tg3_setup_flow_control(tp, 0, 0);
3974 current_link_up = 1;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003975 tp->phy_flags |=
3976 TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan3d3ebe72006-09-27 15:59:15 -07003977 tp->serdes_counter =
3978 SERDES_PARALLEL_DET_TIMEOUT;
3979 } else
3980 goto restart_autoneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 }
3982 }
Michael Chan3d3ebe72006-09-27 15:59:15 -07003983 } else {
3984 tp->serdes_counter = SERDES_AN_TIMEOUT_5704S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00003985 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 }
3987
3988out:
3989 return current_link_up;
3990}
3991
3992static int tg3_setup_fiber_by_hand(struct tg3 *tp, u32 mac_status)
3993{
3994 int current_link_up = 0;
3995
Michael Chan5cf64b82007-05-05 12:11:21 -07003996 if (!(mac_status & MAC_STATUS_PCS_SYNCED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
3999 if (tp->link_config.autoneg == AUTONEG_ENABLE) {
Matt Carlson5be73b42007-12-20 20:09:29 -08004000 u32 txflags, rxflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 int i;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04004002
Matt Carlson5be73b42007-12-20 20:09:29 -08004003 if (fiber_autoneg(tp, &txflags, &rxflags)) {
4004 u32 local_adv = 0, remote_adv = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Matt Carlson5be73b42007-12-20 20:09:29 -08004006 if (txflags & ANEG_CFG_PS1)
4007 local_adv |= ADVERTISE_1000XPAUSE;
4008 if (txflags & ANEG_CFG_PS2)
4009 local_adv |= ADVERTISE_1000XPSE_ASYM;
4010
4011 if (rxflags & MR_LP_ADV_SYM_PAUSE)
4012 remote_adv |= LPA_1000XPAUSE;
4013 if (rxflags & MR_LP_ADV_ASYM_PAUSE)
4014 remote_adv |= LPA_1000XPAUSE_ASYM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
4016 tg3_setup_flow_control(tp, local_adv, remote_adv);
4017
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 current_link_up = 1;
4019 }
4020 for (i = 0; i < 30; i++) {
4021 udelay(20);
4022 tw32_f(MAC_STATUS,
4023 (MAC_STATUS_SYNC_CHANGED |
4024 MAC_STATUS_CFG_CHANGED));
4025 udelay(40);
4026 if ((tr32(MAC_STATUS) &
4027 (MAC_STATUS_SYNC_CHANGED |
4028 MAC_STATUS_CFG_CHANGED)) == 0)
4029 break;
4030 }
4031
4032 mac_status = tr32(MAC_STATUS);
4033 if (current_link_up == 0 &&
4034 (mac_status & MAC_STATUS_PCS_SYNCED) &&
4035 !(mac_status & MAC_STATUS_RCVD_CFG))
4036 current_link_up = 1;
4037 } else {
Matt Carlson5be73b42007-12-20 20:09:29 -08004038 tg3_setup_flow_control(tp, 0, 0);
4039
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 /* Forcing 1000FD link up. */
4041 current_link_up = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
4043 tw32_f(MAC_MODE, (tp->mac_mode | MAC_MODE_SEND_CONFIGS));
4044 udelay(40);
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07004045
4046 tw32_f(MAC_MODE, tp->mac_mode);
4047 udelay(40);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 }
4049
4050out:
4051 return current_link_up;
4052}
4053
4054static int tg3_setup_fiber_phy(struct tg3 *tp, int force_reset)
4055{
4056 u32 orig_pause_cfg;
4057 u16 orig_active_speed;
4058 u8 orig_active_duplex;
4059 u32 mac_status;
4060 int current_link_up;
4061 int i;
4062
Matt Carlson8d018622007-12-20 20:05:44 -08004063 orig_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 orig_active_speed = tp->link_config.active_speed;
4065 orig_active_duplex = tp->link_config.active_duplex;
4066
4067 if (!(tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG) &&
4068 netif_carrier_ok(tp->dev) &&
4069 (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)) {
4070 mac_status = tr32(MAC_STATUS);
4071 mac_status &= (MAC_STATUS_PCS_SYNCED |
4072 MAC_STATUS_SIGNAL_DET |
4073 MAC_STATUS_CFG_CHANGED |
4074 MAC_STATUS_RCVD_CFG);
4075 if (mac_status == (MAC_STATUS_PCS_SYNCED |
4076 MAC_STATUS_SIGNAL_DET)) {
4077 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4078 MAC_STATUS_CFG_CHANGED));
4079 return 0;
4080 }
4081 }
4082
4083 tw32_f(MAC_TX_AUTO_NEG, 0);
4084
4085 tp->mac_mode &= ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
4086 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
4087 tw32_f(MAC_MODE, tp->mac_mode);
4088 udelay(40);
4089
Matt Carlson79eb6902010-02-17 15:17:03 +00004090 if (tp->phy_id == TG3_PHY_ID_BCM8002)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 tg3_init_bcm8002(tp);
4092
4093 /* Enable link change event even when serdes polling. */
4094 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4095 udelay(40);
4096
4097 current_link_up = 0;
4098 mac_status = tr32(MAC_STATUS);
4099
4100 if (tp->tg3_flags2 & TG3_FLG2_HW_AUTONEG)
4101 current_link_up = tg3_setup_fiber_hw_autoneg(tp, mac_status);
4102 else
4103 current_link_up = tg3_setup_fiber_by_hand(tp, mac_status);
4104
Matt Carlson898a56f2009-08-28 14:02:40 +00004105 tp->napi[0].hw_status->status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 (SD_STATUS_UPDATED |
Matt Carlson898a56f2009-08-28 14:02:40 +00004107 (tp->napi[0].hw_status->status & ~SD_STATUS_LINK_CHG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
4109 for (i = 0; i < 100; i++) {
4110 tw32_f(MAC_STATUS, (MAC_STATUS_SYNC_CHANGED |
4111 MAC_STATUS_CFG_CHANGED));
4112 udelay(5);
4113 if ((tr32(MAC_STATUS) & (MAC_STATUS_SYNC_CHANGED |
Michael Chan3d3ebe72006-09-27 15:59:15 -07004114 MAC_STATUS_CFG_CHANGED |
4115 MAC_STATUS_LNKSTATE_CHANGED)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 break;
4117 }
4118
4119 mac_status = tr32(MAC_STATUS);
4120 if ((mac_status & MAC_STATUS_PCS_SYNCED) == 0) {
4121 current_link_up = 0;
Michael Chan3d3ebe72006-09-27 15:59:15 -07004122 if (tp->link_config.autoneg == AUTONEG_ENABLE &&
4123 tp->serdes_counter == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 tw32_f(MAC_MODE, (tp->mac_mode |
4125 MAC_MODE_SEND_CONFIGS));
4126 udelay(1);
4127 tw32_f(MAC_MODE, tp->mac_mode);
4128 }
4129 }
4130
4131 if (current_link_up == 1) {
4132 tp->link_config.active_speed = SPEED_1000;
4133 tp->link_config.active_duplex = DUPLEX_FULL;
4134 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4135 LED_CTRL_LNKLED_OVERRIDE |
4136 LED_CTRL_1000MBPS_ON));
4137 } else {
4138 tp->link_config.active_speed = SPEED_INVALID;
4139 tp->link_config.active_duplex = DUPLEX_INVALID;
4140 tw32(MAC_LED_CTRL, (tp->led_ctrl |
4141 LED_CTRL_LNKLED_OVERRIDE |
4142 LED_CTRL_TRAFFIC_OVERRIDE));
4143 }
4144
4145 if (current_link_up != netif_carrier_ok(tp->dev)) {
4146 if (current_link_up)
4147 netif_carrier_on(tp->dev);
4148 else
4149 netif_carrier_off(tp->dev);
4150 tg3_link_report(tp);
4151 } else {
Matt Carlson8d018622007-12-20 20:05:44 -08004152 u32 now_pause_cfg = tp->link_config.active_flowctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 if (orig_pause_cfg != now_pause_cfg ||
4154 orig_active_speed != tp->link_config.active_speed ||
4155 orig_active_duplex != tp->link_config.active_duplex)
4156 tg3_link_report(tp);
4157 }
4158
4159 return 0;
4160}
4161
Michael Chan747e8f82005-07-25 12:33:22 -07004162static int tg3_setup_fiber_mii_phy(struct tg3 *tp, int force_reset)
4163{
4164 int current_link_up, err = 0;
4165 u32 bmsr, bmcr;
4166 u16 current_speed;
4167 u8 current_duplex;
Matt Carlsonef167e22007-12-20 20:10:01 -08004168 u32 local_adv, remote_adv;
Michael Chan747e8f82005-07-25 12:33:22 -07004169
4170 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
4171 tw32_f(MAC_MODE, tp->mac_mode);
4172 udelay(40);
4173
4174 tw32(MAC_EVENT, 0);
4175
4176 tw32_f(MAC_STATUS,
4177 (MAC_STATUS_SYNC_CHANGED |
4178 MAC_STATUS_CFG_CHANGED |
4179 MAC_STATUS_MI_COMPLETION |
4180 MAC_STATUS_LNKSTATE_CHANGED));
4181 udelay(40);
4182
4183 if (force_reset)
4184 tg3_phy_reset(tp);
4185
4186 current_link_up = 0;
4187 current_speed = SPEED_INVALID;
4188 current_duplex = DUPLEX_INVALID;
4189
4190 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
4191 err |= tg3_readphy(tp, MII_BMSR, &bmsr);
Michael Chand4d2c552006-03-20 17:47:20 -08004192 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
4193 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4194 bmsr |= BMSR_LSTATUS;
4195 else
4196 bmsr &= ~BMSR_LSTATUS;
4197 }
Michael Chan747e8f82005-07-25 12:33:22 -07004198
4199 err |= tg3_readphy(tp, MII_BMCR, &bmcr);
4200
4201 if ((tp->link_config.autoneg == AUTONEG_ENABLE) && !force_reset &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004202 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004203 /* do nothing, just check for link up at the end */
4204 } else if (tp->link_config.autoneg == AUTONEG_ENABLE) {
4205 u32 adv, new_adv;
4206
4207 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4208 new_adv = adv & ~(ADVERTISE_1000XFULL | ADVERTISE_1000XHALF |
4209 ADVERTISE_1000XPAUSE |
4210 ADVERTISE_1000XPSE_ASYM |
4211 ADVERTISE_SLCT);
4212
Matt Carlsonba4d07a2007-12-20 20:08:00 -08004213 new_adv |= tg3_advert_flowctrl_1000X(tp->link_config.flowctrl);
Michael Chan747e8f82005-07-25 12:33:22 -07004214
4215 if (tp->link_config.advertising & ADVERTISED_1000baseT_Half)
4216 new_adv |= ADVERTISE_1000XHALF;
4217 if (tp->link_config.advertising & ADVERTISED_1000baseT_Full)
4218 new_adv |= ADVERTISE_1000XFULL;
4219
4220 if ((new_adv != adv) || !(bmcr & BMCR_ANENABLE)) {
4221 tg3_writephy(tp, MII_ADVERTISE, new_adv);
4222 bmcr |= BMCR_ANENABLE | BMCR_ANRESTART;
4223 tg3_writephy(tp, MII_BMCR, bmcr);
4224
4225 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
Michael Chan3d3ebe72006-09-27 15:59:15 -07004226 tp->serdes_counter = SERDES_AN_TIMEOUT_5714S;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004227 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004228
4229 return err;
4230 }
4231 } else {
4232 u32 new_bmcr;
4233
4234 bmcr &= ~BMCR_SPEED1000;
4235 new_bmcr = bmcr & ~(BMCR_ANENABLE | BMCR_FULLDPLX);
4236
4237 if (tp->link_config.duplex == DUPLEX_FULL)
4238 new_bmcr |= BMCR_FULLDPLX;
4239
4240 if (new_bmcr != bmcr) {
4241 /* BMCR_SPEED1000 is a reserved bit that needs
4242 * to be set on write.
4243 */
4244 new_bmcr |= BMCR_SPEED1000;
4245
4246 /* Force a linkdown */
4247 if (netif_carrier_ok(tp->dev)) {
4248 u32 adv;
4249
4250 err |= tg3_readphy(tp, MII_ADVERTISE, &adv);
4251 adv &= ~(ADVERTISE_1000XFULL |
4252 ADVERTISE_1000XHALF |
4253 ADVERTISE_SLCT);
4254 tg3_writephy(tp, MII_ADVERTISE, adv);
4255 tg3_writephy(tp, MII_BMCR, bmcr |
4256 BMCR_ANRESTART |
4257 BMCR_ANENABLE);
4258 udelay(10);
4259 netif_carrier_off(tp->dev);
4260 }
4261 tg3_writephy(tp, MII_BMCR, new_bmcr);
4262 bmcr = new_bmcr;
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) ==
4266 ASIC_REV_5714) {
4267 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
4268 bmsr |= BMSR_LSTATUS;
4269 else
4270 bmsr &= ~BMSR_LSTATUS;
4271 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004272 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004273 }
4274 }
4275
4276 if (bmsr & BMSR_LSTATUS) {
4277 current_speed = SPEED_1000;
4278 current_link_up = 1;
4279 if (bmcr & BMCR_FULLDPLX)
4280 current_duplex = DUPLEX_FULL;
4281 else
4282 current_duplex = DUPLEX_HALF;
4283
Matt Carlsonef167e22007-12-20 20:10:01 -08004284 local_adv = 0;
4285 remote_adv = 0;
4286
Michael Chan747e8f82005-07-25 12:33:22 -07004287 if (bmcr & BMCR_ANENABLE) {
Matt Carlsonef167e22007-12-20 20:10:01 -08004288 u32 common;
Michael Chan747e8f82005-07-25 12:33:22 -07004289
4290 err |= tg3_readphy(tp, MII_ADVERTISE, &local_adv);
4291 err |= tg3_readphy(tp, MII_LPA, &remote_adv);
4292 common = local_adv & remote_adv;
4293 if (common & (ADVERTISE_1000XHALF |
4294 ADVERTISE_1000XFULL)) {
4295 if (common & ADVERTISE_1000XFULL)
4296 current_duplex = DUPLEX_FULL;
4297 else
4298 current_duplex = DUPLEX_HALF;
Matt Carlson57d8b882010-06-05 17:24:35 +00004299 } else if (!(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
4300 /* Link is up via parallel detect */
Matt Carlson859a5882010-04-05 10:19:28 +00004301 } else {
Michael Chan747e8f82005-07-25 12:33:22 -07004302 current_link_up = 0;
Matt Carlson859a5882010-04-05 10:19:28 +00004303 }
Michael Chan747e8f82005-07-25 12:33:22 -07004304 }
4305 }
4306
Matt Carlsonef167e22007-12-20 20:10:01 -08004307 if (current_link_up == 1 && current_duplex == DUPLEX_FULL)
4308 tg3_setup_flow_control(tp, local_adv, remote_adv);
4309
Michael Chan747e8f82005-07-25 12:33:22 -07004310 tp->mac_mode &= ~MAC_MODE_HALF_DUPLEX;
4311 if (tp->link_config.active_duplex == DUPLEX_HALF)
4312 tp->mac_mode |= MAC_MODE_HALF_DUPLEX;
4313
4314 tw32_f(MAC_MODE, tp->mac_mode);
4315 udelay(40);
4316
4317 tw32_f(MAC_EVENT, MAC_EVENT_LNKSTATE_CHANGED);
4318
4319 tp->link_config.active_speed = current_speed;
4320 tp->link_config.active_duplex = current_duplex;
4321
4322 if (current_link_up != netif_carrier_ok(tp->dev)) {
4323 if (current_link_up)
4324 netif_carrier_on(tp->dev);
4325 else {
4326 netif_carrier_off(tp->dev);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004327 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004328 }
4329 tg3_link_report(tp);
4330 }
4331 return err;
4332}
4333
4334static void tg3_serdes_parallel_detect(struct tg3 *tp)
4335{
Michael Chan3d3ebe72006-09-27 15:59:15 -07004336 if (tp->serdes_counter) {
Michael Chan747e8f82005-07-25 12:33:22 -07004337 /* Give autoneg time to complete. */
Michael Chan3d3ebe72006-09-27 15:59:15 -07004338 tp->serdes_counter--;
Michael Chan747e8f82005-07-25 12:33:22 -07004339 return;
4340 }
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004341
Michael Chan747e8f82005-07-25 12:33:22 -07004342 if (!netif_carrier_ok(tp->dev) &&
4343 (tp->link_config.autoneg == AUTONEG_ENABLE)) {
4344 u32 bmcr;
4345
4346 tg3_readphy(tp, MII_BMCR, &bmcr);
4347 if (bmcr & BMCR_ANENABLE) {
4348 u32 phy1, phy2;
4349
4350 /* Select shadow register 0x1f */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004351 tg3_writephy(tp, MII_TG3_MISC_SHDW, 0x7c00);
4352 tg3_readphy(tp, MII_TG3_MISC_SHDW, &phy1);
Michael Chan747e8f82005-07-25 12:33:22 -07004353
4354 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004355 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4356 MII_TG3_DSP_EXP1_INT_STAT);
4357 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
4358 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004359
4360 if ((phy1 & 0x10) && !(phy2 & 0x20)) {
4361 /* We have signal detect and not receiving
4362 * config code words, link is up by parallel
4363 * detection.
4364 */
4365
4366 bmcr &= ~BMCR_ANENABLE;
4367 bmcr |= BMCR_SPEED1000 | BMCR_FULLDPLX;
4368 tg3_writephy(tp, MII_BMCR, bmcr);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004369 tp->phy_flags |= TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004370 }
4371 }
Matt Carlson859a5882010-04-05 10:19:28 +00004372 } else if (netif_carrier_ok(tp->dev) &&
4373 (tp->link_config.autoneg == AUTONEG_ENABLE) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004374 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT)) {
Michael Chan747e8f82005-07-25 12:33:22 -07004375 u32 phy2;
4376
4377 /* Select expansion interrupt status register */
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00004378 tg3_writephy(tp, MII_TG3_DSP_ADDRESS,
4379 MII_TG3_DSP_EXP1_INT_STAT);
4380 tg3_readphy(tp, MII_TG3_DSP_RW_PORT, &phy2);
Michael Chan747e8f82005-07-25 12:33:22 -07004381 if (phy2 & 0x20) {
4382 u32 bmcr;
4383
4384 /* Config code words received, turn on autoneg. */
4385 tg3_readphy(tp, MII_BMCR, &bmcr);
4386 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANENABLE);
4387
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004388 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chan747e8f82005-07-25 12:33:22 -07004389
4390 }
4391 }
4392}
4393
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394static int tg3_setup_phy(struct tg3 *tp, int force_reset)
4395{
Matt Carlsonf2096f92011-04-05 14:22:48 +00004396 u32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 int err;
4398
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004399 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 err = tg3_setup_fiber_phy(tp, force_reset);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00004401 else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chan747e8f82005-07-25 12:33:22 -07004402 err = tg3_setup_fiber_mii_phy(tp, force_reset);
Matt Carlson859a5882010-04-05 10:19:28 +00004403 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 err = tg3_setup_copper_phy(tp, force_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Matt Carlsonbcb37f62008-11-03 16:52:09 -08004406 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004407 u32 scale;
Matt Carlsonaa6c91f2007-11-12 21:18:04 -08004408
4409 val = tr32(TG3_CPMU_CLCK_STAT) & CPMU_CLCK_STAT_MAC_CLCK_MASK;
4410 if (val == CPMU_CLCK_STAT_MAC_CLCK_62_5)
4411 scale = 65;
4412 else if (val == CPMU_CLCK_STAT_MAC_CLCK_6_25)
4413 scale = 6;
4414 else
4415 scale = 12;
4416
4417 val = tr32(GRC_MISC_CFG) & ~GRC_MISC_CFG_PRESCALAR_MASK;
4418 val |= (scale << GRC_MISC_CFG_PRESCALAR_SHIFT);
4419 tw32(GRC_MISC_CFG, val);
4420 }
4421
Matt Carlsonf2096f92011-04-05 14:22:48 +00004422 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
4423 (6 << TX_LENGTHS_IPG_SHIFT);
4424 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
4425 val |= tr32(MAC_TX_LENGTHS) &
4426 (TX_LENGTHS_JMB_FRM_LEN_MSK |
4427 TX_LENGTHS_CNT_DWN_VAL_MSK);
4428
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 if (tp->link_config.active_speed == SPEED_1000 &&
4430 tp->link_config.active_duplex == DUPLEX_HALF)
Matt Carlsonf2096f92011-04-05 14:22:48 +00004431 tw32(MAC_TX_LENGTHS, val |
4432 (0xff << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 else
Matt Carlsonf2096f92011-04-05 14:22:48 +00004434 tw32(MAC_TX_LENGTHS, val |
4435 (32 << TX_LENGTHS_SLOT_TIME_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
4437 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4438 if (netif_carrier_ok(tp->dev)) {
4439 tw32(HOSTCC_STAT_COAL_TICKS,
David S. Miller15f98502005-05-18 22:49:26 -07004440 tp->coal.stats_block_coalesce_usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 } else {
4442 tw32(HOSTCC_STAT_COAL_TICKS, 0);
4443 }
4444 }
4445
Matt Carlson8ed5d972007-05-07 00:25:49 -07004446 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND) {
Matt Carlsonf2096f92011-04-05 14:22:48 +00004447 val = tr32(PCIE_PWR_MGMT_THRESH);
Matt Carlson8ed5d972007-05-07 00:25:49 -07004448 if (!netif_carrier_ok(tp->dev))
4449 val = (val & ~PCIE_PWR_MGMT_L1_THRESH_MSK) |
4450 tp->pwrmgmt_thresh;
4451 else
4452 val |= PCIE_PWR_MGMT_L1_THRESH_MSK;
4453 tw32(PCIE_PWR_MGMT_THRESH, val);
4454 }
4455
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 return err;
4457}
4458
Matt Carlson66cfd1b2010-09-30 10:34:30 +00004459static inline int tg3_irq_sync(struct tg3 *tp)
4460{
4461 return tp->irq_sync;
4462}
4463
Matt Carlson97bd8e42011-04-13 11:05:04 +00004464static inline void tg3_rd32_loop(struct tg3 *tp, u32 *dst, u32 off, u32 len)
4465{
4466 int i;
4467
4468 dst = (u32 *)((u8 *)dst + off);
4469 for (i = 0; i < len; i += sizeof(u32))
4470 *dst++ = tr32(off + i);
4471}
4472
4473static void tg3_dump_legacy_regs(struct tg3 *tp, u32 *regs)
4474{
4475 tg3_rd32_loop(tp, regs, TG3PCI_VENDOR, 0xb0);
4476 tg3_rd32_loop(tp, regs, MAILBOX_INTERRUPT_0, 0x200);
4477 tg3_rd32_loop(tp, regs, MAC_MODE, 0x4f0);
4478 tg3_rd32_loop(tp, regs, SNDDATAI_MODE, 0xe0);
4479 tg3_rd32_loop(tp, regs, SNDDATAC_MODE, 0x04);
4480 tg3_rd32_loop(tp, regs, SNDBDS_MODE, 0x80);
4481 tg3_rd32_loop(tp, regs, SNDBDI_MODE, 0x48);
4482 tg3_rd32_loop(tp, regs, SNDBDC_MODE, 0x04);
4483 tg3_rd32_loop(tp, regs, RCVLPC_MODE, 0x20);
4484 tg3_rd32_loop(tp, regs, RCVLPC_SELLST_BASE, 0x15c);
4485 tg3_rd32_loop(tp, regs, RCVDBDI_MODE, 0x0c);
4486 tg3_rd32_loop(tp, regs, RCVDBDI_JUMBO_BD, 0x3c);
4487 tg3_rd32_loop(tp, regs, RCVDBDI_BD_PROD_IDX_0, 0x44);
4488 tg3_rd32_loop(tp, regs, RCVDCC_MODE, 0x04);
4489 tg3_rd32_loop(tp, regs, RCVBDI_MODE, 0x20);
4490 tg3_rd32_loop(tp, regs, RCVCC_MODE, 0x14);
4491 tg3_rd32_loop(tp, regs, RCVLSC_MODE, 0x08);
4492 tg3_rd32_loop(tp, regs, MBFREE_MODE, 0x08);
4493 tg3_rd32_loop(tp, regs, HOSTCC_MODE, 0x100);
4494
4495 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX)
4496 tg3_rd32_loop(tp, regs, HOSTCC_RXCOL_TICKS_VEC1, 0x180);
4497
4498 tg3_rd32_loop(tp, regs, MEMARB_MODE, 0x10);
4499 tg3_rd32_loop(tp, regs, BUFMGR_MODE, 0x58);
4500 tg3_rd32_loop(tp, regs, RDMAC_MODE, 0x08);
4501 tg3_rd32_loop(tp, regs, WDMAC_MODE, 0x08);
4502 tg3_rd32_loop(tp, regs, RX_CPU_MODE, 0x04);
4503 tg3_rd32_loop(tp, regs, RX_CPU_STATE, 0x04);
4504 tg3_rd32_loop(tp, regs, RX_CPU_PGMCTR, 0x04);
4505 tg3_rd32_loop(tp, regs, RX_CPU_HWBKPT, 0x04);
4506
4507 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
4508 tg3_rd32_loop(tp, regs, TX_CPU_MODE, 0x04);
4509 tg3_rd32_loop(tp, regs, TX_CPU_STATE, 0x04);
4510 tg3_rd32_loop(tp, regs, TX_CPU_PGMCTR, 0x04);
4511 }
4512
4513 tg3_rd32_loop(tp, regs, GRCMBOX_INTERRUPT_0, 0x110);
4514 tg3_rd32_loop(tp, regs, FTQ_RESET, 0x120);
4515 tg3_rd32_loop(tp, regs, MSGINT_MODE, 0x0c);
4516 tg3_rd32_loop(tp, regs, DMAC_MODE, 0x04);
4517 tg3_rd32_loop(tp, regs, GRC_MODE, 0x4c);
4518
4519 if (tp->tg3_flags & TG3_FLAG_NVRAM)
4520 tg3_rd32_loop(tp, regs, NVRAM_CMD, 0x24);
4521}
4522
4523static void tg3_dump_state(struct tg3 *tp)
4524{
4525 int i;
4526 u32 *regs;
4527
4528 regs = kzalloc(TG3_REG_BLK_SIZE, GFP_ATOMIC);
4529 if (!regs) {
4530 netdev_err(tp->dev, "Failed allocating register dump buffer\n");
4531 return;
4532 }
4533
4534 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
4535 /* Read up to but not including private PCI registers */
4536 for (i = 0; i < TG3_PCIE_TLDLPL_PORT; i += sizeof(u32))
4537 regs[i / sizeof(u32)] = tr32(i);
4538 } else
4539 tg3_dump_legacy_regs(tp, regs);
4540
4541 for (i = 0; i < TG3_REG_BLK_SIZE / sizeof(u32); i += 4) {
4542 if (!regs[i + 0] && !regs[i + 1] &&
4543 !regs[i + 2] && !regs[i + 3])
4544 continue;
4545
4546 netdev_err(tp->dev, "0x%08x: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
4547 i * 4,
4548 regs[i + 0], regs[i + 1], regs[i + 2], regs[i + 3]);
4549 }
4550
4551 kfree(regs);
4552
4553 for (i = 0; i < tp->irq_cnt; i++) {
4554 struct tg3_napi *tnapi = &tp->napi[i];
4555
4556 /* SW status block */
4557 netdev_err(tp->dev,
4558 "%d: Host status block [%08x:%08x:(%04x:%04x:%04x):(%04x:%04x)]\n",
4559 i,
4560 tnapi->hw_status->status,
4561 tnapi->hw_status->status_tag,
4562 tnapi->hw_status->rx_jumbo_consumer,
4563 tnapi->hw_status->rx_consumer,
4564 tnapi->hw_status->rx_mini_consumer,
4565 tnapi->hw_status->idx[0].rx_producer,
4566 tnapi->hw_status->idx[0].tx_consumer);
4567
4568 netdev_err(tp->dev,
4569 "%d: NAPI info [%08x:%08x:(%04x:%04x:%04x):%04x:(%04x:%04x:%04x:%04x)]\n",
4570 i,
4571 tnapi->last_tag, tnapi->last_irq_tag,
4572 tnapi->tx_prod, tnapi->tx_cons, tnapi->tx_pending,
4573 tnapi->rx_rcb_ptr,
4574 tnapi->prodring.rx_std_prod_idx,
4575 tnapi->prodring.rx_std_cons_idx,
4576 tnapi->prodring.rx_jmb_prod_idx,
4577 tnapi->prodring.rx_jmb_cons_idx);
4578 }
4579}
4580
Michael Chandf3e6542006-05-26 17:48:07 -07004581/* This is called whenever we suspect that the system chipset is re-
4582 * ordering the sequence of MMIO to the tx send mailbox. The symptom
4583 * is bogus tx completions. We try to recover by setting the
4584 * TG3_FLAG_MBOX_WRITE_REORDER flag and resetting the chip later
4585 * in the workqueue.
4586 */
4587static void tg3_tx_recover(struct tg3 *tp)
4588{
4589 BUG_ON((tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER) ||
4590 tp->write32_tx_mbox == tg3_write_indirect_mbox);
4591
Matt Carlson5129c3a2010-04-05 10:19:23 +00004592 netdev_warn(tp->dev,
4593 "The system may be re-ordering memory-mapped I/O "
4594 "cycles to the network device, attempting to recover. "
4595 "Please report the problem to the driver maintainer "
4596 "and include system chipset information.\n");
Michael Chandf3e6542006-05-26 17:48:07 -07004597
4598 spin_lock(&tp->lock);
Michael Chandf3e6542006-05-26 17:48:07 -07004599 tp->tg3_flags |= TG3_FLAG_TX_RECOVERY_PENDING;
Michael Chandf3e6542006-05-26 17:48:07 -07004600 spin_unlock(&tp->lock);
4601}
4602
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004603static inline u32 tg3_tx_avail(struct tg3_napi *tnapi)
Michael Chan1b2a7202006-08-07 21:46:02 -07004604{
Matt Carlsonf65aac12010-08-02 11:26:03 +00004605 /* Tell compiler to fetch tx indices from memory. */
4606 barrier();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004607 return tnapi->tx_pending -
4608 ((tnapi->tx_prod - tnapi->tx_cons) & (TG3_TX_RING_SIZE - 1));
Michael Chan1b2a7202006-08-07 21:46:02 -07004609}
4610
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611/* Tigon3 never reports partial packet sends. So we do not
4612 * need special logic to handle SKBs that have not had all
4613 * of their frags sent yet, like SunGEM does.
4614 */
Matt Carlson17375d22009-08-28 14:02:18 +00004615static void tg3_tx(struct tg3_napi *tnapi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616{
Matt Carlson17375d22009-08-28 14:02:18 +00004617 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00004618 u32 hw_idx = tnapi->hw_status->idx[0].tx_consumer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004619 u32 sw_idx = tnapi->tx_cons;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004620 struct netdev_queue *txq;
4621 int index = tnapi - tp->napi;
4622
Matt Carlson19cfaec2009-12-03 08:36:20 +00004623 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004624 index--;
4625
4626 txq = netdev_get_tx_queue(tp->dev, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
4628 while (sw_idx != hw_idx) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00004629 struct ring_info *ri = &tnapi->tx_buffers[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 struct sk_buff *skb = ri->skb;
Michael Chandf3e6542006-05-26 17:48:07 -07004631 int i, tx_bug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Michael Chandf3e6542006-05-26 17:48:07 -07004633 if (unlikely(skb == NULL)) {
4634 tg3_tx_recover(tp);
4635 return;
4636 }
4637
Alexander Duyckf4188d82009-12-02 16:48:38 +00004638 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004639 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004640 skb_headlen(skb),
4641 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
4643 ri->skb = NULL;
4644
4645 sw_idx = NEXT_TX(sw_idx);
4646
4647 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004648 ri = &tnapi->tx_buffers[sw_idx];
Michael Chandf3e6542006-05-26 17:48:07 -07004649 if (unlikely(ri->skb != NULL || sw_idx == hw_idx))
4650 tx_bug = 1;
Alexander Duyckf4188d82009-12-02 16:48:38 +00004651
4652 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004653 dma_unmap_addr(ri, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00004654 skb_shinfo(skb)->frags[i].size,
4655 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 sw_idx = NEXT_TX(sw_idx);
4657 }
4658
David S. Millerf47c11e2005-06-24 20:18:35 -07004659 dev_kfree_skb(skb);
Michael Chandf3e6542006-05-26 17:48:07 -07004660
4661 if (unlikely(tx_bug)) {
4662 tg3_tx_recover(tp);
4663 return;
4664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 }
4666
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004667 tnapi->tx_cons = sw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
Michael Chan1b2a7202006-08-07 21:46:02 -07004669 /* Need to make the tx_cons update visible to tg3_start_xmit()
4670 * before checking for netif_queue_stopped(). Without the
4671 * memory barrier, there is a small possibility that tg3_start_xmit()
4672 * will miss it and cause the queue to be stopped forever.
4673 */
4674 smp_mb();
4675
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004676 if (unlikely(netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004677 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004678 __netif_tx_lock(txq, smp_processor_id());
4679 if (netif_tx_queue_stopped(txq) &&
Matt Carlsonf3f3f272009-08-28 14:03:21 +00004680 (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi)))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00004681 netif_tx_wake_queue(txq);
4682 __netif_tx_unlock(txq);
Michael Chan51b91462005-09-01 17:41:28 -07004683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
4685
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004686static void tg3_rx_skb_free(struct tg3 *tp, struct ring_info *ri, u32 map_sz)
4687{
4688 if (!ri->skb)
4689 return;
4690
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004691 pci_unmap_single(tp->pdev, dma_unmap_addr(ri, mapping),
Matt Carlson2b2cdb62009-11-13 13:03:48 +00004692 map_sz, PCI_DMA_FROMDEVICE);
4693 dev_kfree_skb_any(ri->skb);
4694 ri->skb = NULL;
4695}
4696
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697/* Returns size of skb allocated or < 0 on error.
4698 *
4699 * We only need to fill in the address because the other members
4700 * of the RX descriptor are invariant, see tg3_init_rings.
4701 *
4702 * Note the purposeful assymetry of cpu vs. chip accesses. For
4703 * posting buffers we only dirty the first cache line of the RX
4704 * descriptor (containing the address). Whereas for the RX status
4705 * buffers the cpu only reads the last cacheline of the RX descriptor
4706 * (to fetch the error flags, vlan tag, checksum, and opaque cookie).
4707 */
Matt Carlson86b21e52009-11-13 13:03:45 +00004708static int tg3_alloc_rx_skb(struct tg3 *tp, struct tg3_rx_prodring_set *tpr,
Matt Carlsona3896162009-11-13 13:03:44 +00004709 u32 opaque_key, u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710{
4711 struct tg3_rx_buffer_desc *desc;
Matt Carlsonf94e2902010-10-14 10:37:42 +00004712 struct ring_info *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 struct sk_buff *skb;
4714 dma_addr_t mapping;
4715 int skb_size, dest_idx;
4716
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 switch (opaque_key) {
4718 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004719 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlson21f581a2009-08-28 14:00:25 +00004720 desc = &tpr->rx_std[dest_idx];
4721 map = &tpr->rx_std_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004722 skb_size = tp->rx_pkt_map_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 break;
4724
4725 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004726 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlson79ed5ac2009-08-28 14:00:55 +00004727 desc = &tpr->rx_jmb[dest_idx].std;
Matt Carlson21f581a2009-08-28 14:00:25 +00004728 map = &tpr->rx_jmb_buffers[dest_idx];
Matt Carlson287be122009-08-28 13:58:46 +00004729 skb_size = TG3_RX_JMB_MAP_SZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 break;
4731
4732 default:
4733 return -EINVAL;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735
4736 /* Do not overwrite any of the map or rp information
4737 * until we are sure we can commit to a new buffer.
4738 *
4739 * Callers depend upon this behavior and assume that
4740 * we leave everything unchanged if we fail.
4741 */
Matt Carlson287be122009-08-28 13:58:46 +00004742 skb = netdev_alloc_skb(tp->dev, skb_size + tp->rx_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 if (skb == NULL)
4744 return -ENOMEM;
4745
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 skb_reserve(skb, tp->rx_offset);
4747
Matt Carlson287be122009-08-28 13:58:46 +00004748 mapping = pci_map_single(tp->pdev, skb->data, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 PCI_DMA_FROMDEVICE);
Matt Carlsona21771d2009-11-02 14:25:31 +00004750 if (pci_dma_mapping_error(tp->pdev, mapping)) {
4751 dev_kfree_skb(skb);
4752 return -EIO;
4753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
4755 map->skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004756 dma_unmap_addr_set(map, mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 desc->addr_hi = ((u64)mapping >> 32);
4759 desc->addr_lo = ((u64)mapping & 0xffffffff);
4760
4761 return skb_size;
4762}
4763
4764/* We only need to move over in the address because the other
4765 * members of the RX descriptor are invariant. See notes above
4766 * tg3_alloc_rx_skb for full details.
4767 */
Matt Carlsona3896162009-11-13 13:03:44 +00004768static void tg3_recycle_rx(struct tg3_napi *tnapi,
4769 struct tg3_rx_prodring_set *dpr,
4770 u32 opaque_key, int src_idx,
4771 u32 dest_idx_unmasked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772{
Matt Carlson17375d22009-08-28 14:02:18 +00004773 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 struct tg3_rx_buffer_desc *src_desc, *dest_desc;
4775 struct ring_info *src_map, *dest_map;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004776 struct tg3_rx_prodring_set *spr = &tp->napi[0].prodring;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00004777 int dest_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
4779 switch (opaque_key) {
4780 case RXD_OPAQUE_RING_STD:
Matt Carlson2c49a442010-09-30 10:34:35 +00004781 dest_idx = dest_idx_unmasked & tp->rx_std_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004782 dest_desc = &dpr->rx_std[dest_idx];
4783 dest_map = &dpr->rx_std_buffers[dest_idx];
4784 src_desc = &spr->rx_std[src_idx];
4785 src_map = &spr->rx_std_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 break;
4787
4788 case RXD_OPAQUE_RING_JUMBO:
Matt Carlson2c49a442010-09-30 10:34:35 +00004789 dest_idx = dest_idx_unmasked & tp->rx_jmb_ring_mask;
Matt Carlsona3896162009-11-13 13:03:44 +00004790 dest_desc = &dpr->rx_jmb[dest_idx].std;
4791 dest_map = &dpr->rx_jmb_buffers[dest_idx];
4792 src_desc = &spr->rx_jmb[src_idx].std;
4793 src_map = &spr->rx_jmb_buffers[src_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 break;
4795
4796 default:
4797 return;
Stephen Hemminger855e1112008-04-16 16:37:28 -07004798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
4800 dest_map->skb = src_map->skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004801 dma_unmap_addr_set(dest_map, mapping,
4802 dma_unmap_addr(src_map, mapping));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 dest_desc->addr_hi = src_desc->addr_hi;
4804 dest_desc->addr_lo = src_desc->addr_lo;
Matt Carlsone92967b2010-02-12 14:47:06 +00004805
4806 /* Ensure that the update to the skb happens after the physical
4807 * addresses have been transferred to the new BD location.
4808 */
4809 smp_wmb();
4810
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 src_map->skb = NULL;
4812}
4813
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814/* The RX ring scheme is composed of multiple rings which post fresh
4815 * buffers to the chip, and one special ring the chip uses to report
4816 * status back to the host.
4817 *
4818 * The special ring reports the status of received packets to the
4819 * host. The chip does not write into the original descriptor the
4820 * RX buffer was obtained from. The chip simply takes the original
4821 * descriptor as provided by the host, updates the status and length
4822 * field, then writes this into the next status ring entry.
4823 *
4824 * Each ring the host uses to post buffers to the chip is described
4825 * by a TG3_BDINFO entry in the chips SRAM area. When a packet arrives,
4826 * it is first placed into the on-chip ram. When the packet's length
4827 * is known, it walks down the TG3_BDINFO entries to select the ring.
4828 * Each TG3_BDINFO specifies a MAXLEN field and the first TG3_BDINFO
4829 * which is within the range of the new packet's length is chosen.
4830 *
4831 * The "separate ring for rx status" scheme may sound queer, but it makes
4832 * sense from a cache coherency perspective. If only the host writes
4833 * to the buffer post rings, and only the chip writes to the rx status
4834 * rings, then cache lines never move beyond shared-modified state.
4835 * If both the host and chip were to write into the same ring, cache line
4836 * eviction could occur since both entities want it in an exclusive state.
4837 */
Matt Carlson17375d22009-08-28 14:02:18 +00004838static int tg3_rx(struct tg3_napi *tnapi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839{
Matt Carlson17375d22009-08-28 14:02:18 +00004840 struct tg3 *tp = tnapi->tp;
Michael Chanf92905d2006-06-29 20:14:29 -07004841 u32 work_mask, rx_std_posted = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004842 u32 std_prod_idx, jmb_prod_idx;
Matt Carlson72334482009-08-28 14:03:01 +00004843 u32 sw_idx = tnapi->rx_rcb_ptr;
Michael Chan483ba502005-04-25 15:14:03 -07004844 u16 hw_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 int received;
Matt Carlson8fea32b2010-09-15 08:59:58 +00004846 struct tg3_rx_prodring_set *tpr = &tnapi->prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004848 hw_idx = *(tnapi->rx_rcb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 /*
4850 * We need to order the read of hw_idx and the read of
4851 * the opaque cookie.
4852 */
4853 rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 work_mask = 0;
4855 received = 0;
Matt Carlson43619352009-11-13 13:03:47 +00004856 std_prod_idx = tpr->rx_std_prod_idx;
4857 jmb_prod_idx = tpr->rx_jmb_prod_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 while (sw_idx != hw_idx && budget > 0) {
Matt Carlsonafc081f2009-11-13 13:03:43 +00004859 struct ring_info *ri;
Matt Carlson72334482009-08-28 14:03:01 +00004860 struct tg3_rx_buffer_desc *desc = &tnapi->rx_rcb[sw_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 unsigned int len;
4862 struct sk_buff *skb;
4863 dma_addr_t dma_addr;
4864 u32 opaque_key, desc_idx, *post_ptr;
4865
4866 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
4867 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
4868 if (opaque_key == RXD_OPAQUE_RING_STD) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004869 ri = &tp->napi[0].prodring.rx_std_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004870 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004871 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004872 post_ptr = &std_prod_idx;
Michael Chanf92905d2006-06-29 20:14:29 -07004873 rx_std_posted++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 } else if (opaque_key == RXD_OPAQUE_RING_JUMBO) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00004875 ri = &tp->napi[0].prodring.rx_jmb_buffers[desc_idx];
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00004876 dma_addr = dma_unmap_addr(ri, mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00004877 skb = ri->skb;
Matt Carlson43619352009-11-13 13:03:47 +00004878 post_ptr = &jmb_prod_idx;
Matt Carlson21f581a2009-08-28 14:00:25 +00004879 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 goto next_pkt_nopost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
4882 work_mask |= opaque_key;
4883
4884 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
4885 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII)) {
4886 drop_it:
Matt Carlsona3896162009-11-13 13:03:44 +00004887 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 desc_idx, *post_ptr);
4889 drop_it_no_recycle:
4890 /* Other statistics kept track of by card. */
Eric Dumazetb0057c52010-10-10 19:55:52 +00004891 tp->rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 goto next_pkt;
4893 }
4894
Matt Carlsonad829262008-11-21 17:16:16 -08004895 len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) -
4896 ETH_FCS_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
Matt Carlsond2757fc2010-04-12 06:58:27 +00004898 if (len > TG3_RX_COPY_THRESH(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 int skb_size;
4900
Matt Carlson86b21e52009-11-13 13:03:45 +00004901 skb_size = tg3_alloc_rx_skb(tp, tpr, opaque_key,
Matt Carlsonafc081f2009-11-13 13:03:43 +00004902 *post_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 if (skb_size < 0)
4904 goto drop_it;
4905
Matt Carlson287be122009-08-28 13:58:46 +00004906 pci_unmap_single(tp->pdev, dma_addr, skb_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 PCI_DMA_FROMDEVICE);
4908
Matt Carlson61e800c2010-02-17 15:16:54 +00004909 /* Ensure that the update to the skb happens
4910 * after the usage of the old DMA mapping.
4911 */
4912 smp_wmb();
4913
4914 ri->skb = NULL;
4915
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 skb_put(skb, len);
4917 } else {
4918 struct sk_buff *copy_skb;
4919
Matt Carlsona3896162009-11-13 13:03:44 +00004920 tg3_recycle_rx(tnapi, tpr, opaque_key,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 desc_idx, *post_ptr);
4922
Matt Carlsonbf933c82011-01-25 15:58:49 +00004923 copy_skb = netdev_alloc_skb(tp->dev, len +
Matt Carlson9dc7a112010-04-12 06:58:28 +00004924 TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 if (copy_skb == NULL)
4926 goto drop_it_no_recycle;
4927
Matt Carlsonbf933c82011-01-25 15:58:49 +00004928 skb_reserve(copy_skb, TG3_RAW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 skb_put(copy_skb, len);
4930 pci_dma_sync_single_for_cpu(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03004931 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 pci_dma_sync_single_for_device(tp->pdev, dma_addr, len, PCI_DMA_FROMDEVICE);
4933
4934 /* We'll reuse the original ring buffer. */
4935 skb = copy_skb;
4936 }
4937
Michał Mirosławdc668912011-04-07 03:35:07 +00004938 if ((tp->dev->features & NETIF_F_RXCSUM) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 (desc->type_flags & RXD_FLAG_TCPUDP_CSUM) &&
4940 (((desc->ip_tcp_csum & RXD_TCPCSUM_MASK)
4941 >> RXD_TCPCSUM_SHIFT) == 0xffff))
4942 skb->ip_summed = CHECKSUM_UNNECESSARY;
4943 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07004944 skb_checksum_none_assert(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
4946 skb->protocol = eth_type_trans(skb, tp->dev);
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004947
4948 if (len > (tp->dev->mtu + ETH_HLEN) &&
4949 skb->protocol != htons(ETH_P_8021Q)) {
4950 dev_kfree_skb(skb);
Eric Dumazetb0057c52010-10-10 19:55:52 +00004951 goto drop_it_no_recycle;
Matt Carlsonf7b493e2009-02-25 14:21:52 +00004952 }
4953
Matt Carlson9dc7a112010-04-12 06:58:28 +00004954 if (desc->type_flags & RXD_FLAG_VLAN &&
Matt Carlsonbf933c82011-01-25 15:58:49 +00004955 !(tp->rx_mode & RX_MODE_KEEP_VLAN_TAG))
4956 __vlan_hwaccel_put_tag(skb,
4957 desc->err_vlan & RXD_VLAN_MASK);
Matt Carlson9dc7a112010-04-12 06:58:28 +00004958
Matt Carlsonbf933c82011-01-25 15:58:49 +00004959 napi_gro_receive(&tnapi->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 received++;
4962 budget--;
4963
4964next_pkt:
4965 (*post_ptr)++;
Michael Chanf92905d2006-06-29 20:14:29 -07004966
4967 if (unlikely(rx_std_posted >= tp->rx_std_max_post)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004968 tpr->rx_std_prod_idx = std_prod_idx &
4969 tp->rx_std_ring_mask;
Matt Carlson86cfe4f2010-01-12 10:11:37 +00004970 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4971 tpr->rx_std_prod_idx);
Michael Chanf92905d2006-06-29 20:14:29 -07004972 work_mask &= ~RXD_OPAQUE_RING_STD;
4973 rx_std_posted = 0;
4974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975next_pkt_nopost:
Michael Chan483ba502005-04-25 15:14:03 -07004976 sw_idx++;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00004977 sw_idx &= tp->rx_ret_ring_mask;
Michael Chan52f6d692005-04-25 15:14:32 -07004978
4979 /* Refresh hw_idx to see if there is new work */
4980 if (sw_idx == hw_idx) {
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00004981 hw_idx = *(tnapi->rx_rcb_prod_idx);
Michael Chan52f6d692005-04-25 15:14:32 -07004982 rmb();
4983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 }
4985
4986 /* ACK the status ring. */
Matt Carlson72334482009-08-28 14:03:01 +00004987 tnapi->rx_rcb_ptr = sw_idx;
4988 tw32_rx_mbox(tnapi->consmbox, sw_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
4990 /* Refill RX ring(s). */
Matt Carlsone4af1af2010-02-12 14:47:05 +00004991 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004992 if (work_mask & RXD_OPAQUE_RING_STD) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004993 tpr->rx_std_prod_idx = std_prod_idx &
4994 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00004995 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
4996 tpr->rx_std_prod_idx);
4997 }
4998 if (work_mask & RXD_OPAQUE_RING_JUMBO) {
Matt Carlson2c49a442010-09-30 10:34:35 +00004999 tpr->rx_jmb_prod_idx = jmb_prod_idx &
5000 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005001 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5002 tpr->rx_jmb_prod_idx);
5003 }
5004 mmiowb();
5005 } else if (work_mask) {
5006 /* rx_std_buffers[] and rx_jmb_buffers[] entries must be
5007 * updated before the producer indices can be updated.
5008 */
5009 smp_wmb();
5010
Matt Carlson2c49a442010-09-30 10:34:35 +00005011 tpr->rx_std_prod_idx = std_prod_idx & tp->rx_std_ring_mask;
5012 tpr->rx_jmb_prod_idx = jmb_prod_idx & tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005013
Matt Carlsone4af1af2010-02-12 14:47:05 +00005014 if (tnapi != &tp->napi[1])
5015 napi_schedule(&tp->napi[1].napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017
5018 return received;
5019}
5020
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005021static void tg3_poll_link(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 /* handle link change and other phy events */
5024 if (!(tp->tg3_flags &
5025 (TG3_FLAG_USE_LINKCHG_REG |
5026 TG3_FLAG_POLL_SERDES))) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005027 struct tg3_hw_status *sblk = tp->napi[0].hw_status;
5028
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 if (sblk->status & SD_STATUS_LINK_CHG) {
5030 sblk->status = SD_STATUS_UPDATED |
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005031 (sblk->status & ~SD_STATUS_LINK_CHG);
David S. Millerf47c11e2005-06-24 20:18:35 -07005032 spin_lock(&tp->lock);
Matt Carlsondd477002008-05-25 23:45:58 -07005033 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
5034 tw32_f(MAC_STATUS,
5035 (MAC_STATUS_SYNC_CHANGED |
5036 MAC_STATUS_CFG_CHANGED |
5037 MAC_STATUS_MI_COMPLETION |
5038 MAC_STATUS_LNKSTATE_CHANGED));
5039 udelay(40);
5040 } else
5041 tg3_setup_phy(tp, 0);
David S. Millerf47c11e2005-06-24 20:18:35 -07005042 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 }
5044 }
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005045}
5046
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005047static int tg3_rx_prodring_xfer(struct tg3 *tp,
5048 struct tg3_rx_prodring_set *dpr,
5049 struct tg3_rx_prodring_set *spr)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005050{
5051 u32 si, di, cpycnt, src_prod_idx;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005052 int i, err = 0;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005053
5054 while (1) {
5055 src_prod_idx = spr->rx_std_prod_idx;
5056
5057 /* Make sure updates to the rx_std_buffers[] entries and the
5058 * standard producer index are seen in the correct order.
5059 */
5060 smp_rmb();
5061
5062 if (spr->rx_std_cons_idx == src_prod_idx)
5063 break;
5064
5065 if (spr->rx_std_cons_idx < src_prod_idx)
5066 cpycnt = src_prod_idx - spr->rx_std_cons_idx;
5067 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005068 cpycnt = tp->rx_std_ring_mask + 1 -
5069 spr->rx_std_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005070
Matt Carlson2c49a442010-09-30 10:34:35 +00005071 cpycnt = min(cpycnt,
5072 tp->rx_std_ring_mask + 1 - dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005073
5074 si = spr->rx_std_cons_idx;
5075 di = dpr->rx_std_prod_idx;
5076
Matt Carlsone92967b2010-02-12 14:47:06 +00005077 for (i = di; i < di + cpycnt; i++) {
5078 if (dpr->rx_std_buffers[i].skb) {
5079 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005080 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005081 break;
5082 }
5083 }
5084
5085 if (!cpycnt)
5086 break;
5087
5088 /* Ensure that updates to the rx_std_buffers ring and the
5089 * shadowed hardware producer ring from tg3_recycle_skb() are
5090 * ordered correctly WRT the skb check above.
5091 */
5092 smp_rmb();
5093
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005094 memcpy(&dpr->rx_std_buffers[di],
5095 &spr->rx_std_buffers[si],
5096 cpycnt * sizeof(struct ring_info));
5097
5098 for (i = 0; i < cpycnt; i++, di++, si++) {
5099 struct tg3_rx_buffer_desc *sbd, *dbd;
5100 sbd = &spr->rx_std[si];
5101 dbd = &dpr->rx_std[di];
5102 dbd->addr_hi = sbd->addr_hi;
5103 dbd->addr_lo = sbd->addr_lo;
5104 }
5105
Matt Carlson2c49a442010-09-30 10:34:35 +00005106 spr->rx_std_cons_idx = (spr->rx_std_cons_idx + cpycnt) &
5107 tp->rx_std_ring_mask;
5108 dpr->rx_std_prod_idx = (dpr->rx_std_prod_idx + cpycnt) &
5109 tp->rx_std_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005110 }
5111
5112 while (1) {
5113 src_prod_idx = spr->rx_jmb_prod_idx;
5114
5115 /* Make sure updates to the rx_jmb_buffers[] entries and
5116 * the jumbo producer index are seen in the correct order.
5117 */
5118 smp_rmb();
5119
5120 if (spr->rx_jmb_cons_idx == src_prod_idx)
5121 break;
5122
5123 if (spr->rx_jmb_cons_idx < src_prod_idx)
5124 cpycnt = src_prod_idx - spr->rx_jmb_cons_idx;
5125 else
Matt Carlson2c49a442010-09-30 10:34:35 +00005126 cpycnt = tp->rx_jmb_ring_mask + 1 -
5127 spr->rx_jmb_cons_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005128
5129 cpycnt = min(cpycnt,
Matt Carlson2c49a442010-09-30 10:34:35 +00005130 tp->rx_jmb_ring_mask + 1 - dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005131
5132 si = spr->rx_jmb_cons_idx;
5133 di = dpr->rx_jmb_prod_idx;
5134
Matt Carlsone92967b2010-02-12 14:47:06 +00005135 for (i = di; i < di + cpycnt; i++) {
5136 if (dpr->rx_jmb_buffers[i].skb) {
5137 cpycnt = i - di;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005138 err = -ENOSPC;
Matt Carlsone92967b2010-02-12 14:47:06 +00005139 break;
5140 }
5141 }
5142
5143 if (!cpycnt)
5144 break;
5145
5146 /* Ensure that updates to the rx_jmb_buffers ring and the
5147 * shadowed hardware producer ring from tg3_recycle_skb() are
5148 * ordered correctly WRT the skb check above.
5149 */
5150 smp_rmb();
5151
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005152 memcpy(&dpr->rx_jmb_buffers[di],
5153 &spr->rx_jmb_buffers[si],
5154 cpycnt * sizeof(struct ring_info));
5155
5156 for (i = 0; i < cpycnt; i++, di++, si++) {
5157 struct tg3_rx_buffer_desc *sbd, *dbd;
5158 sbd = &spr->rx_jmb[si].std;
5159 dbd = &dpr->rx_jmb[di].std;
5160 dbd->addr_hi = sbd->addr_hi;
5161 dbd->addr_lo = sbd->addr_lo;
5162 }
5163
Matt Carlson2c49a442010-09-30 10:34:35 +00005164 spr->rx_jmb_cons_idx = (spr->rx_jmb_cons_idx + cpycnt) &
5165 tp->rx_jmb_ring_mask;
5166 dpr->rx_jmb_prod_idx = (dpr->rx_jmb_prod_idx + cpycnt) &
5167 tp->rx_jmb_ring_mask;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005168 }
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005169
5170 return err;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005171}
5172
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005173static int tg3_poll_work(struct tg3_napi *tnapi, int work_done, int budget)
5174{
5175 struct tg3 *tp = tnapi->tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176
5177 /* run TX completion thread */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005178 if (tnapi->hw_status->idx[0].tx_consumer != tnapi->tx_cons) {
Matt Carlson17375d22009-08-28 14:02:18 +00005179 tg3_tx(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005180 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
Michael Chan4fd7ab52007-10-12 01:39:50 -07005181 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 }
5183
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 /* run RX thread, within the bounds set by NAPI.
5185 * All RX "locking" is done by ensuring outside
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005186 * code synchronizes with tg3->napi.poll()
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 */
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00005188 if (*(tnapi->rx_rcb_prod_idx) != tnapi->rx_rcb_ptr)
Matt Carlson17375d22009-08-28 14:02:18 +00005189 work_done += tg3_rx(tnapi, budget - work_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005191 if ((tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) && tnapi == &tp->napi[1]) {
Matt Carlson8fea32b2010-09-15 08:59:58 +00005192 struct tg3_rx_prodring_set *dpr = &tp->napi[0].prodring;
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005193 int i, err = 0;
Matt Carlsone4af1af2010-02-12 14:47:05 +00005194 u32 std_prod_idx = dpr->rx_std_prod_idx;
5195 u32 jmb_prod_idx = dpr->rx_jmb_prod_idx;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005196
Matt Carlsone4af1af2010-02-12 14:47:05 +00005197 for (i = 1; i < tp->irq_cnt; i++)
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005198 err |= tg3_rx_prodring_xfer(tp, dpr,
Matt Carlson8fea32b2010-09-15 08:59:58 +00005199 &tp->napi[i].prodring);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005200
5201 wmb();
5202
Matt Carlsone4af1af2010-02-12 14:47:05 +00005203 if (std_prod_idx != dpr->rx_std_prod_idx)
5204 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG,
5205 dpr->rx_std_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005206
Matt Carlsone4af1af2010-02-12 14:47:05 +00005207 if (jmb_prod_idx != dpr->rx_jmb_prod_idx)
5208 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG,
5209 dpr->rx_jmb_prod_idx);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005210
5211 mmiowb();
Matt Carlsonf89f38b2010-02-12 14:47:07 +00005212
5213 if (err)
5214 tw32_f(HOSTCC_MODE, tp->coal_now);
Matt Carlsonb196c7e2009-11-13 13:03:50 +00005215 }
5216
David S. Miller6f535762007-10-11 18:08:29 -07005217 return work_done;
5218}
David S. Millerf7383c22005-05-18 22:50:53 -07005219
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005220static int tg3_poll_msix(struct napi_struct *napi, int budget)
5221{
5222 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5223 struct tg3 *tp = tnapi->tp;
5224 int work_done = 0;
5225 struct tg3_hw_status *sblk = tnapi->hw_status;
5226
5227 while (1) {
5228 work_done = tg3_poll_work(tnapi, work_done, budget);
5229
5230 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5231 goto tx_recovery;
5232
5233 if (unlikely(work_done >= budget))
5234 break;
5235
Matt Carlsonc6cdf432010-04-05 10:19:26 +00005236 /* tp->last_tag is used in tg3_int_reenable() below
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005237 * to tell the hw how much work has been processed,
5238 * so we must read it before checking for more work.
5239 */
5240 tnapi->last_tag = sblk->status_tag;
5241 tnapi->last_irq_tag = tnapi->last_tag;
5242 rmb();
5243
5244 /* check for RX/TX work to do */
Matt Carlson6d40db72010-04-05 10:19:20 +00005245 if (likely(sblk->idx[0].tx_consumer == tnapi->tx_cons &&
5246 *(tnapi->rx_rcb_prod_idx) == tnapi->rx_rcb_ptr)) {
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005247 napi_complete(napi);
5248 /* Reenable interrupts. */
5249 tw32_mailbox(tnapi->int_mbox, tnapi->last_tag << 24);
5250 mmiowb();
5251 break;
5252 }
5253 }
5254
5255 return work_done;
5256
5257tx_recovery:
5258 /* work_done is guaranteed to be less than budget. */
5259 napi_complete(napi);
5260 schedule_work(&tp->reset_task);
5261 return work_done;
5262}
5263
Matt Carlsone64de4e2011-04-13 11:05:05 +00005264static void tg3_process_error(struct tg3 *tp)
5265{
5266 u32 val;
5267 bool real_error = false;
5268
5269 if (tp->tg3_flags & TG3_FLAG_ERROR_PROCESSED)
5270 return;
5271
5272 /* Check Flow Attention register */
5273 val = tr32(HOSTCC_FLOW_ATTN);
5274 if (val & ~HOSTCC_FLOW_ATTN_MBUF_LWM) {
5275 netdev_err(tp->dev, "FLOW Attention error. Resetting chip.\n");
5276 real_error = true;
5277 }
5278
5279 if (tr32(MSGINT_STATUS) & ~MSGINT_STATUS_MSI_REQ) {
5280 netdev_err(tp->dev, "MSI Status error. Resetting chip.\n");
5281 real_error = true;
5282 }
5283
5284 if (tr32(RDMAC_STATUS) || tr32(WDMAC_STATUS)) {
5285 netdev_err(tp->dev, "DMA Status error. Resetting chip.\n");
5286 real_error = true;
5287 }
5288
5289 if (!real_error)
5290 return;
5291
5292 tg3_dump_state(tp);
5293
5294 tp->tg3_flags |= TG3_FLAG_ERROR_PROCESSED;
5295 schedule_work(&tp->reset_task);
5296}
5297
David S. Miller6f535762007-10-11 18:08:29 -07005298static int tg3_poll(struct napi_struct *napi, int budget)
5299{
Matt Carlson8ef04422009-08-28 14:01:37 +00005300 struct tg3_napi *tnapi = container_of(napi, struct tg3_napi, napi);
5301 struct tg3 *tp = tnapi->tp;
David S. Miller6f535762007-10-11 18:08:29 -07005302 int work_done = 0;
Matt Carlson898a56f2009-08-28 14:02:40 +00005303 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Miller6f535762007-10-11 18:08:29 -07005304
5305 while (1) {
Matt Carlsone64de4e2011-04-13 11:05:05 +00005306 if (sblk->status & SD_STATUS_ERROR)
5307 tg3_process_error(tp);
5308
Matt Carlson35f2d7d2009-11-13 13:03:41 +00005309 tg3_poll_link(tp);
5310
Matt Carlson17375d22009-08-28 14:02:18 +00005311 work_done = tg3_poll_work(tnapi, work_done, budget);
David S. Miller6f535762007-10-11 18:08:29 -07005312
5313 if (unlikely(tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING))
5314 goto tx_recovery;
5315
5316 if (unlikely(work_done >= budget))
5317 break;
5318
Michael Chan4fd7ab52007-10-12 01:39:50 -07005319 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
Matt Carlson17375d22009-08-28 14:02:18 +00005320 /* tp->last_tag is used in tg3_int_reenable() below
Michael Chan4fd7ab52007-10-12 01:39:50 -07005321 * to tell the hw how much work has been processed,
5322 * so we must read it before checking for more work.
5323 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005324 tnapi->last_tag = sblk->status_tag;
5325 tnapi->last_irq_tag = tnapi->last_tag;
Michael Chan4fd7ab52007-10-12 01:39:50 -07005326 rmb();
5327 } else
5328 sblk->status &= ~SD_STATUS_UPDATED;
5329
Matt Carlson17375d22009-08-28 14:02:18 +00005330 if (likely(!tg3_has_work(tnapi))) {
Ben Hutchings288379f2009-01-19 16:43:59 -08005331 napi_complete(napi);
Matt Carlson17375d22009-08-28 14:02:18 +00005332 tg3_int_reenable(tnapi);
David S. Miller6f535762007-10-11 18:08:29 -07005333 break;
5334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 }
5336
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005337 return work_done;
David S. Miller6f535762007-10-11 18:08:29 -07005338
5339tx_recovery:
Michael Chan4fd7ab52007-10-12 01:39:50 -07005340 /* work_done is guaranteed to be less than budget. */
Ben Hutchings288379f2009-01-19 16:43:59 -08005341 napi_complete(napi);
David S. Miller6f535762007-10-11 18:08:29 -07005342 schedule_work(&tp->reset_task);
Michael Chan4fd7ab52007-10-12 01:39:50 -07005343 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344}
5345
Matt Carlson66cfd1b2010-09-30 10:34:30 +00005346static void tg3_napi_disable(struct tg3 *tp)
5347{
5348 int i;
5349
5350 for (i = tp->irq_cnt - 1; i >= 0; i--)
5351 napi_disable(&tp->napi[i].napi);
5352}
5353
5354static void tg3_napi_enable(struct tg3 *tp)
5355{
5356 int i;
5357
5358 for (i = 0; i < tp->irq_cnt; i++)
5359 napi_enable(&tp->napi[i].napi);
5360}
5361
5362static void tg3_napi_init(struct tg3 *tp)
5363{
5364 int i;
5365
5366 netif_napi_add(tp->dev, &tp->napi[0].napi, tg3_poll, 64);
5367 for (i = 1; i < tp->irq_cnt; i++)
5368 netif_napi_add(tp->dev, &tp->napi[i].napi, tg3_poll_msix, 64);
5369}
5370
5371static void tg3_napi_fini(struct tg3 *tp)
5372{
5373 int i;
5374
5375 for (i = 0; i < tp->irq_cnt; i++)
5376 netif_napi_del(&tp->napi[i].napi);
5377}
5378
5379static inline void tg3_netif_stop(struct tg3 *tp)
5380{
5381 tp->dev->trans_start = jiffies; /* prevent tx timeout */
5382 tg3_napi_disable(tp);
5383 netif_tx_disable(tp->dev);
5384}
5385
5386static inline void tg3_netif_start(struct tg3 *tp)
5387{
5388 /* NOTE: unconditional netif_tx_wake_all_queues is only
5389 * appropriate so long as all callers are assured to
5390 * have free tx slots (such as after tg3_init_hw)
5391 */
5392 netif_tx_wake_all_queues(tp->dev);
5393
5394 tg3_napi_enable(tp);
5395 tp->napi[0].hw_status->status |= SD_STATUS_UPDATED;
5396 tg3_enable_ints(tp);
5397}
5398
David S. Millerf47c11e2005-06-24 20:18:35 -07005399static void tg3_irq_quiesce(struct tg3 *tp)
5400{
Matt Carlson4f125f42009-09-01 12:55:02 +00005401 int i;
5402
David S. Millerf47c11e2005-06-24 20:18:35 -07005403 BUG_ON(tp->irq_sync);
5404
5405 tp->irq_sync = 1;
5406 smp_mb();
5407
Matt Carlson4f125f42009-09-01 12:55:02 +00005408 for (i = 0; i < tp->irq_cnt; i++)
5409 synchronize_irq(tp->napi[i].irq_vec);
David S. Millerf47c11e2005-06-24 20:18:35 -07005410}
5411
David S. Millerf47c11e2005-06-24 20:18:35 -07005412/* Fully shutdown all tg3 driver activity elsewhere in the system.
5413 * If irq_sync is non-zero, then the IRQ handler must be synchronized
5414 * with as well. Most of the time, this is not necessary except when
5415 * shutting down the device.
5416 */
5417static inline void tg3_full_lock(struct tg3 *tp, int irq_sync)
5418{
Michael Chan46966542007-07-11 19:47:19 -07005419 spin_lock_bh(&tp->lock);
David S. Millerf47c11e2005-06-24 20:18:35 -07005420 if (irq_sync)
5421 tg3_irq_quiesce(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07005422}
5423
5424static inline void tg3_full_unlock(struct tg3 *tp)
5425{
David S. Millerf47c11e2005-06-24 20:18:35 -07005426 spin_unlock_bh(&tp->lock);
5427}
5428
Michael Chanfcfa0a32006-03-20 22:28:41 -08005429/* One-shot MSI handler - Chip automatically disables interrupt
5430 * after sending MSI so driver doesn't have to do it.
5431 */
David Howells7d12e782006-10-05 14:55:46 +01005432static irqreturn_t tg3_msi_1shot(int irq, void *dev_id)
Michael Chanfcfa0a32006-03-20 22:28:41 -08005433{
Matt Carlson09943a12009-08-28 14:01:57 +00005434 struct tg3_napi *tnapi = dev_id;
5435 struct tg3 *tp = tnapi->tp;
Michael Chanfcfa0a32006-03-20 22:28:41 -08005436
Matt Carlson898a56f2009-08-28 14:02:40 +00005437 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005438 if (tnapi->rx_rcb)
5439 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005440
5441 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005442 napi_schedule(&tnapi->napi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08005443
5444 return IRQ_HANDLED;
5445}
5446
Michael Chan88b06bc2005-04-21 17:13:25 -07005447/* MSI ISR - No need to check for interrupt sharing and no need to
5448 * flush status block and interrupt mailbox. PCI ordering rules
5449 * guarantee that MSI will arrive after the status block.
5450 */
David Howells7d12e782006-10-05 14:55:46 +01005451static irqreturn_t tg3_msi(int irq, void *dev_id)
Michael Chan88b06bc2005-04-21 17:13:25 -07005452{
Matt Carlson09943a12009-08-28 14:01:57 +00005453 struct tg3_napi *tnapi = dev_id;
5454 struct tg3 *tp = tnapi->tp;
Michael Chan88b06bc2005-04-21 17:13:25 -07005455
Matt Carlson898a56f2009-08-28 14:02:40 +00005456 prefetch(tnapi->hw_status);
Matt Carlson0c1d0e22009-09-01 13:16:33 +00005457 if (tnapi->rx_rcb)
5458 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Michael Chan88b06bc2005-04-21 17:13:25 -07005459 /*
David S. Millerfac9b832005-05-18 22:46:34 -07005460 * Writing any value to intr-mbox-0 clears PCI INTA# and
Michael Chan88b06bc2005-04-21 17:13:25 -07005461 * chip-internal interrupt pending events.
David S. Millerfac9b832005-05-18 22:46:34 -07005462 * Writing non-zero to intr-mbox-0 additional tells the
Michael Chan88b06bc2005-04-21 17:13:25 -07005463 * NIC to stop sending us irqs, engaging "in-intr-handler"
5464 * event coalescing.
5465 */
5466 tw32_mailbox(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chan61487482005-09-05 17:53:19 -07005467 if (likely(!tg3_irq_sync(tp)))
Matt Carlson09943a12009-08-28 14:01:57 +00005468 napi_schedule(&tnapi->napi);
Michael Chan61487482005-09-05 17:53:19 -07005469
Michael Chan88b06bc2005-04-21 17:13:25 -07005470 return IRQ_RETVAL(1);
5471}
5472
David Howells7d12e782006-10-05 14:55:46 +01005473static irqreturn_t tg3_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474{
Matt Carlson09943a12009-08-28 14:01:57 +00005475 struct tg3_napi *tnapi = dev_id;
5476 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005477 struct tg3_hw_status *sblk = tnapi->hw_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 unsigned int handled = 1;
5479
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 /* In INTx mode, it is possible for the interrupt to arrive at
5481 * the CPU before the status block posted prior to the interrupt.
5482 * Reading the PCI State register will confirm whether the
5483 * interrupt is ours and will flush the status block.
5484 */
Michael Chand18edcb2007-03-24 20:57:11 -07005485 if (unlikely(!(sblk->status & SD_STATUS_UPDATED))) {
5486 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5487 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5488 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005489 goto out;
David S. Millerfac9b832005-05-18 22:46:34 -07005490 }
Michael Chand18edcb2007-03-24 20:57:11 -07005491 }
5492
5493 /*
5494 * Writing any value to intr-mbox-0 clears PCI INTA# and
5495 * chip-internal interrupt pending events.
5496 * Writing non-zero to intr-mbox-0 additional tells the
5497 * NIC to stop sending us irqs, engaging "in-intr-handler"
5498 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005499 *
5500 * Flush the mailbox to de-assert the IRQ immediately to prevent
5501 * spurious interrupts. The flush impacts performance but
5502 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005503 */
Michael Chanc04cb342007-05-07 00:26:15 -07005504 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Michael Chand18edcb2007-03-24 20:57:11 -07005505 if (tg3_irq_sync(tp))
5506 goto out;
5507 sblk->status &= ~SD_STATUS_UPDATED;
Matt Carlson17375d22009-08-28 14:02:18 +00005508 if (likely(tg3_has_work(tnapi))) {
Matt Carlson72334482009-08-28 14:03:01 +00005509 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson09943a12009-08-28 14:01:57 +00005510 napi_schedule(&tnapi->napi);
Michael Chand18edcb2007-03-24 20:57:11 -07005511 } else {
5512 /* No work, shared interrupt perhaps? re-enable
5513 * interrupts, and flush that PCI write
5514 */
5515 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW,
5516 0x00000000);
David S. Millerfac9b832005-05-18 22:46:34 -07005517 }
David S. Millerf47c11e2005-06-24 20:18:35 -07005518out:
David S. Millerfac9b832005-05-18 22:46:34 -07005519 return IRQ_RETVAL(handled);
5520}
5521
David Howells7d12e782006-10-05 14:55:46 +01005522static irqreturn_t tg3_interrupt_tagged(int irq, void *dev_id)
David S. Millerfac9b832005-05-18 22:46:34 -07005523{
Matt Carlson09943a12009-08-28 14:01:57 +00005524 struct tg3_napi *tnapi = dev_id;
5525 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005526 struct tg3_hw_status *sblk = tnapi->hw_status;
David S. Millerfac9b832005-05-18 22:46:34 -07005527 unsigned int handled = 1;
5528
David S. Millerfac9b832005-05-18 22:46:34 -07005529 /* In INTx mode, it is possible for the interrupt to arrive at
5530 * the CPU before the status block posted prior to the interrupt.
5531 * Reading the PCI State register will confirm whether the
5532 * interrupt is ours and will flush the status block.
5533 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005534 if (unlikely(sblk->status_tag == tnapi->last_irq_tag)) {
Michael Chand18edcb2007-03-24 20:57:11 -07005535 if ((tp->tg3_flags & TG3_FLAG_CHIP_RESETTING) ||
5536 (tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
5537 handled = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07005538 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 }
Michael Chand18edcb2007-03-24 20:57:11 -07005540 }
5541
5542 /*
5543 * writing any value to intr-mbox-0 clears PCI INTA# and
5544 * chip-internal interrupt pending events.
5545 * writing non-zero to intr-mbox-0 additional tells the
5546 * NIC to stop sending us irqs, engaging "in-intr-handler"
5547 * event coalescing.
Michael Chanc04cb342007-05-07 00:26:15 -07005548 *
5549 * Flush the mailbox to de-assert the IRQ immediately to prevent
5550 * spurious interrupts. The flush impacts performance but
5551 * excessive spurious interrupts can be worse in some cases.
Michael Chand18edcb2007-03-24 20:57:11 -07005552 */
Michael Chanc04cb342007-05-07 00:26:15 -07005553 tw32_mailbox_f(MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW, 0x00000001);
Matt Carlson624f8e52009-04-20 06:55:01 +00005554
5555 /*
5556 * In a shared interrupt configuration, sometimes other devices'
5557 * interrupts will scream. We record the current status tag here
5558 * so that the above check can report that the screaming interrupts
5559 * are unhandled. Eventually they will be silenced.
5560 */
Matt Carlson898a56f2009-08-28 14:02:40 +00005561 tnapi->last_irq_tag = sblk->status_tag;
Matt Carlson624f8e52009-04-20 06:55:01 +00005562
Michael Chand18edcb2007-03-24 20:57:11 -07005563 if (tg3_irq_sync(tp))
5564 goto out;
Matt Carlson624f8e52009-04-20 06:55:01 +00005565
Matt Carlson72334482009-08-28 14:03:01 +00005566 prefetch(&tnapi->rx_rcb[tnapi->rx_rcb_ptr]);
Matt Carlson624f8e52009-04-20 06:55:01 +00005567
Matt Carlson09943a12009-08-28 14:01:57 +00005568 napi_schedule(&tnapi->napi);
Matt Carlson624f8e52009-04-20 06:55:01 +00005569
David S. Millerf47c11e2005-06-24 20:18:35 -07005570out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 return IRQ_RETVAL(handled);
5572}
5573
Michael Chan79381092005-04-21 17:13:59 -07005574/* ISR for interrupt test */
David Howells7d12e782006-10-05 14:55:46 +01005575static irqreturn_t tg3_test_isr(int irq, void *dev_id)
Michael Chan79381092005-04-21 17:13:59 -07005576{
Matt Carlson09943a12009-08-28 14:01:57 +00005577 struct tg3_napi *tnapi = dev_id;
5578 struct tg3 *tp = tnapi->tp;
Matt Carlson898a56f2009-08-28 14:02:40 +00005579 struct tg3_hw_status *sblk = tnapi->hw_status;
Michael Chan79381092005-04-21 17:13:59 -07005580
Michael Chanf9804dd2005-09-27 12:13:10 -07005581 if ((sblk->status & SD_STATUS_UPDATED) ||
5582 !(tr32(TG3PCI_PCISTATE) & PCISTATE_INT_NOT_ACTIVE)) {
Michael Chanb16250e2006-09-27 16:10:14 -07005583 tg3_disable_ints(tp);
Michael Chan79381092005-04-21 17:13:59 -07005584 return IRQ_RETVAL(1);
5585 }
5586 return IRQ_RETVAL(0);
5587}
5588
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07005589static int tg3_init_hw(struct tg3 *, int);
Michael Chan944d9802005-05-29 14:57:48 -07005590static int tg3_halt(struct tg3 *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
Michael Chanb9ec6c12006-07-25 16:37:27 -07005592/* Restart hardware after configuration changes, self-test, etc.
5593 * Invoked with tp->lock held.
5594 */
5595static int tg3_restart_hw(struct tg3 *tp, int reset_phy)
Eric Dumazet78c61462008-04-24 23:33:06 -07005596 __releases(tp->lock)
5597 __acquires(tp->lock)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005598{
5599 int err;
5600
5601 err = tg3_init_hw(tp, reset_phy);
5602 if (err) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00005603 netdev_err(tp->dev,
5604 "Failed to re-initialize device, aborting\n");
Michael Chanb9ec6c12006-07-25 16:37:27 -07005605 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
5606 tg3_full_unlock(tp);
5607 del_timer_sync(&tp->timer);
5608 tp->irq_sync = 0;
Matt Carlsonfed97812009-09-01 13:10:19 +00005609 tg3_napi_enable(tp);
Michael Chanb9ec6c12006-07-25 16:37:27 -07005610 dev_close(tp->dev);
5611 tg3_full_lock(tp, 0);
5612 }
5613 return err;
5614}
5615
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616#ifdef CONFIG_NET_POLL_CONTROLLER
5617static void tg3_poll_controller(struct net_device *dev)
5618{
Matt Carlson4f125f42009-09-01 12:55:02 +00005619 int i;
Michael Chan88b06bc2005-04-21 17:13:25 -07005620 struct tg3 *tp = netdev_priv(dev);
5621
Matt Carlson4f125f42009-09-01 12:55:02 +00005622 for (i = 0; i < tp->irq_cnt; i++)
Louis Rillingfe234f02010-03-09 06:14:41 +00005623 tg3_interrupt(tp->napi[i].irq_vec, &tp->napi[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624}
5625#endif
5626
David Howellsc4028952006-11-22 14:57:56 +00005627static void tg3_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628{
David Howellsc4028952006-11-22 14:57:56 +00005629 struct tg3 *tp = container_of(work, struct tg3, reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005630 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 unsigned int restart_timer;
5632
Michael Chan7faa0062006-02-02 17:29:28 -08005633 tg3_full_lock(tp, 0);
Michael Chan7faa0062006-02-02 17:29:28 -08005634
5635 if (!netif_running(tp->dev)) {
Michael Chan7faa0062006-02-02 17:29:28 -08005636 tg3_full_unlock(tp);
5637 return;
5638 }
5639
5640 tg3_full_unlock(tp);
5641
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005642 tg3_phy_stop(tp);
5643
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 tg3_netif_stop(tp);
5645
David S. Millerf47c11e2005-06-24 20:18:35 -07005646 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647
5648 restart_timer = tp->tg3_flags2 & TG3_FLG2_RESTART_TIMER;
5649 tp->tg3_flags2 &= ~TG3_FLG2_RESTART_TIMER;
5650
Michael Chandf3e6542006-05-26 17:48:07 -07005651 if (tp->tg3_flags & TG3_FLAG_TX_RECOVERY_PENDING) {
5652 tp->write32_tx_mbox = tg3_write32_tx_mbox;
5653 tp->write32_rx_mbox = tg3_write_flush_reg32;
5654 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
5655 tp->tg3_flags &= ~TG3_FLAG_TX_RECOVERY_PENDING;
5656 }
5657
Michael Chan944d9802005-05-29 14:57:48 -07005658 tg3_halt(tp, RESET_KIND_SHUTDOWN, 0);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005659 err = tg3_init_hw(tp, 1);
5660 if (err)
Michael Chanb9ec6c12006-07-25 16:37:27 -07005661 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662
5663 tg3_netif_start(tp);
5664
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 if (restart_timer)
5666 mod_timer(&tp->timer, jiffies + 1);
Michael Chan7faa0062006-02-02 17:29:28 -08005667
Michael Chanb9ec6c12006-07-25 16:37:27 -07005668out:
Michael Chan7faa0062006-02-02 17:29:28 -08005669 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07005670
5671 if (!err)
5672 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673}
5674
5675static void tg3_tx_timeout(struct net_device *dev)
5676{
5677 struct tg3 *tp = netdev_priv(dev);
5678
Michael Chanb0408752007-02-13 12:18:30 -08005679 if (netif_msg_tx_err(tp)) {
Joe Perches05dbe002010-02-17 19:44:19 +00005680 netdev_err(dev, "transmit timed out, resetting\n");
Matt Carlson97bd8e42011-04-13 11:05:04 +00005681 tg3_dump_state(tp);
Michael Chanb0408752007-02-13 12:18:30 -08005682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
5684 schedule_work(&tp->reset_task);
5685}
5686
Michael Chanc58ec932005-09-17 00:46:27 -07005687/* Test for DMA buffers crossing any 4GB boundaries: 4G, 8G, etc */
5688static inline int tg3_4g_overflow_test(dma_addr_t mapping, int len)
5689{
5690 u32 base = (u32) mapping & 0xffffffff;
5691
Eric Dumazet807540b2010-09-23 05:40:09 +00005692 return (base > 0xffffdcc0) && (base + len + 8 < base);
Michael Chanc58ec932005-09-17 00:46:27 -07005693}
5694
Michael Chan72f2afb2006-03-06 19:28:35 -08005695/* Test for DMA addresses > 40-bit */
5696static inline int tg3_40bit_overflow_test(struct tg3 *tp, dma_addr_t mapping,
5697 int len)
5698{
5699#if defined(CONFIG_HIGHMEM) && (BITS_PER_LONG == 64)
Michael Chan6728a8e2006-03-27 23:16:49 -08005700 if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG)
Eric Dumazet807540b2010-09-23 05:40:09 +00005701 return ((u64) mapping + len) > DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -08005702 return 0;
5703#else
5704 return 0;
5705#endif
5706}
5707
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005708static void tg3_set_txd(struct tg3_napi *, int, dma_addr_t, int, u32, u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
Michael Chan72f2afb2006-03-06 19:28:35 -08005710/* Workaround 4GB and 40-bit hardware DMA bugs. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00005711static int tigon3_dma_hwbug_workaround(struct tg3_napi *tnapi,
5712 struct sk_buff *skb, u32 last_plus_one,
5713 u32 *start, u32 base_flags, u32 mss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714{
Matt Carlson24f4efd2009-11-13 13:03:35 +00005715 struct tg3 *tp = tnapi->tp;
Matt Carlson41588ba2008-04-19 18:12:33 -07005716 struct sk_buff *new_skb;
Michael Chanc58ec932005-09-17 00:46:27 -07005717 dma_addr_t new_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 u32 entry = *start;
Michael Chanc58ec932005-09-17 00:46:27 -07005719 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
Matt Carlson41588ba2008-04-19 18:12:33 -07005721 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
5722 new_skb = skb_copy(skb, GFP_ATOMIC);
5723 else {
5724 int more_headroom = 4 - ((unsigned long)skb->data & 3);
5725
5726 new_skb = skb_copy_expand(skb,
5727 skb_headroom(skb) + more_headroom,
5728 skb_tailroom(skb), GFP_ATOMIC);
5729 }
5730
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 if (!new_skb) {
Michael Chanc58ec932005-09-17 00:46:27 -07005732 ret = -1;
5733 } else {
5734 /* New SKB is guaranteed to be linear. */
5735 entry = *start;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005736 new_addr = pci_map_single(tp->pdev, new_skb->data, new_skb->len,
5737 PCI_DMA_TODEVICE);
5738 /* Make sure the mapping succeeded */
5739 if (pci_dma_mapping_error(tp->pdev, new_addr)) {
5740 ret = -1;
5741 dev_kfree_skb(new_skb);
5742 new_skb = NULL;
David S. Miller90079ce2008-09-11 04:52:51 -07005743
Michael Chanc58ec932005-09-17 00:46:27 -07005744 /* Make sure new skb does not cross any 4G boundaries.
5745 * Drop the packet if it does.
5746 */
Alexander Duyckf4188d82009-12-02 16:48:38 +00005747 } else if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
5748 tg3_4g_overflow_test(new_addr, new_skb->len)) {
5749 pci_unmap_single(tp->pdev, new_addr, new_skb->len,
5750 PCI_DMA_TODEVICE);
Michael Chanc58ec932005-09-17 00:46:27 -07005751 ret = -1;
5752 dev_kfree_skb(new_skb);
5753 new_skb = NULL;
5754 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005755 tg3_set_txd(tnapi, entry, new_addr, new_skb->len,
Michael Chanc58ec932005-09-17 00:46:27 -07005756 base_flags, 1 | (mss << 1));
5757 *start = NEXT_TX(entry);
5758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 }
5760
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 /* Now clean up the sw ring entries. */
5762 i = 0;
5763 while (entry != last_plus_one) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00005764 int len;
5765
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005766 if (i == 0)
Alexander Duyckf4188d82009-12-02 16:48:38 +00005767 len = skb_headlen(skb);
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005768 else
Alexander Duyckf4188d82009-12-02 16:48:38 +00005769 len = skb_shinfo(skb)->frags[i-1].size;
5770
5771 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005772 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005773 mapping),
5774 len, PCI_DMA_TODEVICE);
5775 if (i == 0) {
5776 tnapi->tx_buffers[entry].skb = new_skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005777 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005778 new_addr);
5779 } else {
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005780 tnapi->tx_buffers[entry].skb = NULL;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 entry = NEXT_TX(entry);
5783 i++;
5784 }
5785
5786 dev_kfree_skb(skb);
5787
Michael Chanc58ec932005-09-17 00:46:27 -07005788 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789}
5790
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005791static void tg3_set_txd(struct tg3_napi *tnapi, int entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 dma_addr_t mapping, int len, u32 flags,
5793 u32 mss_and_is_end)
5794{
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005795 struct tg3_tx_buffer_desc *txd = &tnapi->tx_ring[entry];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 int is_end = (mss_and_is_end & 0x1);
5797 u32 mss = (mss_and_is_end >> 1);
5798 u32 vlan_tag = 0;
5799
5800 if (is_end)
5801 flags |= TXD_FLAG_END;
5802 if (flags & TXD_FLAG_VLAN) {
5803 vlan_tag = flags >> 16;
5804 flags &= 0xffff;
5805 }
5806 vlan_tag |= (mss << TXD_MSS_SHIFT);
5807
5808 txd->addr_hi = ((u64) mapping >> 32);
5809 txd->addr_lo = ((u64) mapping & 0xffffffff);
5810 txd->len_flags = (len << TXD_LEN_SHIFT) | flags;
5811 txd->vlan_tag = vlan_tag << TXD_VLAN_TAG_SHIFT;
5812}
5813
Michael Chan5a6f3072006-03-20 22:28:05 -08005814/* hard_start_xmit for devices that don't have any bugs and
Matt Carlsone849cdc2009-11-13 13:03:38 +00005815 * support TG3_FLG2_HW_TSO_2 and TG3_FLG2_HW_TSO_3 only.
Michael Chan5a6f3072006-03-20 22:28:05 -08005816 */
Stephen Hemminger613573252009-08-31 19:50:58 +00005817static netdev_tx_t tg3_start_xmit(struct sk_buff *skb,
5818 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819{
5820 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 u32 len, entry, base_flags, mss;
David S. Miller90079ce2008-09-11 04:52:51 -07005822 dma_addr_t mapping;
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005823 struct tg3_napi *tnapi;
5824 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005825 unsigned int i, last;
5826
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005827 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
5828 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00005829 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005830 tnapi++;
Michael Chan5a6f3072006-03-20 22:28:05 -08005831
Michael Chan00b70502006-06-17 21:58:45 -07005832 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07005833 * and TX reclaim runs via tp->napi.poll inside of a software
Michael Chan5a6f3072006-03-20 22:28:05 -08005834 * interrupt. Furthermore, IRQ processing runs lockless so we have
5835 * no IRQ context deadlocks to worry about either. Rejoice!
5836 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005837 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005838 if (!netif_tx_queue_stopped(txq)) {
5839 netif_tx_stop_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005840
5841 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00005842 netdev_err(dev,
5843 "BUG! Tx Ring full when queue awake!\n");
Michael Chan5a6f3072006-03-20 22:28:05 -08005844 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005845 return NETDEV_TX_BUSY;
5846 }
5847
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005848 entry = tnapi->tx_prod;
Michael Chan5a6f3072006-03-20 22:28:05 -08005849 base_flags = 0;
Matt Carlsonbe98da62010-07-11 09:31:46 +00005850 mss = skb_shinfo(skb)->gso_size;
5851 if (mss) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005852 int tcp_opt_len, ip_tcp_len;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005853 u32 hdrlen;
Michael Chan5a6f3072006-03-20 22:28:05 -08005854
5855 if (skb_header_cloned(skb) &&
5856 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
5857 dev_kfree_skb(skb);
5858 goto out_unlock;
5859 }
5860
Matt Carlson02e96082010-09-15 08:59:59 +00005861 if (skb_is_gso_v6(skb)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005862 hdrlen = skb_headlen(skb) - ETH_HLEN;
Matt Carlson02e96082010-09-15 08:59:59 +00005863 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005864 struct iphdr *iph = ip_hdr(skb);
5865
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07005866 tcp_opt_len = tcp_optlen(skb);
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03005867 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
Michael Chanb0026622006-07-03 19:42:14 -07005868
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005869 iph->check = 0;
5870 iph->tot_len = htons(mss + ip_tcp_len + tcp_opt_len);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005871 hdrlen = ip_tcp_len + tcp_opt_len;
Michael Chanb0026622006-07-03 19:42:14 -07005872 }
Michael Chan5a6f3072006-03-20 22:28:05 -08005873
Matt Carlsone849cdc2009-11-13 13:03:38 +00005874 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005875 mss |= (hdrlen & 0xc) << 12;
5876 if (hdrlen & 0x10)
5877 base_flags |= 0x00000010;
5878 base_flags |= (hdrlen & 0x3e0) << 5;
5879 } else
5880 mss |= hdrlen << 9;
5881
Michael Chan5a6f3072006-03-20 22:28:05 -08005882 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
5883 TXD_FLAG_CPU_POST_DMA);
5884
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07005885 tcp_hdr(skb)->check = 0;
Michael Chan5a6f3072006-03-20 22:28:05 -08005886
Matt Carlson859a5882010-04-05 10:19:28 +00005887 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005888 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson859a5882010-04-05 10:19:28 +00005889 }
5890
Jesse Grosseab6d182010-10-20 13:56:03 +00005891 if (vlan_tx_tag_present(skb))
Michael Chan5a6f3072006-03-20 22:28:05 -08005892 base_flags |= (TXD_FLAG_VLAN |
5893 (vlan_tx_tag_get(skb) << 16));
Michael Chan5a6f3072006-03-20 22:28:05 -08005894
Alexander Duyckf4188d82009-12-02 16:48:38 +00005895 len = skb_headlen(skb);
5896
5897 /* Queue skb data, a.k.a. the main skb fragment. */
5898 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
5899 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07005900 dev_kfree_skb(skb);
5901 goto out_unlock;
5902 }
5903
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005904 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005905 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005906
Matt Carlsonb703df62009-12-03 08:36:21 +00005907 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00005908 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00005909 base_flags |= TXD_FLAG_JMB_PKT;
5910
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005911 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Michael Chan5a6f3072006-03-20 22:28:05 -08005912 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
5913
5914 entry = NEXT_TX(entry);
5915
5916 /* Now loop through additional data fragments, and queue them. */
5917 if (skb_shinfo(skb)->nr_frags > 0) {
Michael Chan5a6f3072006-03-20 22:28:05 -08005918 last = skb_shinfo(skb)->nr_frags - 1;
5919 for (i = 0; i <= last; i++) {
5920 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5921
5922 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005923 mapping = pci_map_page(tp->pdev,
5924 frag->page,
5925 frag->page_offset,
5926 len, PCI_DMA_TODEVICE);
5927 if (pci_dma_mapping_error(tp->pdev, mapping))
5928 goto dma_error;
5929
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005930 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005931 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00005932 mapping);
Michael Chan5a6f3072006-03-20 22:28:05 -08005933
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005934 tg3_set_txd(tnapi, entry, mapping, len,
Michael Chan5a6f3072006-03-20 22:28:05 -08005935 base_flags, (i == last) | (mss << 1));
5936
5937 entry = NEXT_TX(entry);
5938 }
5939 }
5940
5941 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005942 tw32_tx_mbox(tnapi->prodmbox, entry);
Michael Chan5a6f3072006-03-20 22:28:05 -08005943
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005944 tnapi->tx_prod = entry;
5945 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005946 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005947
5948 /* netif_tx_stop_queue() must be done before checking
5949 * checking tx index in tg3_tx_avail() below, because in
5950 * tg3_tx(), we update tx index before checking for
5951 * netif_tx_queue_stopped().
5952 */
5953 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005954 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlsonfe5f5782009-09-01 13:09:39 +00005955 netif_tx_wake_queue(txq);
Michael Chan5a6f3072006-03-20 22:28:05 -08005956 }
5957
5958out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00005959 mmiowb();
Michael Chan5a6f3072006-03-20 22:28:05 -08005960
5961 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00005962
5963dma_error:
5964 last = i;
5965 entry = tnapi->tx_prod;
5966 tnapi->tx_buffers[entry].skb = NULL;
5967 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005968 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00005969 skb_headlen(skb),
5970 PCI_DMA_TODEVICE);
5971 for (i = 0; i <= last; i++) {
5972 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
5973 entry = NEXT_TX(entry);
5974
5975 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00005976 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00005977 mapping),
5978 frag->size, PCI_DMA_TODEVICE);
5979 }
5980
5981 dev_kfree_skb(skb);
5982 return NETDEV_TX_OK;
Michael Chan5a6f3072006-03-20 22:28:05 -08005983}
5984
Stephen Hemminger613573252009-08-31 19:50:58 +00005985static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *,
5986 struct net_device *);
Michael Chan52c0fd82006-06-29 20:15:54 -07005987
5988/* Use GSO to workaround a rare TSO bug that may be triggered when the
5989 * TSO header is greater than 80 bytes.
5990 */
5991static int tg3_tso_bug(struct tg3 *tp, struct sk_buff *skb)
5992{
5993 struct sk_buff *segs, *nskb;
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005994 u32 frag_cnt_est = skb_shinfo(skb)->gso_segs * 3;
Michael Chan52c0fd82006-06-29 20:15:54 -07005995
5996 /* Estimate the number of fragments in the worst case */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00005997 if (unlikely(tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)) {
Michael Chan52c0fd82006-06-29 20:15:54 -07005998 netif_stop_queue(tp->dev);
Matt Carlsonf65aac12010-08-02 11:26:03 +00005999
6000 /* netif_tx_stop_queue() must be done before checking
6001 * checking tx index in tg3_tx_avail() below, because in
6002 * tg3_tx(), we update tx index before checking for
6003 * netif_tx_queue_stopped().
6004 */
6005 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006006 if (tg3_tx_avail(&tp->napi[0]) <= frag_cnt_est)
Michael Chan7f62ad52007-02-20 23:25:40 -08006007 return NETDEV_TX_BUSY;
6008
6009 netif_wake_queue(tp->dev);
Michael Chan52c0fd82006-06-29 20:15:54 -07006010 }
6011
6012 segs = skb_gso_segment(skb, tp->dev->features & ~NETIF_F_TSO);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07006013 if (IS_ERR(segs))
Michael Chan52c0fd82006-06-29 20:15:54 -07006014 goto tg3_tso_bug_end;
6015
6016 do {
6017 nskb = segs;
6018 segs = segs->next;
6019 nskb->next = NULL;
6020 tg3_start_xmit_dma_bug(nskb, tp->dev);
6021 } while (segs);
6022
6023tg3_tso_bug_end:
6024 dev_kfree_skb(skb);
6025
6026 return NETDEV_TX_OK;
6027}
Michael Chan52c0fd82006-06-29 20:15:54 -07006028
Michael Chan5a6f3072006-03-20 22:28:05 -08006029/* hard_start_xmit for devices that have the 4G bug and/or 40-bit bug and
6030 * support TG3_FLG2_HW_TSO_1 or firmware TSO only.
6031 */
Stephen Hemminger613573252009-08-31 19:50:58 +00006032static netdev_tx_t tg3_start_xmit_dma_bug(struct sk_buff *skb,
6033 struct net_device *dev)
Michael Chan5a6f3072006-03-20 22:28:05 -08006034{
6035 struct tg3 *tp = netdev_priv(dev);
Michael Chan5a6f3072006-03-20 22:28:05 -08006036 u32 len, entry, base_flags, mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 int would_hit_hwbug;
David S. Miller90079ce2008-09-11 04:52:51 -07006038 dma_addr_t mapping;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006039 struct tg3_napi *tnapi;
6040 struct netdev_queue *txq;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006041 unsigned int i, last;
6042
Matt Carlson24f4efd2009-11-13 13:03:35 +00006043 txq = netdev_get_tx_queue(dev, skb_get_queue_mapping(skb));
6044 tnapi = &tp->napi[skb_get_queue_mapping(skb)];
Matt Carlson19cfaec2009-12-03 08:36:20 +00006045 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlson24f4efd2009-11-13 13:03:35 +00006046 tnapi++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
Michael Chan00b70502006-06-17 21:58:45 -07006048 /* We are running in BH disabled context with netif_tx_lock
Stephen Hemmingerbea33482007-10-03 16:41:36 -07006049 * and TX reclaim runs via tp->napi.poll inside of a software
David S. Millerf47c11e2005-06-24 20:18:35 -07006050 * interrupt. Furthermore, IRQ processing runs lockless so we have
6051 * no IRQ context deadlocks to worry about either. Rejoice!
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 */
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006053 if (unlikely(tg3_tx_avail(tnapi) <= (skb_shinfo(skb)->nr_frags + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006054 if (!netif_tx_queue_stopped(txq)) {
6055 netif_tx_stop_queue(txq);
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006056
6057 /* This is a hard error, log it. */
Matt Carlson5129c3a2010-04-05 10:19:23 +00006058 netdev_err(dev,
6059 "BUG! Tx Ring full when queue awake!\n");
Stephen Hemminger1f064a82005-12-06 17:36:44 -08006060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 return NETDEV_TX_BUSY;
6062 }
6063
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006064 entry = tnapi->tx_prod;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 base_flags = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -07006066 if (skb->ip_summed == CHECKSUM_PARTIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 base_flags |= TXD_FLAG_TCPUDP_CSUM;
Matt Carlson24f4efd2009-11-13 13:03:35 +00006068
Matt Carlsonbe98da62010-07-11 09:31:46 +00006069 mss = skb_shinfo(skb)->gso_size;
6070 if (mss) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006071 struct iphdr *iph;
Matt Carlson34195c32010-07-11 09:31:42 +00006072 u32 tcp_opt_len, hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
6074 if (skb_header_cloned(skb) &&
6075 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) {
6076 dev_kfree_skb(skb);
6077 goto out_unlock;
6078 }
6079
Matt Carlson34195c32010-07-11 09:31:42 +00006080 iph = ip_hdr(skb);
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07006081 tcp_opt_len = tcp_optlen(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082
Matt Carlson02e96082010-09-15 08:59:59 +00006083 if (skb_is_gso_v6(skb)) {
Matt Carlson34195c32010-07-11 09:31:42 +00006084 hdr_len = skb_headlen(skb) - ETH_HLEN;
6085 } else {
6086 u32 ip_tcp_len;
6087
6088 ip_tcp_len = ip_hdrlen(skb) + sizeof(struct tcphdr);
6089 hdr_len = ip_tcp_len + tcp_opt_len;
6090
6091 iph->check = 0;
6092 iph->tot_len = htons(mss + hdr_len);
6093 }
6094
Michael Chan52c0fd82006-06-29 20:15:54 -07006095 if (unlikely((ETH_HLEN + hdr_len) > 80) &&
Michael Chan7f62ad52007-02-20 23:25:40 -08006096 (tp->tg3_flags2 & TG3_FLG2_TSO_BUG))
Matt Carlsonde6f31e2010-04-12 06:58:30 +00006097 return tg3_tso_bug(tp, skb);
Michael Chan52c0fd82006-06-29 20:15:54 -07006098
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 base_flags |= (TXD_FLAG_CPU_PRE_DMA |
6100 TXD_FLAG_CPU_POST_DMA);
6101
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO) {
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006103 tcp_hdr(skb)->check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 base_flags &= ~TXD_FLAG_TCPUDP_CSUM;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07006105 } else
6106 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
6107 iph->daddr, 0,
6108 IPPROTO_TCP,
6109 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
Matt Carlson615774f2009-11-13 13:03:39 +00006111 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) {
6112 mss |= (hdr_len & 0xc) << 12;
6113 if (hdr_len & 0x10)
6114 base_flags |= 0x00000010;
6115 base_flags |= (hdr_len & 0x3e0) << 5;
6116 } else if (tp->tg3_flags2 & TG3_FLG2_HW_TSO_2)
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006117 mss |= hdr_len << 9;
6118 else if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_1) ||
6119 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006120 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 int tsflags;
6122
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006123 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 mss |= (tsflags << 11);
6125 }
6126 } else {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006127 if (tcp_opt_len || iph->ihl > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 int tsflags;
6129
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006130 tsflags = (iph->ihl - 5) + (tcp_opt_len >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 base_flags |= tsflags << 12;
6132 }
6133 }
6134 }
Matt Carlsonbf933c82011-01-25 15:58:49 +00006135
Jesse Grosseab6d182010-10-20 13:56:03 +00006136 if (vlan_tx_tag_present(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 base_flags |= (TXD_FLAG_VLAN |
6138 (vlan_tx_tag_get(skb) << 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
Matt Carlsonb703df62009-12-03 08:36:21 +00006140 if ((tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) &&
Matt Carlson8fc2f992010-12-06 08:28:49 +00006141 !mss && skb->len > VLAN_ETH_FRAME_LEN)
Matt Carlson615774f2009-11-13 13:03:39 +00006142 base_flags |= TXD_FLAG_JMB_PKT;
6143
Alexander Duyckf4188d82009-12-02 16:48:38 +00006144 len = skb_headlen(skb);
6145
6146 mapping = pci_map_single(tp->pdev, skb->data, len, PCI_DMA_TODEVICE);
6147 if (pci_dma_mapping_error(tp->pdev, mapping)) {
David S. Miller90079ce2008-09-11 04:52:51 -07006148 dev_kfree_skb(skb);
6149 goto out_unlock;
6150 }
6151
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006152 tnapi->tx_buffers[entry].skb = skb;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006153 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping, mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
6155 would_hit_hwbug = 0;
6156
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006157 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) && len <= 8)
6158 would_hit_hwbug = 1;
6159
Matt Carlson0e1406d2009-11-02 12:33:33 +00006160 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6161 tg3_4g_overflow_test(mapping, len))
Matt Carlson41588ba2008-04-19 18:12:33 -07006162 would_hit_hwbug = 1;
Matt Carlson0e1406d2009-11-02 12:33:33 +00006163
6164 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6165 tg3_40bit_overflow_test(tp, mapping, len))
6166 would_hit_hwbug = 1;
6167
6168 if (tp->tg3_flags3 & TG3_FLG3_5701_DMA_BUG)
Michael Chanc58ec932005-09-17 00:46:27 -07006169 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006171 tg3_set_txd(tnapi, entry, mapping, len, base_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 (skb_shinfo(skb)->nr_frags == 0) | (mss << 1));
6173
6174 entry = NEXT_TX(entry);
6175
6176 /* Now loop through additional data fragments, and queue them. */
6177 if (skb_shinfo(skb)->nr_frags > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 last = skb_shinfo(skb)->nr_frags - 1;
6179 for (i = 0; i <= last; i++) {
6180 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6181
6182 len = frag->size;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006183 mapping = pci_map_page(tp->pdev,
6184 frag->page,
6185 frag->page_offset,
6186 len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006188 tnapi->tx_buffers[entry].skb = NULL;
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006189 dma_unmap_addr_set(&tnapi->tx_buffers[entry], mapping,
Alexander Duyckf4188d82009-12-02 16:48:38 +00006190 mapping);
6191 if (pci_dma_mapping_error(tp->pdev, mapping))
6192 goto dma_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193
Matt Carlson92c6b8d2009-11-02 14:23:27 +00006194 if ((tp->tg3_flags3 & TG3_FLG3_SHORT_DMA_BUG) &&
6195 len <= 8)
6196 would_hit_hwbug = 1;
6197
Matt Carlson0e1406d2009-11-02 12:33:33 +00006198 if ((tp->tg3_flags3 & TG3_FLG3_4G_DMA_BNDRY_BUG) &&
6199 tg3_4g_overflow_test(mapping, len))
Michael Chanc58ec932005-09-17 00:46:27 -07006200 would_hit_hwbug = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201
Matt Carlson0e1406d2009-11-02 12:33:33 +00006202 if ((tp->tg3_flags3 & TG3_FLG3_40BIT_DMA_LIMIT_BUG) &&
6203 tg3_40bit_overflow_test(tp, mapping, len))
Michael Chan72f2afb2006-03-06 19:28:35 -08006204 would_hit_hwbug = 1;
6205
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006207 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 base_flags, (i == last)|(mss << 1));
6209 else
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006210 tg3_set_txd(tnapi, entry, mapping, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 base_flags, (i == last));
6212
6213 entry = NEXT_TX(entry);
6214 }
6215 }
6216
6217 if (would_hit_hwbug) {
6218 u32 last_plus_one = entry;
6219 u32 start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
Michael Chanc58ec932005-09-17 00:46:27 -07006221 start = entry - 1 - skb_shinfo(skb)->nr_frags;
6222 start &= (TG3_TX_RING_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
6224 /* If the workaround fails due to memory/mapping
6225 * failure, silently drop this packet.
6226 */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006227 if (tigon3_dma_hwbug_workaround(tnapi, skb, last_plus_one,
Michael Chanc58ec932005-09-17 00:46:27 -07006228 &start, base_flags, mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 goto out_unlock;
6230
6231 entry = start;
6232 }
6233
6234 /* Packets are ready, update Tx producer idx local and on card. */
Matt Carlson24f4efd2009-11-13 13:03:35 +00006235 tw32_tx_mbox(tnapi->prodmbox, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006237 tnapi->tx_prod = entry;
6238 if (unlikely(tg3_tx_avail(tnapi) <= (MAX_SKB_FRAGS + 1))) {
Matt Carlson24f4efd2009-11-13 13:03:35 +00006239 netif_tx_stop_queue(txq);
Matt Carlsonf65aac12010-08-02 11:26:03 +00006240
6241 /* netif_tx_stop_queue() must be done before checking
6242 * checking tx index in tg3_tx_avail() below, because in
6243 * tg3_tx(), we update tx index before checking for
6244 * netif_tx_queue_stopped().
6245 */
6246 smp_mb();
Matt Carlsonf3f3f272009-08-28 14:03:21 +00006247 if (tg3_tx_avail(tnapi) > TG3_TX_WAKEUP_THRESH(tnapi))
Matt Carlson24f4efd2009-11-13 13:03:35 +00006248 netif_tx_wake_queue(txq);
Michael Chan51b91462005-09-01 17:41:28 -07006249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250
6251out_unlock:
Eric Dumazetcdd0db02009-05-28 00:00:41 +00006252 mmiowb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253
6254 return NETDEV_TX_OK;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006255
6256dma_error:
6257 last = i;
6258 entry = tnapi->tx_prod;
6259 tnapi->tx_buffers[entry].skb = NULL;
6260 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006261 dma_unmap_addr(&tnapi->tx_buffers[entry], mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006262 skb_headlen(skb),
6263 PCI_DMA_TODEVICE);
6264 for (i = 0; i <= last; i++) {
6265 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6266 entry = NEXT_TX(entry);
6267
6268 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006269 dma_unmap_addr(&tnapi->tx_buffers[entry],
Alexander Duyckf4188d82009-12-02 16:48:38 +00006270 mapping),
6271 frag->size, PCI_DMA_TODEVICE);
6272 }
6273
6274 dev_kfree_skb(skb);
6275 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276}
6277
Michał Mirosławdc668912011-04-07 03:35:07 +00006278static u32 tg3_fix_features(struct net_device *dev, u32 features)
6279{
6280 struct tg3 *tp = netdev_priv(dev);
6281
6282 if (dev->mtu > ETH_DATA_LEN && (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
6283 features &= ~NETIF_F_ALL_TSO;
6284
6285 return features;
6286}
6287
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288static inline void tg3_set_mtu(struct net_device *dev, struct tg3 *tp,
6289 int new_mtu)
6290{
6291 dev->mtu = new_mtu;
6292
Michael Chanef7f5ec2005-07-25 12:32:25 -07006293 if (new_mtu > ETH_DATA_LEN) {
Michael Chana4e2b342005-10-26 15:46:52 -07006294 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michał Mirosławdc668912011-04-07 03:35:07 +00006295 netdev_update_features(dev);
Michael Chanef7f5ec2005-07-25 12:32:25 -07006296 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
Matt Carlson859a5882010-04-05 10:19:28 +00006297 } else {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006298 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Matt Carlson859a5882010-04-05 10:19:28 +00006299 }
Michael Chanef7f5ec2005-07-25 12:32:25 -07006300 } else {
Michał Mirosławdc668912011-04-07 03:35:07 +00006301 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanef7f5ec2005-07-25 12:32:25 -07006302 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
Michał Mirosławdc668912011-04-07 03:35:07 +00006303 netdev_update_features(dev);
6304 }
Michael Chan0f893dc2005-07-25 12:30:38 -07006305 tp->tg3_flags &= ~TG3_FLAG_JUMBO_RING_ENABLE;
Michael Chanef7f5ec2005-07-25 12:32:25 -07006306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307}
6308
6309static int tg3_change_mtu(struct net_device *dev, int new_mtu)
6310{
6311 struct tg3 *tp = netdev_priv(dev);
Michael Chanb9ec6c12006-07-25 16:37:27 -07006312 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313
6314 if (new_mtu < TG3_MIN_MTU || new_mtu > TG3_MAX_MTU(tp))
6315 return -EINVAL;
6316
6317 if (!netif_running(dev)) {
6318 /* We'll just catch it later when the
6319 * device is up'd.
6320 */
6321 tg3_set_mtu(dev, tp, new_mtu);
6322 return 0;
6323 }
6324
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006325 tg3_phy_stop(tp);
6326
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 tg3_netif_stop(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07006328
6329 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330
Michael Chan944d9802005-05-29 14:57:48 -07006331 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332
6333 tg3_set_mtu(dev, tp, new_mtu);
6334
Michael Chanb9ec6c12006-07-25 16:37:27 -07006335 err = tg3_restart_hw(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336
Michael Chanb9ec6c12006-07-25 16:37:27 -07006337 if (!err)
6338 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339
David S. Millerf47c11e2005-06-24 20:18:35 -07006340 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07006342 if (!err)
6343 tg3_phy_start(tp);
6344
Michael Chanb9ec6c12006-07-25 16:37:27 -07006345 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346}
6347
Matt Carlson21f581a2009-08-28 14:00:25 +00006348static void tg3_rx_prodring_free(struct tg3 *tp,
6349 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 int i;
6352
Matt Carlson8fea32b2010-09-15 08:59:58 +00006353 if (tpr != &tp->napi[0].prodring) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006354 for (i = tpr->rx_std_cons_idx; i != tpr->rx_std_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006355 i = (i + 1) & tp->rx_std_ring_mask)
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006356 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6357 tp->rx_pkt_map_sz);
6358
6359 if (tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) {
6360 for (i = tpr->rx_jmb_cons_idx;
6361 i != tpr->rx_jmb_prod_idx;
Matt Carlson2c49a442010-09-30 10:34:35 +00006362 i = (i + 1) & tp->rx_jmb_ring_mask) {
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006363 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6364 TG3_RX_JMB_MAP_SZ);
6365 }
6366 }
6367
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006368 return;
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370
Matt Carlson2c49a442010-09-30 10:34:35 +00006371 for (i = 0; i <= tp->rx_std_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006372 tg3_rx_skb_free(tp, &tpr->rx_std_buffers[i],
6373 tp->rx_pkt_map_sz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374
Matt Carlson48035722010-10-14 10:37:43 +00006375 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6376 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006377 for (i = 0; i <= tp->rx_jmb_ring_mask; i++)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006378 tg3_rx_skb_free(tp, &tpr->rx_jmb_buffers[i],
6379 TG3_RX_JMB_MAP_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380 }
6381}
6382
Matt Carlsonc6cdf432010-04-05 10:19:26 +00006383/* Initialize rx rings for packet processing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 *
6385 * The chip has been shut down and the driver detached from
6386 * the networking, so no interrupts or new tx packets will
6387 * end up in the driver. tp->{tx,}lock are held and thus
6388 * we may not sleep.
6389 */
Matt Carlson21f581a2009-08-28 14:00:25 +00006390static int tg3_rx_prodring_alloc(struct tg3 *tp,
6391 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392{
Matt Carlson287be122009-08-28 13:58:46 +00006393 u32 i, rx_pkt_dma_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394
Matt Carlsonb196c7e2009-11-13 13:03:50 +00006395 tpr->rx_std_cons_idx = 0;
6396 tpr->rx_std_prod_idx = 0;
6397 tpr->rx_jmb_cons_idx = 0;
6398 tpr->rx_jmb_prod_idx = 0;
6399
Matt Carlson8fea32b2010-09-15 08:59:58 +00006400 if (tpr != &tp->napi[0].prodring) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006401 memset(&tpr->rx_std_buffers[0], 0,
6402 TG3_RX_STD_BUFF_RING_SIZE(tp));
Matt Carlson48035722010-10-14 10:37:43 +00006403 if (tpr->rx_jmb_buffers)
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006404 memset(&tpr->rx_jmb_buffers[0], 0,
Matt Carlson2c49a442010-09-30 10:34:35 +00006405 TG3_RX_JMB_BUFF_RING_SIZE(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006406 goto done;
6407 }
6408
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 /* Zero out all descriptors. */
Matt Carlson2c49a442010-09-30 10:34:35 +00006410 memset(tpr->rx_std, 0, TG3_RX_STD_RING_BYTES(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411
Matt Carlson287be122009-08-28 13:58:46 +00006412 rx_pkt_dma_sz = TG3_RX_STD_DMA_SZ;
Michael Chana4e2b342005-10-26 15:46:52 -07006413 if ((tp->tg3_flags2 & TG3_FLG2_5780_CLASS) &&
Matt Carlson287be122009-08-28 13:58:46 +00006414 tp->dev->mtu > ETH_DATA_LEN)
6415 rx_pkt_dma_sz = TG3_RX_JMB_DMA_SZ;
6416 tp->rx_pkt_map_sz = TG3_RX_DMA_TO_MAP_SZ(rx_pkt_dma_sz);
Michael Chan7e72aad2005-07-25 12:31:17 -07006417
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 /* Initialize invariants of the rings, we only set this
6419 * stuff once. This works because the card does not
6420 * write into the rx buffer posting rings.
6421 */
Matt Carlson2c49a442010-09-30 10:34:35 +00006422 for (i = 0; i <= tp->rx_std_ring_mask; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 struct tg3_rx_buffer_desc *rxd;
6424
Matt Carlson21f581a2009-08-28 14:00:25 +00006425 rxd = &tpr->rx_std[i];
Matt Carlson287be122009-08-28 13:58:46 +00006426 rxd->idx_len = rx_pkt_dma_sz << RXD_LEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT);
6428 rxd->opaque = (RXD_OPAQUE_RING_STD |
6429 (i << RXD_OPAQUE_INDEX_SHIFT));
6430 }
6431
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006432 /* Now allocate fresh SKBs for each rx ring. */
6433 for (i = 0; i < tp->rx_pending; i++) {
Matt Carlson86b21e52009-11-13 13:03:45 +00006434 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_STD, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006435 netdev_warn(tp->dev,
6436 "Using a smaller RX standard ring. Only "
6437 "%d out of %d buffers were allocated "
6438 "successfully\n", i, tp->rx_pending);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006439 if (i == 0)
6440 goto initfail;
6441 tp->rx_pending = i;
6442 break;
6443 }
6444 }
6445
Matt Carlson48035722010-10-14 10:37:43 +00006446 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) ||
6447 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006448 goto done;
6449
Matt Carlson2c49a442010-09-30 10:34:35 +00006450 memset(tpr->rx_jmb, 0, TG3_RX_JMB_RING_BYTES(tp));
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006451
Matt Carlson0d86df82010-02-17 15:17:00 +00006452 if (!(tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE))
6453 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454
Matt Carlson2c49a442010-09-30 10:34:35 +00006455 for (i = 0; i <= tp->rx_jmb_ring_mask; i++) {
Matt Carlson0d86df82010-02-17 15:17:00 +00006456 struct tg3_rx_buffer_desc *rxd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457
Matt Carlson0d86df82010-02-17 15:17:00 +00006458 rxd = &tpr->rx_jmb[i].std;
6459 rxd->idx_len = TG3_RX_JMB_DMA_SZ << RXD_LEN_SHIFT;
6460 rxd->type_flags = (RXD_FLAG_END << RXD_FLAGS_SHIFT) |
6461 RXD_FLAG_JUMBO;
6462 rxd->opaque = (RXD_OPAQUE_RING_JUMBO |
6463 (i << RXD_OPAQUE_INDEX_SHIFT));
6464 }
6465
6466 for (i = 0; i < tp->rx_jumbo_pending; i++) {
6467 if (tg3_alloc_rx_skb(tp, tpr, RXD_OPAQUE_RING_JUMBO, i) < 0) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00006468 netdev_warn(tp->dev,
6469 "Using a smaller RX jumbo ring. Only %d "
6470 "out of %d buffers were allocated "
6471 "successfully\n", i, tp->rx_jumbo_pending);
Matt Carlson0d86df82010-02-17 15:17:00 +00006472 if (i == 0)
6473 goto initfail;
6474 tp->rx_jumbo_pending = i;
6475 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 }
6477 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006478
6479done:
Michael Chan32d8c572006-07-25 16:38:29 -07006480 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006481
6482initfail:
Matt Carlson21f581a2009-08-28 14:00:25 +00006483 tg3_rx_prodring_free(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006484 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485}
6486
Matt Carlson21f581a2009-08-28 14:00:25 +00006487static void tg3_rx_prodring_fini(struct tg3 *tp,
6488 struct tg3_rx_prodring_set *tpr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489{
Matt Carlson21f581a2009-08-28 14:00:25 +00006490 kfree(tpr->rx_std_buffers);
6491 tpr->rx_std_buffers = NULL;
6492 kfree(tpr->rx_jmb_buffers);
6493 tpr->rx_jmb_buffers = NULL;
6494 if (tpr->rx_std) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006495 dma_free_coherent(&tp->pdev->dev, TG3_RX_STD_RING_BYTES(tp),
6496 tpr->rx_std, tpr->rx_std_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006497 tpr->rx_std = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498 }
Matt Carlson21f581a2009-08-28 14:00:25 +00006499 if (tpr->rx_jmb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006500 dma_free_coherent(&tp->pdev->dev, TG3_RX_JMB_RING_BYTES(tp),
6501 tpr->rx_jmb, tpr->rx_jmb_mapping);
Matt Carlson21f581a2009-08-28 14:00:25 +00006502 tpr->rx_jmb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006504}
6505
Matt Carlson21f581a2009-08-28 14:00:25 +00006506static int tg3_rx_prodring_init(struct tg3 *tp,
6507 struct tg3_rx_prodring_set *tpr)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006508{
Matt Carlson2c49a442010-09-30 10:34:35 +00006509 tpr->rx_std_buffers = kzalloc(TG3_RX_STD_BUFF_RING_SIZE(tp),
6510 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006511 if (!tpr->rx_std_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006512 return -ENOMEM;
6513
Matt Carlson4bae65c2010-11-24 08:31:52 +00006514 tpr->rx_std = dma_alloc_coherent(&tp->pdev->dev,
6515 TG3_RX_STD_RING_BYTES(tp),
6516 &tpr->rx_std_mapping,
6517 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006518 if (!tpr->rx_std)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006519 goto err_out;
6520
Matt Carlson48035722010-10-14 10:37:43 +00006521 if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
6522 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Matt Carlson2c49a442010-09-30 10:34:35 +00006523 tpr->rx_jmb_buffers = kzalloc(TG3_RX_JMB_BUFF_RING_SIZE(tp),
Matt Carlson21f581a2009-08-28 14:00:25 +00006524 GFP_KERNEL);
6525 if (!tpr->rx_jmb_buffers)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006526 goto err_out;
6527
Matt Carlson4bae65c2010-11-24 08:31:52 +00006528 tpr->rx_jmb = dma_alloc_coherent(&tp->pdev->dev,
6529 TG3_RX_JMB_RING_BYTES(tp),
6530 &tpr->rx_jmb_mapping,
6531 GFP_KERNEL);
Matt Carlson21f581a2009-08-28 14:00:25 +00006532 if (!tpr->rx_jmb)
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006533 goto err_out;
6534 }
6535
6536 return 0;
6537
6538err_out:
Matt Carlson21f581a2009-08-28 14:00:25 +00006539 tg3_rx_prodring_fini(tp, tpr);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006540 return -ENOMEM;
6541}
6542
6543/* Free up pending packets in all rx/tx rings.
6544 *
6545 * The chip has been shut down and the driver detached from
6546 * the networking, so no interrupts or new tx packets will
6547 * end up in the driver. tp->{tx,}lock is not held and we are not
6548 * in an interrupt context and thus may sleep.
6549 */
6550static void tg3_free_rings(struct tg3 *tp)
6551{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006552 int i, j;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006553
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006554 for (j = 0; j < tp->irq_cnt; j++) {
6555 struct tg3_napi *tnapi = &tp->napi[j];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006556
Matt Carlson8fea32b2010-09-15 08:59:58 +00006557 tg3_rx_prodring_free(tp, &tnapi->prodring);
Matt Carlsonb28f6422010-06-05 17:24:32 +00006558
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006559 if (!tnapi->tx_buffers)
6560 continue;
6561
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006562 for (i = 0; i < TG3_TX_RING_SIZE; ) {
Alexander Duyckf4188d82009-12-02 16:48:38 +00006563 struct ring_info *txp;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006564 struct sk_buff *skb;
Alexander Duyckf4188d82009-12-02 16:48:38 +00006565 unsigned int k;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006566
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006567 txp = &tnapi->tx_buffers[i];
6568 skb = txp->skb;
6569
6570 if (skb == NULL) {
6571 i++;
6572 continue;
6573 }
6574
Alexander Duyckf4188d82009-12-02 16:48:38 +00006575 pci_unmap_single(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006576 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006577 skb_headlen(skb),
6578 PCI_DMA_TODEVICE);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006579 txp->skb = NULL;
6580
Alexander Duyckf4188d82009-12-02 16:48:38 +00006581 i++;
6582
6583 for (k = 0; k < skb_shinfo(skb)->nr_frags; k++) {
6584 txp = &tnapi->tx_buffers[i & (TG3_TX_RING_SIZE - 1)];
6585 pci_unmap_page(tp->pdev,
FUJITA Tomonori4e5e4f02010-04-12 14:32:09 +00006586 dma_unmap_addr(txp, mapping),
Alexander Duyckf4188d82009-12-02 16:48:38 +00006587 skb_shinfo(skb)->frags[k].size,
6588 PCI_DMA_TODEVICE);
6589 i++;
6590 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006591
6592 dev_kfree_skb_any(skb);
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006593 }
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006594 }
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006595}
6596
6597/* Initialize tx/rx rings for packet processing.
6598 *
6599 * The chip has been shut down and the driver detached from
6600 * the networking, so no interrupts or new tx packets will
6601 * end up in the driver. tp->{tx,}lock are held and thus
6602 * we may not sleep.
6603 */
6604static int tg3_init_rings(struct tg3 *tp)
6605{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006606 int i;
Matt Carlson72334482009-08-28 14:03:01 +00006607
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006608 /* Free up all the SKBs. */
6609 tg3_free_rings(tp);
6610
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006611 for (i = 0; i < tp->irq_cnt; i++) {
6612 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006613
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006614 tnapi->last_tag = 0;
6615 tnapi->last_irq_tag = 0;
6616 tnapi->hw_status->status = 0;
6617 tnapi->hw_status->status_tag = 0;
6618 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6619
6620 tnapi->tx_prod = 0;
6621 tnapi->tx_cons = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006622 if (tnapi->tx_ring)
6623 memset(tnapi->tx_ring, 0, TG3_TX_RING_BYTES);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006624
6625 tnapi->rx_rcb_ptr = 0;
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006626 if (tnapi->rx_rcb)
6627 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006628
Matt Carlson8fea32b2010-09-15 08:59:58 +00006629 if (tg3_rx_prodring_alloc(tp, &tnapi->prodring)) {
Matt Carlsone4af1af2010-02-12 14:47:05 +00006630 tg3_free_rings(tp);
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006631 return -ENOMEM;
Matt Carlsone4af1af2010-02-12 14:47:05 +00006632 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006633 }
Matt Carlson72334482009-08-28 14:03:01 +00006634
Matt Carlson2b2cdb62009-11-13 13:03:48 +00006635 return 0;
Matt Carlsoncf7a7292009-08-28 13:59:57 +00006636}
6637
6638/*
6639 * Must not be invoked with interrupt sources disabled and
6640 * the hardware shutdown down.
6641 */
6642static void tg3_free_consistent(struct tg3 *tp)
6643{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006644 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006645
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006646 for (i = 0; i < tp->irq_cnt; i++) {
6647 struct tg3_napi *tnapi = &tp->napi[i];
6648
6649 if (tnapi->tx_ring) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006650 dma_free_coherent(&tp->pdev->dev, TG3_TX_RING_BYTES,
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006651 tnapi->tx_ring, tnapi->tx_desc_mapping);
6652 tnapi->tx_ring = NULL;
6653 }
6654
6655 kfree(tnapi->tx_buffers);
6656 tnapi->tx_buffers = NULL;
6657
6658 if (tnapi->rx_rcb) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006659 dma_free_coherent(&tp->pdev->dev,
6660 TG3_RX_RCB_RING_BYTES(tp),
6661 tnapi->rx_rcb,
6662 tnapi->rx_rcb_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006663 tnapi->rx_rcb = NULL;
6664 }
6665
Matt Carlson8fea32b2010-09-15 08:59:58 +00006666 tg3_rx_prodring_fini(tp, &tnapi->prodring);
6667
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006668 if (tnapi->hw_status) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006669 dma_free_coherent(&tp->pdev->dev, TG3_HW_STATUS_SIZE,
6670 tnapi->hw_status,
6671 tnapi->status_mapping);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006672 tnapi->hw_status = NULL;
6673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006675
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676 if (tp->hw_stats) {
Matt Carlson4bae65c2010-11-24 08:31:52 +00006677 dma_free_coherent(&tp->pdev->dev, sizeof(struct tg3_hw_stats),
6678 tp->hw_stats, tp->stats_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 tp->hw_stats = NULL;
6680 }
6681}
6682
6683/*
6684 * Must not be invoked with interrupt sources disabled and
6685 * the hardware shutdown down. Can sleep.
6686 */
6687static int tg3_alloc_consistent(struct tg3 *tp)
6688{
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006689 int i;
Matt Carlson898a56f2009-08-28 14:02:40 +00006690
Matt Carlson4bae65c2010-11-24 08:31:52 +00006691 tp->hw_stats = dma_alloc_coherent(&tp->pdev->dev,
6692 sizeof(struct tg3_hw_stats),
6693 &tp->stats_mapping,
6694 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 if (!tp->hw_stats)
6696 goto err_out;
6697
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6699
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006700 for (i = 0; i < tp->irq_cnt; i++) {
6701 struct tg3_napi *tnapi = &tp->napi[i];
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006702 struct tg3_hw_status *sblk;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006703
Matt Carlson4bae65c2010-11-24 08:31:52 +00006704 tnapi->hw_status = dma_alloc_coherent(&tp->pdev->dev,
6705 TG3_HW_STATUS_SIZE,
6706 &tnapi->status_mapping,
6707 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006708 if (!tnapi->hw_status)
6709 goto err_out;
6710
6711 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006712 sblk = tnapi->hw_status;
6713
Matt Carlson8fea32b2010-09-15 08:59:58 +00006714 if (tg3_rx_prodring_init(tp, &tnapi->prodring))
6715 goto err_out;
6716
Matt Carlson19cfaec2009-12-03 08:36:20 +00006717 /* If multivector TSS is enabled, vector 0 does not handle
6718 * tx interrupts. Don't allocate any resources for it.
6719 */
6720 if ((!i && !(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) ||
6721 (i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))) {
6722 tnapi->tx_buffers = kzalloc(sizeof(struct ring_info) *
6723 TG3_TX_RING_SIZE,
6724 GFP_KERNEL);
6725 if (!tnapi->tx_buffers)
6726 goto err_out;
6727
Matt Carlson4bae65c2010-11-24 08:31:52 +00006728 tnapi->tx_ring = dma_alloc_coherent(&tp->pdev->dev,
6729 TG3_TX_RING_BYTES,
6730 &tnapi->tx_desc_mapping,
6731 GFP_KERNEL);
Matt Carlson19cfaec2009-12-03 08:36:20 +00006732 if (!tnapi->tx_ring)
6733 goto err_out;
6734 }
6735
Matt Carlson8d9d7cf2009-09-01 13:19:05 +00006736 /*
6737 * When RSS is enabled, the status block format changes
6738 * slightly. The "rx_jumbo_consumer", "reserved",
6739 * and "rx_mini_consumer" members get mapped to the
6740 * other three rx return ring producer indexes.
6741 */
6742 switch (i) {
6743 default:
6744 tnapi->rx_rcb_prod_idx = &sblk->idx[0].rx_producer;
6745 break;
6746 case 2:
6747 tnapi->rx_rcb_prod_idx = &sblk->rx_jumbo_consumer;
6748 break;
6749 case 3:
6750 tnapi->rx_rcb_prod_idx = &sblk->reserved;
6751 break;
6752 case 4:
6753 tnapi->rx_rcb_prod_idx = &sblk->rx_mini_consumer;
6754 break;
6755 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006756
Matt Carlson0c1d0e22009-09-01 13:16:33 +00006757 /*
6758 * If multivector RSS is enabled, vector 0 does not handle
6759 * rx or tx interrupts. Don't allocate any resources for it.
6760 */
6761 if (!i && (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS))
6762 continue;
6763
Matt Carlson4bae65c2010-11-24 08:31:52 +00006764 tnapi->rx_rcb = dma_alloc_coherent(&tp->pdev->dev,
6765 TG3_RX_RCB_RING_BYTES(tp),
6766 &tnapi->rx_rcb_mapping,
6767 GFP_KERNEL);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006768 if (!tnapi->rx_rcb)
6769 goto err_out;
6770
6771 memset(tnapi->rx_rcb, 0, TG3_RX_RCB_RING_BYTES(tp));
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006772 }
6773
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 return 0;
6775
6776err_out:
6777 tg3_free_consistent(tp);
6778 return -ENOMEM;
6779}
6780
6781#define MAX_WAIT_CNT 1000
6782
6783/* To stop a block, clear the enable bit and poll till it
6784 * clears. tp->lock is held.
6785 */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006786static int tg3_stop_block(struct tg3 *tp, unsigned long ofs, u32 enable_bit, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787{
6788 unsigned int i;
6789 u32 val;
6790
6791 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
6792 switch (ofs) {
6793 case RCVLSC_MODE:
6794 case DMAC_MODE:
6795 case MBFREE_MODE:
6796 case BUFMGR_MODE:
6797 case MEMARB_MODE:
6798 /* We can't enable/disable these bits of the
6799 * 5705/5750, just say success.
6800 */
6801 return 0;
6802
6803 default:
6804 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07006805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 }
6807
6808 val = tr32(ofs);
6809 val &= ~enable_bit;
6810 tw32_f(ofs, val);
6811
6812 for (i = 0; i < MAX_WAIT_CNT; i++) {
6813 udelay(100);
6814 val = tr32(ofs);
6815 if ((val & enable_bit) == 0)
6816 break;
6817 }
6818
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006819 if (i == MAX_WAIT_CNT && !silent) {
Matt Carlson2445e462010-04-05 10:19:21 +00006820 dev_err(&tp->pdev->dev,
6821 "tg3_stop_block timed out, ofs=%lx enable_bit=%x\n",
6822 ofs, enable_bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 return -ENODEV;
6824 }
6825
6826 return 0;
6827}
6828
6829/* tp->lock is held. */
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006830static int tg3_abort_hw(struct tg3 *tp, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831{
6832 int i, err;
6833
6834 tg3_disable_ints(tp);
6835
6836 tp->rx_mode &= ~RX_MODE_ENABLE;
6837 tw32_f(MAC_RX_MODE, tp->rx_mode);
6838 udelay(10);
6839
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006840 err = tg3_stop_block(tp, RCVBDI_MODE, RCVBDI_MODE_ENABLE, silent);
6841 err |= tg3_stop_block(tp, RCVLPC_MODE, RCVLPC_MODE_ENABLE, silent);
6842 err |= tg3_stop_block(tp, RCVLSC_MODE, RCVLSC_MODE_ENABLE, silent);
6843 err |= tg3_stop_block(tp, RCVDBDI_MODE, RCVDBDI_MODE_ENABLE, silent);
6844 err |= tg3_stop_block(tp, RCVDCC_MODE, RCVDCC_MODE_ENABLE, silent);
6845 err |= tg3_stop_block(tp, RCVCC_MODE, RCVCC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006847 err |= tg3_stop_block(tp, SNDBDS_MODE, SNDBDS_MODE_ENABLE, silent);
6848 err |= tg3_stop_block(tp, SNDBDI_MODE, SNDBDI_MODE_ENABLE, silent);
6849 err |= tg3_stop_block(tp, SNDDATAI_MODE, SNDDATAI_MODE_ENABLE, silent);
6850 err |= tg3_stop_block(tp, RDMAC_MODE, RDMAC_MODE_ENABLE, silent);
6851 err |= tg3_stop_block(tp, SNDDATAC_MODE, SNDDATAC_MODE_ENABLE, silent);
6852 err |= tg3_stop_block(tp, DMAC_MODE, DMAC_MODE_ENABLE, silent);
6853 err |= tg3_stop_block(tp, SNDBDC_MODE, SNDBDC_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854
6855 tp->mac_mode &= ~MAC_MODE_TDE_ENABLE;
6856 tw32_f(MAC_MODE, tp->mac_mode);
6857 udelay(40);
6858
6859 tp->tx_mode &= ~TX_MODE_ENABLE;
6860 tw32_f(MAC_TX_MODE, tp->tx_mode);
6861
6862 for (i = 0; i < MAX_WAIT_CNT; i++) {
6863 udelay(100);
6864 if (!(tr32(MAC_TX_MODE) & TX_MODE_ENABLE))
6865 break;
6866 }
6867 if (i >= MAX_WAIT_CNT) {
Matt Carlsonab96b242010-04-05 10:19:22 +00006868 dev_err(&tp->pdev->dev,
6869 "%s timed out, TX_MODE_ENABLE will not clear "
6870 "MAC_TX_MODE=%08x\n", __func__, tr32(MAC_TX_MODE));
Michael Chane6de8ad2005-05-05 14:42:41 -07006871 err |= -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872 }
6873
Michael Chane6de8ad2005-05-05 14:42:41 -07006874 err |= tg3_stop_block(tp, HOSTCC_MODE, HOSTCC_MODE_ENABLE, silent);
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006875 err |= tg3_stop_block(tp, WDMAC_MODE, WDMAC_MODE_ENABLE, silent);
6876 err |= tg3_stop_block(tp, MBFREE_MODE, MBFREE_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
6878 tw32(FTQ_RESET, 0xffffffff);
6879 tw32(FTQ_RESET, 0x00000000);
6880
David S. Millerb3b7d6b2005-05-05 14:40:20 -07006881 err |= tg3_stop_block(tp, BUFMGR_MODE, BUFMGR_MODE_ENABLE, silent);
6882 err |= tg3_stop_block(tp, MEMARB_MODE, MEMARB_MODE_ENABLE, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883
Matt Carlsonf77a6a82009-09-01 13:04:37 +00006884 for (i = 0; i < tp->irq_cnt; i++) {
6885 struct tg3_napi *tnapi = &tp->napi[i];
6886 if (tnapi->hw_status)
6887 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
6888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 if (tp->hw_stats)
6890 memset(tp->hw_stats, 0, sizeof(struct tg3_hw_stats));
6891
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 return err;
6893}
6894
Matt Carlson0d3031d2007-10-10 18:02:43 -07006895static void tg3_ape_send_event(struct tg3 *tp, u32 event)
6896{
6897 int i;
6898 u32 apedata;
6899
Matt Carlsondc6d0742010-09-15 08:59:55 +00006900 /* NCSI does not support APE events */
6901 if (tp->tg3_flags3 & TG3_FLG3_APE_HAS_NCSI)
6902 return;
6903
Matt Carlson0d3031d2007-10-10 18:02:43 -07006904 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
6905 if (apedata != APE_SEG_SIG_MAGIC)
6906 return;
6907
6908 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
Matt Carlson731fd792008-08-15 14:07:51 -07006909 if (!(apedata & APE_FW_STATUS_READY))
Matt Carlson0d3031d2007-10-10 18:02:43 -07006910 return;
6911
6912 /* Wait for up to 1 millisecond for APE to service previous event. */
6913 for (i = 0; i < 10; i++) {
6914 if (tg3_ape_lock(tp, TG3_APE_LOCK_MEM))
6915 return;
6916
6917 apedata = tg3_ape_read32(tp, TG3_APE_EVENT_STATUS);
6918
6919 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6920 tg3_ape_write32(tp, TG3_APE_EVENT_STATUS,
6921 event | APE_EVENT_STATUS_EVENT_PENDING);
6922
6923 tg3_ape_unlock(tp, TG3_APE_LOCK_MEM);
6924
6925 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6926 break;
6927
6928 udelay(100);
6929 }
6930
6931 if (!(apedata & APE_EVENT_STATUS_EVENT_PENDING))
6932 tg3_ape_write32(tp, TG3_APE_EVENT, APE_EVENT_1);
6933}
6934
6935static void tg3_ape_driver_state_change(struct tg3 *tp, int kind)
6936{
6937 u32 event;
6938 u32 apedata;
6939
6940 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE))
6941 return;
6942
6943 switch (kind) {
Matt Carlson33f401a2010-04-05 10:19:27 +00006944 case RESET_KIND_INIT:
6945 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG,
6946 APE_HOST_SEG_SIG_MAGIC);
6947 tg3_ape_write32(tp, TG3_APE_HOST_SEG_LEN,
6948 APE_HOST_SEG_LEN_MAGIC);
6949 apedata = tg3_ape_read32(tp, TG3_APE_HOST_INIT_COUNT);
6950 tg3_ape_write32(tp, TG3_APE_HOST_INIT_COUNT, ++apedata);
6951 tg3_ape_write32(tp, TG3_APE_HOST_DRIVER_ID,
Matt Carlson6867c842010-07-11 09:31:44 +00006952 APE_HOST_DRIVER_ID_MAGIC(TG3_MAJ_NUM, TG3_MIN_NUM));
Matt Carlson33f401a2010-04-05 10:19:27 +00006953 tg3_ape_write32(tp, TG3_APE_HOST_BEHAVIOR,
6954 APE_HOST_BEHAV_NO_PHYLOCK);
Matt Carlsondc6d0742010-09-15 08:59:55 +00006955 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE,
6956 TG3_APE_HOST_DRVR_STATE_START);
Matt Carlson0d3031d2007-10-10 18:02:43 -07006957
Matt Carlson33f401a2010-04-05 10:19:27 +00006958 event = APE_EVENT_STATUS_STATE_START;
6959 break;
6960 case RESET_KIND_SHUTDOWN:
6961 /* With the interface we are currently using,
6962 * APE does not track driver state. Wiping
6963 * out the HOST SEGMENT SIGNATURE forces
6964 * the APE to assume OS absent status.
6965 */
6966 tg3_ape_write32(tp, TG3_APE_HOST_SEG_SIG, 0x0);
Matt Carlsonb2aee152008-11-03 16:51:11 -08006967
Matt Carlsondc6d0742010-09-15 08:59:55 +00006968 if (device_may_wakeup(&tp->pdev->dev) &&
6969 (tp->tg3_flags & TG3_FLAG_WOL_ENABLE)) {
6970 tg3_ape_write32(tp, TG3_APE_HOST_WOL_SPEED,
6971 TG3_APE_HOST_WOL_SPEED_AUTO);
6972 apedata = TG3_APE_HOST_DRVR_STATE_WOL;
6973 } else
6974 apedata = TG3_APE_HOST_DRVR_STATE_UNLOAD;
6975
6976 tg3_ape_write32(tp, TG3_APE_HOST_DRVR_STATE, apedata);
6977
Matt Carlson33f401a2010-04-05 10:19:27 +00006978 event = APE_EVENT_STATUS_STATE_UNLOAD;
6979 break;
6980 case RESET_KIND_SUSPEND:
6981 event = APE_EVENT_STATUS_STATE_SUSPEND;
6982 break;
6983 default:
6984 return;
Matt Carlson0d3031d2007-10-10 18:02:43 -07006985 }
6986
6987 event |= APE_EVENT_STATUS_DRIVER_EVNT | APE_EVENT_STATUS_STATE_CHNGE;
6988
6989 tg3_ape_send_event(tp, event);
6990}
6991
Michael Chane6af3012005-04-21 17:12:05 -07006992/* tp->lock is held. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993static void tg3_write_sig_pre_reset(struct tg3 *tp, int kind)
6994{
David S. Millerf49639e2006-06-09 11:58:36 -07006995 tg3_write_mem(tp, NIC_SRAM_FIRMWARE_MBOX,
6996 NIC_SRAM_FIRMWARE_MBOX_MAGIC1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997
6998 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
6999 switch (kind) {
7000 case RESET_KIND_INIT:
7001 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7002 DRV_STATE_START);
7003 break;
7004
7005 case RESET_KIND_SHUTDOWN:
7006 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7007 DRV_STATE_UNLOAD);
7008 break;
7009
7010 case RESET_KIND_SUSPEND:
7011 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7012 DRV_STATE_SUSPEND);
7013 break;
7014
7015 default:
7016 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007019
7020 if (kind == RESET_KIND_INIT ||
7021 kind == RESET_KIND_SUSPEND)
7022 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023}
7024
7025/* tp->lock is held. */
7026static void tg3_write_sig_post_reset(struct tg3 *tp, int kind)
7027{
7028 if (tp->tg3_flags2 & TG3_FLG2_ASF_NEW_HANDSHAKE) {
7029 switch (kind) {
7030 case RESET_KIND_INIT:
7031 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7032 DRV_STATE_START_DONE);
7033 break;
7034
7035 case RESET_KIND_SHUTDOWN:
7036 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7037 DRV_STATE_UNLOAD_DONE);
7038 break;
7039
7040 default:
7041 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 }
Matt Carlson0d3031d2007-10-10 18:02:43 -07007044
7045 if (kind == RESET_KIND_SHUTDOWN)
7046 tg3_ape_driver_state_change(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047}
7048
7049/* tp->lock is held. */
7050static void tg3_write_sig_legacy(struct tg3 *tp, int kind)
7051{
7052 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
7053 switch (kind) {
7054 case RESET_KIND_INIT:
7055 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7056 DRV_STATE_START);
7057 break;
7058
7059 case RESET_KIND_SHUTDOWN:
7060 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7061 DRV_STATE_UNLOAD);
7062 break;
7063
7064 case RESET_KIND_SUSPEND:
7065 tg3_write_mem(tp, NIC_SRAM_FW_DRV_STATE_MBOX,
7066 DRV_STATE_SUSPEND);
7067 break;
7068
7069 default:
7070 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07007071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072 }
7073}
7074
Michael Chan7a6f4362006-09-27 16:03:31 -07007075static int tg3_poll_fw(struct tg3 *tp)
7076{
7077 int i;
7078 u32 val;
7079
Michael Chanb5d37722006-09-27 16:06:21 -07007080 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Gary Zambrano0ccead12006-11-14 16:34:00 -08007081 /* Wait up to 20ms for init done. */
7082 for (i = 0; i < 200; i++) {
Michael Chanb5d37722006-09-27 16:06:21 -07007083 if (tr32(VCPU_STATUS) & VCPU_STATUS_INIT_DONE)
7084 return 0;
Gary Zambrano0ccead12006-11-14 16:34:00 -08007085 udelay(100);
Michael Chanb5d37722006-09-27 16:06:21 -07007086 }
7087 return -ENODEV;
7088 }
7089
Michael Chan7a6f4362006-09-27 16:03:31 -07007090 /* Wait for firmware initialization to complete. */
7091 for (i = 0; i < 100000; i++) {
7092 tg3_read_mem(tp, NIC_SRAM_FIRMWARE_MBOX, &val);
7093 if (val == ~NIC_SRAM_FIRMWARE_MBOX_MAGIC1)
7094 break;
7095 udelay(10);
7096 }
7097
7098 /* Chip might not be fitted with firmware. Some Sun onboard
7099 * parts are configured like that. So don't signal the timeout
7100 * of the above loop as an error, but do report the lack of
7101 * running firmware once.
7102 */
7103 if (i >= 100000 &&
7104 !(tp->tg3_flags2 & TG3_FLG2_NO_FWARE_REPORTED)) {
7105 tp->tg3_flags2 |= TG3_FLG2_NO_FWARE_REPORTED;
7106
Joe Perches05dbe002010-02-17 19:44:19 +00007107 netdev_info(tp->dev, "No firmware running\n");
Michael Chan7a6f4362006-09-27 16:03:31 -07007108 }
7109
Matt Carlson6b10c162010-02-12 14:47:08 +00007110 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
7111 /* The 57765 A0 needs a little more
7112 * time to do some important work.
7113 */
7114 mdelay(10);
7115 }
7116
Michael Chan7a6f4362006-09-27 16:03:31 -07007117 return 0;
7118}
7119
Michael Chanee6a99b2007-07-18 21:49:10 -07007120/* Save PCI command register before chip reset */
7121static void tg3_save_pci_state(struct tg3 *tp)
7122{
Matt Carlson8a6eac92007-10-21 16:17:55 -07007123 pci_read_config_word(tp->pdev, PCI_COMMAND, &tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007124}
7125
7126/* Restore PCI state after chip reset */
7127static void tg3_restore_pci_state(struct tg3 *tp)
7128{
7129 u32 val;
7130
7131 /* Re-enable indirect register accesses. */
7132 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
7133 tp->misc_host_ctrl);
7134
7135 /* Set MAX PCI retry to zero. */
7136 val = (PCISTATE_ROM_ENABLE | PCISTATE_ROM_RETRY_ENABLE);
7137 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
7138 (tp->tg3_flags & TG3_FLAG_PCIX_MODE))
7139 val |= PCISTATE_RETRY_SAME_DMA;
Matt Carlson0d3031d2007-10-10 18:02:43 -07007140 /* Allow reads and writes to the APE register and memory space. */
7141 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7142 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00007143 PCISTATE_ALLOW_APE_SHMEM_WR |
7144 PCISTATE_ALLOW_APE_PSPACE_WR;
Michael Chanee6a99b2007-07-18 21:49:10 -07007145 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, val);
7146
Matt Carlson8a6eac92007-10-21 16:17:55 -07007147 pci_write_config_word(tp->pdev, PCI_COMMAND, tp->pci_cmd);
Michael Chanee6a99b2007-07-18 21:49:10 -07007148
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007149 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785) {
7150 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
Matt Carlsoncf790032010-11-24 08:31:48 +00007151 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlsonfcb389d2008-11-03 16:55:44 -08007152 else {
7153 pci_write_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
7154 tp->pci_cacheline_sz);
7155 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
7156 tp->pci_lat_timer);
7157 }
Michael Chan114342f2007-10-15 02:12:26 -07007158 }
Matt Carlson5f5c51e2007-11-12 21:19:37 -08007159
Michael Chanee6a99b2007-07-18 21:49:10 -07007160 /* Make sure PCI-X relaxed ordering bit is clear. */
Matt Carlson52f44902008-11-21 17:17:04 -08007161 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Matt Carlson9974a352007-10-07 23:27:28 -07007162 u16 pcix_cmd;
7163
7164 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7165 &pcix_cmd);
7166 pcix_cmd &= ~PCI_X_CMD_ERO;
7167 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
7168 pcix_cmd);
7169 }
Michael Chanee6a99b2007-07-18 21:49:10 -07007170
7171 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) {
Michael Chanee6a99b2007-07-18 21:49:10 -07007172
7173 /* Chip reset on 5780 will reset MSI enable bit,
7174 * so need to restore it.
7175 */
7176 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
7177 u16 ctrl;
7178
7179 pci_read_config_word(tp->pdev,
7180 tp->msi_cap + PCI_MSI_FLAGS,
7181 &ctrl);
7182 pci_write_config_word(tp->pdev,
7183 tp->msi_cap + PCI_MSI_FLAGS,
7184 ctrl | PCI_MSI_FLAGS_ENABLE);
7185 val = tr32(MSGINT_MODE);
7186 tw32(MSGINT_MODE, val | MSGINT_MODE_ENABLE);
7187 }
7188 }
7189}
7190
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191static void tg3_stop_fw(struct tg3 *);
7192
7193/* tp->lock is held. */
7194static int tg3_chip_reset(struct tg3 *tp)
7195{
7196 u32 val;
Michael Chan1ee582d2005-08-09 20:16:46 -07007197 void (*write_op)(struct tg3 *, u32, u32);
Matt Carlson4f125f42009-09-01 12:55:02 +00007198 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199
David S. Millerf49639e2006-06-09 11:58:36 -07007200 tg3_nvram_lock(tp);
7201
Matt Carlson77b483f2008-08-15 14:07:24 -07007202 tg3_ape_lock(tp, TG3_APE_LOCK_GRC);
7203
David S. Millerf49639e2006-06-09 11:58:36 -07007204 /* No matching tg3_nvram_unlock() after this because
7205 * chip reset below will undo the nvram lock.
7206 */
7207 tp->nvram_lock_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208
Michael Chanee6a99b2007-07-18 21:49:10 -07007209 /* GRC_MISC_CFG core clock reset will clear the memory
7210 * enable bit in PCI register 4 and the MSI enable bit
7211 * on some chips, so we save relevant registers here.
7212 */
7213 tg3_save_pci_state(tp);
7214
Michael Chand9ab5ad2006-03-20 22:27:35 -08007215 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08007216 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS))
Michael Chand9ab5ad2006-03-20 22:27:35 -08007217 tw32(GRC_FASTBOOT_PC, 0);
7218
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 /*
7220 * We must avoid the readl() that normally takes place.
7221 * It locks machines, causes machine checks, and other
7222 * fun things. So, temporarily disable the 5701
7223 * hardware workaround, while we do the reset.
7224 */
Michael Chan1ee582d2005-08-09 20:16:46 -07007225 write_op = tp->write32;
7226 if (write_op == tg3_write_flush_reg32)
7227 tp->write32 = tg3_write32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228
Michael Chand18edcb2007-03-24 20:57:11 -07007229 /* Prevent the irq handler from reading or writing PCI registers
7230 * during chip reset when the memory enable bit in the PCI command
7231 * register may be cleared. The chip does not generate interrupt
7232 * at this time, but the irq handler may still be called due to irq
7233 * sharing or irqpoll.
7234 */
7235 tp->tg3_flags |= TG3_FLAG_CHIP_RESETTING;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007236 for (i = 0; i < tp->irq_cnt; i++) {
7237 struct tg3_napi *tnapi = &tp->napi[i];
7238 if (tnapi->hw_status) {
7239 tnapi->hw_status->status = 0;
7240 tnapi->hw_status->status_tag = 0;
7241 }
7242 tnapi->last_tag = 0;
7243 tnapi->last_irq_tag = 0;
Michael Chanb8fa2f32007-04-06 17:35:37 -07007244 }
Michael Chand18edcb2007-03-24 20:57:11 -07007245 smp_mb();
Matt Carlson4f125f42009-09-01 12:55:02 +00007246
7247 for (i = 0; i < tp->irq_cnt; i++)
7248 synchronize_irq(tp->napi[i].irq_vec);
Michael Chand18edcb2007-03-24 20:57:11 -07007249
Matt Carlson255ca312009-08-25 10:07:27 +00007250 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
7251 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
7252 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
7253 }
7254
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 /* do the reset */
7256 val = GRC_MISC_CFG_CORECLK_RESET;
7257
7258 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
Matt Carlson88075d92010-08-02 11:25:58 +00007259 /* Force PCIe 1.0a mode */
7260 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +00007261 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlson88075d92010-08-02 11:25:58 +00007262 tr32(TG3_PCIE_PHY_TSTCTL) ==
7263 (TG3_PCIE_PHY_TSTCTL_PCIE10 | TG3_PCIE_PHY_TSTCTL_PSCRAM))
7264 tw32(TG3_PCIE_PHY_TSTCTL, TG3_PCIE_PHY_TSTCTL_PSCRAM);
7265
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0) {
7267 tw32(GRC_MISC_CFG, (1 << 29));
7268 val |= (1 << 29);
7269 }
7270 }
7271
Michael Chanb5d37722006-09-27 16:06:21 -07007272 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7273 tw32(VCPU_STATUS, tr32(VCPU_STATUS) | VCPU_STATUS_DRV_RESET);
7274 tw32(GRC_VCPU_EXT_CTRL,
7275 tr32(GRC_VCPU_EXT_CTRL) & ~GRC_VCPU_EXT_CTRL_HALT_CPU);
7276 }
7277
Matt Carlsonf37500d2010-08-02 11:25:59 +00007278 /* Manage gphy power for all CPMU absent PCIe devices. */
7279 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
7280 !(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 val |= GRC_MISC_CFG_KEEP_GPHY_POWER;
Matt Carlsonf37500d2010-08-02 11:25:59 +00007282
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 tw32(GRC_MISC_CFG, val);
7284
Michael Chan1ee582d2005-08-09 20:16:46 -07007285 /* restore 5701 hardware bug workaround write method */
7286 tp->write32 = write_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287
7288 /* Unfortunately, we have to delay before the PCI read back.
7289 * Some 575X chips even will not respond to a PCI cfg access
7290 * when the reset command is given to the chip.
7291 *
7292 * How do these hardware designers expect things to work
7293 * properly if the PCI write is posted for a long period
7294 * of time? It is always necessary to have some method by
7295 * which a register read back can occur to push the write
7296 * out which does the reset.
7297 *
7298 * For most tg3 variants the trick below was working.
7299 * Ho hum...
7300 */
7301 udelay(120);
7302
7303 /* Flush PCI posted writes. The normal MMIO registers
7304 * are inaccessible at this time so this is the only
7305 * way to make this reliably (actually, this is no longer
7306 * the case, see above). I tried to use indirect
7307 * register read/write but this upset some 5701 variants.
7308 */
7309 pci_read_config_dword(tp->pdev, PCI_COMMAND, &val);
7310
7311 udelay(120);
7312
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007313 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) && tp->pcie_cap) {
Matt Carlsone7126992009-08-25 10:08:16 +00007314 u16 val16;
7315
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A0) {
7317 int i;
7318 u32 cfg_val;
7319
7320 /* Wait for link training to complete. */
7321 for (i = 0; i < 5000; i++)
7322 udelay(100);
7323
7324 pci_read_config_dword(tp->pdev, 0xc4, &cfg_val);
7325 pci_write_config_dword(tp->pdev, 0xc4,
7326 cfg_val | (1 << 15));
7327 }
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007328
Matt Carlsone7126992009-08-25 10:08:16 +00007329 /* Clear the "no snoop" and "relaxed ordering" bits. */
7330 pci_read_config_word(tp->pdev,
7331 tp->pcie_cap + PCI_EXP_DEVCTL,
7332 &val16);
7333 val16 &= ~(PCI_EXP_DEVCTL_RELAX_EN |
7334 PCI_EXP_DEVCTL_NOSNOOP_EN);
7335 /*
7336 * Older PCIe devices only support the 128 byte
7337 * MPS setting. Enforce the restriction.
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007338 */
Matt Carlson6de34cb2010-08-02 11:25:55 +00007339 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Matt Carlsone7126992009-08-25 10:08:16 +00007340 val16 &= ~PCI_EXP_DEVCTL_PAYLOAD;
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007341 pci_write_config_word(tp->pdev,
7342 tp->pcie_cap + PCI_EXP_DEVCTL,
Matt Carlsone7126992009-08-25 10:08:16 +00007343 val16);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007344
Matt Carlsoncf790032010-11-24 08:31:48 +00007345 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5e7dfd02008-11-21 17:18:16 -08007346
7347 /* Clear error status */
7348 pci_write_config_word(tp->pdev,
7349 tp->pcie_cap + PCI_EXP_DEVSTA,
7350 PCI_EXP_DEVSTA_CED |
7351 PCI_EXP_DEVSTA_NFED |
7352 PCI_EXP_DEVSTA_FED |
7353 PCI_EXP_DEVSTA_URD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354 }
7355
Michael Chanee6a99b2007-07-18 21:49:10 -07007356 tg3_restore_pci_state(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007357
Matt Carlsone64de4e2011-04-13 11:05:05 +00007358 tp->tg3_flags &= ~(TG3_FLAG_CHIP_RESETTING |
7359 TG3_FLAG_ERROR_PROCESSED);
Michael Chand18edcb2007-03-24 20:57:11 -07007360
Michael Chanee6a99b2007-07-18 21:49:10 -07007361 val = 0;
7362 if (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)
Michael Chan4cf78e42005-07-25 12:29:19 -07007363 val = tr32(MEMARB_MODE);
Michael Chanee6a99b2007-07-18 21:49:10 -07007364 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365
7366 if (tp->pci_chip_rev_id == CHIPREV_ID_5750_A3) {
7367 tg3_stop_fw(tp);
7368 tw32(0x5000, 0x400);
7369 }
7370
7371 tw32(GRC_MODE, tp->grc_mode);
7372
7373 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007374 val = tr32(0xc4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375
7376 tw32(0xc4, val | (1 << 15));
7377 }
7378
7379 if ((tp->nic_sram_data_cfg & NIC_SRAM_DATA_CFG_MINI_PCI) != 0 &&
7380 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
7381 tp->pci_clock_ctrl |= CLOCK_CTRL_CLKRUN_OENABLE;
7382 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A0)
7383 tp->pci_clock_ctrl |= CLOCK_CTRL_FORCE_CLKRUN;
7384 tw32(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
7385 }
7386
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007387 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7388 tp->mac_mode = MAC_MODE_APE_TX_EN |
7389 MAC_MODE_APE_RX_EN |
7390 MAC_MODE_TDE_ENABLE;
7391
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007392 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007393 tp->mac_mode |= MAC_MODE_PORT_MODE_TBI;
7394 val = tp->mac_mode;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00007395 } else if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007396 tp->mac_mode |= MAC_MODE_PORT_MODE_GMII;
7397 val = tp->mac_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398 } else
Matt Carlsond2394e6b2010-11-24 08:31:47 +00007399 val = 0;
7400
7401 tw32_f(MAC_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402 udelay(40);
7403
Matt Carlson77b483f2008-08-15 14:07:24 -07007404 tg3_ape_unlock(tp, TG3_APE_LOCK_GRC);
7405
Michael Chan7a6f4362006-09-27 16:03:31 -07007406 err = tg3_poll_fw(tp);
7407 if (err)
7408 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007409
Matt Carlson0a9140c2009-08-28 12:27:50 +00007410 tg3_mdio_start(tp);
7411
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007413 tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
7414 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +00007415 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Andy Gospodarekab0049b2007-09-06 20:42:14 +01007416 val = tr32(0x7c00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417
7418 tw32(0x7c00, val | (1 << 25));
7419 }
7420
Matt Carlsond78b59f2011-04-05 14:22:46 +00007421 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
7422 val = tr32(TG3_CPMU_CLCK_ORIDE);
7423 tw32(TG3_CPMU_CLCK_ORIDE, val & ~CPMU_CLCK_ORIDE_MAC_ORIDE_EN);
7424 }
7425
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 /* Reprobe ASF enable state. */
7427 tp->tg3_flags &= ~TG3_FLAG_ENABLE_ASF;
7428 tp->tg3_flags2 &= ~TG3_FLG2_ASF_NEW_HANDSHAKE;
7429 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
7430 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
7431 u32 nic_cfg;
7432
7433 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
7434 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
7435 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
Matt Carlson4ba526c2008-08-15 14:10:04 -07007436 tp->last_event_jiffies = jiffies;
John W. Linvillecbf46852005-04-21 17:01:29 -07007437 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
7439 }
7440 }
7441
7442 return 0;
7443}
7444
7445/* tp->lock is held. */
7446static void tg3_stop_fw(struct tg3 *tp)
7447{
Matt Carlson0d3031d2007-10-10 18:02:43 -07007448 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
7449 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07007450 /* Wait for RX cpu to ACK the previous event. */
7451 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452
7453 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX, FWCMD_NICDRV_PAUSE_FW);
Matt Carlson4ba526c2008-08-15 14:10:04 -07007454
7455 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456
Matt Carlson7c5026a2008-05-02 16:49:29 -07007457 /* Wait for RX cpu to ACK this event. */
7458 tg3_wait_for_event_ack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459 }
7460}
7461
7462/* tp->lock is held. */
Michael Chan944d9802005-05-29 14:57:48 -07007463static int tg3_halt(struct tg3 *tp, int kind, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464{
7465 int err;
7466
7467 tg3_stop_fw(tp);
7468
Michael Chan944d9802005-05-29 14:57:48 -07007469 tg3_write_sig_pre_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007470
David S. Millerb3b7d6b2005-05-05 14:40:20 -07007471 tg3_abort_hw(tp, silent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472 err = tg3_chip_reset(tp);
7473
Matt Carlsondaba2a62009-04-20 06:58:52 +00007474 __tg3_set_mac_addr(tp, 0);
7475
Michael Chan944d9802005-05-29 14:57:48 -07007476 tg3_write_sig_legacy(tp, kind);
7477 tg3_write_sig_post_reset(tp, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478
7479 if (err)
7480 return err;
7481
7482 return 0;
7483}
7484
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485#define RX_CPU_SCRATCH_BASE 0x30000
7486#define RX_CPU_SCRATCH_SIZE 0x04000
7487#define TX_CPU_SCRATCH_BASE 0x34000
7488#define TX_CPU_SCRATCH_SIZE 0x04000
7489
7490/* tp->lock is held. */
7491static int tg3_halt_cpu(struct tg3 *tp, u32 offset)
7492{
7493 int i;
7494
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +02007495 BUG_ON(offset == TX_CPU_BASE &&
7496 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007497
Michael Chanb5d37722006-09-27 16:06:21 -07007498 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
7499 u32 val = tr32(GRC_VCPU_EXT_CTRL);
7500
7501 tw32(GRC_VCPU_EXT_CTRL, val | GRC_VCPU_EXT_CTRL_HALT_CPU);
7502 return 0;
7503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 if (offset == RX_CPU_BASE) {
7505 for (i = 0; i < 10000; i++) {
7506 tw32(offset + CPU_STATE, 0xffffffff);
7507 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7508 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7509 break;
7510 }
7511
7512 tw32(offset + CPU_STATE, 0xffffffff);
7513 tw32_f(offset + CPU_MODE, CPU_MODE_HALT);
7514 udelay(10);
7515 } else {
7516 for (i = 0; i < 10000; i++) {
7517 tw32(offset + CPU_STATE, 0xffffffff);
7518 tw32(offset + CPU_MODE, CPU_MODE_HALT);
7519 if (tr32(offset + CPU_MODE) & CPU_MODE_HALT)
7520 break;
7521 }
7522 }
7523
7524 if (i >= 10000) {
Joe Perches05dbe002010-02-17 19:44:19 +00007525 netdev_err(tp->dev, "%s timed out, %s CPU\n",
7526 __func__, offset == RX_CPU_BASE ? "RX" : "TX");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007527 return -ENODEV;
7528 }
Michael Chanec41c7d2006-01-17 02:40:55 -08007529
7530 /* Clear firmware's nvram arbitration. */
7531 if (tp->tg3_flags & TG3_FLAG_NVRAM)
7532 tw32(NVRAM_SWARB, SWARB_REQ_CLR0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533 return 0;
7534}
7535
7536struct fw_info {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007537 unsigned int fw_base;
7538 unsigned int fw_len;
7539 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007540};
7541
7542/* tp->lock is held. */
7543static int tg3_load_firmware_cpu(struct tg3 *tp, u32 cpu_base, u32 cpu_scratch_base,
7544 int cpu_scratch_size, struct fw_info *info)
7545{
Michael Chanec41c7d2006-01-17 02:40:55 -08007546 int err, lock_err, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007547 void (*write_op)(struct tg3 *, u32, u32);
7548
7549 if (cpu_base == TX_CPU_BASE &&
7550 (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007551 netdev_err(tp->dev,
7552 "%s: Trying to load TX cpu firmware which is 5705\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007553 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007554 return -EINVAL;
7555 }
7556
7557 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
7558 write_op = tg3_write_mem;
7559 else
7560 write_op = tg3_write_indirect_reg32;
7561
Michael Chan1b628152005-05-29 14:59:49 -07007562 /* It is possible that bootcode is still loading at this point.
7563 * Get the nvram lock first before halting the cpu.
7564 */
Michael Chanec41c7d2006-01-17 02:40:55 -08007565 lock_err = tg3_nvram_lock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007566 err = tg3_halt_cpu(tp, cpu_base);
Michael Chanec41c7d2006-01-17 02:40:55 -08007567 if (!lock_err)
7568 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 if (err)
7570 goto out;
7571
7572 for (i = 0; i < cpu_scratch_size; i += sizeof(u32))
7573 write_op(tp, cpu_scratch_base + i, 0);
7574 tw32(cpu_base + CPU_STATE, 0xffffffff);
7575 tw32(cpu_base + CPU_MODE, tr32(cpu_base+CPU_MODE)|CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007576 for (i = 0; i < (info->fw_len / sizeof(u32)); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577 write_op(tp, (cpu_scratch_base +
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007578 (info->fw_base & 0xffff) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579 (i * sizeof(u32))),
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007580 be32_to_cpu(info->fw_data[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581
7582 err = 0;
7583
7584out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007585 return err;
7586}
7587
7588/* tp->lock is held. */
7589static int tg3_load_5701_a0_firmware_fix(struct tg3 *tp)
7590{
7591 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007592 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007593 int err, i;
7594
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007595 fw_data = (void *)tp->fw->data;
7596
7597 /* Firmware blob starts with version numbers, followed by
7598 start address and length. We are setting complete length.
7599 length = end_address_of_bss - start_address_of_text.
7600 Remainder is the blob to be loaded contiguously
7601 from start address. */
7602
7603 info.fw_base = be32_to_cpu(fw_data[1]);
7604 info.fw_len = tp->fw->size - 12;
7605 info.fw_data = &fw_data[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07007606
7607 err = tg3_load_firmware_cpu(tp, RX_CPU_BASE,
7608 RX_CPU_SCRATCH_BASE, RX_CPU_SCRATCH_SIZE,
7609 &info);
7610 if (err)
7611 return err;
7612
7613 err = tg3_load_firmware_cpu(tp, TX_CPU_BASE,
7614 TX_CPU_SCRATCH_BASE, TX_CPU_SCRATCH_SIZE,
7615 &info);
7616 if (err)
7617 return err;
7618
7619 /* Now startup only the RX cpu. */
7620 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007621 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622
7623 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007624 if (tr32(RX_CPU_BASE + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 break;
7626 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7627 tw32(RX_CPU_BASE + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007628 tw32_f(RX_CPU_BASE + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629 udelay(1000);
7630 }
7631 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007632 netdev_err(tp->dev, "%s fails to set RX CPU PC, is %08x "
7633 "should be %08x\n", __func__,
Joe Perches05dbe002010-02-17 19:44:19 +00007634 tr32(RX_CPU_BASE + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007635 return -ENODEV;
7636 }
7637 tw32(RX_CPU_BASE + CPU_STATE, 0xffffffff);
7638 tw32_f(RX_CPU_BASE + CPU_MODE, 0x00000000);
7639
7640 return 0;
7641}
7642
Linus Torvalds1da177e2005-04-16 15:20:36 -07007643/* 5705 needs a special version of the TSO firmware. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644
7645/* tp->lock is held. */
7646static int tg3_load_tso_firmware(struct tg3 *tp)
7647{
7648 struct fw_info info;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007649 const __be32 *fw_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650 unsigned long cpu_base, cpu_scratch_base, cpu_scratch_size;
7651 int err, i;
7652
7653 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
7654 return 0;
7655
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007656 fw_data = (void *)tp->fw->data;
7657
7658 /* Firmware blob starts with version numbers, followed by
7659 start address and length. We are setting complete length.
7660 length = end_address_of_bss - start_address_of_text.
7661 Remainder is the blob to be loaded contiguously
7662 from start address. */
7663
7664 info.fw_base = be32_to_cpu(fw_data[1]);
7665 cpu_scratch_size = tp->fw_len;
7666 info.fw_len = tp->fw->size - 12;
7667 info.fw_data = &fw_data[3];
7668
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007670 cpu_base = RX_CPU_BASE;
7671 cpu_scratch_base = NIC_SRAM_MBUF_POOL_BASE5705;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673 cpu_base = TX_CPU_BASE;
7674 cpu_scratch_base = TX_CPU_SCRATCH_BASE;
7675 cpu_scratch_size = TX_CPU_SCRATCH_SIZE;
7676 }
7677
7678 err = tg3_load_firmware_cpu(tp, cpu_base,
7679 cpu_scratch_base, cpu_scratch_size,
7680 &info);
7681 if (err)
7682 return err;
7683
7684 /* Now startup the cpu. */
7685 tw32(cpu_base + CPU_STATE, 0xffffffff);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007686 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007687
7688 for (i = 0; i < 5; i++) {
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007689 if (tr32(cpu_base + CPU_PC) == info.fw_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007690 break;
7691 tw32(cpu_base + CPU_STATE, 0xffffffff);
7692 tw32(cpu_base + CPU_MODE, CPU_MODE_HALT);
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08007693 tw32_f(cpu_base + CPU_PC, info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694 udelay(1000);
7695 }
7696 if (i >= 5) {
Matt Carlson5129c3a2010-04-05 10:19:23 +00007697 netdev_err(tp->dev,
7698 "%s fails to set CPU PC, is %08x should be %08x\n",
Joe Perches05dbe002010-02-17 19:44:19 +00007699 __func__, tr32(cpu_base + CPU_PC), info.fw_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700 return -ENODEV;
7701 }
7702 tw32(cpu_base + CPU_STATE, 0xffffffff);
7703 tw32_f(cpu_base + CPU_MODE, 0x00000000);
7704 return 0;
7705}
7706
Linus Torvalds1da177e2005-04-16 15:20:36 -07007707
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708static int tg3_set_mac_addr(struct net_device *dev, void *p)
7709{
7710 struct tg3 *tp = netdev_priv(dev);
7711 struct sockaddr *addr = p;
Michael Chan986e0ae2007-05-05 12:10:20 -07007712 int err = 0, skip_mac_1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007713
Michael Chanf9804dd2005-09-27 12:13:10 -07007714 if (!is_valid_ether_addr(addr->sa_data))
7715 return -EINVAL;
7716
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
7718
Michael Chane75f7c92006-03-20 21:33:26 -08007719 if (!netif_running(dev))
7720 return 0;
7721
Michael Chan58712ef2006-04-29 18:58:01 -07007722 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) {
Michael Chan986e0ae2007-05-05 12:10:20 -07007723 u32 addr0_high, addr0_low, addr1_high, addr1_low;
Michael Chan58712ef2006-04-29 18:58:01 -07007724
Michael Chan986e0ae2007-05-05 12:10:20 -07007725 addr0_high = tr32(MAC_ADDR_0_HIGH);
7726 addr0_low = tr32(MAC_ADDR_0_LOW);
7727 addr1_high = tr32(MAC_ADDR_1_HIGH);
7728 addr1_low = tr32(MAC_ADDR_1_LOW);
7729
7730 /* Skip MAC addr 1 if ASF is using it. */
7731 if ((addr0_high != addr1_high || addr0_low != addr1_low) &&
7732 !(addr1_high == 0 && addr1_low == 0))
7733 skip_mac_1 = 1;
Michael Chan58712ef2006-04-29 18:58:01 -07007734 }
Michael Chan986e0ae2007-05-05 12:10:20 -07007735 spin_lock_bh(&tp->lock);
7736 __tg3_set_mac_addr(tp, skip_mac_1);
7737 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738
Michael Chanb9ec6c12006-07-25 16:37:27 -07007739 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007740}
7741
7742/* tp->lock is held. */
7743static void tg3_set_bdinfo(struct tg3 *tp, u32 bdinfo_addr,
7744 dma_addr_t mapping, u32 maxlen_flags,
7745 u32 nic_addr)
7746{
7747 tg3_write_mem(tp,
7748 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH),
7749 ((u64) mapping >> 32));
7750 tg3_write_mem(tp,
7751 (bdinfo_addr + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW),
7752 ((u64) mapping & 0xffffffff));
7753 tg3_write_mem(tp,
7754 (bdinfo_addr + TG3_BDINFO_MAXLEN_FLAGS),
7755 maxlen_flags);
7756
7757 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7758 tg3_write_mem(tp,
7759 (bdinfo_addr + TG3_BDINFO_NIC_ADDR),
7760 nic_addr);
7761}
7762
7763static void __tg3_set_rx_mode(struct net_device *);
Michael Chand244c892005-07-05 14:42:33 -07007764static void __tg3_set_coalesce(struct tg3 *tp, struct ethtool_coalesce *ec)
David S. Miller15f98502005-05-18 22:49:26 -07007765{
Matt Carlsonb6080e12009-09-01 13:12:00 +00007766 int i;
7767
Matt Carlson19cfaec2009-12-03 08:36:20 +00007768 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)) {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007769 tw32(HOSTCC_TXCOL_TICKS, ec->tx_coalesce_usecs);
7770 tw32(HOSTCC_TXMAX_FRAMES, ec->tx_max_coalesced_frames);
7771 tw32(HOSTCC_TXCOAL_MAXF_INT, ec->tx_max_coalesced_frames_irq);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007772 } else {
7773 tw32(HOSTCC_TXCOL_TICKS, 0);
7774 tw32(HOSTCC_TXMAX_FRAMES, 0);
7775 tw32(HOSTCC_TXCOAL_MAXF_INT, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007776 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007777
Matt Carlson20d73752010-07-11 09:31:41 +00007778 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)) {
Matt Carlson19cfaec2009-12-03 08:36:20 +00007779 tw32(HOSTCC_RXCOL_TICKS, ec->rx_coalesce_usecs);
7780 tw32(HOSTCC_RXMAX_FRAMES, ec->rx_max_coalesced_frames);
7781 tw32(HOSTCC_RXCOAL_MAXF_INT, ec->rx_max_coalesced_frames_irq);
7782 } else {
Matt Carlsonb6080e12009-09-01 13:12:00 +00007783 tw32(HOSTCC_RXCOL_TICKS, 0);
7784 tw32(HOSTCC_RXMAX_FRAMES, 0);
7785 tw32(HOSTCC_RXCOAL_MAXF_INT, 0);
David S. Miller15f98502005-05-18 22:49:26 -07007786 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007787
David S. Miller15f98502005-05-18 22:49:26 -07007788 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7789 u32 val = ec->stats_block_coalesce_usecs;
7790
Matt Carlsonb6080e12009-09-01 13:12:00 +00007791 tw32(HOSTCC_RXCOAL_TICK_INT, ec->rx_coalesce_usecs_irq);
7792 tw32(HOSTCC_TXCOAL_TICK_INT, ec->tx_coalesce_usecs_irq);
7793
David S. Miller15f98502005-05-18 22:49:26 -07007794 if (!netif_carrier_ok(tp->dev))
7795 val = 0;
7796
7797 tw32(HOSTCC_STAT_COAL_TICKS, val);
7798 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007799
7800 for (i = 0; i < tp->irq_cnt - 1; i++) {
7801 u32 reg;
7802
7803 reg = HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18;
7804 tw32(reg, ec->rx_coalesce_usecs);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007805 reg = HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18;
7806 tw32(reg, ec->rx_max_coalesced_frames);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007807 reg = HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18;
7808 tw32(reg, ec->rx_max_coalesced_frames_irq);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007809
7810 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7811 reg = HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18;
7812 tw32(reg, ec->tx_coalesce_usecs);
7813 reg = HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18;
7814 tw32(reg, ec->tx_max_coalesced_frames);
7815 reg = HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18;
7816 tw32(reg, ec->tx_max_coalesced_frames_irq);
7817 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007818 }
7819
7820 for (; i < tp->irq_max - 1; i++) {
7821 tw32(HOSTCC_RXCOL_TICKS_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007822 tw32(HOSTCC_RXMAX_FRAMES_VEC1 + i * 0x18, 0);
Matt Carlsonb6080e12009-09-01 13:12:00 +00007823 tw32(HOSTCC_RXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
Matt Carlson19cfaec2009-12-03 08:36:20 +00007824
7825 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS) {
7826 tw32(HOSTCC_TXCOL_TICKS_VEC1 + i * 0x18, 0);
7827 tw32(HOSTCC_TXMAX_FRAMES_VEC1 + i * 0x18, 0);
7828 tw32(HOSTCC_TXCOAL_MAXF_INT_VEC1 + i * 0x18, 0);
7829 }
Matt Carlsonb6080e12009-09-01 13:12:00 +00007830 }
David S. Miller15f98502005-05-18 22:49:26 -07007831}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832
7833/* tp->lock is held. */
Matt Carlson2d31eca2009-09-01 12:53:31 +00007834static void tg3_rings_reset(struct tg3 *tp)
7835{
7836 int i;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007837 u32 stblk, txrcb, rxrcb, limit;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007838 struct tg3_napi *tnapi = &tp->napi[0];
7839
7840 /* Disable all transmit rings but the first. */
7841 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
7842 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlson0a58d662011-04-05 14:22:45 +00007843 else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlson3d377282010-10-14 10:37:39 +00007844 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 4;
Matt Carlsonb703df62009-12-03 08:36:21 +00007845 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
7846 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE * 2;
Matt Carlson2d31eca2009-09-01 12:53:31 +00007847 else
7848 limit = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7849
7850 for (txrcb = NIC_SRAM_SEND_RCB + TG3_BDINFO_SIZE;
7851 txrcb < limit; txrcb += TG3_BDINFO_SIZE)
7852 tg3_write_mem(tp, txrcb + TG3_BDINFO_MAXLEN_FLAGS,
7853 BDINFO_FLAGS_DISABLED);
7854
7855
7856 /* Disable all receive return rings but the first. */
Matt Carlson0a58d662011-04-05 14:22:45 +00007857 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00007858 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 17;
7859 else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlson2d31eca2009-09-01 12:53:31 +00007860 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 16;
Matt Carlsonb703df62009-12-03 08:36:21 +00007861 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
7862 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson2d31eca2009-09-01 12:53:31 +00007863 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE * 4;
7864 else
7865 limit = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7866
7867 for (rxrcb = NIC_SRAM_RCV_RET_RCB + TG3_BDINFO_SIZE;
7868 rxrcb < limit; rxrcb += TG3_BDINFO_SIZE)
7869 tg3_write_mem(tp, rxrcb + TG3_BDINFO_MAXLEN_FLAGS,
7870 BDINFO_FLAGS_DISABLED);
7871
7872 /* Disable interrupts */
7873 tw32_mailbox_f(tp->napi[0].int_mbox, 1);
7874
7875 /* Zero mailbox registers. */
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007876 if (tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) {
Matt Carlson6fd45cb2010-09-15 08:59:57 +00007877 for (i = 1; i < tp->irq_max; i++) {
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007878 tp->napi[i].tx_prod = 0;
7879 tp->napi[i].tx_cons = 0;
Matt Carlsonc2353a32010-01-20 16:58:08 +00007880 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
7881 tw32_mailbox(tp->napi[i].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007882 tw32_rx_mbox(tp->napi[i].consmbox, 0);
7883 tw32_mailbox_f(tp->napi[i].int_mbox, 1);
7884 }
Matt Carlsonc2353a32010-01-20 16:58:08 +00007885 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS))
7886 tw32_mailbox(tp->napi[0].prodmbox, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007887 } else {
7888 tp->napi[0].tx_prod = 0;
7889 tp->napi[0].tx_cons = 0;
7890 tw32_mailbox(tp->napi[0].prodmbox, 0);
7891 tw32_rx_mbox(tp->napi[0].consmbox, 0);
7892 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007893
7894 /* Make sure the NIC-based send BD rings are disabled. */
7895 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
7896 u32 mbox = MAILBOX_SNDNIC_PROD_IDX_0 + TG3_64BIT_REG_LOW;
7897 for (i = 0; i < 16; i++)
7898 tw32_tx_mbox(mbox + i * 8, 0);
7899 }
7900
7901 txrcb = NIC_SRAM_SEND_RCB;
7902 rxrcb = NIC_SRAM_RCV_RET_RCB;
7903
7904 /* Clear status block in ram. */
7905 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7906
7907 /* Set status block DMA address */
7908 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
7909 ((u64) tnapi->status_mapping >> 32));
7910 tw32(HOSTCC_STATUS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
7911 ((u64) tnapi->status_mapping & 0xffffffff));
7912
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007913 if (tnapi->tx_ring) {
7914 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7915 (TG3_TX_RING_SIZE <<
7916 BDINFO_FLAGS_MAXLEN_SHIFT),
7917 NIC_SRAM_TX_BUFFER_DESC);
7918 txrcb += TG3_BDINFO_SIZE;
7919 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007920
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007921 if (tnapi->rx_rcb) {
7922 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007923 (tp->rx_ret_ring_mask + 1) <<
7924 BDINFO_FLAGS_MAXLEN_SHIFT, 0);
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007925 rxrcb += TG3_BDINFO_SIZE;
7926 }
7927
7928 stblk = HOSTCC_STATBLCK_RING1;
7929
7930 for (i = 1, tnapi++; i < tp->irq_cnt; i++, tnapi++) {
7931 u64 mapping = (u64)tnapi->status_mapping;
7932 tw32(stblk + TG3_64BIT_REG_HIGH, mapping >> 32);
7933 tw32(stblk + TG3_64BIT_REG_LOW, mapping & 0xffffffff);
7934
7935 /* Clear status block in ram. */
7936 memset(tnapi->hw_status, 0, TG3_HW_STATUS_SIZE);
7937
Matt Carlson19cfaec2009-12-03 08:36:20 +00007938 if (tnapi->tx_ring) {
7939 tg3_set_bdinfo(tp, txrcb, tnapi->tx_desc_mapping,
7940 (TG3_TX_RING_SIZE <<
7941 BDINFO_FLAGS_MAXLEN_SHIFT),
7942 NIC_SRAM_TX_BUFFER_DESC);
7943 txrcb += TG3_BDINFO_SIZE;
7944 }
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007945
7946 tg3_set_bdinfo(tp, rxrcb, tnapi->rx_rcb_mapping,
Matt Carlson7cb32cf2010-09-30 10:34:36 +00007947 ((tp->rx_ret_ring_mask + 1) <<
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007948 BDINFO_FLAGS_MAXLEN_SHIFT), 0);
7949
7950 stblk += 8;
Matt Carlsonf77a6a82009-09-01 13:04:37 +00007951 rxrcb += TG3_BDINFO_SIZE;
7952 }
Matt Carlson2d31eca2009-09-01 12:53:31 +00007953}
7954
7955/* tp->lock is held. */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07007956static int tg3_reset_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957{
7958 u32 val, rdmac_mode;
7959 int i, err, limit;
Matt Carlson8fea32b2010-09-15 08:59:58 +00007960 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007961
7962 tg3_disable_ints(tp);
7963
7964 tg3_stop_fw(tp);
7965
7966 tg3_write_sig_pre_reset(tp, RESET_KIND_INIT);
7967
Matt Carlson859a5882010-04-05 10:19:28 +00007968 if (tp->tg3_flags & TG3_FLAG_INIT_COMPLETE)
Michael Chane6de8ad2005-05-05 14:42:41 -07007969 tg3_abort_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007970
Matt Carlson699c0192010-12-06 08:28:51 +00007971 /* Enable MAC control of LPI */
7972 if (tp->phy_flags & TG3_PHYFLG_EEE_CAP) {
7973 tw32_f(TG3_CPMU_EEE_LNKIDL_CTRL,
7974 TG3_CPMU_EEE_LNKIDL_PCIE_NL0 |
7975 TG3_CPMU_EEE_LNKIDL_UART_IDL);
7976
7977 tw32_f(TG3_CPMU_EEE_CTRL,
7978 TG3_CPMU_EEE_CTRL_EXIT_20_1_US);
7979
Matt Carlsona386b902010-12-06 08:28:53 +00007980 val = TG3_CPMU_EEEMD_ERLY_L1_XIT_DET |
7981 TG3_CPMU_EEEMD_LPI_IN_TX |
7982 TG3_CPMU_EEEMD_LPI_IN_RX |
7983 TG3_CPMU_EEEMD_EEE_ENABLE;
7984
7985 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717)
7986 val |= TG3_CPMU_EEEMD_SND_IDX_DET_EN;
7987
7988 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
7989 val |= TG3_CPMU_EEEMD_APE_TX_DET_EN;
7990
7991 tw32_f(TG3_CPMU_EEE_MODE, val);
7992
7993 tw32_f(TG3_CPMU_EEE_DBTMR1,
7994 TG3_CPMU_DBTMR1_PCIEXIT_2047US |
7995 TG3_CPMU_DBTMR1_LNKIDLE_2047US);
7996
7997 tw32_f(TG3_CPMU_EEE_DBTMR2,
Matt Carlsond7f2ab22011-01-25 15:58:56 +00007998 TG3_CPMU_DBTMR2_APE_TX_2047US |
Matt Carlsona386b902010-12-06 08:28:53 +00007999 TG3_CPMU_DBTMR2_TXIDXEQ_2047US);
Matt Carlson699c0192010-12-06 08:28:51 +00008000 }
8001
Matt Carlson603f1172010-02-12 14:47:10 +00008002 if (reset_phy)
Michael Chand4d2c552006-03-20 17:47:20 -08008003 tg3_phy_reset(tp);
8004
Linus Torvalds1da177e2005-04-16 15:20:36 -07008005 err = tg3_chip_reset(tp);
8006 if (err)
8007 return err;
8008
8009 tg3_write_sig_legacy(tp, RESET_KIND_INIT);
8010
Matt Carlsonbcb37f62008-11-03 16:52:09 -08008011 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008012 val = tr32(TG3_CPMU_CTRL);
8013 val &= ~(CPMU_CTRL_LINK_AWARE_MODE | CPMU_CTRL_LINK_IDLE_MODE);
8014 tw32(TG3_CPMU_CTRL, val);
Matt Carlson9acb9612007-11-12 21:10:06 -08008015
8016 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8017 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8018 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8019 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
8020
8021 val = tr32(TG3_CPMU_LNK_AWARE_PWRMD);
8022 val &= ~CPMU_LNK_AWARE_MACCLK_MASK;
8023 val |= CPMU_LNK_AWARE_MACCLK_6_25;
8024 tw32(TG3_CPMU_LNK_AWARE_PWRMD, val);
8025
8026 val = tr32(TG3_CPMU_HST_ACC);
8027 val &= ~CPMU_HST_ACC_MACCLK_MASK;
8028 val |= CPMU_HST_ACC_MACCLK_6_25;
8029 tw32(TG3_CPMU_HST_ACC, val);
Matt Carlsond30cdd22007-10-07 23:28:35 -07008030 }
8031
Matt Carlson33466d92009-04-20 06:57:41 +00008032 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
8033 val = tr32(PCIE_PWR_MGMT_THRESH) & ~PCIE_PWR_MGMT_L1_THRESH_MSK;
8034 val |= PCIE_PWR_MGMT_EXT_ASPM_TMR_EN |
8035 PCIE_PWR_MGMT_L1_THRESH_4MS;
8036 tw32(PCIE_PWR_MGMT_THRESH, val);
Matt Carlson521e6b92009-08-25 10:06:01 +00008037
8038 val = tr32(TG3_PCIE_EIDLE_DELAY) & ~TG3_PCIE_EIDLE_DELAY_MASK;
8039 tw32(TG3_PCIE_EIDLE_DELAY, val | TG3_PCIE_EIDLE_DELAY_13_CLKS);
8040
8041 tw32(TG3_CORR_ERR_STAT, TG3_CORR_ERR_STAT_CLEAR);
Matt Carlson33466d92009-04-20 06:57:41 +00008042
Matt Carlsonf40386c2009-11-02 14:24:02 +00008043 val = tr32(TG3_PCIE_LNKCTL) & ~TG3_PCIE_LNKCTL_L1_PLL_PD_EN;
8044 tw32(TG3_PCIE_LNKCTL, val | TG3_PCIE_LNKCTL_L1_PLL_PD_DIS);
Matt Carlson255ca312009-08-25 10:07:27 +00008045 }
8046
Matt Carlson614b05902010-01-20 16:58:02 +00008047 if (tp->tg3_flags3 & TG3_FLG3_L1PLLPD_EN) {
8048 u32 grc_mode = tr32(GRC_MODE);
8049
8050 /* Access the lower 1K of PL PCIE block registers. */
8051 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8052 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
8053
8054 val = tr32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1);
8055 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL1,
8056 val | TG3_PCIE_PL_LO_PHYCTL1_L1PLLPD_EN);
8057
8058 tw32(GRC_MODE, grc_mode);
8059 }
8060
Matt Carlson5093eed2010-11-24 08:31:45 +00008061 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
8062 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0) {
8063 u32 grc_mode = tr32(GRC_MODE);
Matt Carlsoncea46462010-04-12 06:58:24 +00008064
Matt Carlson5093eed2010-11-24 08:31:45 +00008065 /* Access the lower 1K of PL PCIE block registers. */
8066 val = grc_mode & ~GRC_MODE_PCIE_PORT_MASK;
8067 tw32(GRC_MODE, val | GRC_MODE_PCIE_PL_SEL);
Matt Carlsoncea46462010-04-12 06:58:24 +00008068
Matt Carlson5093eed2010-11-24 08:31:45 +00008069 val = tr32(TG3_PCIE_TLDLPL_PORT +
8070 TG3_PCIE_PL_LO_PHYCTL5);
8071 tw32(TG3_PCIE_TLDLPL_PORT + TG3_PCIE_PL_LO_PHYCTL5,
8072 val | TG3_PCIE_PL_LO_PHYCTL5_DIS_L2CLKREQ);
Matt Carlsoncea46462010-04-12 06:58:24 +00008073
Matt Carlson5093eed2010-11-24 08:31:45 +00008074 tw32(GRC_MODE, grc_mode);
8075 }
Matt Carlsona977dbe2010-04-12 06:58:26 +00008076
8077 val = tr32(TG3_CPMU_LSPD_10MB_CLK);
8078 val &= ~CPMU_LSPD_10MB_MACCLK_MASK;
8079 val |= CPMU_LSPD_10MB_MACCLK_6_25;
8080 tw32(TG3_CPMU_LSPD_10MB_CLK, val);
Matt Carlsoncea46462010-04-12 06:58:24 +00008081 }
8082
Linus Torvalds1da177e2005-04-16 15:20:36 -07008083 /* This works around an issue with Athlon chipsets on
8084 * B3 tigon3 silicon. This bit has no effect on any
8085 * other revision. But do not set this on PCI Express
Matt Carlson795d01c2007-10-07 23:28:17 -07008086 * chips and don't even touch the clocks if the CPMU is present.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008087 */
Matt Carlson795d01c2007-10-07 23:28:17 -07008088 if (!(tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)) {
8089 if (!(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
8090 tp->pci_clock_ctrl |= CLOCK_CTRL_DELAY_PCI_GRANT;
8091 tw32_f(TG3PCI_CLOCK_CTRL, tp->pci_clock_ctrl);
8092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008093
8094 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0 &&
8095 (tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
8096 val = tr32(TG3PCI_PCISTATE);
8097 val |= PCISTATE_RETRY_SAME_DMA;
8098 tw32(TG3PCI_PCISTATE, val);
8099 }
8100
Matt Carlson0d3031d2007-10-10 18:02:43 -07008101 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
8102 /* Allow reads and writes to the
8103 * APE register and memory space.
8104 */
8105 val = tr32(TG3PCI_PCISTATE);
8106 val |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +00008107 PCISTATE_ALLOW_APE_SHMEM_WR |
8108 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -07008109 tw32(TG3PCI_PCISTATE, val);
8110 }
8111
Linus Torvalds1da177e2005-04-16 15:20:36 -07008112 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_BX) {
8113 /* Enable some hw fixes. */
8114 val = tr32(TG3PCI_MSI_DATA);
8115 val |= (1 << 26) | (1 << 28) | (1 << 29);
8116 tw32(TG3PCI_MSI_DATA, val);
8117 }
8118
8119 /* Descriptor ring init may make accesses to the
8120 * NIC SRAM area to setup the TX descriptors, so we
8121 * can only do this after the hardware has been
8122 * successfully reset.
8123 */
Michael Chan32d8c572006-07-25 16:38:29 -07008124 err = tg3_init_rings(tp);
8125 if (err)
8126 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008127
Matt Carlson1407deb2011-04-05 14:22:44 +00008128 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008129 val = tr32(TG3PCI_DMA_RW_CTRL) &
8130 ~DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
Matt Carlson1a319022010-04-12 06:58:25 +00008131 if (tp->pci_chip_rev_id == CHIPREV_ID_57765_A0)
8132 val &= ~DMA_RWCTRL_CRDRDR_RDMA_MRRS_MSK;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +00008133 tw32(TG3PCI_DMA_RW_CTRL, val | tp->dma_rwctrl);
8134 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5784 &&
8135 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5761) {
Matt Carlsond30cdd22007-10-07 23:28:35 -07008136 /* This value is determined during the probe time DMA
8137 * engine test, tg3_test_dma.
8138 */
8139 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
8140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008141
8142 tp->grc_mode &= ~(GRC_MODE_HOST_SENDBDS |
8143 GRC_MODE_4X_NIC_SEND_RINGS |
8144 GRC_MODE_NO_TX_PHDR_CSUM |
8145 GRC_MODE_NO_RX_PHDR_CSUM);
8146 tp->grc_mode |= GRC_MODE_HOST_SENDBDS;
Michael Chand2d746f2006-04-06 21:45:39 -07008147
8148 /* Pseudo-header checksum is done by hardware logic and not
8149 * the offload processers, so make the chip do the pseudo-
8150 * header checksums on receive. For transmit it is more
8151 * convenient to do the pseudo-header checksum in software
8152 * as Linux does that on transmit for us in all cases.
8153 */
8154 tp->grc_mode |= GRC_MODE_NO_TX_PHDR_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155
8156 tw32(GRC_MODE,
8157 tp->grc_mode |
8158 (GRC_MODE_IRQ_ON_MAC_ATTN | GRC_MODE_HOST_STACKUP));
8159
8160 /* Setup the timer prescalar register. Clock is always 66Mhz. */
8161 val = tr32(GRC_MISC_CFG);
8162 val &= ~0xff;
8163 val |= (65 << GRC_MISC_CFG_PRESCALAR_SHIFT);
8164 tw32(GRC_MISC_CFG, val);
8165
8166 /* Initialize MBUF/DESC pool. */
John W. Linvillecbf46852005-04-21 17:01:29 -07008167 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168 /* Do nothing. */
8169 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5705) {
8170 tw32(BUFMGR_MB_POOL_ADDR, NIC_SRAM_MBUF_POOL_BASE);
8171 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
8172 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE64);
8173 else
8174 tw32(BUFMGR_MB_POOL_SIZE, NIC_SRAM_MBUF_POOL_SIZE96);
8175 tw32(BUFMGR_DMA_DESC_POOL_ADDR, NIC_SRAM_DMA_DESC_POOL_BASE);
8176 tw32(BUFMGR_DMA_DESC_POOL_SIZE, NIC_SRAM_DMA_DESC_POOL_SIZE);
Matt Carlson859a5882010-04-05 10:19:28 +00008177 } else if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178 int fw_len;
8179
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -08008180 fw_len = tp->fw_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008181 fw_len = (fw_len + (0x80 - 1)) & ~(0x80 - 1);
8182 tw32(BUFMGR_MB_POOL_ADDR,
8183 NIC_SRAM_MBUF_POOL_BASE5705 + fw_len);
8184 tw32(BUFMGR_MB_POOL_SIZE,
8185 NIC_SRAM_MBUF_POOL_SIZE5705 - fw_len - 0xa00);
8186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008187
Michael Chan0f893dc2005-07-25 12:30:38 -07008188 if (tp->dev->mtu <= ETH_DATA_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008189 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8190 tp->bufmgr_config.mbuf_read_dma_low_water);
8191 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8192 tp->bufmgr_config.mbuf_mac_rx_low_water);
8193 tw32(BUFMGR_MB_HIGH_WATER,
8194 tp->bufmgr_config.mbuf_high_water);
8195 } else {
8196 tw32(BUFMGR_MB_RDMA_LOW_WATER,
8197 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo);
8198 tw32(BUFMGR_MB_MACRX_LOW_WATER,
8199 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo);
8200 tw32(BUFMGR_MB_HIGH_WATER,
8201 tp->bufmgr_config.mbuf_high_water_jumbo);
8202 }
8203 tw32(BUFMGR_DMA_LOW_WATER,
8204 tp->bufmgr_config.dma_low_water);
8205 tw32(BUFMGR_DMA_HIGH_WATER,
8206 tp->bufmgr_config.dma_high_water);
8207
Matt Carlsond309a462010-09-30 10:34:31 +00008208 val = BUFMGR_MODE_ENABLE | BUFMGR_MODE_ATTN_ENABLE;
8209 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
8210 val |= BUFMGR_MODE_NO_TX_UNDERRUN;
Matt Carlson4d958472011-04-20 07:57:35 +00008211 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
8212 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
8213 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0)
8214 val |= BUFMGR_MODE_MBLOW_ATTN_ENAB;
Matt Carlsond309a462010-09-30 10:34:31 +00008215 tw32(BUFMGR_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216 for (i = 0; i < 2000; i++) {
8217 if (tr32(BUFMGR_MODE) & BUFMGR_MODE_ENABLE)
8218 break;
8219 udelay(10);
8220 }
8221 if (i >= 2000) {
Joe Perches05dbe002010-02-17 19:44:19 +00008222 netdev_err(tp->dev, "%s cannot enable BUFMGR\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008223 return -ENODEV;
8224 }
8225
8226 /* Setup replenish threshold. */
Michael Chanf92905d2006-06-29 20:14:29 -07008227 val = tp->rx_pending / 8;
8228 if (val == 0)
8229 val = 1;
8230 else if (val > tp->rx_std_max_post)
8231 val = tp->rx_std_max_post;
Michael Chanb5d37722006-09-27 16:06:21 -07008232 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
8233 if (tp->pci_chip_rev_id == CHIPREV_ID_5906_A1)
8234 tw32(ISO_PKT_TX, (tr32(ISO_PKT_TX) & ~0x3) | 0x2);
8235
8236 if (val > (TG3_RX_INTERNAL_RING_SZ_5906 / 2))
8237 val = TG3_RX_INTERNAL_RING_SZ_5906 / 2;
8238 }
Michael Chanf92905d2006-06-29 20:14:29 -07008239
8240 tw32(RCVBDI_STD_THRESH, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008241
8242 /* Initialize TG3_BDINFO's at:
8243 * RCVDBDI_STD_BD: standard eth size rx ring
8244 * RCVDBDI_JUMBO_BD: jumbo frame rx ring
8245 * RCVDBDI_MINI_BD: small frame rx ring (??? does not work)
8246 *
8247 * like so:
8248 * TG3_BDINFO_HOST_ADDR: high/low parts of DMA address of ring
8249 * TG3_BDINFO_MAXLEN_FLAGS: (rx max buffer size << 16) |
8250 * ring attribute flags
8251 * TG3_BDINFO_NIC_ADDR: location of descriptors in nic SRAM
8252 *
8253 * Standard receive ring @ NIC_SRAM_RX_BUFFER_DESC, 512 entries.
8254 * Jumbo receive ring @ NIC_SRAM_RX_JUMBO_BUFFER_DESC, 256 entries.
8255 *
8256 * The size of each ring is fixed in the firmware, but the location is
8257 * configurable.
8258 */
8259 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008260 ((u64) tpr->rx_std_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261 tw32(RCVDBDI_STD_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008262 ((u64) tpr->rx_std_mapping & 0xffffffff));
Matt Carlson0a58d662011-04-05 14:22:45 +00008263 if (!(tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson87668d32009-11-13 13:03:34 +00008264 tw32(RCVDBDI_STD_BD + TG3_BDINFO_NIC_ADDR,
8265 NIC_SRAM_RX_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008266
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008267 /* Disable the mini ring */
8268 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269 tw32(RCVDBDI_MINI_BD + TG3_BDINFO_MAXLEN_FLAGS,
8270 BDINFO_FLAGS_DISABLED);
8271
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008272 /* Program the jumbo buffer descriptor ring control
8273 * blocks on those devices that have them.
8274 */
Matt Carlsonbb18bb92011-03-09 16:58:19 +00008275 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson4d163b72011-01-25 15:58:48 +00008276 ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
8277 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008278 /* Setup replenish threshold. */
8279 tw32(RCVBDI_JUMBO_THRESH, tp->rx_jumbo_pending / 8);
8280
Michael Chan0f893dc2005-07-25 12:30:38 -07008281 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008282 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_HIGH,
Matt Carlson21f581a2009-08-28 14:00:25 +00008283 ((u64) tpr->rx_jmb_mapping >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008284 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_HOST_ADDR + TG3_64BIT_REG_LOW,
Matt Carlson21f581a2009-08-28 14:00:25 +00008285 ((u64) tpr->rx_jmb_mapping & 0xffffffff));
Matt Carlsonde9f5232011-04-05 14:22:43 +00008286 val = TG3_RX_JMB_RING_SIZE(tp) <<
8287 BDINFO_FLAGS_MAXLEN_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008288 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
Matt Carlsonde9f5232011-04-05 14:22:43 +00008289 val | BDINFO_FLAGS_USE_EXT_RECV);
Matt Carlsona50d0792010-06-05 17:24:37 +00008290 if (!(tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG) ||
8291 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson87668d32009-11-13 13:03:34 +00008292 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_NIC_ADDR,
8293 NIC_SRAM_RX_JUMBO_BUFFER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008294 } else {
8295 tw32(RCVDBDI_JUMBO_BD + TG3_BDINFO_MAXLEN_FLAGS,
8296 BDINFO_FLAGS_DISABLED);
8297 }
8298
Matt Carlson1407deb2011-04-05 14:22:44 +00008299 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008300 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlsonde9f5232011-04-05 14:22:43 +00008301 val = TG3_RX_STD_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008302 else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008303 val = TG3_RX_STD_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008304 val <<= BDINFO_FLAGS_MAXLEN_SHIFT;
8305 val |= (TG3_RX_STD_DMA_SZ << 2);
8306 } else
Matt Carlson04380d42010-04-12 06:58:29 +00008307 val = TG3_RX_STD_DMA_SZ << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008308 } else
Matt Carlsonde9f5232011-04-05 14:22:43 +00008309 val = TG3_RX_STD_MAX_SIZE_5700 << BDINFO_FLAGS_MAXLEN_SHIFT;
Matt Carlsonfdb72b32009-08-28 13:57:12 +00008310
8311 tw32(RCVDBDI_STD_BD + TG3_BDINFO_MAXLEN_FLAGS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008312
Matt Carlson411da642009-11-13 13:03:46 +00008313 tpr->rx_std_prod_idx = tp->rx_pending;
Matt Carlson66711e62009-11-13 13:03:49 +00008314 tw32_rx_mbox(TG3_RX_STD_PROD_IDX_REG, tpr->rx_std_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315
Matt Carlson411da642009-11-13 13:03:46 +00008316 tpr->rx_jmb_prod_idx = (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) ?
Matt Carlson21f581a2009-08-28 14:00:25 +00008317 tp->rx_jumbo_pending : 0;
Matt Carlson66711e62009-11-13 13:03:49 +00008318 tw32_rx_mbox(TG3_RX_JMB_PROD_IDX_REG, tpr->rx_jmb_prod_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008319
Matt Carlson1407deb2011-04-05 14:22:44 +00008320 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00008321 tw32(STD_REPLENISH_LWM, 32);
8322 tw32(JMB_REPLENISH_LWM, 16);
8323 }
8324
Matt Carlson2d31eca2009-09-01 12:53:31 +00008325 tg3_rings_reset(tp);
8326
Linus Torvalds1da177e2005-04-16 15:20:36 -07008327 /* Initialize MAC address and backoff seed. */
Michael Chan986e0ae2007-05-05 12:10:20 -07008328 __tg3_set_mac_addr(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008329
8330 /* MTU + ethernet header + FCS + optional VLAN tag */
Matt Carlsonf7b493e2009-02-25 14:21:52 +00008331 tw32(MAC_RX_MTU_SIZE,
8332 tp->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008333
8334 /* The slot time is changed by tg3_setup_phy if we
8335 * run at gigabit with half duplex.
8336 */
Matt Carlsonf2096f92011-04-05 14:22:48 +00008337 val = (2 << TX_LENGTHS_IPG_CRS_SHIFT) |
8338 (6 << TX_LENGTHS_IPG_SHIFT) |
8339 (32 << TX_LENGTHS_SLOT_TIME_SHIFT);
8340
8341 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8342 val |= tr32(MAC_TX_LENGTHS) &
8343 (TX_LENGTHS_JMB_FRM_LEN_MSK |
8344 TX_LENGTHS_CNT_DWN_VAL_MSK);
8345
8346 tw32(MAC_TX_LENGTHS, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008347
8348 /* Receive rules. */
8349 tw32(MAC_RCV_RULE_CFG, RCV_RULE_CFG_DEFAULT_CLASS);
8350 tw32(RCVLPC_CONFIG, 0x0181);
8351
8352 /* Calculate RDMAC_MODE setting early, we need it to determine
8353 * the RCVLPC_STATE_ENABLE mask.
8354 */
8355 rdmac_mode = (RDMAC_MODE_ENABLE | RDMAC_MODE_TGTABORT_ENAB |
8356 RDMAC_MODE_MSTABORT_ENAB | RDMAC_MODE_PARITYERR_ENAB |
8357 RDMAC_MODE_ADDROFLOW_ENAB | RDMAC_MODE_FIFOOFLOW_ENAB |
8358 RDMAC_MODE_FIFOURUN_ENAB | RDMAC_MODE_FIFOOREAD_ENAB |
8359 RDMAC_MODE_LNGREAD_ENAB);
Michael Chan85e94ce2005-04-21 17:05:28 -07008360
Matt Carlsondeabaac2010-11-24 08:31:50 +00008361 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717)
Matt Carlson0339e4e2010-02-12 14:47:09 +00008362 rdmac_mode |= RDMAC_MODE_MULT_DMA_RD_DIS;
8363
Matt Carlson57e69832008-05-25 23:48:31 -07008364 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -08008365 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8366 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlsond30cdd22007-10-07 23:28:35 -07008367 rdmac_mode |= RDMAC_MODE_BD_SBD_CRPT_ENAB |
8368 RDMAC_MODE_MBUF_RBD_CRPT_ENAB |
8369 RDMAC_MODE_MBUF_SBD_CRPT_ENAB;
8370
Matt Carlsonc5908932011-03-09 16:58:25 +00008371 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8372 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008373 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE &&
Matt Carlsonc13e3712007-05-05 11:50:04 -07008374 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008375 rdmac_mode |= RDMAC_MODE_FIFO_SIZE_128;
8376 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
8377 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
8378 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8379 }
8380 }
8381
Michael Chan85e94ce2005-04-21 17:05:28 -07008382 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)
8383 rdmac_mode |= RDMAC_MODE_FIFO_LONG_BURST;
8384
Linus Torvalds1da177e2005-04-16 15:20:36 -07008385 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
Matt Carlson027455a2008-12-21 20:19:30 -08008386 rdmac_mode |= RDMAC_MODE_IPV4_LSO_EN;
8387
Matt Carlsone849cdc2009-11-13 13:03:38 +00008388 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
8389 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlson027455a2008-12-21 20:19:30 -08008390 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
8391 rdmac_mode |= RDMAC_MODE_IPV6_LSO_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008392
Matt Carlsonf2096f92011-04-05 14:22:48 +00008393 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
8394 rdmac_mode |= tr32(RDMAC_MODE) & RDMAC_MODE_H2BNC_VLAN_DET;
8395
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008396 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
8397 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
8398 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
8399 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +00008400 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008401 val = tr32(TG3_RDMA_RSRVCTRL_REG);
Matt Carlsond78b59f2011-04-05 14:22:46 +00008402 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8403 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb4495ed2011-01-25 15:58:47 +00008404 val &= ~(TG3_RDMA_RSRVCTRL_TXMRGN_MASK |
8405 TG3_RDMA_RSRVCTRL_FIFO_LWM_MASK |
8406 TG3_RDMA_RSRVCTRL_FIFO_HWM_MASK);
8407 val |= TG3_RDMA_RSRVCTRL_TXMRGN_320B |
8408 TG3_RDMA_RSRVCTRL_FIFO_LWM_1_5K |
8409 TG3_RDMA_RSRVCTRL_FIFO_HWM_1_5K;
Matt Carlsonb75cc0e2010-11-24 08:31:46 +00008410 }
Matt Carlson41a8a7e2010-09-15 08:59:53 +00008411 tw32(TG3_RDMA_RSRVCTRL_REG,
8412 val | TG3_RDMA_RSRVCTRL_FIFO_OFLW_FIX);
8413 }
8414
Matt Carlsond78b59f2011-04-05 14:22:46 +00008415 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
8416 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsond309a462010-09-30 10:34:31 +00008417 val = tr32(TG3_LSO_RD_DMA_CRPTEN_CTRL);
8418 tw32(TG3_LSO_RD_DMA_CRPTEN_CTRL, val |
8419 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_BD_4K |
8420 TG3_LSO_RD_DMA_CRPTEN_CTRL_BLEN_LSO_4K);
8421 }
8422
Linus Torvalds1da177e2005-04-16 15:20:36 -07008423 /* Receive/send statistics. */
Michael Chan16613942006-06-29 20:15:13 -07008424 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
8425 val = tr32(RCVLPC_STATS_ENABLE);
8426 val &= ~RCVLPC_STATSENAB_DACK_FIX;
8427 tw32(RCVLPC_STATS_ENABLE, val);
8428 } else if ((rdmac_mode & RDMAC_MODE_FIFO_SIZE_128) &&
8429 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008430 val = tr32(RCVLPC_STATS_ENABLE);
8431 val &= ~RCVLPC_STATSENAB_LNGBRST_RFIX;
8432 tw32(RCVLPC_STATS_ENABLE, val);
8433 } else {
8434 tw32(RCVLPC_STATS_ENABLE, 0xffffff);
8435 }
8436 tw32(RCVLPC_STATSCTRL, RCVLPC_STATSCTRL_ENABLE);
8437 tw32(SNDDATAI_STATSENAB, 0xffffff);
8438 tw32(SNDDATAI_STATSCTRL,
8439 (SNDDATAI_SCTRL_ENABLE |
8440 SNDDATAI_SCTRL_FASTUPD));
8441
8442 /* Setup host coalescing engine. */
8443 tw32(HOSTCC_MODE, 0);
8444 for (i = 0; i < 2000; i++) {
8445 if (!(tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE))
8446 break;
8447 udelay(10);
8448 }
8449
Michael Chand244c892005-07-05 14:42:33 -07008450 __tg3_set_coalesce(tp, &tp->coal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451
Linus Torvalds1da177e2005-04-16 15:20:36 -07008452 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8453 /* Status/statistics block address. See tg3_timer,
8454 * the tg3_periodic_fetch_stats call there, and
8455 * tg3_get_stats to see how this works for 5705/5750 chips.
8456 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008457 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_HIGH,
8458 ((u64) tp->stats_mapping >> 32));
8459 tw32(HOSTCC_STATS_BLK_HOST_ADDR + TG3_64BIT_REG_LOW,
8460 ((u64) tp->stats_mapping & 0xffffffff));
8461 tw32(HOSTCC_STATS_BLK_NIC_ADDR, NIC_SRAM_STATS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008462
Linus Torvalds1da177e2005-04-16 15:20:36 -07008463 tw32(HOSTCC_STATUS_BLK_NIC_ADDR, NIC_SRAM_STATUS_BLK);
Matt Carlson2d31eca2009-09-01 12:53:31 +00008464
8465 /* Clear statistics and status block memory areas */
8466 for (i = NIC_SRAM_STATS_BLK;
8467 i < NIC_SRAM_STATUS_BLK + TG3_HW_STATUS_SIZE;
8468 i += sizeof(u32)) {
8469 tg3_write_mem(tp, i, 0);
8470 udelay(40);
8471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008472 }
8473
8474 tw32(HOSTCC_MODE, HOSTCC_MODE_ENABLE | tp->coalesce_mode);
8475
8476 tw32(RCVCC_MODE, RCVCC_MODE_ENABLE | RCVCC_MODE_ATTN_ENABLE);
8477 tw32(RCVLPC_MODE, RCVLPC_MODE_ENABLE);
8478 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8479 tw32(RCVLSC_MODE, RCVLSC_MODE_ENABLE | RCVLSC_MODE_ATTN_ENABLE);
8480
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008481 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
8482 tp->phy_flags &= ~TG3_PHYFLG_PARALLEL_DETECT;
Michael Chanc94e3942005-09-27 12:12:42 -07008483 /* reset to prevent losing 1st rx packet intermittently */
8484 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8485 udelay(10);
8486 }
8487
Matt Carlson3bda1252008-08-15 14:08:22 -07008488 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +00008489 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -07008490 else
8491 tp->mac_mode = 0;
8492 tp->mac_mode |= MAC_MODE_TXSTAT_ENABLE | MAC_MODE_RXSTAT_ENABLE |
Linus Torvalds1da177e2005-04-16 15:20:36 -07008493 MAC_MODE_TDE_ENABLE | MAC_MODE_RDE_ENABLE | MAC_MODE_FHDE_ENABLE;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008494 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008495 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsone8f3f6c2007-07-11 19:47:55 -07008496 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700)
8497 tp->mac_mode |= MAC_MODE_LINK_POLARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008498 tw32_f(MAC_MODE, tp->mac_mode | MAC_MODE_RXSTAT_CLEAR | MAC_MODE_TXSTAT_CLEAR);
8499 udelay(40);
8500
Michael Chan314fba32005-04-21 17:07:04 -07008501 /* tp->grc_local_ctrl is partially set up during tg3_get_invariants().
Michael Chan9d26e212006-12-07 00:21:14 -08008502 * If TG3_FLG2_IS_NIC is zero, we should read the
Michael Chan314fba32005-04-21 17:07:04 -07008503 * register to preserve the GPIO settings for LOMs. The GPIOs,
8504 * whether used as inputs or outputs, are set by boot code after
8505 * reset.
8506 */
Michael Chan9d26e212006-12-07 00:21:14 -08008507 if (!(tp->tg3_flags2 & TG3_FLG2_IS_NIC)) {
Michael Chan314fba32005-04-21 17:07:04 -07008508 u32 gpio_mask;
8509
Michael Chan9d26e212006-12-07 00:21:14 -08008510 gpio_mask = GRC_LCLCTRL_GPIO_OE0 | GRC_LCLCTRL_GPIO_OE1 |
8511 GRC_LCLCTRL_GPIO_OE2 | GRC_LCLCTRL_GPIO_OUTPUT0 |
8512 GRC_LCLCTRL_GPIO_OUTPUT1 | GRC_LCLCTRL_GPIO_OUTPUT2;
Michael Chan3e7d83b2005-04-21 17:10:36 -07008513
8514 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
8515 gpio_mask |= GRC_LCLCTRL_GPIO_OE3 |
8516 GRC_LCLCTRL_GPIO_OUTPUT3;
8517
Michael Chanaf36e6b2006-03-23 01:28:06 -08008518 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
8519 gpio_mask |= GRC_LCLCTRL_GPIO_UART_SEL;
8520
Gary Zambranoaaf84462007-05-05 11:51:45 -07008521 tp->grc_local_ctrl &= ~gpio_mask;
Michael Chan314fba32005-04-21 17:07:04 -07008522 tp->grc_local_ctrl |= tr32(GRC_LOCAL_CTRL) & gpio_mask;
8523
8524 /* GPIO1 must be driven high for eeprom write protect */
Michael Chan9d26e212006-12-07 00:21:14 -08008525 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT)
8526 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
8527 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan314fba32005-04-21 17:07:04 -07008528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008529 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8530 udelay(100);
8531
Matt Carlson0583d522011-01-25 15:58:50 +00008532 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
8533 tp->irq_cnt > 1) {
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008534 val = tr32(MSGINT_MODE);
8535 val |= MSGINT_MODE_MULTIVEC_EN | MSGINT_MODE_ENABLE;
8536 tw32(MSGINT_MODE, val);
8537 }
8538
Linus Torvalds1da177e2005-04-16 15:20:36 -07008539 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
8540 tw32_f(DMAC_MODE, DMAC_MODE_ENABLE);
8541 udelay(40);
8542 }
8543
8544 val = (WDMAC_MODE_ENABLE | WDMAC_MODE_TGTABORT_ENAB |
8545 WDMAC_MODE_MSTABORT_ENAB | WDMAC_MODE_PARITYERR_ENAB |
8546 WDMAC_MODE_ADDROFLOW_ENAB | WDMAC_MODE_FIFOOFLOW_ENAB |
8547 WDMAC_MODE_FIFOURUN_ENAB | WDMAC_MODE_FIFOOREAD_ENAB |
8548 WDMAC_MODE_LNGREAD_ENAB);
8549
Matt Carlsonc5908932011-03-09 16:58:25 +00008550 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
8551 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
Matt Carlson29ea0952009-08-25 10:07:54 +00008552 if ((tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008553 (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 ||
8554 tp->pci_chip_rev_id == CHIPREV_ID_5705_A2)) {
8555 /* nothing */
8556 } else if (!(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH) &&
Matt Carlsonc5908932011-03-09 16:58:25 +00008557 !(tp->tg3_flags2 & TG3_FLG2_IS_5788)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008558 val |= WDMAC_MODE_RX_ACCEL;
8559 }
8560 }
8561
Michael Chand9ab5ad2006-03-20 22:27:35 -08008562 /* Enable host coalescing bug fix */
Matt Carlson321d32a2008-11-21 17:22:19 -08008563 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlsonf51f3562008-05-25 23:45:08 -07008564 val |= WDMAC_MODE_STATUS_TAG_FIX;
Michael Chand9ab5ad2006-03-20 22:27:35 -08008565
Matt Carlson788a0352009-11-02 14:26:03 +00008566 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
8567 val |= WDMAC_MODE_BURST_ALL_DATA;
8568
Linus Torvalds1da177e2005-04-16 15:20:36 -07008569 tw32_f(WDMAC_MODE, val);
8570 udelay(40);
8571
Matt Carlson9974a352007-10-07 23:27:28 -07008572 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
8573 u16 pcix_cmd;
8574
8575 pci_read_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8576 &pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008577 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703) {
Matt Carlson9974a352007-10-07 23:27:28 -07008578 pcix_cmd &= ~PCI_X_CMD_MAX_READ;
8579 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008580 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
Matt Carlson9974a352007-10-07 23:27:28 -07008581 pcix_cmd &= ~(PCI_X_CMD_MAX_SPLIT | PCI_X_CMD_MAX_READ);
8582 pcix_cmd |= PCI_X_CMD_READ_2K;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008583 }
Matt Carlson9974a352007-10-07 23:27:28 -07008584 pci_write_config_word(tp->pdev, tp->pcix_cap + PCI_X_CMD,
8585 pcix_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008586 }
8587
8588 tw32_f(RDMAC_MODE, rdmac_mode);
8589 udelay(40);
8590
8591 tw32(RCVDCC_MODE, RCVDCC_MODE_ENABLE | RCVDCC_MODE_ATTN_ENABLE);
8592 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
8593 tw32(MBFREE_MODE, MBFREE_MODE_ENABLE);
Matt Carlson9936bcf2007-10-10 18:03:07 -07008594
8595 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
8596 tw32(SNDDATAC_MODE,
8597 SNDDATAC_MODE_ENABLE | SNDDATAC_MODE_CDELAY);
8598 else
8599 tw32(SNDDATAC_MODE, SNDDATAC_MODE_ENABLE);
8600
Linus Torvalds1da177e2005-04-16 15:20:36 -07008601 tw32(SNDBDC_MODE, SNDBDC_MODE_ENABLE | SNDBDC_MODE_ATTN_ENABLE);
8602 tw32(RCVBDI_MODE, RCVBDI_MODE_ENABLE | RCVBDI_MODE_RCB_ATTN_ENAB);
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008603 val = RCVDBDI_MODE_ENABLE | RCVDBDI_MODE_INV_RING_SZ;
Matt Carlsonde9f5232011-04-05 14:22:43 +00008604 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
Matt Carlson7cb32cf2010-09-30 10:34:36 +00008605 val |= RCVDBDI_MODE_LRG_RING_SZ;
8606 tw32(RCVDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008607 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008608 if (tp->tg3_flags2 & TG3_FLG2_HW_TSO)
8609 tw32(SNDDATAI_MODE, SNDDATAI_MODE_ENABLE | 0x8);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008610 val = SNDBDI_MODE_ENABLE | SNDBDI_MODE_ATTN_ENABLE;
Matt Carlson19cfaec2009-12-03 08:36:20 +00008611 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008612 val |= SNDBDI_MODE_MULTI_TXQ_EN;
8613 tw32(SNDBDI_MODE, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008614 tw32(SNDBDS_MODE, SNDBDS_MODE_ENABLE | SNDBDS_MODE_ATTN_ENABLE);
8615
8616 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
8617 err = tg3_load_5701_a0_firmware_fix(tp);
8618 if (err)
8619 return err;
8620 }
8621
Linus Torvalds1da177e2005-04-16 15:20:36 -07008622 if (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) {
8623 err = tg3_load_tso_firmware(tp);
8624 if (err)
8625 return err;
8626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008627
8628 tp->tx_mode = TX_MODE_ENABLE;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008629
Matt Carlsonb1d05212010-06-05 17:24:31 +00008630 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
8631 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
8632 tp->tx_mode |= TX_MODE_MBUF_LOCKUP_FIX;
Matt Carlsonf2096f92011-04-05 14:22:48 +00008633
8634 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
8635 val = TX_MODE_JMB_FRM_LEN | TX_MODE_CNT_DN_MODE;
8636 tp->tx_mode &= ~val;
8637 tp->tx_mode |= tr32(MAC_TX_MODE) & val;
8638 }
8639
Linus Torvalds1da177e2005-04-16 15:20:36 -07008640 tw32_f(MAC_TX_MODE, tp->tx_mode);
8641 udelay(100);
8642
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008643 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS) {
8644 u32 reg = MAC_RSS_INDIR_TBL_0;
8645 u8 *ent = (u8 *)&val;
8646
8647 /* Setup the indirection table */
8648 for (i = 0; i < TG3_RSS_INDIR_TBL_SIZE; i++) {
8649 int idx = i % sizeof(val);
8650
Matt Carlson5efeeea2010-07-11 09:31:40 +00008651 ent[idx] = i % (tp->irq_cnt - 1);
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008652 if (idx == sizeof(val) - 1) {
8653 tw32(reg, val);
8654 reg += 4;
8655 }
8656 }
8657
8658 /* Setup the "secret" hash key. */
8659 tw32(MAC_RSS_HASH_KEY_0, 0x5f865437);
8660 tw32(MAC_RSS_HASH_KEY_1, 0xe4ac62cc);
8661 tw32(MAC_RSS_HASH_KEY_2, 0x50103a45);
8662 tw32(MAC_RSS_HASH_KEY_3, 0x36621985);
8663 tw32(MAC_RSS_HASH_KEY_4, 0xbf14c0e8);
8664 tw32(MAC_RSS_HASH_KEY_5, 0x1bc27a1e);
8665 tw32(MAC_RSS_HASH_KEY_6, 0x84f4b556);
8666 tw32(MAC_RSS_HASH_KEY_7, 0x094ea6fe);
8667 tw32(MAC_RSS_HASH_KEY_8, 0x7dda01e7);
8668 tw32(MAC_RSS_HASH_KEY_9, 0xc04d7481);
8669 }
8670
Linus Torvalds1da177e2005-04-16 15:20:36 -07008671 tp->rx_mode = RX_MODE_ENABLE;
Matt Carlson321d32a2008-11-21 17:22:19 -08008672 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Michael Chanaf36e6b2006-03-23 01:28:06 -08008673 tp->rx_mode |= RX_MODE_IPV6_CSUM_ENABLE;
8674
Matt Carlsonbaf8a942009-09-01 13:13:00 +00008675 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
8676 tp->rx_mode |= RX_MODE_RSS_ENABLE |
8677 RX_MODE_RSS_ITBL_HASH_BITS_7 |
8678 RX_MODE_RSS_IPV6_HASH_EN |
8679 RX_MODE_RSS_TCP_IPV6_HASH_EN |
8680 RX_MODE_RSS_IPV4_HASH_EN |
8681 RX_MODE_RSS_TCP_IPV4_HASH_EN;
8682
Linus Torvalds1da177e2005-04-16 15:20:36 -07008683 tw32_f(MAC_RX_MODE, tp->rx_mode);
8684 udelay(10);
8685
Linus Torvalds1da177e2005-04-16 15:20:36 -07008686 tw32(MAC_LED_CTRL, tp->led_ctrl);
8687
8688 tw32(MAC_MI_STAT, MAC_MI_STAT_LNKSTAT_ATTN_ENAB);
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008689 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008690 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
8691 udelay(10);
8692 }
8693 tw32_f(MAC_RX_MODE, tp->rx_mode);
8694 udelay(10);
8695
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008696 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008697 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008698 !(tp->phy_flags & TG3_PHYFLG_SERDES_PREEMPHASIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008699 /* Set drive transmission level to 1.2V */
8700 /* only if the signal pre-emphasis bit is not set */
8701 val = tr32(MAC_SERDES_CFG);
8702 val &= 0xfffff000;
8703 val |= 0x880;
8704 tw32(MAC_SERDES_CFG, val);
8705 }
8706 if (tp->pci_chip_rev_id == CHIPREV_ID_5703_A1)
8707 tw32(MAC_SERDES_CFG, 0x616000);
8708 }
8709
8710 /* Prevent chip from dropping frames when flow control
8711 * is enabled.
8712 */
Matt Carlson666bc832010-01-20 16:58:03 +00008713 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
8714 val = 1;
8715 else
8716 val = 2;
8717 tw32_f(MAC_LOW_WMARK_MAX_RX_FRAME, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008718
8719 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008720 (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008721 /* Use hardware link auto-negotiation */
8722 tp->tg3_flags2 |= TG3_FLG2_HW_AUTONEG;
8723 }
8724
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008725 if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Michael Chand4d2c552006-03-20 17:47:20 -08008726 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714)) {
8727 u32 tmp;
8728
8729 tmp = tr32(SERDES_RX_CTRL);
8730 tw32(SERDES_RX_CTRL, tmp | SERDES_RX_SIG_DETECT);
8731 tp->grc_local_ctrl &= ~GRC_LCLCTRL_USE_EXT_SIG_DETECT;
8732 tp->grc_local_ctrl |= GRC_LCLCTRL_USE_SIG_DETECT;
8733 tw32(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
8734 }
8735
Matt Carlsondd477002008-05-25 23:45:58 -07008736 if (!(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Matt Carlson80096062010-08-02 11:26:06 +00008737 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) {
8738 tp->phy_flags &= ~TG3_PHYFLG_IS_LOW_POWER;
Matt Carlsondd477002008-05-25 23:45:58 -07008739 tp->link_config.speed = tp->link_config.orig_speed;
8740 tp->link_config.duplex = tp->link_config.orig_duplex;
8741 tp->link_config.autoneg = tp->link_config.orig_autoneg;
8742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008743
Matt Carlsondd477002008-05-25 23:45:58 -07008744 err = tg3_setup_phy(tp, 0);
8745 if (err)
8746 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008747
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008748 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
8749 !(tp->phy_flags & TG3_PHYFLG_IS_FET)) {
Matt Carlsondd477002008-05-25 23:45:58 -07008750 u32 tmp;
8751
8752 /* Clear CRC stats. */
8753 if (!tg3_readphy(tp, MII_TG3_TEST1, &tmp)) {
8754 tg3_writephy(tp, MII_TG3_TEST1,
8755 tmp | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00008756 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &tmp);
Matt Carlsondd477002008-05-25 23:45:58 -07008757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008758 }
8759 }
8760
8761 __tg3_set_rx_mode(tp->dev);
8762
8763 /* Initialize receive rules. */
8764 tw32(MAC_RCV_RULE_0, 0xc2000000 & RCV_RULE_DISABLE_MASK);
8765 tw32(MAC_RCV_VALUE_0, 0xffffffff & RCV_RULE_DISABLE_MASK);
8766 tw32(MAC_RCV_RULE_1, 0x86000004 & RCV_RULE_DISABLE_MASK);
8767 tw32(MAC_RCV_VALUE_1, 0xffffffff & RCV_RULE_DISABLE_MASK);
8768
Michael Chan4cf78e42005-07-25 12:29:19 -07008769 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Michael Chana4e2b342005-10-26 15:46:52 -07008770 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07008771 limit = 8;
8772 else
8773 limit = 16;
8774 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
8775 limit -= 4;
8776 switch (limit) {
8777 case 16:
8778 tw32(MAC_RCV_RULE_15, 0); tw32(MAC_RCV_VALUE_15, 0);
8779 case 15:
8780 tw32(MAC_RCV_RULE_14, 0); tw32(MAC_RCV_VALUE_14, 0);
8781 case 14:
8782 tw32(MAC_RCV_RULE_13, 0); tw32(MAC_RCV_VALUE_13, 0);
8783 case 13:
8784 tw32(MAC_RCV_RULE_12, 0); tw32(MAC_RCV_VALUE_12, 0);
8785 case 12:
8786 tw32(MAC_RCV_RULE_11, 0); tw32(MAC_RCV_VALUE_11, 0);
8787 case 11:
8788 tw32(MAC_RCV_RULE_10, 0); tw32(MAC_RCV_VALUE_10, 0);
8789 case 10:
8790 tw32(MAC_RCV_RULE_9, 0); tw32(MAC_RCV_VALUE_9, 0);
8791 case 9:
8792 tw32(MAC_RCV_RULE_8, 0); tw32(MAC_RCV_VALUE_8, 0);
8793 case 8:
8794 tw32(MAC_RCV_RULE_7, 0); tw32(MAC_RCV_VALUE_7, 0);
8795 case 7:
8796 tw32(MAC_RCV_RULE_6, 0); tw32(MAC_RCV_VALUE_6, 0);
8797 case 6:
8798 tw32(MAC_RCV_RULE_5, 0); tw32(MAC_RCV_VALUE_5, 0);
8799 case 5:
8800 tw32(MAC_RCV_RULE_4, 0); tw32(MAC_RCV_VALUE_4, 0);
8801 case 4:
8802 /* tw32(MAC_RCV_RULE_3, 0); tw32(MAC_RCV_VALUE_3, 0); */
8803 case 3:
8804 /* tw32(MAC_RCV_RULE_2, 0); tw32(MAC_RCV_VALUE_2, 0); */
8805 case 2:
8806 case 1:
8807
8808 default:
8809 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -07008810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008811
Matt Carlson9ce768e2007-10-11 19:49:11 -07008812 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
8813 /* Write our heartbeat update interval to APE. */
8814 tg3_ape_write32(tp, TG3_APE_HOST_HEARTBEAT_INT_MS,
8815 APE_HOST_HEARTBEAT_INT_DISABLE);
Matt Carlson0d3031d2007-10-10 18:02:43 -07008816
Linus Torvalds1da177e2005-04-16 15:20:36 -07008817 tg3_write_sig_post_reset(tp, RESET_KIND_INIT);
8818
Linus Torvalds1da177e2005-04-16 15:20:36 -07008819 return 0;
8820}
8821
8822/* Called at device open time to get the chip ready for
8823 * packet processing. Invoked with tp->lock held.
8824 */
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07008825static int tg3_init_hw(struct tg3 *tp, int reset_phy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008826{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008827 tg3_switch_clocks(tp);
8828
8829 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
8830
Matt Carlson2f751b62008-08-04 23:17:34 -07008831 return tg3_reset_hw(tp, reset_phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008832}
8833
8834#define TG3_STAT_ADD32(PSTAT, REG) \
8835do { u32 __val = tr32(REG); \
8836 (PSTAT)->low += __val; \
8837 if ((PSTAT)->low < __val) \
8838 (PSTAT)->high += 1; \
8839} while (0)
8840
8841static void tg3_periodic_fetch_stats(struct tg3 *tp)
8842{
8843 struct tg3_hw_stats *sp = tp->hw_stats;
8844
8845 if (!netif_carrier_ok(tp->dev))
8846 return;
8847
8848 TG3_STAT_ADD32(&sp->tx_octets, MAC_TX_STATS_OCTETS);
8849 TG3_STAT_ADD32(&sp->tx_collisions, MAC_TX_STATS_COLLISIONS);
8850 TG3_STAT_ADD32(&sp->tx_xon_sent, MAC_TX_STATS_XON_SENT);
8851 TG3_STAT_ADD32(&sp->tx_xoff_sent, MAC_TX_STATS_XOFF_SENT);
8852 TG3_STAT_ADD32(&sp->tx_mac_errors, MAC_TX_STATS_MAC_ERRORS);
8853 TG3_STAT_ADD32(&sp->tx_single_collisions, MAC_TX_STATS_SINGLE_COLLISIONS);
8854 TG3_STAT_ADD32(&sp->tx_mult_collisions, MAC_TX_STATS_MULT_COLLISIONS);
8855 TG3_STAT_ADD32(&sp->tx_deferred, MAC_TX_STATS_DEFERRED);
8856 TG3_STAT_ADD32(&sp->tx_excessive_collisions, MAC_TX_STATS_EXCESSIVE_COL);
8857 TG3_STAT_ADD32(&sp->tx_late_collisions, MAC_TX_STATS_LATE_COL);
8858 TG3_STAT_ADD32(&sp->tx_ucast_packets, MAC_TX_STATS_UCAST);
8859 TG3_STAT_ADD32(&sp->tx_mcast_packets, MAC_TX_STATS_MCAST);
8860 TG3_STAT_ADD32(&sp->tx_bcast_packets, MAC_TX_STATS_BCAST);
8861
8862 TG3_STAT_ADD32(&sp->rx_octets, MAC_RX_STATS_OCTETS);
8863 TG3_STAT_ADD32(&sp->rx_fragments, MAC_RX_STATS_FRAGMENTS);
8864 TG3_STAT_ADD32(&sp->rx_ucast_packets, MAC_RX_STATS_UCAST);
8865 TG3_STAT_ADD32(&sp->rx_mcast_packets, MAC_RX_STATS_MCAST);
8866 TG3_STAT_ADD32(&sp->rx_bcast_packets, MAC_RX_STATS_BCAST);
8867 TG3_STAT_ADD32(&sp->rx_fcs_errors, MAC_RX_STATS_FCS_ERRORS);
8868 TG3_STAT_ADD32(&sp->rx_align_errors, MAC_RX_STATS_ALIGN_ERRORS);
8869 TG3_STAT_ADD32(&sp->rx_xon_pause_rcvd, MAC_RX_STATS_XON_PAUSE_RECVD);
8870 TG3_STAT_ADD32(&sp->rx_xoff_pause_rcvd, MAC_RX_STATS_XOFF_PAUSE_RECVD);
8871 TG3_STAT_ADD32(&sp->rx_mac_ctrl_rcvd, MAC_RX_STATS_MAC_CTRL_RECVD);
8872 TG3_STAT_ADD32(&sp->rx_xoff_entered, MAC_RX_STATS_XOFF_ENTERED);
8873 TG3_STAT_ADD32(&sp->rx_frame_too_long_errors, MAC_RX_STATS_FRAME_TOO_LONG);
8874 TG3_STAT_ADD32(&sp->rx_jabbers, MAC_RX_STATS_JABBERS);
8875 TG3_STAT_ADD32(&sp->rx_undersize_packets, MAC_RX_STATS_UNDERSIZE);
Michael Chan463d3052006-05-22 16:36:27 -07008876
8877 TG3_STAT_ADD32(&sp->rxbds_empty, RCVLPC_NO_RCV_BD_CNT);
Matt Carlson4d958472011-04-20 07:57:35 +00008878 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5717) {
8879 TG3_STAT_ADD32(&sp->rx_discards, RCVLPC_IN_DISCARDS_CNT);
8880 } else {
8881 u32 val = tr32(HOSTCC_FLOW_ATTN);
8882 val = (val & HOSTCC_FLOW_ATTN_MBUF_LWM) ? 1 : 0;
8883 if (val) {
8884 tw32(HOSTCC_FLOW_ATTN, HOSTCC_FLOW_ATTN_MBUF_LWM);
8885 sp->rx_discards.low += val;
8886 if (sp->rx_discards.low < val)
8887 sp->rx_discards.high += 1;
8888 }
8889 sp->mbuf_lwm_thresh_hit = sp->rx_discards;
8890 }
Michael Chan463d3052006-05-22 16:36:27 -07008891 TG3_STAT_ADD32(&sp->rx_errors, RCVLPC_IN_ERRORS_CNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008892}
8893
8894static void tg3_timer(unsigned long __opaque)
8895{
8896 struct tg3 *tp = (struct tg3 *) __opaque;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008897
Michael Chanf475f162006-03-27 23:20:14 -08008898 if (tp->irq_sync)
8899 goto restart_timer;
8900
David S. Millerf47c11e2005-06-24 20:18:35 -07008901 spin_lock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008902
David S. Millerfac9b832005-05-18 22:46:34 -07008903 if (!(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
8904 /* All of this garbage is because when using non-tagged
8905 * IRQ status the mailbox/status_block protocol the chip
8906 * uses with the cpu is race prone.
8907 */
Matt Carlson898a56f2009-08-28 14:02:40 +00008908 if (tp->napi[0].hw_status->status & SD_STATUS_UPDATED) {
David S. Millerfac9b832005-05-18 22:46:34 -07008909 tw32(GRC_LOCAL_CTRL,
8910 tp->grc_local_ctrl | GRC_LCLCTRL_SETINT);
8911 } else {
8912 tw32(HOSTCC_MODE, tp->coalesce_mode |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00008913 HOSTCC_MODE_ENABLE | HOSTCC_MODE_NOW);
David S. Millerfac9b832005-05-18 22:46:34 -07008914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008915
David S. Millerfac9b832005-05-18 22:46:34 -07008916 if (!(tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
8917 tp->tg3_flags2 |= TG3_FLG2_RESTART_TIMER;
David S. Millerf47c11e2005-06-24 20:18:35 -07008918 spin_unlock(&tp->lock);
David S. Millerfac9b832005-05-18 22:46:34 -07008919 schedule_work(&tp->reset_task);
8920 return;
8921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008922 }
8923
Linus Torvalds1da177e2005-04-16 15:20:36 -07008924 /* This part only runs once per second. */
8925 if (!--tp->timer_counter) {
David S. Millerfac9b832005-05-18 22:46:34 -07008926 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
8927 tg3_periodic_fetch_stats(tp);
8928
Matt Carlson52b02d02010-10-14 10:37:41 +00008929 if (tp->setlpicnt && !--tp->setlpicnt) {
8930 u32 val = tr32(TG3_CPMU_EEE_MODE);
8931 tw32(TG3_CPMU_EEE_MODE,
8932 val | TG3_CPMU_EEEMD_LPI_ENABLE);
8933 }
8934
Linus Torvalds1da177e2005-04-16 15:20:36 -07008935 if (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) {
8936 u32 mac_stat;
8937 int phy_event;
8938
8939 mac_stat = tr32(MAC_STATUS);
8940
8941 phy_event = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008942 if (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008943 if (mac_stat & MAC_STATUS_MI_INTERRUPT)
8944 phy_event = 1;
8945 } else if (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)
8946 phy_event = 1;
8947
8948 if (phy_event)
8949 tg3_setup_phy(tp, 0);
8950 } else if (tp->tg3_flags & TG3_FLAG_POLL_SERDES) {
8951 u32 mac_stat = tr32(MAC_STATUS);
8952 int need_setup = 0;
8953
8954 if (netif_carrier_ok(tp->dev) &&
8955 (mac_stat & MAC_STATUS_LNKSTATE_CHANGED)) {
8956 need_setup = 1;
8957 }
Matt Carlsonbe98da62010-07-11 09:31:46 +00008958 if (!netif_carrier_ok(tp->dev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07008959 (mac_stat & (MAC_STATUS_PCS_SYNCED |
8960 MAC_STATUS_SIGNAL_DET))) {
8961 need_setup = 1;
8962 }
8963 if (need_setup) {
Michael Chan3d3ebe72006-09-27 15:59:15 -07008964 if (!tp->serdes_counter) {
8965 tw32_f(MAC_MODE,
8966 (tp->mac_mode &
8967 ~MAC_MODE_PORT_MODE_MASK));
8968 udelay(40);
8969 tw32_f(MAC_MODE, tp->mac_mode);
8970 udelay(40);
8971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008972 tg3_setup_phy(tp, 0);
8973 }
Matt Carlsonf07e9af2010-08-02 11:26:07 +00008974 } else if ((tp->phy_flags & TG3_PHYFLG_MII_SERDES) &&
Matt Carlson2138c002010-07-11 09:31:43 +00008975 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Michael Chan747e8f82005-07-25 12:33:22 -07008976 tg3_serdes_parallel_detect(tp);
Matt Carlson57d8b882010-06-05 17:24:35 +00008977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008978
8979 tp->timer_counter = tp->timer_multiplier;
8980 }
8981
Michael Chan130b8e42006-09-27 16:00:40 -07008982 /* Heartbeat is only sent once every 2 seconds.
8983 *
8984 * The heartbeat is to tell the ASF firmware that the host
8985 * driver is still alive. In the event that the OS crashes,
8986 * ASF needs to reset the hardware to free up the FIFO space
8987 * that may be filled with rx packets destined for the host.
8988 * If the FIFO is full, ASF will no longer function properly.
8989 *
8990 * Unintended resets have been reported on real time kernels
8991 * where the timer doesn't run on time. Netpoll will also have
8992 * same problem.
8993 *
8994 * The new FWCMD_NICDRV_ALIVE3 command tells the ASF firmware
8995 * to check the ring condition when the heartbeat is expiring
8996 * before doing the reset. This will prevent most unintended
8997 * resets.
8998 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008999 if (!--tp->asf_counter) {
Matt Carlsonbc7959b2008-08-15 14:08:55 -07009000 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
9001 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson7c5026a2008-05-02 16:49:29 -07009002 tg3_wait_for_event_ack(tp);
9003
Michael Chanbbadf502006-04-06 21:46:34 -07009004 tg3_write_mem(tp, NIC_SRAM_FW_CMD_MBOX,
Michael Chan130b8e42006-09-27 16:00:40 -07009005 FWCMD_NICDRV_ALIVE3);
Michael Chanbbadf502006-04-06 21:46:34 -07009006 tg3_write_mem(tp, NIC_SRAM_FW_CMD_LEN_MBOX, 4);
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009007 tg3_write_mem(tp, NIC_SRAM_FW_CMD_DATA_MBOX,
9008 TG3_FW_UPDATE_TIMEOUT_SEC);
Matt Carlson4ba526c2008-08-15 14:10:04 -07009009
9010 tg3_generate_fw_event(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009011 }
9012 tp->asf_counter = tp->asf_multiplier;
9013 }
9014
David S. Millerf47c11e2005-06-24 20:18:35 -07009015 spin_unlock(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009016
Michael Chanf475f162006-03-27 23:20:14 -08009017restart_timer:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009018 tp->timer.expires = jiffies + tp->timer_offset;
9019 add_timer(&tp->timer);
9020}
9021
Matt Carlson4f125f42009-09-01 12:55:02 +00009022static int tg3_request_irq(struct tg3 *tp, int irq_num)
Michael Chanfcfa0a32006-03-20 22:28:41 -08009023{
David Howells7d12e782006-10-05 14:55:46 +01009024 irq_handler_t fn;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009025 unsigned long flags;
Matt Carlson4f125f42009-09-01 12:55:02 +00009026 char *name;
9027 struct tg3_napi *tnapi = &tp->napi[irq_num];
9028
9029 if (tp->irq_cnt == 1)
9030 name = tp->dev->name;
9031 else {
9032 name = &tnapi->irq_lbl[0];
9033 snprintf(name, IFNAMSIZ, "%s-%d", tp->dev->name, irq_num);
9034 name[IFNAMSIZ-1] = 0;
9035 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009036
Matt Carlson679563f2009-09-01 12:55:46 +00009037 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
Michael Chanfcfa0a32006-03-20 22:28:41 -08009038 fn = tg3_msi;
9039 if (tp->tg3_flags2 & TG3_FLG2_1SHOT_MSI)
9040 fn = tg3_msi_1shot;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009041 flags = 0;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009042 } else {
9043 fn = tg3_interrupt;
9044 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9045 fn = tg3_interrupt_tagged;
Javier Martinez Canillasab392d22011-03-28 16:27:31 +00009046 flags = IRQF_SHARED;
Michael Chanfcfa0a32006-03-20 22:28:41 -08009047 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009048
9049 return request_irq(tnapi->irq_vec, fn, flags, name, tnapi);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009050}
9051
Michael Chan79381092005-04-21 17:13:59 -07009052static int tg3_test_interrupt(struct tg3 *tp)
9053{
Matt Carlson09943a12009-08-28 14:01:57 +00009054 struct tg3_napi *tnapi = &tp->napi[0];
Michael Chan79381092005-04-21 17:13:59 -07009055 struct net_device *dev = tp->dev;
Michael Chanb16250e2006-09-27 16:10:14 -07009056 int err, i, intr_ok = 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009057 u32 val;
Michael Chan79381092005-04-21 17:13:59 -07009058
Michael Chand4bc3922005-05-29 14:59:20 -07009059 if (!netif_running(dev))
9060 return -ENODEV;
9061
Michael Chan79381092005-04-21 17:13:59 -07009062 tg3_disable_ints(tp);
9063
Matt Carlson4f125f42009-09-01 12:55:02 +00009064 free_irq(tnapi->irq_vec, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009065
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009066 /*
9067 * Turn off MSI one shot mode. Otherwise this test has no
9068 * observable way to know whether the interrupt was delivered.
9069 */
Matt Carlson1407deb2011-04-05 14:22:44 +00009070 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009071 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
9072 val = tr32(MSGINT_MODE) | MSGINT_MODE_ONE_SHOT_DISABLE;
9073 tw32(MSGINT_MODE, val);
9074 }
9075
Matt Carlson4f125f42009-09-01 12:55:02 +00009076 err = request_irq(tnapi->irq_vec, tg3_test_isr,
Matt Carlson09943a12009-08-28 14:01:57 +00009077 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, tnapi);
Michael Chan79381092005-04-21 17:13:59 -07009078 if (err)
9079 return err;
9080
Matt Carlson898a56f2009-08-28 14:02:40 +00009081 tnapi->hw_status->status &= ~SD_STATUS_UPDATED;
Michael Chan79381092005-04-21 17:13:59 -07009082 tg3_enable_ints(tp);
9083
9084 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +00009085 tnapi->coal_now);
Michael Chan79381092005-04-21 17:13:59 -07009086
9087 for (i = 0; i < 5; i++) {
Michael Chanb16250e2006-09-27 16:10:14 -07009088 u32 int_mbox, misc_host_ctrl;
9089
Matt Carlson898a56f2009-08-28 14:02:40 +00009090 int_mbox = tr32_mailbox(tnapi->int_mbox);
Michael Chanb16250e2006-09-27 16:10:14 -07009091 misc_host_ctrl = tr32(TG3PCI_MISC_HOST_CTRL);
9092
9093 if ((int_mbox != 0) ||
9094 (misc_host_ctrl & MISC_HOST_CTRL_MASK_PCI_INT)) {
9095 intr_ok = 1;
Michael Chan79381092005-04-21 17:13:59 -07009096 break;
Michael Chanb16250e2006-09-27 16:10:14 -07009097 }
9098
Michael Chan79381092005-04-21 17:13:59 -07009099 msleep(10);
9100 }
9101
9102 tg3_disable_ints(tp);
9103
Matt Carlson4f125f42009-09-01 12:55:02 +00009104 free_irq(tnapi->irq_vec, tnapi);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009105
Matt Carlson4f125f42009-09-01 12:55:02 +00009106 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009107
9108 if (err)
9109 return err;
9110
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009111 if (intr_ok) {
9112 /* Reenable MSI one shot mode. */
Matt Carlson1407deb2011-04-05 14:22:44 +00009113 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009114 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
9115 val = tr32(MSGINT_MODE) & ~MSGINT_MODE_ONE_SHOT_DISABLE;
9116 tw32(MSGINT_MODE, val);
9117 }
Michael Chan79381092005-04-21 17:13:59 -07009118 return 0;
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009119 }
Michael Chan79381092005-04-21 17:13:59 -07009120
9121 return -EIO;
9122}
9123
9124/* Returns 0 if MSI test succeeds or MSI test fails and INTx mode is
9125 * successfully restored
9126 */
9127static int tg3_test_msi(struct tg3 *tp)
9128{
Michael Chan79381092005-04-21 17:13:59 -07009129 int err;
9130 u16 pci_cmd;
9131
9132 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSI))
9133 return 0;
9134
9135 /* Turn off SERR reporting in case MSI terminates with Master
9136 * Abort.
9137 */
9138 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
9139 pci_write_config_word(tp->pdev, PCI_COMMAND,
9140 pci_cmd & ~PCI_COMMAND_SERR);
9141
9142 err = tg3_test_interrupt(tp);
9143
9144 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
9145
9146 if (!err)
9147 return 0;
9148
9149 /* other failures */
9150 if (err != -EIO)
9151 return err;
9152
9153 /* MSI test failed, go back to INTx mode */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009154 netdev_warn(tp->dev, "No interrupt was generated using MSI. Switching "
9155 "to INTx mode. Please report this failure to the PCI "
9156 "maintainer and include system chipset information\n");
Michael Chan79381092005-04-21 17:13:59 -07009157
Matt Carlson4f125f42009-09-01 12:55:02 +00009158 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Matt Carlson09943a12009-08-28 14:01:57 +00009159
Michael Chan79381092005-04-21 17:13:59 -07009160 pci_disable_msi(tp->pdev);
9161
9162 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI;
Andre Detschdc8bf1b2010-04-26 07:27:07 +00009163 tp->napi[0].irq_vec = tp->pdev->irq;
Michael Chan79381092005-04-21 17:13:59 -07009164
Matt Carlson4f125f42009-09-01 12:55:02 +00009165 err = tg3_request_irq(tp, 0);
Michael Chan79381092005-04-21 17:13:59 -07009166 if (err)
9167 return err;
9168
9169 /* Need to reset the chip because the MSI cycle may have terminated
9170 * with Master Abort.
9171 */
David S. Millerf47c11e2005-06-24 20:18:35 -07009172 tg3_full_lock(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009173
Michael Chan944d9802005-05-29 14:57:48 -07009174 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009175 err = tg3_init_hw(tp, 1);
Michael Chan79381092005-04-21 17:13:59 -07009176
David S. Millerf47c11e2005-06-24 20:18:35 -07009177 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009178
9179 if (err)
Matt Carlson4f125f42009-09-01 12:55:02 +00009180 free_irq(tp->napi[0].irq_vec, &tp->napi[0]);
Michael Chan79381092005-04-21 17:13:59 -07009181
9182 return err;
9183}
9184
Matt Carlson9e9fd122009-01-19 16:57:45 -08009185static int tg3_request_firmware(struct tg3 *tp)
9186{
9187 const __be32 *fw_data;
9188
9189 if (request_firmware(&tp->fw, tp->fw_needed, &tp->pdev->dev)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009190 netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
9191 tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009192 return -ENOENT;
9193 }
9194
9195 fw_data = (void *)tp->fw->data;
9196
9197 /* Firmware blob starts with version numbers, followed by
9198 * start address and _full_ length including BSS sections
9199 * (which must be longer than the actual data, of course
9200 */
9201
9202 tp->fw_len = be32_to_cpu(fw_data[2]); /* includes bss */
9203 if (tp->fw_len < (tp->fw->size - 12)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009204 netdev_err(tp->dev, "bogus length %d in \"%s\"\n",
9205 tp->fw_len, tp->fw_needed);
Matt Carlson9e9fd122009-01-19 16:57:45 -08009206 release_firmware(tp->fw);
9207 tp->fw = NULL;
9208 return -EINVAL;
9209 }
9210
9211 /* We no longer need firmware; we have it. */
9212 tp->fw_needed = NULL;
9213 return 0;
9214}
9215
Matt Carlson679563f2009-09-01 12:55:46 +00009216static bool tg3_enable_msix(struct tg3 *tp)
9217{
9218 int i, rc, cpus = num_online_cpus();
9219 struct msix_entry msix_ent[tp->irq_max];
9220
9221 if (cpus == 1)
9222 /* Just fallback to the simpler MSI mode. */
9223 return false;
9224
9225 /*
9226 * We want as many rx rings enabled as there are cpus.
9227 * The first MSIX vector only deals with link interrupts, etc,
9228 * so we add one to the number of vectors we are requesting.
9229 */
9230 tp->irq_cnt = min_t(unsigned, cpus + 1, tp->irq_max);
9231
9232 for (i = 0; i < tp->irq_max; i++) {
9233 msix_ent[i].entry = i;
9234 msix_ent[i].vector = 0;
9235 }
9236
9237 rc = pci_enable_msix(tp->pdev, msix_ent, tp->irq_cnt);
Matt Carlson2430b032010-06-05 17:24:34 +00009238 if (rc < 0) {
9239 return false;
9240 } else if (rc != 0) {
Matt Carlson679563f2009-09-01 12:55:46 +00009241 if (pci_enable_msix(tp->pdev, msix_ent, rc))
9242 return false;
Joe Perches05dbe002010-02-17 19:44:19 +00009243 netdev_notice(tp->dev, "Requested %d MSI-X vectors, received %d\n",
9244 tp->irq_cnt, rc);
Matt Carlson679563f2009-09-01 12:55:46 +00009245 tp->irq_cnt = rc;
9246 }
9247
9248 for (i = 0; i < tp->irq_max; i++)
9249 tp->napi[i].irq_vec = msix_ent[i].vector;
9250
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009251 netif_set_real_num_tx_queues(tp->dev, 1);
9252 rc = tp->irq_cnt > 1 ? tp->irq_cnt - 1 : 1;
9253 if (netif_set_real_num_rx_queues(tp->dev, rc)) {
9254 pci_disable_msix(tp->pdev);
9255 return false;
9256 }
Matt Carlsonb92b9042010-11-24 08:31:51 +00009257
9258 if (tp->irq_cnt > 1) {
Matt Carlson2430b032010-06-05 17:24:34 +00009259 tp->tg3_flags3 |= TG3_FLG3_ENABLE_RSS;
Matt Carlsond78b59f2011-04-05 14:22:46 +00009260
9261 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
9262 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720) {
Matt Carlsonb92b9042010-11-24 08:31:51 +00009263 tp->tg3_flags3 |= TG3_FLG3_ENABLE_TSS;
9264 netif_set_real_num_tx_queues(tp->dev, tp->irq_cnt - 1);
9265 }
9266 }
Matt Carlson2430b032010-06-05 17:24:34 +00009267
Matt Carlson679563f2009-09-01 12:55:46 +00009268 return true;
9269}
9270
Matt Carlson07b01732009-08-28 14:01:15 +00009271static void tg3_ints_init(struct tg3 *tp)
9272{
Matt Carlson679563f2009-09-01 12:55:46 +00009273 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI_OR_MSIX) &&
9274 !(tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)) {
Matt Carlson07b01732009-08-28 14:01:15 +00009275 /* All MSI supporting chips should support tagged
9276 * status. Assert that this is the case.
9277 */
Matt Carlson5129c3a2010-04-05 10:19:23 +00009278 netdev_warn(tp->dev,
9279 "MSI without TAGGED_STATUS? Not using MSI\n");
Matt Carlson679563f2009-09-01 12:55:46 +00009280 goto defcfg;
Matt Carlson07b01732009-08-28 14:01:15 +00009281 }
Matt Carlson4f125f42009-09-01 12:55:02 +00009282
Matt Carlson679563f2009-09-01 12:55:46 +00009283 if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX) && tg3_enable_msix(tp))
9284 tp->tg3_flags2 |= TG3_FLG2_USING_MSIX;
9285 else if ((tp->tg3_flags & TG3_FLAG_SUPPORT_MSI) &&
9286 pci_enable_msi(tp->pdev) == 0)
9287 tp->tg3_flags2 |= TG3_FLG2_USING_MSI;
9288
9289 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI_OR_MSIX) {
9290 u32 msi_mode = tr32(MSGINT_MODE);
Matt Carlson0583d522011-01-25 15:58:50 +00009291 if ((tp->tg3_flags2 & TG3_FLG2_USING_MSIX) &&
9292 tp->irq_cnt > 1)
Matt Carlsonbaf8a942009-09-01 13:13:00 +00009293 msi_mode |= MSGINT_MODE_MULTIVEC_EN;
Matt Carlson679563f2009-09-01 12:55:46 +00009294 tw32(MSGINT_MODE, msi_mode | MSGINT_MODE_ENABLE);
9295 }
9296defcfg:
9297 if (!(tp->tg3_flags2 & TG3_FLG2_USING_MSIX)) {
9298 tp->irq_cnt = 1;
9299 tp->napi[0].irq_vec = tp->pdev->irq;
Ben Hutchings2ddaad32010-09-27 22:11:51 -07009300 netif_set_real_num_tx_queues(tp->dev, 1);
Matt Carlson85407882010-10-06 13:40:58 -07009301 netif_set_real_num_rx_queues(tp->dev, 1);
Matt Carlson679563f2009-09-01 12:55:46 +00009302 }
Matt Carlson07b01732009-08-28 14:01:15 +00009303}
9304
9305static void tg3_ints_fini(struct tg3 *tp)
9306{
Matt Carlson679563f2009-09-01 12:55:46 +00009307 if (tp->tg3_flags2 & TG3_FLG2_USING_MSIX)
9308 pci_disable_msix(tp->pdev);
9309 else if (tp->tg3_flags2 & TG3_FLG2_USING_MSI)
9310 pci_disable_msi(tp->pdev);
9311 tp->tg3_flags2 &= ~TG3_FLG2_USING_MSI_OR_MSIX;
Matt Carlson774ee752010-08-02 11:25:56 +00009312 tp->tg3_flags3 &= ~(TG3_FLG3_ENABLE_RSS | TG3_FLG3_ENABLE_TSS);
Matt Carlson07b01732009-08-28 14:01:15 +00009313}
9314
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315static int tg3_open(struct net_device *dev)
9316{
9317 struct tg3 *tp = netdev_priv(dev);
Matt Carlson4f125f42009-09-01 12:55:02 +00009318 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009319
Matt Carlson9e9fd122009-01-19 16:57:45 -08009320 if (tp->fw_needed) {
9321 err = tg3_request_firmware(tp);
9322 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0) {
9323 if (err)
9324 return err;
9325 } else if (err) {
Joe Perches05dbe002010-02-17 19:44:19 +00009326 netdev_warn(tp->dev, "TSO capability disabled\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009327 tp->tg3_flags2 &= ~TG3_FLG2_TSO_CAPABLE;
9328 } else if (!(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE)) {
Joe Perches05dbe002010-02-17 19:44:19 +00009329 netdev_notice(tp->dev, "TSO capability restored\n");
Matt Carlson9e9fd122009-01-19 16:57:45 -08009330 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
9331 }
9332 }
9333
Michael Chanc49a1562006-12-17 17:07:29 -08009334 netif_carrier_off(tp->dev);
9335
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009336 err = tg3_power_up(tp);
Matt Carlson2f751b62008-08-04 23:17:34 -07009337 if (err)
Michael Chanbc1c7562006-03-20 17:48:03 -08009338 return err;
Matt Carlson2f751b62008-08-04 23:17:34 -07009339
9340 tg3_full_lock(tp, 0);
Michael Chanbc1c7562006-03-20 17:48:03 -08009341
Linus Torvalds1da177e2005-04-16 15:20:36 -07009342 tg3_disable_ints(tp);
9343 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
9344
David S. Millerf47c11e2005-06-24 20:18:35 -07009345 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009346
Matt Carlson679563f2009-09-01 12:55:46 +00009347 /*
9348 * Setup interrupts first so we know how
9349 * many NAPI resources to allocate
9350 */
9351 tg3_ints_init(tp);
9352
Linus Torvalds1da177e2005-04-16 15:20:36 -07009353 /* The placement of this call is tied
9354 * to the setup and use of Host TX descriptors.
9355 */
9356 err = tg3_alloc_consistent(tp);
9357 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009358 goto err_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009359
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009360 tg3_napi_init(tp);
9361
Matt Carlsonfed97812009-09-01 13:10:19 +00009362 tg3_napi_enable(tp);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07009363
Matt Carlson4f125f42009-09-01 12:55:02 +00009364 for (i = 0; i < tp->irq_cnt; i++) {
9365 struct tg3_napi *tnapi = &tp->napi[i];
9366 err = tg3_request_irq(tp, i);
9367 if (err) {
9368 for (i--; i >= 0; i--)
9369 free_irq(tnapi->irq_vec, tnapi);
9370 break;
9371 }
9372 }
Matt Carlson07b01732009-08-28 14:01:15 +00009373
9374 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009375 goto err_out2;
Matt Carlson07b01732009-08-28 14:01:15 +00009376
David S. Millerf47c11e2005-06-24 20:18:35 -07009377 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009378
Gary Zambrano8e7a22e2006-04-29 18:59:13 -07009379 err = tg3_init_hw(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009380 if (err) {
Michael Chan944d9802005-05-29 14:57:48 -07009381 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009382 tg3_free_rings(tp);
9383 } else {
David S. Millerfac9b832005-05-18 22:46:34 -07009384 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS)
9385 tp->timer_offset = HZ;
9386 else
9387 tp->timer_offset = HZ / 10;
9388
9389 BUG_ON(tp->timer_offset > HZ);
9390 tp->timer_counter = tp->timer_multiplier =
9391 (HZ / tp->timer_offset);
9392 tp->asf_counter = tp->asf_multiplier =
Michael Chan28fbef72005-10-26 15:48:35 -07009393 ((HZ / tp->timer_offset) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009394
9395 init_timer(&tp->timer);
9396 tp->timer.expires = jiffies + tp->timer_offset;
9397 tp->timer.data = (unsigned long) tp;
9398 tp->timer.function = tg3_timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009399 }
9400
David S. Millerf47c11e2005-06-24 20:18:35 -07009401 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009402
Matt Carlson07b01732009-08-28 14:01:15 +00009403 if (err)
Matt Carlson679563f2009-09-01 12:55:46 +00009404 goto err_out3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009405
Michael Chan79381092005-04-21 17:13:59 -07009406 if (tp->tg3_flags2 & TG3_FLG2_USING_MSI) {
9407 err = tg3_test_msi(tp);
David S. Millerfac9b832005-05-18 22:46:34 -07009408
Michael Chan79381092005-04-21 17:13:59 -07009409 if (err) {
David S. Millerf47c11e2005-06-24 20:18:35 -07009410 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -07009411 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan79381092005-04-21 17:13:59 -07009412 tg3_free_rings(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -07009413 tg3_full_unlock(tp);
Michael Chan79381092005-04-21 17:13:59 -07009414
Matt Carlson679563f2009-09-01 12:55:46 +00009415 goto err_out2;
Michael Chan79381092005-04-21 17:13:59 -07009416 }
Michael Chanfcfa0a32006-03-20 22:28:41 -08009417
Matt Carlson1407deb2011-04-05 14:22:44 +00009418 if (!(tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlsonc885e822010-08-02 11:25:57 +00009419 (tp->tg3_flags2 & TG3_FLG2_USING_MSI)) {
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009420 u32 val = tr32(PCIE_TRANSACTION_CFG);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009421
Matt Carlsonf6eb9b12009-09-01 13:19:53 +00009422 tw32(PCIE_TRANSACTION_CFG,
9423 val | PCIE_TRANS_CFG_1SHOT_MSI);
Michael Chanfcfa0a32006-03-20 22:28:41 -08009424 }
Michael Chan79381092005-04-21 17:13:59 -07009425 }
9426
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009427 tg3_phy_start(tp);
9428
David S. Millerf47c11e2005-06-24 20:18:35 -07009429 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009430
Michael Chan79381092005-04-21 17:13:59 -07009431 add_timer(&tp->timer);
9432 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433 tg3_enable_ints(tp);
9434
David S. Millerf47c11e2005-06-24 20:18:35 -07009435 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009436
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009437 netif_tx_start_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009438
9439 return 0;
Matt Carlson07b01732009-08-28 14:01:15 +00009440
Matt Carlson679563f2009-09-01 12:55:46 +00009441err_out3:
Matt Carlson4f125f42009-09-01 12:55:02 +00009442 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9443 struct tg3_napi *tnapi = &tp->napi[i];
9444 free_irq(tnapi->irq_vec, tnapi);
9445 }
Matt Carlson07b01732009-08-28 14:01:15 +00009446
Matt Carlson679563f2009-09-01 12:55:46 +00009447err_out2:
Matt Carlsonfed97812009-09-01 13:10:19 +00009448 tg3_napi_disable(tp);
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009449 tg3_napi_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009450 tg3_free_consistent(tp);
Matt Carlson679563f2009-09-01 12:55:46 +00009451
9452err_out1:
9453 tg3_ints_fini(tp);
Matt Carlson07b01732009-08-28 14:01:15 +00009454 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009455}
9456
Eric Dumazet511d2222010-07-07 20:44:24 +00009457static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *,
9458 struct rtnl_link_stats64 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009459static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *);
9460
9461static int tg3_close(struct net_device *dev)
9462{
Matt Carlson4f125f42009-09-01 12:55:02 +00009463 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009464 struct tg3 *tp = netdev_priv(dev);
9465
Matt Carlsonfed97812009-09-01 13:10:19 +00009466 tg3_napi_disable(tp);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07009467 cancel_work_sync(&tp->reset_task);
Michael Chan7faa0062006-02-02 17:29:28 -08009468
Matt Carlsonfe5f5782009-09-01 13:09:39 +00009469 netif_tx_stop_all_queues(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009470
9471 del_timer_sync(&tp->timer);
9472
Matt Carlson24bb4fb2009-10-05 17:55:29 +00009473 tg3_phy_stop(tp);
9474
David S. Millerf47c11e2005-06-24 20:18:35 -07009475 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009476
9477 tg3_disable_ints(tp);
9478
Michael Chan944d9802005-05-29 14:57:48 -07009479 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009480 tg3_free_rings(tp);
Michael Chan5cf64b82007-05-05 12:11:21 -07009481 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009482
David S. Millerf47c11e2005-06-24 20:18:35 -07009483 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009484
Matt Carlson4f125f42009-09-01 12:55:02 +00009485 for (i = tp->irq_cnt - 1; i >= 0; i--) {
9486 struct tg3_napi *tnapi = &tp->napi[i];
9487 free_irq(tnapi->irq_vec, tnapi);
9488 }
Matt Carlson07b01732009-08-28 14:01:15 +00009489
9490 tg3_ints_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009491
Eric Dumazet511d2222010-07-07 20:44:24 +00009492 tg3_get_stats64(tp->dev, &tp->net_stats_prev);
9493
Linus Torvalds1da177e2005-04-16 15:20:36 -07009494 memcpy(&tp->estats_prev, tg3_get_estats(tp),
9495 sizeof(tp->estats_prev));
9496
Matt Carlson66cfd1b2010-09-30 10:34:30 +00009497 tg3_napi_fini(tp);
9498
Linus Torvalds1da177e2005-04-16 15:20:36 -07009499 tg3_free_consistent(tp);
9500
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +00009501 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -08009502
9503 netif_carrier_off(tp->dev);
9504
Linus Torvalds1da177e2005-04-16 15:20:36 -07009505 return 0;
9506}
9507
Eric Dumazet511d2222010-07-07 20:44:24 +00009508static inline u64 get_stat64(tg3_stat64_t *val)
Stefan Buehler816f8b82008-08-15 14:10:54 -07009509{
9510 return ((u64)val->high << 32) | ((u64)val->low);
9511}
9512
Eric Dumazet511d2222010-07-07 20:44:24 +00009513static u64 calc_crc_errors(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009514{
9515 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9516
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009517 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07009518 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
9519 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009520 u32 val;
9521
David S. Millerf47c11e2005-06-24 20:18:35 -07009522 spin_lock_bh(&tp->lock);
Michael Chan569a5df2007-02-13 12:18:15 -08009523 if (!tg3_readphy(tp, MII_TG3_TEST1, &val)) {
9524 tg3_writephy(tp, MII_TG3_TEST1,
9525 val | MII_TG3_TEST1_CRC_EN);
Matt Carlsonf08aa1a2010-08-02 11:26:05 +00009526 tg3_readphy(tp, MII_TG3_RXR_COUNTERS, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009527 } else
9528 val = 0;
David S. Millerf47c11e2005-06-24 20:18:35 -07009529 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009530
9531 tp->phy_crc_errors += val;
9532
9533 return tp->phy_crc_errors;
9534 }
9535
9536 return get_stat64(&hw_stats->rx_fcs_errors);
9537}
9538
9539#define ESTAT_ADD(member) \
9540 estats->member = old_estats->member + \
Eric Dumazet511d2222010-07-07 20:44:24 +00009541 get_stat64(&hw_stats->member)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009542
9543static struct tg3_ethtool_stats *tg3_get_estats(struct tg3 *tp)
9544{
9545 struct tg3_ethtool_stats *estats = &tp->estats;
9546 struct tg3_ethtool_stats *old_estats = &tp->estats_prev;
9547 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9548
9549 if (!hw_stats)
9550 return old_estats;
9551
9552 ESTAT_ADD(rx_octets);
9553 ESTAT_ADD(rx_fragments);
9554 ESTAT_ADD(rx_ucast_packets);
9555 ESTAT_ADD(rx_mcast_packets);
9556 ESTAT_ADD(rx_bcast_packets);
9557 ESTAT_ADD(rx_fcs_errors);
9558 ESTAT_ADD(rx_align_errors);
9559 ESTAT_ADD(rx_xon_pause_rcvd);
9560 ESTAT_ADD(rx_xoff_pause_rcvd);
9561 ESTAT_ADD(rx_mac_ctrl_rcvd);
9562 ESTAT_ADD(rx_xoff_entered);
9563 ESTAT_ADD(rx_frame_too_long_errors);
9564 ESTAT_ADD(rx_jabbers);
9565 ESTAT_ADD(rx_undersize_packets);
9566 ESTAT_ADD(rx_in_length_errors);
9567 ESTAT_ADD(rx_out_length_errors);
9568 ESTAT_ADD(rx_64_or_less_octet_packets);
9569 ESTAT_ADD(rx_65_to_127_octet_packets);
9570 ESTAT_ADD(rx_128_to_255_octet_packets);
9571 ESTAT_ADD(rx_256_to_511_octet_packets);
9572 ESTAT_ADD(rx_512_to_1023_octet_packets);
9573 ESTAT_ADD(rx_1024_to_1522_octet_packets);
9574 ESTAT_ADD(rx_1523_to_2047_octet_packets);
9575 ESTAT_ADD(rx_2048_to_4095_octet_packets);
9576 ESTAT_ADD(rx_4096_to_8191_octet_packets);
9577 ESTAT_ADD(rx_8192_to_9022_octet_packets);
9578
9579 ESTAT_ADD(tx_octets);
9580 ESTAT_ADD(tx_collisions);
9581 ESTAT_ADD(tx_xon_sent);
9582 ESTAT_ADD(tx_xoff_sent);
9583 ESTAT_ADD(tx_flow_control);
9584 ESTAT_ADD(tx_mac_errors);
9585 ESTAT_ADD(tx_single_collisions);
9586 ESTAT_ADD(tx_mult_collisions);
9587 ESTAT_ADD(tx_deferred);
9588 ESTAT_ADD(tx_excessive_collisions);
9589 ESTAT_ADD(tx_late_collisions);
9590 ESTAT_ADD(tx_collide_2times);
9591 ESTAT_ADD(tx_collide_3times);
9592 ESTAT_ADD(tx_collide_4times);
9593 ESTAT_ADD(tx_collide_5times);
9594 ESTAT_ADD(tx_collide_6times);
9595 ESTAT_ADD(tx_collide_7times);
9596 ESTAT_ADD(tx_collide_8times);
9597 ESTAT_ADD(tx_collide_9times);
9598 ESTAT_ADD(tx_collide_10times);
9599 ESTAT_ADD(tx_collide_11times);
9600 ESTAT_ADD(tx_collide_12times);
9601 ESTAT_ADD(tx_collide_13times);
9602 ESTAT_ADD(tx_collide_14times);
9603 ESTAT_ADD(tx_collide_15times);
9604 ESTAT_ADD(tx_ucast_packets);
9605 ESTAT_ADD(tx_mcast_packets);
9606 ESTAT_ADD(tx_bcast_packets);
9607 ESTAT_ADD(tx_carrier_sense_errors);
9608 ESTAT_ADD(tx_discards);
9609 ESTAT_ADD(tx_errors);
9610
9611 ESTAT_ADD(dma_writeq_full);
9612 ESTAT_ADD(dma_write_prioq_full);
9613 ESTAT_ADD(rxbds_empty);
9614 ESTAT_ADD(rx_discards);
9615 ESTAT_ADD(rx_errors);
9616 ESTAT_ADD(rx_threshold_hit);
9617
9618 ESTAT_ADD(dma_readq_full);
9619 ESTAT_ADD(dma_read_prioq_full);
9620 ESTAT_ADD(tx_comp_queue_full);
9621
9622 ESTAT_ADD(ring_set_send_prod_index);
9623 ESTAT_ADD(ring_status_update);
9624 ESTAT_ADD(nic_irqs);
9625 ESTAT_ADD(nic_avoided_irqs);
9626 ESTAT_ADD(nic_tx_threshold_hit);
9627
9628 return estats;
9629}
9630
Eric Dumazet511d2222010-07-07 20:44:24 +00009631static struct rtnl_link_stats64 *tg3_get_stats64(struct net_device *dev,
9632 struct rtnl_link_stats64 *stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009633{
9634 struct tg3 *tp = netdev_priv(dev);
Eric Dumazet511d2222010-07-07 20:44:24 +00009635 struct rtnl_link_stats64 *old_stats = &tp->net_stats_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009636 struct tg3_hw_stats *hw_stats = tp->hw_stats;
9637
9638 if (!hw_stats)
9639 return old_stats;
9640
9641 stats->rx_packets = old_stats->rx_packets +
9642 get_stat64(&hw_stats->rx_ucast_packets) +
9643 get_stat64(&hw_stats->rx_mcast_packets) +
9644 get_stat64(&hw_stats->rx_bcast_packets);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009645
Linus Torvalds1da177e2005-04-16 15:20:36 -07009646 stats->tx_packets = old_stats->tx_packets +
9647 get_stat64(&hw_stats->tx_ucast_packets) +
9648 get_stat64(&hw_stats->tx_mcast_packets) +
9649 get_stat64(&hw_stats->tx_bcast_packets);
9650
9651 stats->rx_bytes = old_stats->rx_bytes +
9652 get_stat64(&hw_stats->rx_octets);
9653 stats->tx_bytes = old_stats->tx_bytes +
9654 get_stat64(&hw_stats->tx_octets);
9655
9656 stats->rx_errors = old_stats->rx_errors +
John W. Linville4f63b872005-09-12 14:43:18 -07009657 get_stat64(&hw_stats->rx_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009658 stats->tx_errors = old_stats->tx_errors +
9659 get_stat64(&hw_stats->tx_errors) +
9660 get_stat64(&hw_stats->tx_mac_errors) +
9661 get_stat64(&hw_stats->tx_carrier_sense_errors) +
9662 get_stat64(&hw_stats->tx_discards);
9663
9664 stats->multicast = old_stats->multicast +
9665 get_stat64(&hw_stats->rx_mcast_packets);
9666 stats->collisions = old_stats->collisions +
9667 get_stat64(&hw_stats->tx_collisions);
9668
9669 stats->rx_length_errors = old_stats->rx_length_errors +
9670 get_stat64(&hw_stats->rx_frame_too_long_errors) +
9671 get_stat64(&hw_stats->rx_undersize_packets);
9672
9673 stats->rx_over_errors = old_stats->rx_over_errors +
9674 get_stat64(&hw_stats->rxbds_empty);
9675 stats->rx_frame_errors = old_stats->rx_frame_errors +
9676 get_stat64(&hw_stats->rx_align_errors);
9677 stats->tx_aborted_errors = old_stats->tx_aborted_errors +
9678 get_stat64(&hw_stats->tx_discards);
9679 stats->tx_carrier_errors = old_stats->tx_carrier_errors +
9680 get_stat64(&hw_stats->tx_carrier_sense_errors);
9681
9682 stats->rx_crc_errors = old_stats->rx_crc_errors +
9683 calc_crc_errors(tp);
9684
John W. Linville4f63b872005-09-12 14:43:18 -07009685 stats->rx_missed_errors = old_stats->rx_missed_errors +
9686 get_stat64(&hw_stats->rx_discards);
9687
Eric Dumazetb0057c52010-10-10 19:55:52 +00009688 stats->rx_dropped = tp->rx_dropped;
9689
Linus Torvalds1da177e2005-04-16 15:20:36 -07009690 return stats;
9691}
9692
9693static inline u32 calc_crc(unsigned char *buf, int len)
9694{
9695 u32 reg;
9696 u32 tmp;
9697 int j, k;
9698
9699 reg = 0xffffffff;
9700
9701 for (j = 0; j < len; j++) {
9702 reg ^= buf[j];
9703
9704 for (k = 0; k < 8; k++) {
9705 tmp = reg & 0x01;
9706
9707 reg >>= 1;
9708
Matt Carlson859a5882010-04-05 10:19:28 +00009709 if (tmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009710 reg ^= 0xedb88320;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009711 }
9712 }
9713
9714 return ~reg;
9715}
9716
9717static void tg3_set_multi(struct tg3 *tp, unsigned int accept_all)
9718{
9719 /* accept or reject all multicast frames */
9720 tw32(MAC_HASH_REG_0, accept_all ? 0xffffffff : 0);
9721 tw32(MAC_HASH_REG_1, accept_all ? 0xffffffff : 0);
9722 tw32(MAC_HASH_REG_2, accept_all ? 0xffffffff : 0);
9723 tw32(MAC_HASH_REG_3, accept_all ? 0xffffffff : 0);
9724}
9725
9726static void __tg3_set_rx_mode(struct net_device *dev)
9727{
9728 struct tg3 *tp = netdev_priv(dev);
9729 u32 rx_mode;
9730
9731 rx_mode = tp->rx_mode & ~(RX_MODE_PROMISC |
9732 RX_MODE_KEEP_VLAN_TAG);
9733
Matt Carlsonbf933c82011-01-25 15:58:49 +00009734#if !defined(CONFIG_VLAN_8021Q) && !defined(CONFIG_VLAN_8021Q_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009735 /* When ASF is in use, we always keep the RX_MODE_KEEP_VLAN_TAG
9736 * flag clear.
9737 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009738 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
9739 rx_mode |= RX_MODE_KEEP_VLAN_TAG;
9740#endif
9741
9742 if (dev->flags & IFF_PROMISC) {
9743 /* Promiscuous mode. */
9744 rx_mode |= RX_MODE_PROMISC;
9745 } else if (dev->flags & IFF_ALLMULTI) {
9746 /* Accept all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009747 tg3_set_multi(tp, 1);
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00009748 } else if (netdev_mc_empty(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009749 /* Reject all multicast. */
Matt Carlsonde6f31e2010-04-12 06:58:30 +00009750 tg3_set_multi(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009751 } else {
9752 /* Accept one or more multicast(s). */
Jiri Pirko22bedad2010-04-01 21:22:57 +00009753 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009754 u32 mc_filter[4] = { 0, };
9755 u32 regidx;
9756 u32 bit;
9757 u32 crc;
9758
Jiri Pirko22bedad2010-04-01 21:22:57 +00009759 netdev_for_each_mc_addr(ha, dev) {
9760 crc = calc_crc(ha->addr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009761 bit = ~crc & 0x7f;
9762 regidx = (bit & 0x60) >> 5;
9763 bit &= 0x1f;
9764 mc_filter[regidx] |= (1 << bit);
9765 }
9766
9767 tw32(MAC_HASH_REG_0, mc_filter[0]);
9768 tw32(MAC_HASH_REG_1, mc_filter[1]);
9769 tw32(MAC_HASH_REG_2, mc_filter[2]);
9770 tw32(MAC_HASH_REG_3, mc_filter[3]);
9771 }
9772
9773 if (rx_mode != tp->rx_mode) {
9774 tp->rx_mode = rx_mode;
9775 tw32_f(MAC_RX_MODE, rx_mode);
9776 udelay(10);
9777 }
9778}
9779
9780static void tg3_set_rx_mode(struct net_device *dev)
9781{
9782 struct tg3 *tp = netdev_priv(dev);
9783
Michael Chane75f7c92006-03-20 21:33:26 -08009784 if (!netif_running(dev))
9785 return;
9786
David S. Millerf47c11e2005-06-24 20:18:35 -07009787 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009788 __tg3_set_rx_mode(dev);
David S. Millerf47c11e2005-06-24 20:18:35 -07009789 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009790}
9791
Linus Torvalds1da177e2005-04-16 15:20:36 -07009792static int tg3_get_regs_len(struct net_device *dev)
9793{
Matt Carlson97bd8e42011-04-13 11:05:04 +00009794 return TG3_REG_BLK_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009795}
9796
9797static void tg3_get_regs(struct net_device *dev,
9798 struct ethtool_regs *regs, void *_p)
9799{
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009801
9802 regs->version = 0;
9803
Matt Carlson97bd8e42011-04-13 11:05:04 +00009804 memset(_p, 0, TG3_REG_BLK_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009805
Matt Carlson80096062010-08-02 11:26:06 +00009806 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009807 return;
9808
David S. Millerf47c11e2005-06-24 20:18:35 -07009809 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810
Matt Carlson97bd8e42011-04-13 11:05:04 +00009811 tg3_dump_legacy_regs(tp, (u32 *)_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009812
David S. Millerf47c11e2005-06-24 20:18:35 -07009813 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009814}
9815
9816static int tg3_get_eeprom_len(struct net_device *dev)
9817{
9818 struct tg3 *tp = netdev_priv(dev);
9819
9820 return tp->nvram_size;
9821}
9822
Linus Torvalds1da177e2005-04-16 15:20:36 -07009823static int tg3_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9824{
9825 struct tg3 *tp = netdev_priv(dev);
9826 int ret;
9827 u8 *pd;
Al Virob9fc7dc2007-12-17 22:59:57 -08009828 u32 i, offset, len, b_offset, b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009829 __be32 val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009830
Matt Carlsondf259d82009-04-20 06:57:14 +00009831 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
9832 return -EINVAL;
9833
Matt Carlson80096062010-08-02 11:26:06 +00009834 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009835 return -EAGAIN;
9836
Linus Torvalds1da177e2005-04-16 15:20:36 -07009837 offset = eeprom->offset;
9838 len = eeprom->len;
9839 eeprom->len = 0;
9840
9841 eeprom->magic = TG3_EEPROM_MAGIC;
9842
9843 if (offset & 3) {
9844 /* adjustments to start on required 4 byte boundary */
9845 b_offset = offset & 3;
9846 b_count = 4 - b_offset;
9847 if (b_count > len) {
9848 /* i.e. offset=1 len=2 */
9849 b_count = len;
9850 }
Matt Carlsona9dc5292009-02-25 14:25:30 +00009851 ret = tg3_nvram_read_be32(tp, offset-b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009852 if (ret)
9853 return ret;
Matt Carlsonbe98da62010-07-11 09:31:46 +00009854 memcpy(data, ((char *)&val) + b_offset, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009855 len -= b_count;
9856 offset += b_count;
Matt Carlsonc6cdf432010-04-05 10:19:26 +00009857 eeprom->len += b_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009858 }
9859
Lucas De Marchi25985ed2011-03-30 22:57:33 -03009860 /* read bytes up to the last 4 byte boundary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009861 pd = &data[eeprom->len];
9862 for (i = 0; i < (len - (len & 3)); i += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +00009863 ret = tg3_nvram_read_be32(tp, offset + i, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009864 if (ret) {
9865 eeprom->len += i;
9866 return ret;
9867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07009868 memcpy(pd + i, &val, 4);
9869 }
9870 eeprom->len += i;
9871
9872 if (len & 3) {
9873 /* read last bytes not ending on 4 byte boundary */
9874 pd = &data[eeprom->len];
9875 b_count = len & 3;
9876 b_offset = offset + len - b_count;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009877 ret = tg3_nvram_read_be32(tp, b_offset, &val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009878 if (ret)
9879 return ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009880 memcpy(pd, &val, b_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009881 eeprom->len += b_count;
9882 }
9883 return 0;
9884}
9885
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009886static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009887
9888static int tg3_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom, u8 *data)
9889{
9890 struct tg3 *tp = netdev_priv(dev);
9891 int ret;
Al Virob9fc7dc2007-12-17 22:59:57 -08009892 u32 offset, len, b_offset, odd_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009893 u8 *buf;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009894 __be32 start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009895
Matt Carlson80096062010-08-02 11:26:06 +00009896 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Michael Chanbc1c7562006-03-20 17:48:03 -08009897 return -EAGAIN;
9898
Matt Carlsondf259d82009-04-20 06:57:14 +00009899 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
9900 eeprom->magic != TG3_EEPROM_MAGIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009901 return -EINVAL;
9902
9903 offset = eeprom->offset;
9904 len = eeprom->len;
9905
9906 if ((b_offset = (offset & 3))) {
9907 /* adjustments to start on required 4 byte boundary */
Matt Carlsona9dc5292009-02-25 14:25:30 +00009908 ret = tg3_nvram_read_be32(tp, offset-b_offset, &start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009909 if (ret)
9910 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009911 len += b_offset;
9912 offset &= ~3;
Michael Chan1c8594b2005-04-21 17:12:46 -07009913 if (len < 4)
9914 len = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009915 }
9916
9917 odd_len = 0;
Michael Chan1c8594b2005-04-21 17:12:46 -07009918 if (len & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009919 /* adjustments to end on required 4 byte boundary */
9920 odd_len = 1;
9921 len = (len + 3) & ~3;
Matt Carlsona9dc5292009-02-25 14:25:30 +00009922 ret = tg3_nvram_read_be32(tp, offset+len-4, &end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07009923 if (ret)
9924 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009925 }
9926
9927 buf = data;
9928 if (b_offset || odd_len) {
9929 buf = kmalloc(len, GFP_KERNEL);
Andy Gospodarekab0049b2007-09-06 20:42:14 +01009930 if (!buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009931 return -ENOMEM;
9932 if (b_offset)
9933 memcpy(buf, &start, 4);
9934 if (odd_len)
9935 memcpy(buf+len-4, &end, 4);
9936 memcpy(buf + b_offset, data, eeprom->len);
9937 }
9938
9939 ret = tg3_nvram_write_block(tp, offset, len, buf);
9940
9941 if (buf != data)
9942 kfree(buf);
9943
9944 return ret;
9945}
9946
9947static int tg3_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9948{
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009949 struct tg3 *tp = netdev_priv(dev);
9950
9951 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009952 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009953 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009954 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009955 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
9956 return phy_ethtool_gset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009957 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009958
Linus Torvalds1da177e2005-04-16 15:20:36 -07009959 cmd->supported = (SUPPORTED_Autoneg);
9960
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009961 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009962 cmd->supported |= (SUPPORTED_1000baseT_Half |
9963 SUPPORTED_1000baseT_Full);
9964
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009965 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009966 cmd->supported |= (SUPPORTED_100baseT_Half |
9967 SUPPORTED_100baseT_Full |
9968 SUPPORTED_10baseT_Half |
9969 SUPPORTED_10baseT_Full |
Matt Carlson3bebab52007-11-12 21:22:40 -08009970 SUPPORTED_TP);
Karsten Keilef348142006-05-12 12:49:08 -07009971 cmd->port = PORT_TP;
9972 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009973 cmd->supported |= SUPPORTED_FIBRE;
Karsten Keilef348142006-05-12 12:49:08 -07009974 cmd->port = PORT_FIBRE;
9975 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009976
Linus Torvalds1da177e2005-04-16 15:20:36 -07009977 cmd->advertising = tp->link_config.advertising;
9978 if (netif_running(dev)) {
9979 cmd->speed = tp->link_config.active_speed;
9980 cmd->duplex = tp->link_config.active_duplex;
Matt Carlson64c22182010-10-14 10:37:44 +00009981 } else {
9982 cmd->speed = SPEED_INVALID;
9983 cmd->duplex = DUPLEX_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009984 }
Matt Carlson882e9792009-09-01 13:21:36 +00009985 cmd->phy_address = tp->phy_addr;
Matt Carlson7e5856b2009-02-25 14:23:01 +00009986 cmd->transceiver = XCVR_INTERNAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009987 cmd->autoneg = tp->link_config.autoneg;
9988 cmd->maxtxpkt = 0;
9989 cmd->maxrxpkt = 0;
9990 return 0;
9991}
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009992
Linus Torvalds1da177e2005-04-16 15:20:36 -07009993static int tg3_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
9994{
9995 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -04009996
Matt Carlsonb02fd9e2008-05-25 23:47:41 -07009997 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +00009998 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +00009999 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010000 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010001 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
10002 return phy_ethtool_sset(phydev, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010003 }
10004
Matt Carlson7e5856b2009-02-25 14:23:01 +000010005 if (cmd->autoneg != AUTONEG_ENABLE &&
10006 cmd->autoneg != AUTONEG_DISABLE)
Michael Chan37ff2382005-10-26 15:49:51 -070010007 return -EINVAL;
Matt Carlson7e5856b2009-02-25 14:23:01 +000010008
10009 if (cmd->autoneg == AUTONEG_DISABLE &&
10010 cmd->duplex != DUPLEX_FULL &&
10011 cmd->duplex != DUPLEX_HALF)
Michael Chan37ff2382005-10-26 15:49:51 -070010012 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010013
Matt Carlson7e5856b2009-02-25 14:23:01 +000010014 if (cmd->autoneg == AUTONEG_ENABLE) {
10015 u32 mask = ADVERTISED_Autoneg |
10016 ADVERTISED_Pause |
10017 ADVERTISED_Asym_Pause;
10018
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010019 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010020 mask |= ADVERTISED_1000baseT_Half |
10021 ADVERTISED_1000baseT_Full;
10022
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010023 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
Matt Carlson7e5856b2009-02-25 14:23:01 +000010024 mask |= ADVERTISED_100baseT_Half |
10025 ADVERTISED_100baseT_Full |
10026 ADVERTISED_10baseT_Half |
10027 ADVERTISED_10baseT_Full |
10028 ADVERTISED_TP;
10029 else
10030 mask |= ADVERTISED_FIBRE;
10031
10032 if (cmd->advertising & ~mask)
10033 return -EINVAL;
10034
10035 mask &= (ADVERTISED_1000baseT_Half |
10036 ADVERTISED_1000baseT_Full |
10037 ADVERTISED_100baseT_Half |
10038 ADVERTISED_100baseT_Full |
10039 ADVERTISED_10baseT_Half |
10040 ADVERTISED_10baseT_Full);
10041
10042 cmd->advertising &= mask;
10043 } else {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010044 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES) {
Matt Carlson7e5856b2009-02-25 14:23:01 +000010045 if (cmd->speed != SPEED_1000)
10046 return -EINVAL;
10047
10048 if (cmd->duplex != DUPLEX_FULL)
10049 return -EINVAL;
10050 } else {
10051 if (cmd->speed != SPEED_100 &&
10052 cmd->speed != SPEED_10)
10053 return -EINVAL;
10054 }
10055 }
10056
David S. Millerf47c11e2005-06-24 20:18:35 -070010057 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010058
10059 tp->link_config.autoneg = cmd->autoneg;
10060 if (cmd->autoneg == AUTONEG_ENABLE) {
Andy Gospodarek405d8e52007-10-08 01:08:47 -070010061 tp->link_config.advertising = (cmd->advertising |
10062 ADVERTISED_Autoneg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010063 tp->link_config.speed = SPEED_INVALID;
10064 tp->link_config.duplex = DUPLEX_INVALID;
10065 } else {
10066 tp->link_config.advertising = 0;
10067 tp->link_config.speed = cmd->speed;
10068 tp->link_config.duplex = cmd->duplex;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010069 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010070
Michael Chan24fcad62006-12-17 17:06:46 -080010071 tp->link_config.orig_speed = tp->link_config.speed;
10072 tp->link_config.orig_duplex = tp->link_config.duplex;
10073 tp->link_config.orig_autoneg = tp->link_config.autoneg;
10074
Linus Torvalds1da177e2005-04-16 15:20:36 -070010075 if (netif_running(dev))
10076 tg3_setup_phy(tp, 1);
10077
David S. Millerf47c11e2005-06-24 20:18:35 -070010078 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010079
Linus Torvalds1da177e2005-04-16 15:20:36 -070010080 return 0;
10081}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010082
Linus Torvalds1da177e2005-04-16 15:20:36 -070010083static void tg3_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10084{
10085 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010086
Linus Torvalds1da177e2005-04-16 15:20:36 -070010087 strcpy(info->driver, DRV_MODULE_NAME);
10088 strcpy(info->version, DRV_MODULE_VERSION);
Michael Chanc4e65752006-03-20 22:29:32 -080010089 strcpy(info->fw_version, tp->fw_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010090 strcpy(info->bus_info, pci_name(tp->pdev));
10091}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010092
Linus Torvalds1da177e2005-04-16 15:20:36 -070010093static void tg3_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10094{
10095 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010096
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010097 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
10098 device_can_wakeup(&tp->pdev->dev))
Gary Zambranoa85feb82007-05-05 11:52:19 -070010099 wol->supported = WAKE_MAGIC;
10100 else
10101 wol->supported = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010102 wol->wolopts = 0;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080010103 if ((tp->tg3_flags & TG3_FLAG_WOL_ENABLE) &&
10104 device_can_wakeup(&tp->pdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010105 wol->wolopts = WAKE_MAGIC;
10106 memset(&wol->sopass, 0, sizeof(wol->sopass));
10107}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010108
Linus Torvalds1da177e2005-04-16 15:20:36 -070010109static int tg3_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
10110{
10111 struct tg3 *tp = netdev_priv(dev);
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010112 struct device *dp = &tp->pdev->dev;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010113
Linus Torvalds1da177e2005-04-16 15:20:36 -070010114 if (wol->wolopts & ~WAKE_MAGIC)
10115 return -EINVAL;
10116 if ((wol->wolopts & WAKE_MAGIC) &&
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070010117 !((tp->tg3_flags & TG3_FLAG_WOL_CAP) && device_can_wakeup(dp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010118 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010119
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010120 device_set_wakeup_enable(dp, wol->wolopts & WAKE_MAGIC);
10121
David S. Millerf47c11e2005-06-24 20:18:35 -070010122 spin_lock_bh(&tp->lock);
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010123 if (device_may_wakeup(dp))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010124 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010125 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070010126 tp->tg3_flags &= ~TG3_FLAG_WOL_ENABLE;
David S. Millerf47c11e2005-06-24 20:18:35 -070010127 spin_unlock_bh(&tp->lock);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010128
Rafael J. Wysockif2dc0d12010-10-25 13:01:55 +000010129
Linus Torvalds1da177e2005-04-16 15:20:36 -070010130 return 0;
10131}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010132
Linus Torvalds1da177e2005-04-16 15:20:36 -070010133static u32 tg3_get_msglevel(struct net_device *dev)
10134{
10135 struct tg3 *tp = netdev_priv(dev);
10136 return tp->msg_enable;
10137}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010138
Linus Torvalds1da177e2005-04-16 15:20:36 -070010139static void tg3_set_msglevel(struct net_device *dev, u32 value)
10140{
10141 struct tg3 *tp = netdev_priv(dev);
10142 tp->msg_enable = value;
10143}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010144
Linus Torvalds1da177e2005-04-16 15:20:36 -070010145static int tg3_nway_reset(struct net_device *dev)
10146{
10147 struct tg3 *tp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010148 int r;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010149
Linus Torvalds1da177e2005-04-16 15:20:36 -070010150 if (!netif_running(dev))
10151 return -EAGAIN;
10152
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010153 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Michael Chanc94e3942005-09-27 12:12:42 -070010154 return -EINVAL;
10155
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010156 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010157 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010158 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000010159 r = phy_start_aneg(tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR]);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010160 } else {
10161 u32 bmcr;
10162
10163 spin_lock_bh(&tp->lock);
10164 r = -EINVAL;
10165 tg3_readphy(tp, MII_BMCR, &bmcr);
10166 if (!tg3_readphy(tp, MII_BMCR, &bmcr) &&
10167 ((bmcr & BMCR_ANENABLE) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010168 (tp->phy_flags & TG3_PHYFLG_PARALLEL_DETECT))) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010169 tg3_writephy(tp, MII_BMCR, bmcr | BMCR_ANRESTART |
10170 BMCR_ANENABLE);
10171 r = 0;
10172 }
10173 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010174 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010175
Linus Torvalds1da177e2005-04-16 15:20:36 -070010176 return r;
10177}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010178
Linus Torvalds1da177e2005-04-16 15:20:36 -070010179static void tg3_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10180{
10181 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010182
Matt Carlson2c49a442010-09-30 10:34:35 +000010183 ering->rx_max_pending = tp->rx_std_ring_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010184 ering->rx_mini_max_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010185 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
Matt Carlson2c49a442010-09-30 10:34:35 +000010186 ering->rx_jumbo_max_pending = tp->rx_jmb_ring_mask;
Michael Chan4f81c322006-03-20 21:33:42 -080010187 else
10188 ering->rx_jumbo_max_pending = 0;
10189
10190 ering->tx_max_pending = TG3_TX_RING_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010191
10192 ering->rx_pending = tp->rx_pending;
10193 ering->rx_mini_pending = 0;
Michael Chan4f81c322006-03-20 21:33:42 -080010194 if (tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE)
10195 ering->rx_jumbo_pending = tp->rx_jumbo_pending;
10196 else
10197 ering->rx_jumbo_pending = 0;
10198
Matt Carlsonf3f3f272009-08-28 14:03:21 +000010199 ering->tx_pending = tp->napi[0].tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010200}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010201
Linus Torvalds1da177e2005-04-16 15:20:36 -070010202static int tg3_set_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
10203{
10204 struct tg3 *tp = netdev_priv(dev);
Matt Carlson646c9ed2009-09-01 12:58:41 +000010205 int i, irq_sync = 0, err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010206
Matt Carlson2c49a442010-09-30 10:34:35 +000010207 if ((ering->rx_pending > tp->rx_std_ring_mask) ||
10208 (ering->rx_jumbo_pending > tp->rx_jmb_ring_mask) ||
Michael Chanbc3a9252006-10-18 20:55:18 -070010209 (ering->tx_pending > TG3_TX_RING_SIZE - 1) ||
10210 (ering->tx_pending <= MAX_SKB_FRAGS) ||
Michael Chan7f62ad52007-02-20 23:25:40 -080010211 ((tp->tg3_flags2 & TG3_FLG2_TSO_BUG) &&
Michael Chanbc3a9252006-10-18 20:55:18 -070010212 (ering->tx_pending <= (MAX_SKB_FRAGS * 3))))
Linus Torvalds1da177e2005-04-16 15:20:36 -070010213 return -EINVAL;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010214
Michael Chanbbe832c2005-06-24 20:20:04 -070010215 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010216 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010217 tg3_netif_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010218 irq_sync = 1;
10219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010220
Michael Chanbbe832c2005-06-24 20:20:04 -070010221 tg3_full_lock(tp, irq_sync);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010222
Linus Torvalds1da177e2005-04-16 15:20:36 -070010223 tp->rx_pending = ering->rx_pending;
10224
10225 if ((tp->tg3_flags2 & TG3_FLG2_MAX_RXPEND_64) &&
10226 tp->rx_pending > 63)
10227 tp->rx_pending = 63;
10228 tp->rx_jumbo_pending = ering->rx_jumbo_pending;
Matt Carlson646c9ed2009-09-01 12:58:41 +000010229
Matt Carlson6fd45cb2010-09-15 08:59:57 +000010230 for (i = 0; i < tp->irq_max; i++)
Matt Carlson646c9ed2009-09-01 12:58:41 +000010231 tp->napi[i].tx_pending = ering->tx_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010232
10233 if (netif_running(dev)) {
Michael Chan944d9802005-05-29 14:57:48 -070010234 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chanb9ec6c12006-07-25 16:37:27 -070010235 err = tg3_restart_hw(tp, 1);
10236 if (!err)
10237 tg3_netif_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070010238 }
10239
David S. Millerf47c11e2005-06-24 20:18:35 -070010240 tg3_full_unlock(tp);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010241
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010242 if (irq_sync && !err)
10243 tg3_phy_start(tp);
10244
Michael Chanb9ec6c12006-07-25 16:37:27 -070010245 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010246}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010247
Linus Torvalds1da177e2005-04-16 15:20:36 -070010248static void tg3_get_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10249{
10250 struct tg3 *tp = netdev_priv(dev);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010251
Linus Torvalds1da177e2005-04-16 15:20:36 -070010252 epause->autoneg = (tp->tg3_flags & TG3_FLAG_PAUSE_AUTONEG) != 0;
Matt Carlson8d018622007-12-20 20:05:44 -080010253
Steve Glendinninge18ce342008-12-16 02:00:00 -080010254 if (tp->link_config.active_flowctrl & FLOW_CTRL_RX)
Matt Carlson8d018622007-12-20 20:05:44 -080010255 epause->rx_pause = 1;
10256 else
10257 epause->rx_pause = 0;
10258
Steve Glendinninge18ce342008-12-16 02:00:00 -080010259 if (tp->link_config.active_flowctrl & FLOW_CTRL_TX)
Matt Carlson8d018622007-12-20 20:05:44 -080010260 epause->tx_pause = 1;
10261 else
10262 epause->tx_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010263}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010264
Linus Torvalds1da177e2005-04-16 15:20:36 -070010265static int tg3_set_pauseparam(struct net_device *dev, struct ethtool_pauseparam *epause)
10266{
10267 struct tg3 *tp = netdev_priv(dev);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010268 int err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010269
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010270 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson27121682010-02-17 15:16:57 +000010271 u32 newadv;
10272 struct phy_device *phydev;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010273
Matt Carlson27121682010-02-17 15:16:57 +000010274 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010275
Matt Carlson27121682010-02-17 15:16:57 +000010276 if (!(phydev->supported & SUPPORTED_Pause) ||
10277 (!(phydev->supported & SUPPORTED_Asym_Pause) &&
Nicolas Kaiser2259dca2010-10-07 23:29:27 +000010278 (epause->rx_pause != epause->tx_pause)))
Matt Carlson27121682010-02-17 15:16:57 +000010279 return -EINVAL;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010280
Matt Carlson27121682010-02-17 15:16:57 +000010281 tp->link_config.flowctrl = 0;
10282 if (epause->rx_pause) {
10283 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010284
Matt Carlson27121682010-02-17 15:16:57 +000010285 if (epause->tx_pause) {
Steve Glendinninge18ce342008-12-16 02:00:00 -080010286 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlson27121682010-02-17 15:16:57 +000010287 newadv = ADVERTISED_Pause;
10288 } else
10289 newadv = ADVERTISED_Pause |
10290 ADVERTISED_Asym_Pause;
10291 } else if (epause->tx_pause) {
10292 tp->link_config.flowctrl |= FLOW_CTRL_TX;
10293 newadv = ADVERTISED_Asym_Pause;
10294 } else
10295 newadv = 0;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010296
Matt Carlson27121682010-02-17 15:16:57 +000010297 if (epause->autoneg)
10298 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10299 else
10300 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10301
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010302 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson27121682010-02-17 15:16:57 +000010303 u32 oldadv = phydev->advertising &
10304 (ADVERTISED_Pause | ADVERTISED_Asym_Pause);
10305 if (oldadv != newadv) {
10306 phydev->advertising &=
10307 ~(ADVERTISED_Pause |
10308 ADVERTISED_Asym_Pause);
10309 phydev->advertising |= newadv;
10310 if (phydev->autoneg) {
10311 /*
10312 * Always renegotiate the link to
10313 * inform our link partner of our
10314 * flow control settings, even if the
10315 * flow control is forced. Let
10316 * tg3_adjust_link() do the final
10317 * flow control setup.
10318 */
10319 return phy_start_aneg(phydev);
10320 }
10321 }
10322
10323 if (!epause->autoneg)
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010324 tg3_setup_flow_control(tp, 0, 0);
Matt Carlson27121682010-02-17 15:16:57 +000010325 } else {
10326 tp->link_config.orig_advertising &=
10327 ~(ADVERTISED_Pause |
10328 ADVERTISED_Asym_Pause);
10329 tp->link_config.orig_advertising |= newadv;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010330 }
10331 } else {
10332 int irq_sync = 0;
10333
10334 if (netif_running(dev)) {
10335 tg3_netif_stop(tp);
10336 irq_sync = 1;
10337 }
10338
10339 tg3_full_lock(tp, irq_sync);
10340
10341 if (epause->autoneg)
10342 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
10343 else
10344 tp->tg3_flags &= ~TG3_FLAG_PAUSE_AUTONEG;
10345 if (epause->rx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010346 tp->link_config.flowctrl |= FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010347 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010348 tp->link_config.flowctrl &= ~FLOW_CTRL_RX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010349 if (epause->tx_pause)
Steve Glendinninge18ce342008-12-16 02:00:00 -080010350 tp->link_config.flowctrl |= FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010351 else
Steve Glendinninge18ce342008-12-16 02:00:00 -080010352 tp->link_config.flowctrl &= ~FLOW_CTRL_TX;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070010353
10354 if (netif_running(dev)) {
10355 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
10356 err = tg3_restart_hw(tp, 1);
10357 if (!err)
10358 tg3_netif_start(tp);
10359 }
10360
10361 tg3_full_unlock(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070010362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070010363
Michael Chanb9ec6c12006-07-25 16:37:27 -070010364 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010365}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010366
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010367static int tg3_get_sset_count(struct net_device *dev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010368{
Jeff Garzikb9f2c042007-10-03 18:07:32 -070010369 switch (sset) {
10370 case ETH_SS_TEST:
10371 return TG3_NUM_TEST;
10372 case ETH_SS_STATS:
10373 return TG3_NUM_STATS;
10374 default:
10375 return -EOPNOTSUPP;
10376 }
Michael Chan4cafd3f2005-05-29 14:56:34 -070010377}
10378
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010379static void tg3_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070010380{
10381 switch (stringset) {
10382 case ETH_SS_STATS:
10383 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
10384 break;
Michael Chan4cafd3f2005-05-29 14:56:34 -070010385 case ETH_SS_TEST:
10386 memcpy(buf, &ethtool_test_keys, sizeof(ethtool_test_keys));
10387 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070010388 default:
10389 WARN_ON(1); /* we need a WARN() */
10390 break;
10391 }
10392}
10393
stephen hemminger81b87092011-04-04 08:43:50 +000010394static int tg3_set_phys_id(struct net_device *dev,
10395 enum ethtool_phys_id_state state)
Michael Chan4009a932005-09-05 17:52:54 -070010396{
10397 struct tg3 *tp = netdev_priv(dev);
Michael Chan4009a932005-09-05 17:52:54 -070010398
10399 if (!netif_running(tp->dev))
10400 return -EAGAIN;
10401
stephen hemminger81b87092011-04-04 08:43:50 +000010402 switch (state) {
10403 case ETHTOOL_ID_ACTIVE:
Allan, Bruce Wfce55922011-04-13 13:09:10 +000010404 return 1; /* cycle on/off once per second */
Michael Chan4009a932005-09-05 17:52:54 -070010405
stephen hemminger81b87092011-04-04 08:43:50 +000010406 case ETHTOOL_ID_ON:
10407 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10408 LED_CTRL_1000MBPS_ON |
10409 LED_CTRL_100MBPS_ON |
10410 LED_CTRL_10MBPS_ON |
10411 LED_CTRL_TRAFFIC_OVERRIDE |
10412 LED_CTRL_TRAFFIC_BLINK |
10413 LED_CTRL_TRAFFIC_LED);
10414 break;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010415
stephen hemminger81b87092011-04-04 08:43:50 +000010416 case ETHTOOL_ID_OFF:
10417 tw32(MAC_LED_CTRL, LED_CTRL_LNKLED_OVERRIDE |
10418 LED_CTRL_TRAFFIC_OVERRIDE);
10419 break;
Michael Chan4009a932005-09-05 17:52:54 -070010420
stephen hemminger81b87092011-04-04 08:43:50 +000010421 case ETHTOOL_ID_INACTIVE:
10422 tw32(MAC_LED_CTRL, tp->led_ctrl);
10423 break;
Michael Chan4009a932005-09-05 17:52:54 -070010424 }
stephen hemminger81b87092011-04-04 08:43:50 +000010425
Michael Chan4009a932005-09-05 17:52:54 -070010426 return 0;
10427}
10428
Matt Carlsonde6f31e2010-04-12 06:58:30 +000010429static void tg3_get_ethtool_stats(struct net_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010430 struct ethtool_stats *estats, u64 *tmp_stats)
10431{
10432 struct tg3 *tp = netdev_priv(dev);
10433 memcpy(tmp_stats, tg3_get_estats(tp), sizeof(tp->estats));
10434}
10435
Matt Carlsonc3e94502011-04-13 11:05:08 +000010436static __be32 * tg3_vpd_readblock(struct tg3 *tp)
10437{
10438 int i;
10439 __be32 *buf;
10440 u32 offset = 0, len = 0;
10441 u32 magic, val;
10442
10443 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
10444 tg3_nvram_read(tp, 0, &magic))
10445 return NULL;
10446
10447 if (magic == TG3_EEPROM_MAGIC) {
10448 for (offset = TG3_NVM_DIR_START;
10449 offset < TG3_NVM_DIR_END;
10450 offset += TG3_NVM_DIRENT_SIZE) {
10451 if (tg3_nvram_read(tp, offset, &val))
10452 return NULL;
10453
10454 if ((val >> TG3_NVM_DIRTYPE_SHIFT) ==
10455 TG3_NVM_DIRTYPE_EXTVPD)
10456 break;
10457 }
10458
10459 if (offset != TG3_NVM_DIR_END) {
10460 len = (val & TG3_NVM_DIRTYPE_LENMSK) * 4;
10461 if (tg3_nvram_read(tp, offset + 4, &offset))
10462 return NULL;
10463
10464 offset = tg3_nvram_logical_addr(tp, offset);
10465 }
10466 }
10467
10468 if (!offset || !len) {
10469 offset = TG3_NVM_VPD_OFF;
10470 len = TG3_NVM_VPD_LEN;
10471 }
10472
10473 buf = kmalloc(len, GFP_KERNEL);
10474 if (buf == NULL)
10475 return NULL;
10476
10477 if (magic == TG3_EEPROM_MAGIC) {
10478 for (i = 0; i < len; i += 4) {
10479 /* The data is in little-endian format in NVRAM.
10480 * Use the big-endian read routines to preserve
10481 * the byte order as it exists in NVRAM.
10482 */
10483 if (tg3_nvram_read_be32(tp, offset + i, &buf[i/4]))
10484 goto error;
10485 }
10486 } else {
10487 u8 *ptr;
10488 ssize_t cnt;
10489 unsigned int pos = 0;
10490
10491 ptr = (u8 *)&buf[0];
10492 for (i = 0; pos < len && i < 3; i++, pos += cnt, ptr += cnt) {
10493 cnt = pci_read_vpd(tp->pdev, pos,
10494 len - pos, ptr);
10495 if (cnt == -ETIMEDOUT || cnt == -EINTR)
10496 cnt = 0;
10497 else if (cnt < 0)
10498 goto error;
10499 }
10500 if (pos != len)
10501 goto error;
10502 }
10503
10504 return buf;
10505
10506error:
10507 kfree(buf);
10508 return NULL;
10509}
10510
Michael Chan566f86a2005-05-29 14:56:58 -070010511#define NVRAM_TEST_SIZE 0x100
Matt Carlsona5767de2007-11-12 21:10:58 -080010512#define NVRAM_SELFBOOT_FORMAT1_0_SIZE 0x14
10513#define NVRAM_SELFBOOT_FORMAT1_2_SIZE 0x18
10514#define NVRAM_SELFBOOT_FORMAT1_3_SIZE 0x1c
Michael Chanb16250e2006-09-27 16:10:14 -070010515#define NVRAM_SELFBOOT_HW_SIZE 0x20
10516#define NVRAM_SELFBOOT_DATA_SIZE 0x1c
Michael Chan566f86a2005-05-29 14:56:58 -070010517
10518static int tg3_test_nvram(struct tg3 *tp)
10519{
Al Virob9fc7dc2007-12-17 22:59:57 -080010520 u32 csum, magic;
Matt Carlsona9dc5292009-02-25 14:25:30 +000010521 __be32 *buf;
Andy Gospodarekab0049b2007-09-06 20:42:14 +010010522 int i, j, k, err = 0, size;
Michael Chan566f86a2005-05-29 14:56:58 -070010523
Matt Carlsondf259d82009-04-20 06:57:14 +000010524 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM)
10525 return 0;
10526
Matt Carlsone4f34112009-02-25 14:25:00 +000010527 if (tg3_nvram_read(tp, 0, &magic) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080010528 return -EIO;
10529
Michael Chan1b277772006-03-20 22:27:48 -080010530 if (magic == TG3_EEPROM_MAGIC)
10531 size = NVRAM_TEST_SIZE;
Michael Chanb16250e2006-09-27 16:10:14 -070010532 else if ((magic & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW) {
Matt Carlsona5767de2007-11-12 21:10:58 -080010533 if ((magic & TG3_EEPROM_SB_FORMAT_MASK) ==
10534 TG3_EEPROM_SB_FORMAT_1) {
10535 switch (magic & TG3_EEPROM_SB_REVISION_MASK) {
10536 case TG3_EEPROM_SB_REVISION_0:
10537 size = NVRAM_SELFBOOT_FORMAT1_0_SIZE;
10538 break;
10539 case TG3_EEPROM_SB_REVISION_2:
10540 size = NVRAM_SELFBOOT_FORMAT1_2_SIZE;
10541 break;
10542 case TG3_EEPROM_SB_REVISION_3:
10543 size = NVRAM_SELFBOOT_FORMAT1_3_SIZE;
10544 break;
10545 default:
10546 return 0;
10547 }
10548 } else
Michael Chan1b277772006-03-20 22:27:48 -080010549 return 0;
Michael Chanb16250e2006-09-27 16:10:14 -070010550 } else if ((magic & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
10551 size = NVRAM_SELFBOOT_HW_SIZE;
10552 else
Michael Chan1b277772006-03-20 22:27:48 -080010553 return -EIO;
10554
10555 buf = kmalloc(size, GFP_KERNEL);
Michael Chan566f86a2005-05-29 14:56:58 -070010556 if (buf == NULL)
10557 return -ENOMEM;
10558
Michael Chan1b277772006-03-20 22:27:48 -080010559 err = -EIO;
10560 for (i = 0, j = 0; i < size; i += 4, j++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000010561 err = tg3_nvram_read_be32(tp, i, &buf[j]);
10562 if (err)
Michael Chan566f86a2005-05-29 14:56:58 -070010563 break;
Michael Chan566f86a2005-05-29 14:56:58 -070010564 }
Michael Chan1b277772006-03-20 22:27:48 -080010565 if (i < size)
Michael Chan566f86a2005-05-29 14:56:58 -070010566 goto out;
10567
Michael Chan1b277772006-03-20 22:27:48 -080010568 /* Selfboot format */
Matt Carlsona9dc5292009-02-25 14:25:30 +000010569 magic = be32_to_cpu(buf[0]);
Al Virob9fc7dc2007-12-17 22:59:57 -080010570 if ((magic & TG3_EEPROM_MAGIC_FW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010571 TG3_EEPROM_MAGIC_FW) {
Michael Chan1b277772006-03-20 22:27:48 -080010572 u8 *buf8 = (u8 *) buf, csum8 = 0;
10573
Al Virob9fc7dc2007-12-17 22:59:57 -080010574 if ((magic & TG3_EEPROM_SB_REVISION_MASK) ==
Matt Carlsona5767de2007-11-12 21:10:58 -080010575 TG3_EEPROM_SB_REVISION_2) {
10576 /* For rev 2, the csum doesn't include the MBA. */
10577 for (i = 0; i < TG3_EEPROM_SB_F1R2_MBA_OFF; i++)
10578 csum8 += buf8[i];
10579 for (i = TG3_EEPROM_SB_F1R2_MBA_OFF + 4; i < size; i++)
10580 csum8 += buf8[i];
10581 } else {
10582 for (i = 0; i < size; i++)
10583 csum8 += buf8[i];
10584 }
Michael Chan1b277772006-03-20 22:27:48 -080010585
Adrian Bunkad96b482006-04-05 22:21:04 -070010586 if (csum8 == 0) {
10587 err = 0;
10588 goto out;
10589 }
10590
10591 err = -EIO;
10592 goto out;
Michael Chan1b277772006-03-20 22:27:48 -080010593 }
Michael Chan566f86a2005-05-29 14:56:58 -070010594
Al Virob9fc7dc2007-12-17 22:59:57 -080010595 if ((magic & TG3_EEPROM_MAGIC_HW_MSK) ==
Michael Chanb16250e2006-09-27 16:10:14 -070010596 TG3_EEPROM_MAGIC_HW) {
10597 u8 data[NVRAM_SELFBOOT_DATA_SIZE];
Matt Carlsona9dc5292009-02-25 14:25:30 +000010598 u8 parity[NVRAM_SELFBOOT_DATA_SIZE];
Michael Chanb16250e2006-09-27 16:10:14 -070010599 u8 *buf8 = (u8 *) buf;
Michael Chanb16250e2006-09-27 16:10:14 -070010600
10601 /* Separate the parity bits and the data bytes. */
10602 for (i = 0, j = 0, k = 0; i < NVRAM_SELFBOOT_HW_SIZE; i++) {
10603 if ((i == 0) || (i == 8)) {
10604 int l;
10605 u8 msk;
10606
10607 for (l = 0, msk = 0x80; l < 7; l++, msk >>= 1)
10608 parity[k++] = buf8[i] & msk;
10609 i++;
Matt Carlson859a5882010-04-05 10:19:28 +000010610 } else if (i == 16) {
Michael Chanb16250e2006-09-27 16:10:14 -070010611 int l;
10612 u8 msk;
10613
10614 for (l = 0, msk = 0x20; l < 6; l++, msk >>= 1)
10615 parity[k++] = buf8[i] & msk;
10616 i++;
10617
10618 for (l = 0, msk = 0x80; l < 8; l++, msk >>= 1)
10619 parity[k++] = buf8[i] & msk;
10620 i++;
10621 }
10622 data[j++] = buf8[i];
10623 }
10624
10625 err = -EIO;
10626 for (i = 0; i < NVRAM_SELFBOOT_DATA_SIZE; i++) {
10627 u8 hw8 = hweight8(data[i]);
10628
10629 if ((hw8 & 0x1) && parity[i])
10630 goto out;
10631 else if (!(hw8 & 0x1) && !parity[i])
10632 goto out;
10633 }
10634 err = 0;
10635 goto out;
10636 }
10637
Matt Carlson01c3a392011-03-09 16:58:20 +000010638 err = -EIO;
10639
Michael Chan566f86a2005-05-29 14:56:58 -070010640 /* Bootstrap checksum at offset 0x10 */
10641 csum = calc_crc((unsigned char *) buf, 0x10);
Matt Carlson01c3a392011-03-09 16:58:20 +000010642 if (csum != le32_to_cpu(buf[0x10/4]))
Michael Chan566f86a2005-05-29 14:56:58 -070010643 goto out;
10644
10645 /* Manufacturing block starts at offset 0x74, checksum at 0xfc */
10646 csum = calc_crc((unsigned char *) &buf[0x74/4], 0x88);
Matt Carlson01c3a392011-03-09 16:58:20 +000010647 if (csum != le32_to_cpu(buf[0xfc/4]))
Matt Carlsona9dc5292009-02-25 14:25:30 +000010648 goto out;
Michael Chan566f86a2005-05-29 14:56:58 -070010649
Matt Carlsonc3e94502011-04-13 11:05:08 +000010650 kfree(buf);
10651
10652 buf = tg3_vpd_readblock(tp);
10653 if (!buf)
10654 return -ENOMEM;
Matt Carlsond4894f32011-03-09 16:58:21 +000010655
10656 i = pci_vpd_find_tag((u8 *)buf, 0, TG3_NVM_VPD_LEN,
10657 PCI_VPD_LRDT_RO_DATA);
10658 if (i > 0) {
10659 j = pci_vpd_lrdt_size(&((u8 *)buf)[i]);
10660 if (j < 0)
10661 goto out;
10662
10663 if (i + PCI_VPD_LRDT_TAG_SIZE + j > TG3_NVM_VPD_LEN)
10664 goto out;
10665
10666 i += PCI_VPD_LRDT_TAG_SIZE;
10667 j = pci_vpd_find_info_keyword((u8 *)buf, i, j,
10668 PCI_VPD_RO_KEYWORD_CHKSUM);
10669 if (j > 0) {
10670 u8 csum8 = 0;
10671
10672 j += PCI_VPD_INFO_FLD_HDR_SIZE;
10673
10674 for (i = 0; i <= j; i++)
10675 csum8 += ((u8 *)buf)[i];
10676
10677 if (csum8)
10678 goto out;
10679 }
10680 }
10681
Michael Chan566f86a2005-05-29 14:56:58 -070010682 err = 0;
10683
10684out:
10685 kfree(buf);
10686 return err;
10687}
10688
Michael Chanca430072005-05-29 14:57:23 -070010689#define TG3_SERDES_TIMEOUT_SEC 2
10690#define TG3_COPPER_TIMEOUT_SEC 6
10691
10692static int tg3_test_link(struct tg3 *tp)
10693{
10694 int i, max;
10695
10696 if (!netif_running(tp->dev))
10697 return -ENODEV;
10698
Matt Carlsonf07e9af2010-08-02 11:26:07 +000010699 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
Michael Chanca430072005-05-29 14:57:23 -070010700 max = TG3_SERDES_TIMEOUT_SEC;
10701 else
10702 max = TG3_COPPER_TIMEOUT_SEC;
10703
10704 for (i = 0; i < max; i++) {
10705 if (netif_carrier_ok(tp->dev))
10706 return 0;
10707
10708 if (msleep_interruptible(1000))
10709 break;
10710 }
10711
10712 return -EIO;
10713}
10714
Michael Chana71116d2005-05-29 14:58:11 -070010715/* Only test the commonly used registers */
David S. Miller30ca3e32006-03-20 23:02:36 -080010716static int tg3_test_registers(struct tg3 *tp)
Michael Chana71116d2005-05-29 14:58:11 -070010717{
Michael Chanb16250e2006-09-27 16:10:14 -070010718 int i, is_5705, is_5750;
Michael Chana71116d2005-05-29 14:58:11 -070010719 u32 offset, read_mask, write_mask, val, save_val, read_val;
10720 static struct {
10721 u16 offset;
10722 u16 flags;
10723#define TG3_FL_5705 0x1
10724#define TG3_FL_NOT_5705 0x2
10725#define TG3_FL_NOT_5788 0x4
Michael Chanb16250e2006-09-27 16:10:14 -070010726#define TG3_FL_NOT_5750 0x8
Michael Chana71116d2005-05-29 14:58:11 -070010727 u32 read_mask;
10728 u32 write_mask;
10729 } reg_tbl[] = {
10730 /* MAC Control Registers */
10731 { MAC_MODE, TG3_FL_NOT_5705,
10732 0x00000000, 0x00ef6f8c },
10733 { MAC_MODE, TG3_FL_5705,
10734 0x00000000, 0x01ef6b8c },
10735 { MAC_STATUS, TG3_FL_NOT_5705,
10736 0x03800107, 0x00000000 },
10737 { MAC_STATUS, TG3_FL_5705,
10738 0x03800100, 0x00000000 },
10739 { MAC_ADDR_0_HIGH, 0x0000,
10740 0x00000000, 0x0000ffff },
10741 { MAC_ADDR_0_LOW, 0x0000,
Matt Carlsonc6cdf432010-04-05 10:19:26 +000010742 0x00000000, 0xffffffff },
Michael Chana71116d2005-05-29 14:58:11 -070010743 { MAC_RX_MTU_SIZE, 0x0000,
10744 0x00000000, 0x0000ffff },
10745 { MAC_TX_MODE, 0x0000,
10746 0x00000000, 0x00000070 },
10747 { MAC_TX_LENGTHS, 0x0000,
10748 0x00000000, 0x00003fff },
10749 { MAC_RX_MODE, TG3_FL_NOT_5705,
10750 0x00000000, 0x000007fc },
10751 { MAC_RX_MODE, TG3_FL_5705,
10752 0x00000000, 0x000007dc },
10753 { MAC_HASH_REG_0, 0x0000,
10754 0x00000000, 0xffffffff },
10755 { MAC_HASH_REG_1, 0x0000,
10756 0x00000000, 0xffffffff },
10757 { MAC_HASH_REG_2, 0x0000,
10758 0x00000000, 0xffffffff },
10759 { MAC_HASH_REG_3, 0x0000,
10760 0x00000000, 0xffffffff },
10761
10762 /* Receive Data and Receive BD Initiator Control Registers. */
10763 { RCVDBDI_JUMBO_BD+0, TG3_FL_NOT_5705,
10764 0x00000000, 0xffffffff },
10765 { RCVDBDI_JUMBO_BD+4, TG3_FL_NOT_5705,
10766 0x00000000, 0xffffffff },
10767 { RCVDBDI_JUMBO_BD+8, TG3_FL_NOT_5705,
10768 0x00000000, 0x00000003 },
10769 { RCVDBDI_JUMBO_BD+0xc, TG3_FL_NOT_5705,
10770 0x00000000, 0xffffffff },
10771 { RCVDBDI_STD_BD+0, 0x0000,
10772 0x00000000, 0xffffffff },
10773 { RCVDBDI_STD_BD+4, 0x0000,
10774 0x00000000, 0xffffffff },
10775 { RCVDBDI_STD_BD+8, 0x0000,
10776 0x00000000, 0xffff0002 },
10777 { RCVDBDI_STD_BD+0xc, 0x0000,
10778 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010779
Michael Chana71116d2005-05-29 14:58:11 -070010780 /* Receive BD Initiator Control Registers. */
10781 { RCVBDI_STD_THRESH, TG3_FL_NOT_5705,
10782 0x00000000, 0xffffffff },
10783 { RCVBDI_STD_THRESH, TG3_FL_5705,
10784 0x00000000, 0x000003ff },
10785 { RCVBDI_JUMBO_THRESH, TG3_FL_NOT_5705,
10786 0x00000000, 0xffffffff },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010787
Michael Chana71116d2005-05-29 14:58:11 -070010788 /* Host Coalescing Control Registers. */
10789 { HOSTCC_MODE, TG3_FL_NOT_5705,
10790 0x00000000, 0x00000004 },
10791 { HOSTCC_MODE, TG3_FL_5705,
10792 0x00000000, 0x000000f6 },
10793 { HOSTCC_RXCOL_TICKS, TG3_FL_NOT_5705,
10794 0x00000000, 0xffffffff },
10795 { HOSTCC_RXCOL_TICKS, TG3_FL_5705,
10796 0x00000000, 0x000003ff },
10797 { HOSTCC_TXCOL_TICKS, TG3_FL_NOT_5705,
10798 0x00000000, 0xffffffff },
10799 { HOSTCC_TXCOL_TICKS, TG3_FL_5705,
10800 0x00000000, 0x000003ff },
10801 { HOSTCC_RXMAX_FRAMES, TG3_FL_NOT_5705,
10802 0x00000000, 0xffffffff },
10803 { HOSTCC_RXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10804 0x00000000, 0x000000ff },
10805 { HOSTCC_TXMAX_FRAMES, TG3_FL_NOT_5705,
10806 0x00000000, 0xffffffff },
10807 { HOSTCC_TXMAX_FRAMES, TG3_FL_5705 | TG3_FL_NOT_5788,
10808 0x00000000, 0x000000ff },
10809 { HOSTCC_RXCOAL_TICK_INT, TG3_FL_NOT_5705,
10810 0x00000000, 0xffffffff },
10811 { HOSTCC_TXCOAL_TICK_INT, TG3_FL_NOT_5705,
10812 0x00000000, 0xffffffff },
10813 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10814 0x00000000, 0xffffffff },
10815 { HOSTCC_RXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10816 0x00000000, 0x000000ff },
10817 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_NOT_5705,
10818 0x00000000, 0xffffffff },
10819 { HOSTCC_TXCOAL_MAXF_INT, TG3_FL_5705 | TG3_FL_NOT_5788,
10820 0x00000000, 0x000000ff },
10821 { HOSTCC_STAT_COAL_TICKS, TG3_FL_NOT_5705,
10822 0x00000000, 0xffffffff },
10823 { HOSTCC_STATS_BLK_HOST_ADDR, TG3_FL_NOT_5705,
10824 0x00000000, 0xffffffff },
10825 { HOSTCC_STATS_BLK_HOST_ADDR+4, TG3_FL_NOT_5705,
10826 0x00000000, 0xffffffff },
10827 { HOSTCC_STATUS_BLK_HOST_ADDR, 0x0000,
10828 0x00000000, 0xffffffff },
10829 { HOSTCC_STATUS_BLK_HOST_ADDR+4, 0x0000,
10830 0x00000000, 0xffffffff },
10831 { HOSTCC_STATS_BLK_NIC_ADDR, 0x0000,
10832 0xffffffff, 0x00000000 },
10833 { HOSTCC_STATUS_BLK_NIC_ADDR, 0x0000,
10834 0xffffffff, 0x00000000 },
10835
10836 /* Buffer Manager Control Registers. */
Michael Chanb16250e2006-09-27 16:10:14 -070010837 { BUFMGR_MB_POOL_ADDR, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010838 0x00000000, 0x007fff80 },
Michael Chanb16250e2006-09-27 16:10:14 -070010839 { BUFMGR_MB_POOL_SIZE, TG3_FL_NOT_5750,
Michael Chana71116d2005-05-29 14:58:11 -070010840 0x00000000, 0x007fffff },
10841 { BUFMGR_MB_RDMA_LOW_WATER, 0x0000,
10842 0x00000000, 0x0000003f },
10843 { BUFMGR_MB_MACRX_LOW_WATER, 0x0000,
10844 0x00000000, 0x000001ff },
10845 { BUFMGR_MB_HIGH_WATER, 0x0000,
10846 0x00000000, 0x000001ff },
10847 { BUFMGR_DMA_DESC_POOL_ADDR, TG3_FL_NOT_5705,
10848 0xffffffff, 0x00000000 },
10849 { BUFMGR_DMA_DESC_POOL_SIZE, TG3_FL_NOT_5705,
10850 0xffffffff, 0x00000000 },
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010851
Michael Chana71116d2005-05-29 14:58:11 -070010852 /* Mailbox Registers */
10853 { GRCMBOX_RCVSTD_PROD_IDX+4, 0x0000,
10854 0x00000000, 0x000001ff },
10855 { GRCMBOX_RCVJUMBO_PROD_IDX+4, TG3_FL_NOT_5705,
10856 0x00000000, 0x000001ff },
10857 { GRCMBOX_RCVRET_CON_IDX_0+4, 0x0000,
10858 0x00000000, 0x000007ff },
10859 { GRCMBOX_SNDHOST_PROD_IDX_0+4, 0x0000,
10860 0x00000000, 0x000001ff },
10861
10862 { 0xffff, 0x0000, 0x00000000, 0x00000000 },
10863 };
10864
Michael Chanb16250e2006-09-27 16:10:14 -070010865 is_5705 = is_5750 = 0;
10866 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chana71116d2005-05-29 14:58:11 -070010867 is_5705 = 1;
Michael Chanb16250e2006-09-27 16:10:14 -070010868 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
10869 is_5750 = 1;
10870 }
Michael Chana71116d2005-05-29 14:58:11 -070010871
10872 for (i = 0; reg_tbl[i].offset != 0xffff; i++) {
10873 if (is_5705 && (reg_tbl[i].flags & TG3_FL_NOT_5705))
10874 continue;
10875
10876 if (!is_5705 && (reg_tbl[i].flags & TG3_FL_5705))
10877 continue;
10878
10879 if ((tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
10880 (reg_tbl[i].flags & TG3_FL_NOT_5788))
10881 continue;
10882
Michael Chanb16250e2006-09-27 16:10:14 -070010883 if (is_5750 && (reg_tbl[i].flags & TG3_FL_NOT_5750))
10884 continue;
10885
Michael Chana71116d2005-05-29 14:58:11 -070010886 offset = (u32) reg_tbl[i].offset;
10887 read_mask = reg_tbl[i].read_mask;
10888 write_mask = reg_tbl[i].write_mask;
10889
10890 /* Save the original register content */
10891 save_val = tr32(offset);
10892
10893 /* Determine the read-only value. */
10894 read_val = save_val & read_mask;
10895
10896 /* Write zero to the register, then make sure the read-only bits
10897 * are not changed and the read/write bits are all zeros.
10898 */
10899 tw32(offset, 0);
10900
10901 val = tr32(offset);
10902
10903 /* Test the read-only and read/write bits. */
10904 if (((val & read_mask) != read_val) || (val & write_mask))
10905 goto out;
10906
10907 /* Write ones to all the bits defined by RdMask and WrMask, then
10908 * make sure the read-only bits are not changed and the
10909 * read/write bits are all ones.
10910 */
10911 tw32(offset, read_mask | write_mask);
10912
10913 val = tr32(offset);
10914
10915 /* Test the read-only bits. */
10916 if ((val & read_mask) != read_val)
10917 goto out;
10918
10919 /* Test the read/write bits. */
10920 if ((val & write_mask) != write_mask)
10921 goto out;
10922
10923 tw32(offset, save_val);
10924 }
10925
10926 return 0;
10927
10928out:
Michael Chan9f88f292006-12-07 00:22:54 -080010929 if (netif_msg_hw(tp))
Matt Carlson2445e462010-04-05 10:19:21 +000010930 netdev_err(tp->dev,
10931 "Register test failed at offset %x\n", offset);
Michael Chana71116d2005-05-29 14:58:11 -070010932 tw32(offset, save_val);
10933 return -EIO;
10934}
10935
Michael Chan7942e1d2005-05-29 14:58:36 -070010936static int tg3_do_mem_test(struct tg3 *tp, u32 offset, u32 len)
10937{
Arjan van de Venf71e1302006-03-03 21:33:57 -050010938 static const u32 test_pattern[] = { 0x00000000, 0xffffffff, 0xaa55a55a };
Michael Chan7942e1d2005-05-29 14:58:36 -070010939 int i;
10940 u32 j;
10941
Alejandro Martinez Ruize9edda62007-10-15 03:37:43 +020010942 for (i = 0; i < ARRAY_SIZE(test_pattern); i++) {
Michael Chan7942e1d2005-05-29 14:58:36 -070010943 for (j = 0; j < len; j += 4) {
10944 u32 val;
10945
10946 tg3_write_mem(tp, offset + j, test_pattern[i]);
10947 tg3_read_mem(tp, offset + j, &val);
10948 if (val != test_pattern[i])
10949 return -EIO;
10950 }
10951 }
10952 return 0;
10953}
10954
10955static int tg3_test_memory(struct tg3 *tp)
10956{
10957 static struct mem_entry {
10958 u32 offset;
10959 u32 len;
10960 } mem_tbl_570x[] = {
Michael Chan38690192005-12-19 16:27:28 -080010961 { 0x00000000, 0x00b50},
Michael Chan7942e1d2005-05-29 14:58:36 -070010962 { 0x00002000, 0x1c000},
10963 { 0xffffffff, 0x00000}
10964 }, mem_tbl_5705[] = {
10965 { 0x00000100, 0x0000c},
10966 { 0x00000200, 0x00008},
Michael Chan7942e1d2005-05-29 14:58:36 -070010967 { 0x00004000, 0x00800},
10968 { 0x00006000, 0x01000},
10969 { 0x00008000, 0x02000},
10970 { 0x00010000, 0x0e000},
10971 { 0xffffffff, 0x00000}
Michael Chan79f4d132006-03-20 22:28:57 -080010972 }, mem_tbl_5755[] = {
10973 { 0x00000200, 0x00008},
10974 { 0x00004000, 0x00800},
10975 { 0x00006000, 0x00800},
10976 { 0x00008000, 0x02000},
10977 { 0x00010000, 0x0c000},
10978 { 0xffffffff, 0x00000}
Michael Chanb16250e2006-09-27 16:10:14 -070010979 }, mem_tbl_5906[] = {
10980 { 0x00000200, 0x00008},
10981 { 0x00004000, 0x00400},
10982 { 0x00006000, 0x00400},
10983 { 0x00008000, 0x01000},
10984 { 0x00010000, 0x01000},
10985 { 0xffffffff, 0x00000}
Matt Carlson8b5a6c42010-01-20 16:58:06 +000010986 }, mem_tbl_5717[] = {
10987 { 0x00000200, 0x00008},
10988 { 0x00010000, 0x0a000},
10989 { 0x00020000, 0x13c00},
10990 { 0xffffffff, 0x00000}
10991 }, mem_tbl_57765[] = {
10992 { 0x00000200, 0x00008},
10993 { 0x00004000, 0x00800},
10994 { 0x00006000, 0x09800},
10995 { 0x00010000, 0x0a000},
10996 { 0xffffffff, 0x00000}
Michael Chan7942e1d2005-05-29 14:58:36 -070010997 };
10998 struct mem_entry *mem_tbl;
10999 int err = 0;
11000 int i;
11001
Matt Carlson0a58d662011-04-05 14:22:45 +000011002 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlson8b5a6c42010-01-20 16:58:06 +000011003 mem_tbl = mem_tbl_5717;
11004 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
11005 mem_tbl = mem_tbl_57765;
11006 else if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Matt Carlson321d32a2008-11-21 17:22:19 -080011007 mem_tbl = mem_tbl_5755;
11008 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
11009 mem_tbl = mem_tbl_5906;
11010 else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS)
11011 mem_tbl = mem_tbl_5705;
11012 else
Michael Chan7942e1d2005-05-29 14:58:36 -070011013 mem_tbl = mem_tbl_570x;
11014
11015 for (i = 0; mem_tbl[i].offset != 0xffffffff; i++) {
Matt Carlsonbe98da62010-07-11 09:31:46 +000011016 err = tg3_do_mem_test(tp, mem_tbl[i].offset, mem_tbl[i].len);
11017 if (err)
Michael Chan7942e1d2005-05-29 14:58:36 -070011018 break;
11019 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011020
Michael Chan7942e1d2005-05-29 14:58:36 -070011021 return err;
11022}
11023
Michael Chan9f40dea2005-09-05 17:53:06 -070011024#define TG3_MAC_LOOPBACK 0
11025#define TG3_PHY_LOOPBACK 1
11026
Matt Carlson4852a862011-04-13 11:05:07 +000011027static int tg3_run_loopback(struct tg3 *tp, u32 pktsz, int loopback_mode)
Michael Chanc76949a2005-05-29 14:58:59 -070011028{
Michael Chan9f40dea2005-09-05 17:53:06 -070011029 u32 mac_mode, rx_start_idx, rx_idx, tx_idx, opaque_key;
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011030 u32 desc_idx, coal_now;
Michael Chanc76949a2005-05-29 14:58:59 -070011031 struct sk_buff *skb, *rx_skb;
11032 u8 *tx_data;
11033 dma_addr_t map;
11034 int num_pkts, tx_len, rx_len, i, err;
11035 struct tg3_rx_buffer_desc *desc;
Matt Carlson898a56f2009-08-28 14:02:40 +000011036 struct tg3_napi *tnapi, *rnapi;
Matt Carlson8fea32b2010-09-15 08:59:58 +000011037 struct tg3_rx_prodring_set *tpr = &tp->napi[0].prodring;
Michael Chanc76949a2005-05-29 14:58:59 -070011038
Matt Carlsonc8873402010-02-12 14:47:11 +000011039 tnapi = &tp->napi[0];
11040 rnapi = &tp->napi[0];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011041 if (tp->irq_cnt > 1) {
Matt Carlson1da85aa2010-09-30 10:34:34 +000011042 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_RSS)
11043 rnapi = &tp->napi[1];
Matt Carlsonc8873402010-02-12 14:47:11 +000011044 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_TSS)
11045 tnapi = &tp->napi[1];
Matt Carlson0c1d0e22009-09-01 13:16:33 +000011046 }
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011047 coal_now = tnapi->coal_now | rnapi->coal_now;
Matt Carlson898a56f2009-08-28 14:02:40 +000011048
Michael Chan9f40dea2005-09-05 17:53:06 -070011049 if (loopback_mode == TG3_MAC_LOOPBACK) {
Michael Chanc94e3942005-09-27 12:12:42 -070011050 /* HW errata - mac loopback fails in some cases on 5780.
11051 * Normal traffic and PHY loopback are not affected by
Matt Carlsonaba49f22011-01-25 15:58:53 +000011052 * errata. Also, the MAC loopback test is deprecated for
11053 * all newer ASIC revisions.
Michael Chanc94e3942005-09-27 12:12:42 -070011054 */
Matt Carlsonaba49f22011-01-25 15:58:53 +000011055 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
11056 (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT))
Michael Chanc94e3942005-09-27 12:12:42 -070011057 return 0;
11058
Matt Carlson49692ca2011-01-25 15:58:52 +000011059 mac_mode = tp->mac_mode &
11060 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
11061 mac_mode |= MAC_MODE_PORT_INT_LPBACK;
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011062 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11063 mac_mode |= MAC_MODE_LINK_POLARITY;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011064 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
Michael Chan3f7045c2006-09-27 16:02:29 -070011065 mac_mode |= MAC_MODE_PORT_MODE_MII;
11066 else
11067 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chan9f40dea2005-09-05 17:53:06 -070011068 tw32(MAC_MODE, mac_mode);
11069 } else if (loopback_mode == TG3_PHY_LOOPBACK) {
Michael Chan3f7045c2006-09-27 16:02:29 -070011070 u32 val;
11071
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011072 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson7f97a4b2009-08-25 10:10:03 +000011073 tg3_phy_fet_toggle_apd(tp, false);
Michael Chan5d64ad32006-12-07 00:19:40 -080011074 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED100;
11075 } else
11076 val = BMCR_LOOPBACK | BMCR_FULLDPLX | BMCR_SPEED1000;
Michael Chan3f7045c2006-09-27 16:02:29 -070011077
Matt Carlson9ef8ca92007-07-11 19:48:29 -070011078 tg3_phy_toggle_automdix(tp, 0);
11079
Michael Chan3f7045c2006-09-27 16:02:29 -070011080 tg3_writephy(tp, MII_BMCR, val);
Michael Chanc94e3942005-09-27 12:12:42 -070011081 udelay(40);
Michael Chan5d64ad32006-12-07 00:19:40 -080011082
Matt Carlson49692ca2011-01-25 15:58:52 +000011083 mac_mode = tp->mac_mode &
11084 ~(MAC_MODE_PORT_MODE_MASK | MAC_MODE_HALF_DUPLEX);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011085 if (tp->phy_flags & TG3_PHYFLG_IS_FET) {
Matt Carlson1061b7c2010-02-12 14:47:12 +000011086 tg3_writephy(tp, MII_TG3_FET_PTEST,
11087 MII_TG3_FET_PTEST_FRC_TX_LINK |
11088 MII_TG3_FET_PTEST_FRC_TX_LOCK);
11089 /* The write needs to be flushed for the AC131 */
11090 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
11091 tg3_readphy(tp, MII_TG3_FET_PTEST, &val);
Michael Chan5d64ad32006-12-07 00:19:40 -080011092 mac_mode |= MAC_MODE_PORT_MODE_MII;
11093 } else
11094 mac_mode |= MAC_MODE_PORT_MODE_GMII;
Michael Chanb16250e2006-09-27 16:10:14 -070011095
Michael Chanc94e3942005-09-27 12:12:42 -070011096 /* reset to prevent losing 1st rx packet intermittently */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011097 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES) {
Michael Chanc94e3942005-09-27 12:12:42 -070011098 tw32_f(MAC_RX_MODE, RX_MODE_RESET);
11099 udelay(10);
11100 tw32_f(MAC_RX_MODE, tp->rx_mode);
11101 }
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011102 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) {
Matt Carlson79eb6902010-02-17 15:17:03 +000011103 u32 masked_phy_id = tp->phy_id & TG3_PHY_ID_MASK;
11104 if (masked_phy_id == TG3_PHY_ID_BCM5401)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011105 mac_mode &= ~MAC_MODE_LINK_POLARITY;
Matt Carlson79eb6902010-02-17 15:17:03 +000011106 else if (masked_phy_id == TG3_PHY_ID_BCM5411)
Matt Carlsone8f3f6c2007-07-11 19:47:55 -070011107 mac_mode |= MAC_MODE_LINK_POLARITY;
Michael Chanff18ff02006-03-27 23:17:27 -080011108 tg3_writephy(tp, MII_TG3_EXT_CTRL,
11109 MII_TG3_EXT_CTRL_LNK3_LED_MODE);
11110 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011111 tw32(MAC_MODE, mac_mode);
Matt Carlson49692ca2011-01-25 15:58:52 +000011112
11113 /* Wait for link */
11114 for (i = 0; i < 100; i++) {
11115 if (tr32(MAC_TX_STATUS) & TX_STATUS_LINK_UP)
11116 break;
11117 mdelay(1);
11118 }
Matt Carlson859a5882010-04-05 10:19:28 +000011119 } else {
Michael Chan9f40dea2005-09-05 17:53:06 -070011120 return -EINVAL;
Matt Carlson859a5882010-04-05 10:19:28 +000011121 }
Michael Chanc76949a2005-05-29 14:58:59 -070011122
11123 err = -EIO;
11124
Matt Carlson4852a862011-04-13 11:05:07 +000011125 tx_len = pktsz;
David S. Millera20e9c62006-07-31 22:38:16 -070011126 skb = netdev_alloc_skb(tp->dev, tx_len);
Jesper Juhla50bb7b2006-05-09 23:14:35 -070011127 if (!skb)
11128 return -ENOMEM;
11129
Michael Chanc76949a2005-05-29 14:58:59 -070011130 tx_data = skb_put(skb, tx_len);
11131 memcpy(tx_data, tp->dev->dev_addr, 6);
11132 memset(tx_data + 6, 0x0, 8);
11133
Matt Carlson4852a862011-04-13 11:05:07 +000011134 tw32(MAC_RX_MTU_SIZE, tx_len + ETH_FCS_LEN);
Michael Chanc76949a2005-05-29 14:58:59 -070011135
11136 for (i = 14; i < tx_len; i++)
11137 tx_data[i] = (u8) (i & 0xff);
11138
Alexander Duyckf4188d82009-12-02 16:48:38 +000011139 map = pci_map_single(tp->pdev, skb->data, tx_len, PCI_DMA_TODEVICE);
11140 if (pci_dma_mapping_error(tp->pdev, map)) {
Matt Carlsona21771d2009-11-02 14:25:31 +000011141 dev_kfree_skb(skb);
11142 return -EIO;
11143 }
Michael Chanc76949a2005-05-29 14:58:59 -070011144
11145 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011146 rnapi->coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011147
11148 udelay(10);
11149
Matt Carlson898a56f2009-08-28 14:02:40 +000011150 rx_start_idx = rnapi->hw_status->idx[0].rx_producer;
Michael Chanc76949a2005-05-29 14:58:59 -070011151
Michael Chanc76949a2005-05-29 14:58:59 -070011152 num_pkts = 0;
11153
Alexander Duyckf4188d82009-12-02 16:48:38 +000011154 tg3_set_txd(tnapi, tnapi->tx_prod, map, tx_len, 0, 1);
Michael Chanc76949a2005-05-29 14:58:59 -070011155
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011156 tnapi->tx_prod++;
Michael Chanc76949a2005-05-29 14:58:59 -070011157 num_pkts++;
11158
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011159 tw32_tx_mbox(tnapi->prodmbox, tnapi->tx_prod);
11160 tr32_mailbox(tnapi->prodmbox);
Michael Chanc76949a2005-05-29 14:58:59 -070011161
11162 udelay(10);
11163
Matt Carlson303fc922009-11-02 14:27:34 +000011164 /* 350 usec to allow enough time on some 10/100 Mbps devices. */
11165 for (i = 0; i < 35; i++) {
Michael Chanc76949a2005-05-29 14:58:59 -070011166 tw32_f(HOSTCC_MODE, tp->coalesce_mode | HOSTCC_MODE_ENABLE |
Matt Carlsonfd2ce372009-09-01 12:51:13 +000011167 coal_now);
Michael Chanc76949a2005-05-29 14:58:59 -070011168
11169 udelay(10);
11170
Matt Carlson898a56f2009-08-28 14:02:40 +000011171 tx_idx = tnapi->hw_status->idx[0].tx_consumer;
11172 rx_idx = rnapi->hw_status->idx[0].rx_producer;
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011173 if ((tx_idx == tnapi->tx_prod) &&
Michael Chanc76949a2005-05-29 14:58:59 -070011174 (rx_idx == (rx_start_idx + num_pkts)))
11175 break;
11176 }
11177
Alexander Duyckf4188d82009-12-02 16:48:38 +000011178 pci_unmap_single(tp->pdev, map, tx_len, PCI_DMA_TODEVICE);
Michael Chanc76949a2005-05-29 14:58:59 -070011179 dev_kfree_skb(skb);
11180
Matt Carlsonf3f3f272009-08-28 14:03:21 +000011181 if (tx_idx != tnapi->tx_prod)
Michael Chanc76949a2005-05-29 14:58:59 -070011182 goto out;
11183
11184 if (rx_idx != rx_start_idx + num_pkts)
11185 goto out;
11186
Matt Carlson72334482009-08-28 14:03:01 +000011187 desc = &rnapi->rx_rcb[rx_start_idx];
Michael Chanc76949a2005-05-29 14:58:59 -070011188 desc_idx = desc->opaque & RXD_OPAQUE_INDEX_MASK;
11189 opaque_key = desc->opaque & RXD_OPAQUE_RING_MASK;
Michael Chanc76949a2005-05-29 14:58:59 -070011190
11191 if ((desc->err_vlan & RXD_ERR_MASK) != 0 &&
11192 (desc->err_vlan != RXD_ERR_ODD_NIBBLE_RCVD_MII))
11193 goto out;
11194
11195 rx_len = ((desc->idx_len & RXD_LEN_MASK) >> RXD_LEN_SHIFT) - 4;
11196 if (rx_len != tx_len)
11197 goto out;
11198
Matt Carlson4852a862011-04-13 11:05:07 +000011199 if (pktsz <= TG3_RX_STD_DMA_SZ - ETH_FCS_LEN) {
11200 if (opaque_key != RXD_OPAQUE_RING_STD)
11201 goto out;
Michael Chanc76949a2005-05-29 14:58:59 -070011202
Matt Carlson4852a862011-04-13 11:05:07 +000011203 rx_skb = tpr->rx_std_buffers[desc_idx].skb;
11204 map = dma_unmap_addr(&tpr->rx_std_buffers[desc_idx], mapping);
11205 } else {
11206 if (opaque_key != RXD_OPAQUE_RING_JUMBO)
11207 goto out;
11208
11209 rx_skb = tpr->rx_jmb_buffers[desc_idx].skb;
11210 map = dma_unmap_addr(&tpr->rx_jmb_buffers[desc_idx], mapping);
11211 }
11212
Michael Chanc76949a2005-05-29 14:58:59 -070011213 pci_dma_sync_single_for_cpu(tp->pdev, map, rx_len, PCI_DMA_FROMDEVICE);
11214
11215 for (i = 14; i < tx_len; i++) {
11216 if (*(rx_skb->data + i) != (u8) (i & 0xff))
11217 goto out;
11218 }
11219 err = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011220
Michael Chanc76949a2005-05-29 14:58:59 -070011221 /* tg3_free_rings will unmap and free the rx_skb */
11222out:
11223 return err;
11224}
11225
Michael Chan9f40dea2005-09-05 17:53:06 -070011226#define TG3_MAC_LOOPBACK_FAILED 1
11227#define TG3_PHY_LOOPBACK_FAILED 2
11228#define TG3_LOOPBACK_FAILED (TG3_MAC_LOOPBACK_FAILED | \
11229 TG3_PHY_LOOPBACK_FAILED)
11230
11231static int tg3_test_loopback(struct tg3 *tp)
11232{
11233 int err = 0;
Matt Carlsonab789042011-01-25 15:58:54 +000011234 u32 eee_cap, cpmuctrl = 0;
Michael Chan9f40dea2005-09-05 17:53:06 -070011235
11236 if (!netif_running(tp->dev))
11237 return TG3_LOOPBACK_FAILED;
11238
Matt Carlsonab789042011-01-25 15:58:54 +000011239 eee_cap = tp->phy_flags & TG3_PHYFLG_EEE_CAP;
11240 tp->phy_flags &= ~TG3_PHYFLG_EEE_CAP;
11241
Michael Chanb9ec6c12006-07-25 16:37:27 -070011242 err = tg3_reset_hw(tp, 1);
Matt Carlsonab789042011-01-25 15:58:54 +000011243 if (err) {
11244 err = TG3_LOOPBACK_FAILED;
11245 goto done;
11246 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011247
Matt Carlson6833c042008-11-21 17:18:59 -080011248 /* Turn off gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011249 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011250 tg3_phy_toggle_apd(tp, false);
11251
Matt Carlson321d32a2008-11-21 17:22:19 -080011252 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011253 int i;
11254 u32 status;
11255
11256 tw32(TG3_CPMU_MUTEX_REQ, CPMU_MUTEX_REQ_DRIVER);
11257
11258 /* Wait for up to 40 microseconds to acquire lock. */
11259 for (i = 0; i < 4; i++) {
11260 status = tr32(TG3_CPMU_MUTEX_GNT);
11261 if (status == CPMU_MUTEX_GNT_DRIVER)
11262 break;
11263 udelay(10);
11264 }
11265
Matt Carlsonab789042011-01-25 15:58:54 +000011266 if (status != CPMU_MUTEX_GNT_DRIVER) {
11267 err = TG3_LOOPBACK_FAILED;
11268 goto done;
11269 }
Matt Carlson9936bcf2007-10-10 18:03:07 -070011270
Matt Carlsonb2a5c192008-04-03 21:44:44 -070011271 /* Turn off link-based power management. */
Matt Carlsone8750932007-11-12 21:11:51 -080011272 cpmuctrl = tr32(TG3_CPMU_CTRL);
Matt Carlson109115e2008-05-02 16:48:59 -070011273 tw32(TG3_CPMU_CTRL,
11274 cpmuctrl & ~(CPMU_CTRL_LINK_SPEED_MODE |
11275 CPMU_CTRL_LINK_AWARE_MODE));
Matt Carlson9936bcf2007-10-10 18:03:07 -070011276 }
11277
Matt Carlson4852a862011-04-13 11:05:07 +000011278 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_MAC_LOOPBACK))
Michael Chan9f40dea2005-09-05 17:53:06 -070011279 err |= TG3_MAC_LOOPBACK_FAILED;
Matt Carlson9936bcf2007-10-10 18:03:07 -070011280
Matt Carlson4852a862011-04-13 11:05:07 +000011281 if ((tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) &&
11282 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_MAC_LOOPBACK))
11283 err |= (TG3_MAC_LOOPBACK_FAILED << 2);
11284
Matt Carlson321d32a2008-11-21 17:22:19 -080011285 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT) {
Matt Carlson9936bcf2007-10-10 18:03:07 -070011286 tw32(TG3_CPMU_CTRL, cpmuctrl);
11287
11288 /* Release the mutex */
11289 tw32(TG3_CPMU_MUTEX_GNT, CPMU_MUTEX_GNT_DRIVER);
11290 }
11291
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011292 if (!(tp->phy_flags & TG3_PHYFLG_PHY_SERDES) &&
Matt Carlsondd477002008-05-25 23:45:58 -070011293 !(tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)) {
Matt Carlson4852a862011-04-13 11:05:07 +000011294 if (tg3_run_loopback(tp, ETH_FRAME_LEN, TG3_PHY_LOOPBACK))
Michael Chan9f40dea2005-09-05 17:53:06 -070011295 err |= TG3_PHY_LOOPBACK_FAILED;
Matt Carlson4852a862011-04-13 11:05:07 +000011296 if ((tp->tg3_flags & TG3_FLAG_JUMBO_RING_ENABLE) &&
11297 tg3_run_loopback(tp, 9000 + ETH_HLEN, TG3_PHY_LOOPBACK))
11298 err |= (TG3_PHY_LOOPBACK_FAILED << 2);
Michael Chan9f40dea2005-09-05 17:53:06 -070011299 }
11300
Matt Carlson6833c042008-11-21 17:18:59 -080011301 /* Re-enable gphy autopowerdown. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011302 if (tp->phy_flags & TG3_PHYFLG_ENABLE_APD)
Matt Carlson6833c042008-11-21 17:18:59 -080011303 tg3_phy_toggle_apd(tp, true);
11304
Matt Carlsonab789042011-01-25 15:58:54 +000011305done:
11306 tp->phy_flags |= eee_cap;
11307
Michael Chan9f40dea2005-09-05 17:53:06 -070011308 return err;
11309}
11310
Michael Chan4cafd3f2005-05-29 14:56:34 -070011311static void tg3_self_test(struct net_device *dev, struct ethtool_test *etest,
11312 u64 *data)
11313{
Michael Chan566f86a2005-05-29 14:56:58 -070011314 struct tg3 *tp = netdev_priv(dev);
11315
Matt Carlson80096062010-08-02 11:26:06 +000011316 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011317 tg3_power_up(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011318
Michael Chan566f86a2005-05-29 14:56:58 -070011319 memset(data, 0, sizeof(u64) * TG3_NUM_TEST);
11320
11321 if (tg3_test_nvram(tp) != 0) {
11322 etest->flags |= ETH_TEST_FL_FAILED;
11323 data[0] = 1;
11324 }
Michael Chanca430072005-05-29 14:57:23 -070011325 if (tg3_test_link(tp) != 0) {
11326 etest->flags |= ETH_TEST_FL_FAILED;
11327 data[1] = 1;
11328 }
Michael Chana71116d2005-05-29 14:58:11 -070011329 if (etest->flags & ETH_TEST_FL_OFFLINE) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011330 int err, err2 = 0, irq_sync = 0;
Michael Chana71116d2005-05-29 14:58:11 -070011331
Michael Chanbbe832c2005-06-24 20:20:04 -070011332 if (netif_running(dev)) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011333 tg3_phy_stop(tp);
Michael Chanbbe832c2005-06-24 20:20:04 -070011334 tg3_netif_stop(tp);
11335 irq_sync = 1;
11336 }
11337
11338 tg3_full_lock(tp, irq_sync);
Michael Chana71116d2005-05-29 14:58:11 -070011339
11340 tg3_halt(tp, RESET_KIND_SUSPEND, 1);
Michael Chanec41c7d2006-01-17 02:40:55 -080011341 err = tg3_nvram_lock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011342 tg3_halt_cpu(tp, RX_CPU_BASE);
11343 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
11344 tg3_halt_cpu(tp, TX_CPU_BASE);
Michael Chanec41c7d2006-01-17 02:40:55 -080011345 if (!err)
11346 tg3_nvram_unlock(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011347
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011348 if (tp->phy_flags & TG3_PHYFLG_MII_SERDES)
Michael Chand9ab5ad2006-03-20 22:27:35 -080011349 tg3_phy_reset(tp);
11350
Michael Chana71116d2005-05-29 14:58:11 -070011351 if (tg3_test_registers(tp) != 0) {
11352 etest->flags |= ETH_TEST_FL_FAILED;
11353 data[2] = 1;
11354 }
Michael Chan7942e1d2005-05-29 14:58:36 -070011355 if (tg3_test_memory(tp) != 0) {
11356 etest->flags |= ETH_TEST_FL_FAILED;
11357 data[3] = 1;
11358 }
Michael Chan9f40dea2005-09-05 17:53:06 -070011359 if ((data[4] = tg3_test_loopback(tp)) != 0)
Michael Chanc76949a2005-05-29 14:58:59 -070011360 etest->flags |= ETH_TEST_FL_FAILED;
Michael Chana71116d2005-05-29 14:58:11 -070011361
David S. Millerf47c11e2005-06-24 20:18:35 -070011362 tg3_full_unlock(tp);
11363
Michael Chand4bc3922005-05-29 14:59:20 -070011364 if (tg3_test_interrupt(tp) != 0) {
11365 etest->flags |= ETH_TEST_FL_FAILED;
11366 data[5] = 1;
11367 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011368
11369 tg3_full_lock(tp, 0);
Michael Chand4bc3922005-05-29 14:59:20 -070011370
Michael Chana71116d2005-05-29 14:58:11 -070011371 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
11372 if (netif_running(dev)) {
11373 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011374 err2 = tg3_restart_hw(tp, 1);
11375 if (!err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070011376 tg3_netif_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011377 }
David S. Millerf47c11e2005-06-24 20:18:35 -070011378
11379 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011380
11381 if (irq_sync && !err2)
11382 tg3_phy_start(tp);
Michael Chana71116d2005-05-29 14:58:11 -070011383 }
Matt Carlson80096062010-08-02 11:26:06 +000011384 if (tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER)
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000011385 tg3_power_down(tp);
Michael Chanbc1c7562006-03-20 17:48:03 -080011386
Michael Chan4cafd3f2005-05-29 14:56:34 -070011387}
11388
Linus Torvalds1da177e2005-04-16 15:20:36 -070011389static int tg3_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
11390{
11391 struct mii_ioctl_data *data = if_mii(ifr);
11392 struct tg3 *tp = netdev_priv(dev);
11393 int err;
11394
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011395 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011396 struct phy_device *phydev;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011397 if (!(tp->phy_flags & TG3_PHYFLG_IS_CONNECTED))
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011398 return -EAGAIN;
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000011399 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Richard Cochran28b04112010-07-17 08:48:55 +000011400 return phy_mii_ioctl(phydev, ifr, cmd);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070011401 }
11402
Matt Carlson33f401a2010-04-05 10:19:27 +000011403 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011404 case SIOCGMIIPHY:
Matt Carlson882e9792009-09-01 13:21:36 +000011405 data->phy_id = tp->phy_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011406
11407 /* fallthru */
11408 case SIOCGMIIREG: {
11409 u32 mii_regval;
11410
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011411 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011412 break; /* We have no PHY */
11413
Matt Carlsonf746a312011-01-25 15:58:51 +000011414 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) ||
11415 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
11416 !netif_running(dev)))
Michael Chanbc1c7562006-03-20 17:48:03 -080011417 return -EAGAIN;
11418
David S. Millerf47c11e2005-06-24 20:18:35 -070011419 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011420 err = tg3_readphy(tp, data->reg_num & 0x1f, &mii_regval);
David S. Millerf47c11e2005-06-24 20:18:35 -070011421 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011422
11423 data->val_out = mii_regval;
11424
11425 return err;
11426 }
11427
11428 case SIOCSMIIREG:
Matt Carlsonf07e9af2010-08-02 11:26:07 +000011429 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011430 break; /* We have no PHY */
11431
Matt Carlsonf746a312011-01-25 15:58:51 +000011432 if ((tp->phy_flags & TG3_PHYFLG_IS_LOW_POWER) ||
11433 ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) &&
11434 !netif_running(dev)))
Michael Chanbc1c7562006-03-20 17:48:03 -080011435 return -EAGAIN;
11436
David S. Millerf47c11e2005-06-24 20:18:35 -070011437 spin_lock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011438 err = tg3_writephy(tp, data->reg_num & 0x1f, data->val_in);
David S. Millerf47c11e2005-06-24 20:18:35 -070011439 spin_unlock_bh(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070011440
11441 return err;
11442
11443 default:
11444 /* do nothing */
11445 break;
11446 }
11447 return -EOPNOTSUPP;
11448}
11449
David S. Miller15f98502005-05-18 22:49:26 -070011450static int tg3_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11451{
11452 struct tg3 *tp = netdev_priv(dev);
11453
11454 memcpy(ec, &tp->coal, sizeof(*ec));
11455 return 0;
11456}
11457
Michael Chand244c892005-07-05 14:42:33 -070011458static int tg3_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
11459{
11460 struct tg3 *tp = netdev_priv(dev);
11461 u32 max_rxcoal_tick_int = 0, max_txcoal_tick_int = 0;
11462 u32 max_stat_coal_ticks = 0, min_stat_coal_ticks = 0;
11463
11464 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS)) {
11465 max_rxcoal_tick_int = MAX_RXCOAL_TICK_INT;
11466 max_txcoal_tick_int = MAX_TXCOAL_TICK_INT;
11467 max_stat_coal_ticks = MAX_STAT_COAL_TICKS;
11468 min_stat_coal_ticks = MIN_STAT_COAL_TICKS;
11469 }
11470
11471 if ((ec->rx_coalesce_usecs > MAX_RXCOL_TICKS) ||
11472 (ec->tx_coalesce_usecs > MAX_TXCOL_TICKS) ||
11473 (ec->rx_max_coalesced_frames > MAX_RXMAX_FRAMES) ||
11474 (ec->tx_max_coalesced_frames > MAX_TXMAX_FRAMES) ||
11475 (ec->rx_coalesce_usecs_irq > max_rxcoal_tick_int) ||
11476 (ec->tx_coalesce_usecs_irq > max_txcoal_tick_int) ||
11477 (ec->rx_max_coalesced_frames_irq > MAX_RXCOAL_MAXF_INT) ||
11478 (ec->tx_max_coalesced_frames_irq > MAX_TXCOAL_MAXF_INT) ||
11479 (ec->stats_block_coalesce_usecs > max_stat_coal_ticks) ||
11480 (ec->stats_block_coalesce_usecs < min_stat_coal_ticks))
11481 return -EINVAL;
11482
11483 /* No rx interrupts will be generated if both are zero */
11484 if ((ec->rx_coalesce_usecs == 0) &&
11485 (ec->rx_max_coalesced_frames == 0))
11486 return -EINVAL;
11487
11488 /* No tx interrupts will be generated if both are zero */
11489 if ((ec->tx_coalesce_usecs == 0) &&
11490 (ec->tx_max_coalesced_frames == 0))
11491 return -EINVAL;
11492
11493 /* Only copy relevant parameters, ignore all others. */
11494 tp->coal.rx_coalesce_usecs = ec->rx_coalesce_usecs;
11495 tp->coal.tx_coalesce_usecs = ec->tx_coalesce_usecs;
11496 tp->coal.rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
11497 tp->coal.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
11498 tp->coal.rx_coalesce_usecs_irq = ec->rx_coalesce_usecs_irq;
11499 tp->coal.tx_coalesce_usecs_irq = ec->tx_coalesce_usecs_irq;
11500 tp->coal.rx_max_coalesced_frames_irq = ec->rx_max_coalesced_frames_irq;
11501 tp->coal.tx_max_coalesced_frames_irq = ec->tx_max_coalesced_frames_irq;
11502 tp->coal.stats_block_coalesce_usecs = ec->stats_block_coalesce_usecs;
11503
11504 if (netif_running(dev)) {
11505 tg3_full_lock(tp, 0);
11506 __tg3_set_coalesce(tp, &tp->coal);
11507 tg3_full_unlock(tp);
11508 }
11509 return 0;
11510}
11511
Jeff Garzik7282d492006-09-13 14:30:00 -040011512static const struct ethtool_ops tg3_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011513 .get_settings = tg3_get_settings,
11514 .set_settings = tg3_set_settings,
11515 .get_drvinfo = tg3_get_drvinfo,
11516 .get_regs_len = tg3_get_regs_len,
11517 .get_regs = tg3_get_regs,
11518 .get_wol = tg3_get_wol,
11519 .set_wol = tg3_set_wol,
11520 .get_msglevel = tg3_get_msglevel,
11521 .set_msglevel = tg3_set_msglevel,
11522 .nway_reset = tg3_nway_reset,
11523 .get_link = ethtool_op_get_link,
11524 .get_eeprom_len = tg3_get_eeprom_len,
11525 .get_eeprom = tg3_get_eeprom,
11526 .set_eeprom = tg3_set_eeprom,
11527 .get_ringparam = tg3_get_ringparam,
11528 .set_ringparam = tg3_set_ringparam,
11529 .get_pauseparam = tg3_get_pauseparam,
11530 .set_pauseparam = tg3_set_pauseparam,
Michael Chan4cafd3f2005-05-29 14:56:34 -070011531 .self_test = tg3_self_test,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011532 .get_strings = tg3_get_strings,
stephen hemminger81b87092011-04-04 08:43:50 +000011533 .set_phys_id = tg3_set_phys_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011534 .get_ethtool_stats = tg3_get_ethtool_stats,
David S. Miller15f98502005-05-18 22:49:26 -070011535 .get_coalesce = tg3_get_coalesce,
Michael Chand244c892005-07-05 14:42:33 -070011536 .set_coalesce = tg3_set_coalesce,
Jeff Garzikb9f2c042007-10-03 18:07:32 -070011537 .get_sset_count = tg3_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070011538};
11539
11540static void __devinit tg3_get_eeprom_size(struct tg3 *tp)
11541{
Michael Chan1b277772006-03-20 22:27:48 -080011542 u32 cursize, val, magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011543
11544 tp->nvram_size = EEPROM_CHIP_SIZE;
11545
Matt Carlsone4f34112009-02-25 14:25:00 +000011546 if (tg3_nvram_read(tp, 0, &magic) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011547 return;
11548
Michael Chanb16250e2006-09-27 16:10:14 -070011549 if ((magic != TG3_EEPROM_MAGIC) &&
11550 ((magic & TG3_EEPROM_MAGIC_FW_MSK) != TG3_EEPROM_MAGIC_FW) &&
11551 ((magic & TG3_EEPROM_MAGIC_HW_MSK) != TG3_EEPROM_MAGIC_HW))
Linus Torvalds1da177e2005-04-16 15:20:36 -070011552 return;
11553
11554 /*
11555 * Size the chip by reading offsets at increasing powers of two.
11556 * When we encounter our validation signature, we know the addressing
11557 * has wrapped around, and thus have our chip size.
11558 */
Michael Chan1b277772006-03-20 22:27:48 -080011559 cursize = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011560
11561 while (cursize < tp->nvram_size) {
Matt Carlsone4f34112009-02-25 14:25:00 +000011562 if (tg3_nvram_read(tp, cursize, &val) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011563 return;
11564
Michael Chan18201802006-03-20 22:29:15 -080011565 if (val == magic)
Linus Torvalds1da177e2005-04-16 15:20:36 -070011566 break;
11567
11568 cursize <<= 1;
11569 }
11570
11571 tp->nvram_size = cursize;
11572}
Jeff Garzik6aa20a22006-09-13 13:24:59 -040011573
Linus Torvalds1da177e2005-04-16 15:20:36 -070011574static void __devinit tg3_get_nvram_size(struct tg3 *tp)
11575{
11576 u32 val;
11577
Matt Carlsondf259d82009-04-20 06:57:14 +000011578 if ((tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) ||
11579 tg3_nvram_read(tp, 0, &val) != 0)
Michael Chan1b277772006-03-20 22:27:48 -080011580 return;
11581
11582 /* Selfboot format */
Michael Chan18201802006-03-20 22:29:15 -080011583 if (val != TG3_EEPROM_MAGIC) {
Michael Chan1b277772006-03-20 22:27:48 -080011584 tg3_get_eeprom_size(tp);
11585 return;
11586 }
11587
Matt Carlson6d348f22009-02-25 14:25:52 +000011588 if (tg3_nvram_read(tp, 0xf0, &val) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011589 if (val != 0) {
Matt Carlson6d348f22009-02-25 14:25:52 +000011590 /* This is confusing. We want to operate on the
11591 * 16-bit value at offset 0xf2. The tg3_nvram_read()
11592 * call will read from NVRAM and byteswap the data
11593 * according to the byteswapping settings for all
11594 * other register accesses. This ensures the data we
11595 * want will always reside in the lower 16-bits.
11596 * However, the data in NVRAM is in LE format, which
11597 * means the data from the NVRAM read will always be
11598 * opposite the endianness of the CPU. The 16-bit
11599 * byteswap then brings the data to CPU endianness.
11600 */
11601 tp->nvram_size = swab16((u16)(val & 0x0000ffff)) * 1024;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011602 return;
11603 }
11604 }
Matt Carlsonfd1122a2008-05-02 16:48:36 -070011605 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011606}
11607
11608static void __devinit tg3_get_nvram_info(struct tg3 *tp)
11609{
11610 u32 nvcfg1;
11611
11612 nvcfg1 = tr32(NVRAM_CFG1);
11613 if (nvcfg1 & NVRAM_CFG1_FLASHIF_ENAB) {
11614 tp->tg3_flags2 |= TG3_FLG2_FLASH;
Matt Carlson8590a602009-08-28 12:29:16 +000011615 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011616 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11617 tw32(NVRAM_CFG1, nvcfg1);
11618 }
11619
Michael Chan4c987482005-09-05 17:52:38 -070011620 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750) ||
Michael Chana4e2b342005-10-26 15:46:52 -070011621 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011622 switch (nvcfg1 & NVRAM_CFG1_VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011623 case FLASH_VENDOR_ATMEL_FLASH_BUFFERED:
11624 tp->nvram_jedecnum = JEDEC_ATMEL;
11625 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11626 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11627 break;
11628 case FLASH_VENDOR_ATMEL_FLASH_UNBUFFERED:
11629 tp->nvram_jedecnum = JEDEC_ATMEL;
11630 tp->nvram_pagesize = ATMEL_AT25F512_PAGE_SIZE;
11631 break;
11632 case FLASH_VENDOR_ATMEL_EEPROM:
11633 tp->nvram_jedecnum = JEDEC_ATMEL;
11634 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11635 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11636 break;
11637 case FLASH_VENDOR_ST:
11638 tp->nvram_jedecnum = JEDEC_ST;
11639 tp->nvram_pagesize = ST_M45PEX0_PAGE_SIZE;
11640 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11641 break;
11642 case FLASH_VENDOR_SAIFUN:
11643 tp->nvram_jedecnum = JEDEC_SAIFUN;
11644 tp->nvram_pagesize = SAIFUN_SA25F0XX_PAGE_SIZE;
11645 break;
11646 case FLASH_VENDOR_SST_SMALL:
11647 case FLASH_VENDOR_SST_LARGE:
11648 tp->nvram_jedecnum = JEDEC_SST;
11649 tp->nvram_pagesize = SST_25VF0X0_PAGE_SIZE;
11650 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070011651 }
Matt Carlson8590a602009-08-28 12:29:16 +000011652 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070011653 tp->nvram_jedecnum = JEDEC_ATMEL;
11654 tp->nvram_pagesize = ATMEL_AT45DB0X1B_PAGE_SIZE;
11655 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11656 }
11657}
11658
Matt Carlsona1b950d2009-09-01 13:20:17 +000011659static void __devinit tg3_nvram_get_pagesize(struct tg3 *tp, u32 nvmcfg1)
11660{
11661 switch (nvmcfg1 & NVRAM_CFG1_5752PAGE_SIZE_MASK) {
11662 case FLASH_5752PAGE_SIZE_256:
11663 tp->nvram_pagesize = 256;
11664 break;
11665 case FLASH_5752PAGE_SIZE_512:
11666 tp->nvram_pagesize = 512;
11667 break;
11668 case FLASH_5752PAGE_SIZE_1K:
11669 tp->nvram_pagesize = 1024;
11670 break;
11671 case FLASH_5752PAGE_SIZE_2K:
11672 tp->nvram_pagesize = 2048;
11673 break;
11674 case FLASH_5752PAGE_SIZE_4K:
11675 tp->nvram_pagesize = 4096;
11676 break;
11677 case FLASH_5752PAGE_SIZE_264:
11678 tp->nvram_pagesize = 264;
11679 break;
11680 case FLASH_5752PAGE_SIZE_528:
11681 tp->nvram_pagesize = 528;
11682 break;
11683 }
11684}
11685
Michael Chan361b4ac2005-04-21 17:11:21 -070011686static void __devinit tg3_get_5752_nvram_info(struct tg3 *tp)
11687{
11688 u32 nvcfg1;
11689
11690 nvcfg1 = tr32(NVRAM_CFG1);
11691
Michael Chane6af3012005-04-21 17:12:05 -070011692 /* NVRAM protection for TPM */
11693 if (nvcfg1 & (1 << 27))
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011694 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Michael Chane6af3012005-04-21 17:12:05 -070011695
Michael Chan361b4ac2005-04-21 17:11:21 -070011696 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011697 case FLASH_5752VENDOR_ATMEL_EEPROM_64KHZ:
11698 case FLASH_5752VENDOR_ATMEL_EEPROM_376KHZ:
11699 tp->nvram_jedecnum = JEDEC_ATMEL;
11700 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11701 break;
11702 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11703 tp->nvram_jedecnum = JEDEC_ATMEL;
11704 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11705 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11706 break;
11707 case FLASH_5752VENDOR_ST_M45PE10:
11708 case FLASH_5752VENDOR_ST_M45PE20:
11709 case FLASH_5752VENDOR_ST_M45PE40:
11710 tp->nvram_jedecnum = JEDEC_ST;
11711 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11712 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11713 break;
Michael Chan361b4ac2005-04-21 17:11:21 -070011714 }
11715
11716 if (tp->tg3_flags2 & TG3_FLG2_FLASH) {
Matt Carlsona1b950d2009-09-01 13:20:17 +000011717 tg3_nvram_get_pagesize(tp, nvcfg1);
Matt Carlson8590a602009-08-28 12:29:16 +000011718 } else {
Michael Chan361b4ac2005-04-21 17:11:21 -070011719 /* For eeprom, set pagesize to maximum eeprom size */
11720 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11721
11722 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11723 tw32(NVRAM_CFG1, nvcfg1);
11724 }
11725}
11726
Michael Chand3c7b882006-03-23 01:28:25 -080011727static void __devinit tg3_get_5755_nvram_info(struct tg3 *tp)
11728{
Matt Carlson989a9d22007-05-05 11:51:05 -070011729 u32 nvcfg1, protect = 0;
Michael Chand3c7b882006-03-23 01:28:25 -080011730
11731 nvcfg1 = tr32(NVRAM_CFG1);
11732
11733 /* NVRAM protection for TPM */
Matt Carlson989a9d22007-05-05 11:51:05 -070011734 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011735 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson989a9d22007-05-05 11:51:05 -070011736 protect = 1;
11737 }
Michael Chand3c7b882006-03-23 01:28:25 -080011738
Matt Carlson989a9d22007-05-05 11:51:05 -070011739 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11740 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011741 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11742 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11743 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11744 case FLASH_5755VENDOR_ATMEL_FLASH_5:
11745 tp->nvram_jedecnum = JEDEC_ATMEL;
11746 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11747 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11748 tp->nvram_pagesize = 264;
11749 if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_1 ||
11750 nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_5)
11751 tp->nvram_size = (protect ? 0x3e200 :
11752 TG3_NVRAM_SIZE_512KB);
11753 else if (nvcfg1 == FLASH_5755VENDOR_ATMEL_FLASH_2)
11754 tp->nvram_size = (protect ? 0x1f200 :
11755 TG3_NVRAM_SIZE_256KB);
11756 else
11757 tp->nvram_size = (protect ? 0x1f200 :
11758 TG3_NVRAM_SIZE_128KB);
11759 break;
11760 case FLASH_5752VENDOR_ST_M45PE10:
11761 case FLASH_5752VENDOR_ST_M45PE20:
11762 case FLASH_5752VENDOR_ST_M45PE40:
11763 tp->nvram_jedecnum = JEDEC_ST;
11764 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11765 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11766 tp->nvram_pagesize = 256;
11767 if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE10)
11768 tp->nvram_size = (protect ?
11769 TG3_NVRAM_SIZE_64KB :
11770 TG3_NVRAM_SIZE_128KB);
11771 else if (nvcfg1 == FLASH_5752VENDOR_ST_M45PE20)
11772 tp->nvram_size = (protect ?
11773 TG3_NVRAM_SIZE_64KB :
11774 TG3_NVRAM_SIZE_256KB);
11775 else
11776 tp->nvram_size = (protect ?
11777 TG3_NVRAM_SIZE_128KB :
11778 TG3_NVRAM_SIZE_512KB);
11779 break;
Michael Chand3c7b882006-03-23 01:28:25 -080011780 }
11781}
11782
Michael Chan1b277772006-03-20 22:27:48 -080011783static void __devinit tg3_get_5787_nvram_info(struct tg3 *tp)
11784{
11785 u32 nvcfg1;
11786
11787 nvcfg1 = tr32(NVRAM_CFG1);
11788
11789 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
Matt Carlson8590a602009-08-28 12:29:16 +000011790 case FLASH_5787VENDOR_ATMEL_EEPROM_64KHZ:
11791 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11792 case FLASH_5787VENDOR_MICRO_EEPROM_64KHZ:
11793 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11794 tp->nvram_jedecnum = JEDEC_ATMEL;
11795 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11796 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
Michael Chan1b277772006-03-20 22:27:48 -080011797
Matt Carlson8590a602009-08-28 12:29:16 +000011798 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11799 tw32(NVRAM_CFG1, nvcfg1);
11800 break;
11801 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11802 case FLASH_5755VENDOR_ATMEL_FLASH_1:
11803 case FLASH_5755VENDOR_ATMEL_FLASH_2:
11804 case FLASH_5755VENDOR_ATMEL_FLASH_3:
11805 tp->nvram_jedecnum = JEDEC_ATMEL;
11806 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11807 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11808 tp->nvram_pagesize = 264;
11809 break;
11810 case FLASH_5752VENDOR_ST_M45PE10:
11811 case FLASH_5752VENDOR_ST_M45PE20:
11812 case FLASH_5752VENDOR_ST_M45PE40:
11813 tp->nvram_jedecnum = JEDEC_ST;
11814 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11815 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11816 tp->nvram_pagesize = 256;
11817 break;
Michael Chan1b277772006-03-20 22:27:48 -080011818 }
11819}
11820
Matt Carlson6b91fa02007-10-10 18:01:09 -070011821static void __devinit tg3_get_5761_nvram_info(struct tg3 *tp)
11822{
11823 u32 nvcfg1, protect = 0;
11824
11825 nvcfg1 = tr32(NVRAM_CFG1);
11826
11827 /* NVRAM protection for TPM */
11828 if (nvcfg1 & (1 << 27)) {
Matt Carlsonf66a29b2009-11-13 13:03:36 +000011829 tp->tg3_flags3 |= TG3_FLG3_PROTECTED_NVRAM;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011830 protect = 1;
11831 }
11832
11833 nvcfg1 &= NVRAM_CFG1_5752VENDOR_MASK;
11834 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011835 case FLASH_5761VENDOR_ATMEL_ADB021D:
11836 case FLASH_5761VENDOR_ATMEL_ADB041D:
11837 case FLASH_5761VENDOR_ATMEL_ADB081D:
11838 case FLASH_5761VENDOR_ATMEL_ADB161D:
11839 case FLASH_5761VENDOR_ATMEL_MDB021D:
11840 case FLASH_5761VENDOR_ATMEL_MDB041D:
11841 case FLASH_5761VENDOR_ATMEL_MDB081D:
11842 case FLASH_5761VENDOR_ATMEL_MDB161D:
11843 tp->nvram_jedecnum = JEDEC_ATMEL;
11844 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11845 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11846 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
11847 tp->nvram_pagesize = 256;
11848 break;
11849 case FLASH_5761VENDOR_ST_A_M45PE20:
11850 case FLASH_5761VENDOR_ST_A_M45PE40:
11851 case FLASH_5761VENDOR_ST_A_M45PE80:
11852 case FLASH_5761VENDOR_ST_A_M45PE16:
11853 case FLASH_5761VENDOR_ST_M_M45PE20:
11854 case FLASH_5761VENDOR_ST_M_M45PE40:
11855 case FLASH_5761VENDOR_ST_M_M45PE80:
11856 case FLASH_5761VENDOR_ST_M_M45PE16:
11857 tp->nvram_jedecnum = JEDEC_ST;
11858 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11859 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11860 tp->nvram_pagesize = 256;
11861 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011862 }
11863
11864 if (protect) {
11865 tp->nvram_size = tr32(NVRAM_ADDR_LOCKOUT);
11866 } else {
11867 switch (nvcfg1) {
Matt Carlson8590a602009-08-28 12:29:16 +000011868 case FLASH_5761VENDOR_ATMEL_ADB161D:
11869 case FLASH_5761VENDOR_ATMEL_MDB161D:
11870 case FLASH_5761VENDOR_ST_A_M45PE16:
11871 case FLASH_5761VENDOR_ST_M_M45PE16:
11872 tp->nvram_size = TG3_NVRAM_SIZE_2MB;
11873 break;
11874 case FLASH_5761VENDOR_ATMEL_ADB081D:
11875 case FLASH_5761VENDOR_ATMEL_MDB081D:
11876 case FLASH_5761VENDOR_ST_A_M45PE80:
11877 case FLASH_5761VENDOR_ST_M_M45PE80:
11878 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
11879 break;
11880 case FLASH_5761VENDOR_ATMEL_ADB041D:
11881 case FLASH_5761VENDOR_ATMEL_MDB041D:
11882 case FLASH_5761VENDOR_ST_A_M45PE40:
11883 case FLASH_5761VENDOR_ST_M_M45PE40:
11884 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11885 break;
11886 case FLASH_5761VENDOR_ATMEL_ADB021D:
11887 case FLASH_5761VENDOR_ATMEL_MDB021D:
11888 case FLASH_5761VENDOR_ST_A_M45PE20:
11889 case FLASH_5761VENDOR_ST_M_M45PE20:
11890 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11891 break;
Matt Carlson6b91fa02007-10-10 18:01:09 -070011892 }
11893 }
11894}
11895
Michael Chanb5d37722006-09-27 16:06:21 -070011896static void __devinit tg3_get_5906_nvram_info(struct tg3 *tp)
11897{
11898 tp->nvram_jedecnum = JEDEC_ATMEL;
11899 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11900 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11901}
11902
Matt Carlson321d32a2008-11-21 17:22:19 -080011903static void __devinit tg3_get_57780_nvram_info(struct tg3 *tp)
11904{
11905 u32 nvcfg1;
11906
11907 nvcfg1 = tr32(NVRAM_CFG1);
11908
11909 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11910 case FLASH_5787VENDOR_ATMEL_EEPROM_376KHZ:
11911 case FLASH_5787VENDOR_MICRO_EEPROM_376KHZ:
11912 tp->nvram_jedecnum = JEDEC_ATMEL;
11913 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11914 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11915
11916 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11917 tw32(NVRAM_CFG1, nvcfg1);
11918 return;
11919 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11920 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11921 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11922 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11923 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11924 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11925 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11926 tp->nvram_jedecnum = JEDEC_ATMEL;
11927 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11928 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11929
11930 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11931 case FLASH_5752VENDOR_ATMEL_FLASH_BUFFERED:
11932 case FLASH_57780VENDOR_ATMEL_AT45DB011D:
11933 case FLASH_57780VENDOR_ATMEL_AT45DB011B:
11934 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11935 break;
11936 case FLASH_57780VENDOR_ATMEL_AT45DB021D:
11937 case FLASH_57780VENDOR_ATMEL_AT45DB021B:
11938 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11939 break;
11940 case FLASH_57780VENDOR_ATMEL_AT45DB041D:
11941 case FLASH_57780VENDOR_ATMEL_AT45DB041B:
11942 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11943 break;
11944 }
11945 break;
11946 case FLASH_5752VENDOR_ST_M45PE10:
11947 case FLASH_5752VENDOR_ST_M45PE20:
11948 case FLASH_5752VENDOR_ST_M45PE40:
11949 tp->nvram_jedecnum = JEDEC_ST;
11950 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11951 tp->tg3_flags2 |= TG3_FLG2_FLASH;
11952
11953 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11954 case FLASH_5752VENDOR_ST_M45PE10:
11955 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
11956 break;
11957 case FLASH_5752VENDOR_ST_M45PE20:
11958 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
11959 break;
11960 case FLASH_5752VENDOR_ST_M45PE40:
11961 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
11962 break;
11963 }
11964 break;
11965 default:
Matt Carlsondf259d82009-04-20 06:57:14 +000011966 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
Matt Carlson321d32a2008-11-21 17:22:19 -080011967 return;
11968 }
11969
Matt Carlsona1b950d2009-09-01 13:20:17 +000011970 tg3_nvram_get_pagesize(tp, nvcfg1);
11971 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
Matt Carlson321d32a2008-11-21 17:22:19 -080011972 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlsona1b950d2009-09-01 13:20:17 +000011973}
11974
11975
11976static void __devinit tg3_get_5717_nvram_info(struct tg3 *tp)
11977{
11978 u32 nvcfg1;
11979
11980 nvcfg1 = tr32(NVRAM_CFG1);
11981
11982 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
11983 case FLASH_5717VENDOR_ATMEL_EEPROM:
11984 case FLASH_5717VENDOR_MICRO_EEPROM:
11985 tp->nvram_jedecnum = JEDEC_ATMEL;
11986 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
11987 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
11988
11989 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
11990 tw32(NVRAM_CFG1, nvcfg1);
11991 return;
11992 case FLASH_5717VENDOR_ATMEL_MDB011D:
11993 case FLASH_5717VENDOR_ATMEL_ADB011B:
11994 case FLASH_5717VENDOR_ATMEL_ADB011D:
11995 case FLASH_5717VENDOR_ATMEL_MDB021D:
11996 case FLASH_5717VENDOR_ATMEL_ADB021B:
11997 case FLASH_5717VENDOR_ATMEL_ADB021D:
11998 case FLASH_5717VENDOR_ATMEL_45USPT:
11999 tp->nvram_jedecnum = JEDEC_ATMEL;
12000 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12001 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12002
12003 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12004 case FLASH_5717VENDOR_ATMEL_MDB021D:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012005 /* Detect size with tg3_nvram_get_size() */
12006 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012007 case FLASH_5717VENDOR_ATMEL_ADB021B:
12008 case FLASH_5717VENDOR_ATMEL_ADB021D:
12009 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12010 break;
12011 default:
12012 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12013 break;
12014 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012015 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012016 case FLASH_5717VENDOR_ST_M_M25PE10:
12017 case FLASH_5717VENDOR_ST_A_M25PE10:
12018 case FLASH_5717VENDOR_ST_M_M45PE10:
12019 case FLASH_5717VENDOR_ST_A_M45PE10:
12020 case FLASH_5717VENDOR_ST_M_M25PE20:
12021 case FLASH_5717VENDOR_ST_A_M25PE20:
12022 case FLASH_5717VENDOR_ST_M_M45PE20:
12023 case FLASH_5717VENDOR_ST_A_M45PE20:
12024 case FLASH_5717VENDOR_ST_25USPT:
12025 case FLASH_5717VENDOR_ST_45USPT:
12026 tp->nvram_jedecnum = JEDEC_ST;
12027 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12028 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12029
12030 switch (nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK) {
12031 case FLASH_5717VENDOR_ST_M_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012032 case FLASH_5717VENDOR_ST_M_M45PE20:
Matt Carlson66ee33b2011-04-05 14:22:51 +000012033 /* Detect size with tg3_nvram_get_size() */
12034 break;
12035 case FLASH_5717VENDOR_ST_A_M25PE20:
Matt Carlsona1b950d2009-09-01 13:20:17 +000012036 case FLASH_5717VENDOR_ST_A_M45PE20:
12037 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12038 break;
12039 default:
12040 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12041 break;
12042 }
Matt Carlson321d32a2008-11-21 17:22:19 -080012043 break;
Matt Carlsona1b950d2009-09-01 13:20:17 +000012044 default:
12045 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
12046 return;
Matt Carlson321d32a2008-11-21 17:22:19 -080012047 }
Matt Carlsona1b950d2009-09-01 13:20:17 +000012048
12049 tg3_nvram_get_pagesize(tp, nvcfg1);
12050 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
12051 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
Matt Carlson321d32a2008-11-21 17:22:19 -080012052}
12053
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012054static void __devinit tg3_get_5720_nvram_info(struct tg3 *tp)
12055{
12056 u32 nvcfg1, nvmpinstrp;
12057
12058 nvcfg1 = tr32(NVRAM_CFG1);
12059 nvmpinstrp = nvcfg1 & NVRAM_CFG1_5752VENDOR_MASK;
12060
12061 switch (nvmpinstrp) {
12062 case FLASH_5720_EEPROM_HD:
12063 case FLASH_5720_EEPROM_LD:
12064 tp->nvram_jedecnum = JEDEC_ATMEL;
12065 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12066
12067 nvcfg1 &= ~NVRAM_CFG1_COMPAT_BYPASS;
12068 tw32(NVRAM_CFG1, nvcfg1);
12069 if (nvmpinstrp == FLASH_5720_EEPROM_HD)
12070 tp->nvram_pagesize = ATMEL_AT24C512_CHIP_SIZE;
12071 else
12072 tp->nvram_pagesize = ATMEL_AT24C02_CHIP_SIZE;
12073 return;
12074 case FLASH_5720VENDOR_M_ATMEL_DB011D:
12075 case FLASH_5720VENDOR_A_ATMEL_DB011B:
12076 case FLASH_5720VENDOR_A_ATMEL_DB011D:
12077 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12078 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12079 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12080 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12081 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12082 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12083 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12084 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12085 case FLASH_5720VENDOR_ATMEL_45USPT:
12086 tp->nvram_jedecnum = JEDEC_ATMEL;
12087 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12088 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12089
12090 switch (nvmpinstrp) {
12091 case FLASH_5720VENDOR_M_ATMEL_DB021D:
12092 case FLASH_5720VENDOR_A_ATMEL_DB021B:
12093 case FLASH_5720VENDOR_A_ATMEL_DB021D:
12094 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12095 break;
12096 case FLASH_5720VENDOR_M_ATMEL_DB041D:
12097 case FLASH_5720VENDOR_A_ATMEL_DB041B:
12098 case FLASH_5720VENDOR_A_ATMEL_DB041D:
12099 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12100 break;
12101 case FLASH_5720VENDOR_M_ATMEL_DB081D:
12102 case FLASH_5720VENDOR_A_ATMEL_DB081D:
12103 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12104 break;
12105 default:
12106 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12107 break;
12108 }
12109 break;
12110 case FLASH_5720VENDOR_M_ST_M25PE10:
12111 case FLASH_5720VENDOR_M_ST_M45PE10:
12112 case FLASH_5720VENDOR_A_ST_M25PE10:
12113 case FLASH_5720VENDOR_A_ST_M45PE10:
12114 case FLASH_5720VENDOR_M_ST_M25PE20:
12115 case FLASH_5720VENDOR_M_ST_M45PE20:
12116 case FLASH_5720VENDOR_A_ST_M25PE20:
12117 case FLASH_5720VENDOR_A_ST_M45PE20:
12118 case FLASH_5720VENDOR_M_ST_M25PE40:
12119 case FLASH_5720VENDOR_M_ST_M45PE40:
12120 case FLASH_5720VENDOR_A_ST_M25PE40:
12121 case FLASH_5720VENDOR_A_ST_M45PE40:
12122 case FLASH_5720VENDOR_M_ST_M25PE80:
12123 case FLASH_5720VENDOR_M_ST_M45PE80:
12124 case FLASH_5720VENDOR_A_ST_M25PE80:
12125 case FLASH_5720VENDOR_A_ST_M45PE80:
12126 case FLASH_5720VENDOR_ST_25USPT:
12127 case FLASH_5720VENDOR_ST_45USPT:
12128 tp->nvram_jedecnum = JEDEC_ST;
12129 tp->tg3_flags |= TG3_FLAG_NVRAM_BUFFERED;
12130 tp->tg3_flags2 |= TG3_FLG2_FLASH;
12131
12132 switch (nvmpinstrp) {
12133 case FLASH_5720VENDOR_M_ST_M25PE20:
12134 case FLASH_5720VENDOR_M_ST_M45PE20:
12135 case FLASH_5720VENDOR_A_ST_M25PE20:
12136 case FLASH_5720VENDOR_A_ST_M45PE20:
12137 tp->nvram_size = TG3_NVRAM_SIZE_256KB;
12138 break;
12139 case FLASH_5720VENDOR_M_ST_M25PE40:
12140 case FLASH_5720VENDOR_M_ST_M45PE40:
12141 case FLASH_5720VENDOR_A_ST_M25PE40:
12142 case FLASH_5720VENDOR_A_ST_M45PE40:
12143 tp->nvram_size = TG3_NVRAM_SIZE_512KB;
12144 break;
12145 case FLASH_5720VENDOR_M_ST_M25PE80:
12146 case FLASH_5720VENDOR_M_ST_M45PE80:
12147 case FLASH_5720VENDOR_A_ST_M25PE80:
12148 case FLASH_5720VENDOR_A_ST_M45PE80:
12149 tp->nvram_size = TG3_NVRAM_SIZE_1MB;
12150 break;
12151 default:
12152 tp->nvram_size = TG3_NVRAM_SIZE_128KB;
12153 break;
12154 }
12155 break;
12156 default:
12157 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM;
12158 return;
12159 }
12160
12161 tg3_nvram_get_pagesize(tp, nvcfg1);
12162 if (tp->nvram_pagesize != 264 && tp->nvram_pagesize != 528)
12163 tp->tg3_flags3 |= TG3_FLG3_NO_NVRAM_ADDR_TRANS;
12164}
12165
Linus Torvalds1da177e2005-04-16 15:20:36 -070012166/* Chips other than 5700/5701 use the NVRAM for fetching info. */
12167static void __devinit tg3_nvram_init(struct tg3 *tp)
12168{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012169 tw32_f(GRC_EEPROM_ADDR,
12170 (EEPROM_ADDR_FSM_RESET |
12171 (EEPROM_DEFAULT_CLOCK_PERIOD <<
12172 EEPROM_ADDR_CLKPERD_SHIFT)));
12173
Michael Chan9d57f012006-12-07 00:23:25 -080012174 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012175
12176 /* Enable seeprom accesses. */
12177 tw32_f(GRC_LOCAL_CTRL,
12178 tr32(GRC_LOCAL_CTRL) | GRC_LCLCTRL_AUTO_SEEPROM);
12179 udelay(100);
12180
12181 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
12182 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) {
12183 tp->tg3_flags |= TG3_FLAG_NVRAM;
12184
Michael Chanec41c7d2006-01-17 02:40:55 -080012185 if (tg3_nvram_lock(tp)) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000012186 netdev_warn(tp->dev,
12187 "Cannot get nvram lock, %s failed\n",
Joe Perches05dbe002010-02-17 19:44:19 +000012188 __func__);
Michael Chanec41c7d2006-01-17 02:40:55 -080012189 return;
12190 }
Michael Chane6af3012005-04-21 17:12:05 -070012191 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012192
Matt Carlson989a9d22007-05-05 11:51:05 -070012193 tp->nvram_size = 0;
12194
Michael Chan361b4ac2005-04-21 17:11:21 -070012195 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
12196 tg3_get_5752_nvram_info(tp);
Michael Chand3c7b882006-03-23 01:28:25 -080012197 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
12198 tg3_get_5755_nvram_info(tp);
Matt Carlsond30cdd22007-10-07 23:28:35 -070012199 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson57e69832008-05-25 23:48:31 -070012200 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
12201 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
Michael Chan1b277772006-03-20 22:27:48 -080012202 tg3_get_5787_nvram_info(tp);
Matt Carlson6b91fa02007-10-10 18:01:09 -070012203 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761)
12204 tg3_get_5761_nvram_info(tp);
Michael Chanb5d37722006-09-27 16:06:21 -070012205 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
12206 tg3_get_5906_nvram_info(tp);
Matt Carlsonb703df62009-12-03 08:36:21 +000012207 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
12208 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Matt Carlson321d32a2008-11-21 17:22:19 -080012209 tg3_get_57780_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012210 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
12211 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlsona1b950d2009-09-01 13:20:17 +000012212 tg3_get_5717_nvram_info(tp);
Matt Carlson9b91b5f2011-04-05 14:22:47 +000012213 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
12214 tg3_get_5720_nvram_info(tp);
Michael Chan361b4ac2005-04-21 17:11:21 -070012215 else
12216 tg3_get_nvram_info(tp);
12217
Matt Carlson989a9d22007-05-05 11:51:05 -070012218 if (tp->nvram_size == 0)
12219 tg3_get_nvram_size(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012220
Michael Chane6af3012005-04-21 17:12:05 -070012221 tg3_disable_nvram_access(tp);
Michael Chan381291b2005-12-13 21:08:21 -080012222 tg3_nvram_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012223
12224 } else {
12225 tp->tg3_flags &= ~(TG3_FLAG_NVRAM | TG3_FLAG_NVRAM_BUFFERED);
12226
12227 tg3_get_eeprom_size(tp);
12228 }
12229}
12230
Linus Torvalds1da177e2005-04-16 15:20:36 -070012231static int tg3_nvram_write_block_using_eeprom(struct tg3 *tp,
12232 u32 offset, u32 len, u8 *buf)
12233{
12234 int i, j, rc = 0;
12235 u32 val;
12236
12237 for (i = 0; i < len; i += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012238 u32 addr;
Matt Carlsona9dc5292009-02-25 14:25:30 +000012239 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012240
12241 addr = offset + i;
12242
12243 memcpy(&data, buf + i, 4);
12244
Matt Carlson62cedd12009-04-20 14:52:29 -070012245 /*
12246 * The SEEPROM interface expects the data to always be opposite
12247 * the native endian format. We accomplish this by reversing
12248 * all the operations that would have been performed on the
12249 * data from a call to tg3_nvram_read_be32().
12250 */
12251 tw32(GRC_EEPROM_DATA, swab32(be32_to_cpu(data)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012252
12253 val = tr32(GRC_EEPROM_ADDR);
12254 tw32(GRC_EEPROM_ADDR, val | EEPROM_ADDR_COMPLETE);
12255
12256 val &= ~(EEPROM_ADDR_ADDR_MASK | EEPROM_ADDR_DEVID_MASK |
12257 EEPROM_ADDR_READ);
12258 tw32(GRC_EEPROM_ADDR, val |
12259 (0 << EEPROM_ADDR_DEVID_SHIFT) |
12260 (addr & EEPROM_ADDR_ADDR_MASK) |
12261 EEPROM_ADDR_START |
12262 EEPROM_ADDR_WRITE);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012263
Michael Chan9d57f012006-12-07 00:23:25 -080012264 for (j = 0; j < 1000; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012265 val = tr32(GRC_EEPROM_ADDR);
12266
12267 if (val & EEPROM_ADDR_COMPLETE)
12268 break;
Michael Chan9d57f012006-12-07 00:23:25 -080012269 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012270 }
12271 if (!(val & EEPROM_ADDR_COMPLETE)) {
12272 rc = -EBUSY;
12273 break;
12274 }
12275 }
12276
12277 return rc;
12278}
12279
12280/* offset and length are dword aligned */
12281static int tg3_nvram_write_block_unbuffered(struct tg3 *tp, u32 offset, u32 len,
12282 u8 *buf)
12283{
12284 int ret = 0;
12285 u32 pagesize = tp->nvram_pagesize;
12286 u32 pagemask = pagesize - 1;
12287 u32 nvram_cmd;
12288 u8 *tmp;
12289
12290 tmp = kmalloc(pagesize, GFP_KERNEL);
12291 if (tmp == NULL)
12292 return -ENOMEM;
12293
12294 while (len) {
12295 int j;
Michael Chane6af3012005-04-21 17:12:05 -070012296 u32 phy_addr, page_off, size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012297
12298 phy_addr = offset & ~pagemask;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012299
Linus Torvalds1da177e2005-04-16 15:20:36 -070012300 for (j = 0; j < pagesize; j += 4) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000012301 ret = tg3_nvram_read_be32(tp, phy_addr + j,
12302 (__be32 *) (tmp + j));
12303 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012304 break;
12305 }
12306 if (ret)
12307 break;
12308
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012309 page_off = offset & pagemask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012310 size = pagesize;
12311 if (len < size)
12312 size = len;
12313
12314 len -= size;
12315
12316 memcpy(tmp + page_off, buf, size);
12317
12318 offset = offset + (pagesize - page_off);
12319
Michael Chane6af3012005-04-21 17:12:05 -070012320 tg3_enable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012321
12322 /*
12323 * Before we can erase the flash page, we need
12324 * to issue a special "write enable" command.
12325 */
12326 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12327
12328 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12329 break;
12330
12331 /* Erase the target page */
12332 tw32(NVRAM_ADDR, phy_addr);
12333
12334 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR |
12335 NVRAM_CMD_FIRST | NVRAM_CMD_LAST | NVRAM_CMD_ERASE;
12336
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012337 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012338 break;
12339
12340 /* Issue another write enable to start the write. */
12341 nvram_cmd = NVRAM_CMD_WREN | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12342
12343 if (tg3_nvram_exec_cmd(tp, nvram_cmd))
12344 break;
12345
12346 for (j = 0; j < pagesize; j += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012347 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012348
Al Virob9fc7dc2007-12-17 22:59:57 -080012349 data = *((__be32 *) (tmp + j));
Matt Carlsona9dc5292009-02-25 14:25:30 +000012350
Al Virob9fc7dc2007-12-17 22:59:57 -080012351 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012352
12353 tw32(NVRAM_ADDR, phy_addr + j);
12354
12355 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE |
12356 NVRAM_CMD_WR;
12357
12358 if (j == 0)
12359 nvram_cmd |= NVRAM_CMD_FIRST;
12360 else if (j == (pagesize - 4))
12361 nvram_cmd |= NVRAM_CMD_LAST;
12362
12363 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12364 break;
12365 }
12366 if (ret)
12367 break;
12368 }
12369
12370 nvram_cmd = NVRAM_CMD_WRDI | NVRAM_CMD_GO | NVRAM_CMD_DONE;
12371 tg3_nvram_exec_cmd(tp, nvram_cmd);
12372
12373 kfree(tmp);
12374
12375 return ret;
12376}
12377
12378/* offset and length are dword aligned */
12379static int tg3_nvram_write_block_buffered(struct tg3 *tp, u32 offset, u32 len,
12380 u8 *buf)
12381{
12382 int i, ret = 0;
12383
12384 for (i = 0; i < len; i += 4, offset += 4) {
Al Virob9fc7dc2007-12-17 22:59:57 -080012385 u32 page_off, phy_addr, nvram_cmd;
12386 __be32 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012387
12388 memcpy(&data, buf + i, 4);
Al Virob9fc7dc2007-12-17 22:59:57 -080012389 tw32(NVRAM_WRDATA, be32_to_cpu(data));
Linus Torvalds1da177e2005-04-16 15:20:36 -070012390
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012391 page_off = offset % tp->nvram_pagesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012392
Michael Chan18201802006-03-20 22:29:15 -080012393 phy_addr = tg3_nvram_phys_addr(tp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012394
12395 tw32(NVRAM_ADDR, phy_addr);
12396
12397 nvram_cmd = NVRAM_CMD_GO | NVRAM_CMD_DONE | NVRAM_CMD_WR;
12398
Matt Carlsonc6cdf432010-04-05 10:19:26 +000012399 if (page_off == 0 || i == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012400 nvram_cmd |= NVRAM_CMD_FIRST;
Michael Chanf6d9a252006-04-29 19:00:24 -070012401 if (page_off == (tp->nvram_pagesize - 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012402 nvram_cmd |= NVRAM_CMD_LAST;
12403
12404 if (i == (len - 4))
12405 nvram_cmd |= NVRAM_CMD_LAST;
12406
Matt Carlson321d32a2008-11-21 17:22:19 -080012407 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5752 &&
12408 !(tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Michael Chan4c987482005-09-05 17:52:38 -070012409 (tp->nvram_jedecnum == JEDEC_ST) &&
12410 (nvram_cmd & NVRAM_CMD_FIRST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012411
12412 if ((ret = tg3_nvram_exec_cmd(tp,
12413 NVRAM_CMD_WREN | NVRAM_CMD_GO |
12414 NVRAM_CMD_DONE)))
12415
12416 break;
12417 }
12418 if (!(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12419 /* We always do complete word writes to eeprom. */
12420 nvram_cmd |= (NVRAM_CMD_FIRST | NVRAM_CMD_LAST);
12421 }
12422
12423 if ((ret = tg3_nvram_exec_cmd(tp, nvram_cmd)))
12424 break;
12425 }
12426 return ret;
12427}
12428
12429/* offset and length are dword aligned */
12430static int tg3_nvram_write_block(struct tg3 *tp, u32 offset, u32 len, u8 *buf)
12431{
12432 int ret;
12433
Linus Torvalds1da177e2005-04-16 15:20:36 -070012434 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012435 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl &
12436 ~GRC_LCLCTRL_GPIO_OUTPUT1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012437 udelay(40);
12438 }
12439
12440 if (!(tp->tg3_flags & TG3_FLAG_NVRAM)) {
12441 ret = tg3_nvram_write_block_using_eeprom(tp, offset, len, buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012442 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012443 u32 grc_mode;
12444
Michael Chanec41c7d2006-01-17 02:40:55 -080012445 ret = tg3_nvram_lock(tp);
12446 if (ret)
12447 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012448
Michael Chane6af3012005-04-21 17:12:05 -070012449 tg3_enable_nvram_access(tp);
12450 if ((tp->tg3_flags2 & TG3_FLG2_5750_PLUS) &&
Matt Carlsonf66a29b2009-11-13 13:03:36 +000012451 !(tp->tg3_flags3 & TG3_FLG3_PROTECTED_NVRAM))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012452 tw32(NVRAM_WRITE1, 0x406);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012453
12454 grc_mode = tr32(GRC_MODE);
12455 tw32(GRC_MODE, grc_mode | GRC_MODE_NVRAM_WR_ENABLE);
12456
12457 if ((tp->tg3_flags & TG3_FLAG_NVRAM_BUFFERED) ||
12458 !(tp->tg3_flags2 & TG3_FLG2_FLASH)) {
12459
12460 ret = tg3_nvram_write_block_buffered(tp, offset, len,
12461 buf);
Matt Carlson859a5882010-04-05 10:19:28 +000012462 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012463 ret = tg3_nvram_write_block_unbuffered(tp, offset, len,
12464 buf);
12465 }
12466
12467 grc_mode = tr32(GRC_MODE);
12468 tw32(GRC_MODE, grc_mode & ~GRC_MODE_NVRAM_WR_ENABLE);
12469
Michael Chane6af3012005-04-21 17:12:05 -070012470 tg3_disable_nvram_access(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012471 tg3_nvram_unlock(tp);
12472 }
12473
12474 if (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT) {
Michael Chan314fba32005-04-21 17:07:04 -070012475 tw32_f(GRC_LOCAL_CTRL, tp->grc_local_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012476 udelay(40);
12477 }
12478
12479 return ret;
12480}
12481
12482struct subsys_tbl_ent {
12483 u16 subsys_vendor, subsys_devid;
12484 u32 phy_id;
12485};
12486
Matt Carlson24daf2b2010-02-17 15:17:02 +000012487static struct subsys_tbl_ent subsys_id_to_phy_id[] __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012488 /* Broadcom boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012489 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012490 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A6, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012491 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012492 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A5, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012493 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012494 TG3PCI_SUBDEVICE_ID_BROADCOM_95700T6, TG3_PHY_ID_BCM8002 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012495 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12496 TG3PCI_SUBDEVICE_ID_BROADCOM_95700A9, 0 },
12497 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012498 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T1, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012499 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012500 TG3PCI_SUBDEVICE_ID_BROADCOM_95701T8, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012501 { TG3PCI_SUBVENDOR_ID_BROADCOM,
12502 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A7, 0 },
12503 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012504 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A10, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012505 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012506 TG3PCI_SUBDEVICE_ID_BROADCOM_95701A12, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012507 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012508 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX1, TG3_PHY_ID_BCM5703 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012509 { TG3PCI_SUBVENDOR_ID_BROADCOM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012510 TG3PCI_SUBDEVICE_ID_BROADCOM_95703AX2, TG3_PHY_ID_BCM5703 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012511
12512 /* 3com boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012513 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012514 TG3PCI_SUBDEVICE_ID_3COM_3C996T, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012515 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012516 TG3PCI_SUBDEVICE_ID_3COM_3C996BT, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012517 { TG3PCI_SUBVENDOR_ID_3COM,
12518 TG3PCI_SUBDEVICE_ID_3COM_3C996SX, 0 },
12519 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012520 TG3PCI_SUBDEVICE_ID_3COM_3C1000T, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012521 { TG3PCI_SUBVENDOR_ID_3COM,
Matt Carlson79eb6902010-02-17 15:17:03 +000012522 TG3PCI_SUBDEVICE_ID_3COM_3C940BR01, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012523
12524 /* DELL boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012525 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012526 TG3PCI_SUBDEVICE_ID_DELL_VIPER, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012527 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012528 TG3PCI_SUBDEVICE_ID_DELL_JAGUAR, TG3_PHY_ID_BCM5401 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012529 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012530 TG3PCI_SUBDEVICE_ID_DELL_MERLOT, TG3_PHY_ID_BCM5411 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012531 { TG3PCI_SUBVENDOR_ID_DELL,
Matt Carlson79eb6902010-02-17 15:17:03 +000012532 TG3PCI_SUBDEVICE_ID_DELL_SLIM_MERLOT, TG3_PHY_ID_BCM5411 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012533
12534 /* Compaq boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012535 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012536 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012537 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012538 TG3PCI_SUBDEVICE_ID_COMPAQ_BANSHEE_2, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012539 { TG3PCI_SUBVENDOR_ID_COMPAQ,
12540 TG3PCI_SUBDEVICE_ID_COMPAQ_CHANGELING, 0 },
12541 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012542 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780, TG3_PHY_ID_BCM5701 },
Matt Carlson24daf2b2010-02-17 15:17:02 +000012543 { TG3PCI_SUBVENDOR_ID_COMPAQ,
Matt Carlson79eb6902010-02-17 15:17:03 +000012544 TG3PCI_SUBDEVICE_ID_COMPAQ_NC7780_2, TG3_PHY_ID_BCM5701 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070012545
12546 /* IBM boards. */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012547 { TG3PCI_SUBVENDOR_ID_IBM,
12548 TG3PCI_SUBDEVICE_ID_IBM_5703SAX2, 0 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012549};
12550
Matt Carlson24daf2b2010-02-17 15:17:02 +000012551static struct subsys_tbl_ent * __devinit tg3_lookup_by_subsys(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012552{
12553 int i;
12554
12555 for (i = 0; i < ARRAY_SIZE(subsys_id_to_phy_id); i++) {
12556 if ((subsys_id_to_phy_id[i].subsys_vendor ==
12557 tp->pdev->subsystem_vendor) &&
12558 (subsys_id_to_phy_id[i].subsys_devid ==
12559 tp->pdev->subsystem_device))
12560 return &subsys_id_to_phy_id[i];
12561 }
12562 return NULL;
12563}
12564
Michael Chan7d0c41e2005-04-21 17:06:20 -070012565static void __devinit tg3_get_eeprom_hw_cfg(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012566{
Linus Torvalds1da177e2005-04-16 15:20:36 -070012567 u32 val;
Michael Chancaf636c72006-03-22 01:05:31 -080012568 u16 pmcsr;
12569
12570 /* On some early chips the SRAM cannot be accessed in D3hot state,
12571 * so need make sure we're in D0.
12572 */
12573 pci_read_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, &pmcsr);
12574 pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
12575 pci_write_config_word(tp->pdev, tp->pm_cap + PCI_PM_CTRL, pmcsr);
12576 msleep(1);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012577
12578 /* Make sure register accesses (indirect or otherwise)
12579 * will function correctly.
12580 */
12581 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
12582 tp->misc_host_ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012583
David S. Millerf49639e2006-06-09 11:58:36 -070012584 /* The memory arbiter has to be enabled in order for SRAM accesses
12585 * to succeed. Normally on powerup the tg3 chip firmware will make
12586 * sure it is enabled, but other entities such as system netboot
12587 * code might disable it.
12588 */
12589 val = tr32(MEMARB_MODE);
12590 tw32(MEMARB_MODE, val | MEMARB_MODE_ENABLE);
12591
Matt Carlson79eb6902010-02-17 15:17:03 +000012592 tp->phy_id = TG3_PHY_ID_INVALID;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012593 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12594
Gary Zambranoa85feb82007-05-05 11:52:19 -070012595 /* Assume an onboard device and WOL capable by default. */
12596 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT | TG3_FLAG_WOL_CAP;
David S. Miller72b845e2006-03-14 14:11:48 -080012597
Michael Chanb5d37722006-09-27 16:06:21 -070012598 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chan9d26e212006-12-07 00:21:14 -080012599 if (!(tr32(PCIE_TRANSACTION_CFG) & PCIE_TRANS_CFG_LOM)) {
Michael Chanb5d37722006-09-27 16:06:21 -070012600 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012601 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12602 }
Matt Carlson0527ba32007-10-10 18:03:30 -070012603 val = tr32(VCPU_CFGSHDW);
12604 if (val & VCPU_CFGSHDW_ASPM_DBNC)
Matt Carlson8ed5d972007-05-07 00:25:49 -070012605 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
Matt Carlson0527ba32007-10-10 18:03:30 -070012606 if ((val & VCPU_CFGSHDW_WOL_ENABLE) &&
Matt Carlson20232762008-12-21 20:18:56 -080012607 (val & VCPU_CFGSHDW_WOL_MAGPKT))
Matt Carlson0527ba32007-10-10 18:03:30 -070012608 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012609 goto done;
Michael Chanb5d37722006-09-27 16:06:21 -070012610 }
12611
Linus Torvalds1da177e2005-04-16 15:20:36 -070012612 tg3_read_mem(tp, NIC_SRAM_DATA_SIG, &val);
12613 if (val == NIC_SRAM_DATA_SIG_MAGIC) {
12614 u32 nic_cfg, led_cfg;
Matt Carlsona9daf362008-05-25 23:49:44 -070012615 u32 nic_phy_id, ver, cfg2 = 0, cfg4 = 0, eeprom_phy_id;
Michael Chan7d0c41e2005-04-21 17:06:20 -070012616 int eeprom_phy_serdes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012617
12618 tg3_read_mem(tp, NIC_SRAM_DATA_CFG, &nic_cfg);
12619 tp->nic_sram_data_cfg = nic_cfg;
12620
12621 tg3_read_mem(tp, NIC_SRAM_DATA_VER, &ver);
12622 ver >>= NIC_SRAM_DATA_VER_SHIFT;
12623 if ((GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700) &&
12624 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701) &&
12625 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5703) &&
12626 (ver > 0) && (ver < 0x100))
12627 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_2, &cfg2);
12628
Matt Carlsona9daf362008-05-25 23:49:44 -070012629 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785)
12630 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_4, &cfg4);
12631
Linus Torvalds1da177e2005-04-16 15:20:36 -070012632 if ((nic_cfg & NIC_SRAM_DATA_CFG_PHY_TYPE_MASK) ==
12633 NIC_SRAM_DATA_CFG_PHY_TYPE_FIBER)
12634 eeprom_phy_serdes = 1;
12635
12636 tg3_read_mem(tp, NIC_SRAM_DATA_PHY_ID, &nic_phy_id);
12637 if (nic_phy_id != 0) {
12638 u32 id1 = nic_phy_id & NIC_SRAM_DATA_PHY_ID1_MASK;
12639 u32 id2 = nic_phy_id & NIC_SRAM_DATA_PHY_ID2_MASK;
12640
12641 eeprom_phy_id = (id1 >> 16) << 10;
12642 eeprom_phy_id |= (id2 & 0xfc00) << 16;
12643 eeprom_phy_id |= (id2 & 0x03ff) << 0;
12644 } else
12645 eeprom_phy_id = 0;
12646
Michael Chan7d0c41e2005-04-21 17:06:20 -070012647 tp->phy_id = eeprom_phy_id;
Michael Chan747e8f82005-07-25 12:33:22 -070012648 if (eeprom_phy_serdes) {
Matt Carlsona50d0792010-06-05 17:24:37 +000012649 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012650 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Matt Carlsona50d0792010-06-05 17:24:37 +000012651 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012652 tp->phy_flags |= TG3_PHYFLG_MII_SERDES;
Michael Chan747e8f82005-07-25 12:33:22 -070012653 }
Michael Chan7d0c41e2005-04-21 17:06:20 -070012654
John W. Linvillecbf46852005-04-21 17:01:29 -070012655 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012656 led_cfg = cfg2 & (NIC_SRAM_DATA_CFG_LED_MODE_MASK |
12657 SHASTA_EXT_LED_MODE_MASK);
John W. Linvillecbf46852005-04-21 17:01:29 -070012658 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070012659 led_cfg = nic_cfg & NIC_SRAM_DATA_CFG_LED_MODE_MASK;
12660
12661 switch (led_cfg) {
12662 default:
12663 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_1:
12664 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12665 break;
12666
12667 case NIC_SRAM_DATA_CFG_LED_MODE_PHY_2:
12668 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12669 break;
12670
12671 case NIC_SRAM_DATA_CFG_LED_MODE_MAC:
12672 tp->led_ctrl = LED_CTRL_MODE_MAC;
Michael Chan9ba27792005-06-06 15:16:20 -070012673
12674 /* Default to PHY_1_MODE if 0 (MAC_MODE) is
12675 * read on some older 5700/5701 bootcode.
12676 */
12677 if (GET_ASIC_REV(tp->pci_chip_rev_id) ==
12678 ASIC_REV_5700 ||
12679 GET_ASIC_REV(tp->pci_chip_rev_id) ==
12680 ASIC_REV_5701)
12681 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
12682
Linus Torvalds1da177e2005-04-16 15:20:36 -070012683 break;
12684
12685 case SHASTA_EXT_LED_SHARED:
12686 tp->led_ctrl = LED_CTRL_MODE_SHARED;
12687 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0 &&
12688 tp->pci_chip_rev_id != CHIPREV_ID_5750_A1)
12689 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12690 LED_CTRL_MODE_PHY_2);
12691 break;
12692
12693 case SHASTA_EXT_LED_MAC:
12694 tp->led_ctrl = LED_CTRL_MODE_SHASTA_MAC;
12695 break;
12696
12697 case SHASTA_EXT_LED_COMBO:
12698 tp->led_ctrl = LED_CTRL_MODE_COMBO;
12699 if (tp->pci_chip_rev_id != CHIPREV_ID_5750_A0)
12700 tp->led_ctrl |= (LED_CTRL_MODE_PHY_1 |
12701 LED_CTRL_MODE_PHY_2);
12702 break;
12703
Stephen Hemminger855e1112008-04-16 16:37:28 -070012704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012705
12706 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
12707 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) &&
12708 tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)
12709 tp->led_ctrl = LED_CTRL_MODE_PHY_2;
12710
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012711 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5784_AX)
12712 tp->led_ctrl = LED_CTRL_MODE_PHY_1;
Matt Carlson5f608912007-11-12 21:17:07 -080012713
Michael Chan9d26e212006-12-07 00:21:14 -080012714 if (nic_cfg & NIC_SRAM_DATA_CFG_EEPROM_WP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012715 tp->tg3_flags |= TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012716 if ((tp->pdev->subsystem_vendor ==
12717 PCI_VENDOR_ID_ARIMA) &&
12718 (tp->pdev->subsystem_device == 0x205a ||
12719 tp->pdev->subsystem_device == 0x2063))
12720 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
12721 } else {
David S. Millerf49639e2006-06-09 11:58:36 -070012722 tp->tg3_flags &= ~TG3_FLAG_EEPROM_WRITE_PROT;
Michael Chan9d26e212006-12-07 00:21:14 -080012723 tp->tg3_flags2 |= TG3_FLG2_IS_NIC;
12724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070012725
12726 if (nic_cfg & NIC_SRAM_DATA_CFG_ASF_ENABLE) {
12727 tp->tg3_flags |= TG3_FLAG_ENABLE_ASF;
John W. Linvillecbf46852005-04-21 17:01:29 -070012728 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012729 tp->tg3_flags2 |= TG3_FLG2_ASF_NEW_HANDSHAKE;
12730 }
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012731
12732 if ((nic_cfg & NIC_SRAM_DATA_CFG_APE_ENABLE) &&
12733 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
Matt Carlson0d3031d2007-10-10 18:02:43 -070012734 tp->tg3_flags3 |= TG3_FLG3_ENABLE_APE;
Matt Carlsonb2b98d42008-11-03 16:52:32 -080012735
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012736 if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES &&
Gary Zambranoa85feb82007-05-05 11:52:19 -070012737 !(nic_cfg & NIC_SRAM_DATA_CFG_FIBER_WOL))
12738 tp->tg3_flags &= ~TG3_FLAG_WOL_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012739
Rafael J. Wysocki12dac072008-07-30 16:37:33 -070012740 if ((tp->tg3_flags & TG3_FLAG_WOL_CAP) &&
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012741 (nic_cfg & NIC_SRAM_DATA_CFG_WOL_ENABLE))
Matt Carlson0527ba32007-10-10 18:03:30 -070012742 tp->tg3_flags |= TG3_FLAG_WOL_ENABLE;
12743
Linus Torvalds1da177e2005-04-16 15:20:36 -070012744 if (cfg2 & (1 << 17))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012745 tp->phy_flags |= TG3_PHYFLG_CAPACITIVE_COUPLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012746
12747 /* serdes signal pre-emphasis in register 0x590 set by */
12748 /* bootcode if bit 18 is set */
12749 if (cfg2 & (1 << 18))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012750 tp->phy_flags |= TG3_PHYFLG_SERDES_PREEMPHASIS;
Matt Carlson8ed5d972007-05-07 00:25:49 -070012751
Matt Carlson1407deb2011-04-05 14:22:44 +000012752 if (((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) ||
Matt Carlson2e1e3292010-11-24 08:31:53 +000012753 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
12754 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX))) &&
Matt Carlson6833c042008-11-21 17:18:59 -080012755 (cfg2 & NIC_SRAM_DATA_CFG_2_APD_EN))
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012756 tp->phy_flags |= TG3_PHYFLG_ENABLE_APD;
Matt Carlson6833c042008-11-21 17:18:59 -080012757
Matt Carlson8c69b1e2010-08-02 11:26:00 +000012758 if ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
12759 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlson1407deb2011-04-05 14:22:44 +000012760 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Matt Carlson8ed5d972007-05-07 00:25:49 -070012761 u32 cfg3;
12762
12763 tg3_read_mem(tp, NIC_SRAM_DATA_CFG_3, &cfg3);
12764 if (cfg3 & NIC_SRAM_ASPM_DEBOUNCE)
12765 tp->tg3_flags |= TG3_FLAG_ASPM_WORKAROUND;
12766 }
Matt Carlsona9daf362008-05-25 23:49:44 -070012767
Matt Carlson14417062010-02-17 15:16:59 +000012768 if (cfg4 & NIC_SRAM_RGMII_INBAND_DISABLE)
12769 tp->tg3_flags3 |= TG3_FLG3_RGMII_INBAND_DISABLE;
Matt Carlsona9daf362008-05-25 23:49:44 -070012770 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_RX_EN)
12771 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_RX_EN;
12772 if (cfg4 & NIC_SRAM_RGMII_EXT_IBND_TX_EN)
12773 tp->tg3_flags3 |= TG3_FLG3_RGMII_EXT_IBND_TX_EN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012774 }
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012775done:
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012776 if (tp->tg3_flags & TG3_FLAG_WOL_CAP)
12777 device_set_wakeup_enable(&tp->pdev->dev,
Matt Carlson05ac4cb2008-11-03 16:53:46 -080012778 tp->tg3_flags & TG3_FLAG_WOL_ENABLE);
Rafael J. Wysocki43067ed2011-02-10 06:53:09 +000012779 else
12780 device_set_wakeup_capable(&tp->pdev->dev, false);
Michael Chan7d0c41e2005-04-21 17:06:20 -070012781}
12782
Matt Carlsonb2a5c192008-04-03 21:44:44 -070012783static int __devinit tg3_issue_otp_command(struct tg3 *tp, u32 cmd)
12784{
12785 int i;
12786 u32 val;
12787
12788 tw32(OTP_CTRL, cmd | OTP_CTRL_OTP_CMD_START);
12789 tw32(OTP_CTRL, cmd);
12790
12791 /* Wait for up to 1 ms for command to execute. */
12792 for (i = 0; i < 100; i++) {
12793 val = tr32(OTP_STATUS);
12794 if (val & OTP_STATUS_CMD_DONE)
12795 break;
12796 udelay(10);
12797 }
12798
12799 return (val & OTP_STATUS_CMD_DONE) ? 0 : -EBUSY;
12800}
12801
12802/* Read the gphy configuration from the OTP region of the chip. The gphy
12803 * configuration is a 32-bit value that straddles the alignment boundary.
12804 * We do two 32-bit reads and then shift and merge the results.
12805 */
12806static u32 __devinit tg3_read_otp_phycfg(struct tg3 *tp)
12807{
12808 u32 bhalf_otp, thalf_otp;
12809
12810 tw32(OTP_MODE, OTP_MODE_OTP_THRU_GRC);
12811
12812 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_INIT))
12813 return 0;
12814
12815 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC1);
12816
12817 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12818 return 0;
12819
12820 thalf_otp = tr32(OTP_READ_DATA);
12821
12822 tw32(OTP_ADDRESS, OTP_ADDRESS_MAGIC2);
12823
12824 if (tg3_issue_otp_command(tp, OTP_CTRL_OTP_CMD_READ))
12825 return 0;
12826
12827 bhalf_otp = tr32(OTP_READ_DATA);
12828
12829 return ((thalf_otp & 0x0000ffff) << 16) | (bhalf_otp >> 16);
12830}
12831
Matt Carlsone256f8a2011-03-09 16:58:24 +000012832static void __devinit tg3_phy_init_link_config(struct tg3 *tp)
12833{
12834 u32 adv = ADVERTISED_Autoneg |
12835 ADVERTISED_Pause;
12836
12837 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
12838 adv |= ADVERTISED_1000baseT_Half |
12839 ADVERTISED_1000baseT_Full;
12840
12841 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
12842 adv |= ADVERTISED_100baseT_Half |
12843 ADVERTISED_100baseT_Full |
12844 ADVERTISED_10baseT_Half |
12845 ADVERTISED_10baseT_Full |
12846 ADVERTISED_TP;
12847 else
12848 adv |= ADVERTISED_FIBRE;
12849
12850 tp->link_config.advertising = adv;
12851 tp->link_config.speed = SPEED_INVALID;
12852 tp->link_config.duplex = DUPLEX_INVALID;
12853 tp->link_config.autoneg = AUTONEG_ENABLE;
12854 tp->link_config.active_speed = SPEED_INVALID;
12855 tp->link_config.active_duplex = DUPLEX_INVALID;
12856 tp->link_config.orig_speed = SPEED_INVALID;
12857 tp->link_config.orig_duplex = DUPLEX_INVALID;
12858 tp->link_config.orig_autoneg = AUTONEG_INVALID;
12859}
12860
Michael Chan7d0c41e2005-04-21 17:06:20 -070012861static int __devinit tg3_phy_probe(struct tg3 *tp)
12862{
12863 u32 hw_phy_id_1, hw_phy_id_2;
12864 u32 hw_phy_id, hw_phy_id_masked;
12865 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012866
Matt Carlsone256f8a2011-03-09 16:58:24 +000012867 /* flow control autonegotiation is default behavior */
12868 tp->tg3_flags |= TG3_FLAG_PAUSE_AUTONEG;
12869 tp->link_config.flowctrl = FLOW_CTRL_TX | FLOW_CTRL_RX;
12870
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070012871 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB)
12872 return tg3_phy_init(tp);
12873
Linus Torvalds1da177e2005-04-16 15:20:36 -070012874 /* Reading the PHY ID register can conflict with ASF
Nick Andrew877d0312009-01-26 11:06:57 +010012875 * firmware access to the PHY hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012876 */
12877 err = 0;
Matt Carlson0d3031d2007-10-10 18:02:43 -070012878 if ((tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
12879 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)) {
Matt Carlson79eb6902010-02-17 15:17:03 +000012880 hw_phy_id = hw_phy_id_masked = TG3_PHY_ID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012881 } else {
12882 /* Now read the physical PHY_ID from the chip and verify
12883 * that it is sane. If it doesn't look good, we fall back
12884 * to either the hard-coded table based PHY_ID and failing
12885 * that the value found in the eeprom area.
12886 */
12887 err |= tg3_readphy(tp, MII_PHYSID1, &hw_phy_id_1);
12888 err |= tg3_readphy(tp, MII_PHYSID2, &hw_phy_id_2);
12889
12890 hw_phy_id = (hw_phy_id_1 & 0xffff) << 10;
12891 hw_phy_id |= (hw_phy_id_2 & 0xfc00) << 16;
12892 hw_phy_id |= (hw_phy_id_2 & 0x03ff) << 0;
12893
Matt Carlson79eb6902010-02-17 15:17:03 +000012894 hw_phy_id_masked = hw_phy_id & TG3_PHY_ID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012895 }
12896
Matt Carlson79eb6902010-02-17 15:17:03 +000012897 if (!err && TG3_KNOWN_PHY_ID(hw_phy_id_masked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012898 tp->phy_id = hw_phy_id;
Matt Carlson79eb6902010-02-17 15:17:03 +000012899 if (hw_phy_id_masked == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012900 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Michael Chanda6b2d02005-08-19 12:54:29 -070012901 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012902 tp->phy_flags &= ~TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012903 } else {
Matt Carlson79eb6902010-02-17 15:17:03 +000012904 if (tp->phy_id != TG3_PHY_ID_INVALID) {
Michael Chan7d0c41e2005-04-21 17:06:20 -070012905 /* Do nothing, phy ID already set up in
12906 * tg3_get_eeprom_hw_cfg().
12907 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012908 } else {
12909 struct subsys_tbl_ent *p;
12910
12911 /* No eeprom signature? Try the hardcoded
12912 * subsys device table.
12913 */
Matt Carlson24daf2b2010-02-17 15:17:02 +000012914 p = tg3_lookup_by_subsys(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070012915 if (!p)
12916 return -ENODEV;
12917
12918 tp->phy_id = p->phy_id;
12919 if (!tp->phy_id ||
Matt Carlson79eb6902010-02-17 15:17:03 +000012920 tp->phy_id == TG3_PHY_ID_BCM8002)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012921 tp->phy_flags |= TG3_PHYFLG_PHY_SERDES;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012922 }
12923 }
12924
Matt Carlsona6b68da2010-12-06 08:28:52 +000012925 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
12926 ((tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 &&
12927 tp->pci_chip_rev_id != CHIPREV_ID_5717_A0) ||
12928 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 &&
12929 tp->pci_chip_rev_id != CHIPREV_ID_57765_A0)))
Matt Carlson52b02d02010-10-14 10:37:41 +000012930 tp->phy_flags |= TG3_PHYFLG_EEE_CAP;
12931
Matt Carlsone256f8a2011-03-09 16:58:24 +000012932 tg3_phy_init_link_config(tp);
12933
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012934 if (!(tp->phy_flags & TG3_PHYFLG_ANY_SERDES) &&
Matt Carlson0d3031d2007-10-10 18:02:43 -070012935 !(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -070012936 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)) {
Michael Chan3600d912006-12-07 00:21:48 -080012937 u32 bmsr, adv_reg, tg3_ctrl, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012938
12939 tg3_readphy(tp, MII_BMSR, &bmsr);
12940 if (!tg3_readphy(tp, MII_BMSR, &bmsr) &&
12941 (bmsr & BMSR_LSTATUS))
12942 goto skip_phy_reset;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040012943
Linus Torvalds1da177e2005-04-16 15:20:36 -070012944 err = tg3_phy_reset(tp);
12945 if (err)
12946 return err;
12947
12948 adv_reg = (ADVERTISE_10HALF | ADVERTISE_10FULL |
12949 ADVERTISE_100HALF | ADVERTISE_100FULL |
12950 ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
12951 tg3_ctrl = 0;
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012952 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012953 tg3_ctrl = (MII_TG3_CTRL_ADV_1000_HALF |
12954 MII_TG3_CTRL_ADV_1000_FULL);
12955 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
12956 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0)
12957 tg3_ctrl |= (MII_TG3_CTRL_AS_MASTER |
12958 MII_TG3_CTRL_ENABLE_AS_MASTER);
12959 }
12960
Michael Chan3600d912006-12-07 00:21:48 -080012961 mask = (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
12962 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full |
12963 ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full);
12964 if (!tg3_copper_is_advertising_all(tp, mask)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012965 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
12966
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012967 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012968 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12969
12970 tg3_writephy(tp, MII_BMCR,
12971 BMCR_ANENABLE | BMCR_ANRESTART);
12972 }
12973 tg3_phy_set_wirespeed(tp);
12974
12975 tg3_writephy(tp, MII_ADVERTISE, adv_reg);
Matt Carlsonf07e9af2010-08-02 11:26:07 +000012976 if (!(tp->phy_flags & TG3_PHYFLG_10_100_ONLY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070012977 tg3_writephy(tp, MII_TG3_CTRL, tg3_ctrl);
12978 }
12979
12980skip_phy_reset:
Matt Carlson79eb6902010-02-17 15:17:03 +000012981 if ((tp->phy_id & TG3_PHY_ID_MASK) == TG3_PHY_ID_BCM5401) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012982 err = tg3_init_5401phy_dsp(tp);
12983 if (err)
12984 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012985
Linus Torvalds1da177e2005-04-16 15:20:36 -070012986 err = tg3_init_5401phy_dsp(tp);
12987 }
12988
Linus Torvalds1da177e2005-04-16 15:20:36 -070012989 return err;
12990}
12991
Matt Carlson184b8902010-04-05 10:19:25 +000012992static void __devinit tg3_read_vpd(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012993{
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012994 u8 *vpd_data;
Matt Carlson4181b2c2010-02-26 14:04:45 +000012995 unsigned int block_end, rosize, len;
Matt Carlson184b8902010-04-05 10:19:25 +000012996 int j, i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012997
Matt Carlsonc3e94502011-04-13 11:05:08 +000012998 vpd_data = (u8 *)tg3_vpd_readblock(tp);
Matt Carlsona4a8bb12010-09-15 09:00:00 +000012999 if (!vpd_data)
13000 goto out_no_vpd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013001
Matt Carlson4181b2c2010-02-26 14:04:45 +000013002 i = pci_vpd_find_tag(vpd_data, 0, TG3_NVM_VPD_LEN,
13003 PCI_VPD_LRDT_RO_DATA);
13004 if (i < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013005 goto out_not_found;
Matt Carlson4181b2c2010-02-26 14:04:45 +000013006
13007 rosize = pci_vpd_lrdt_size(&vpd_data[i]);
13008 block_end = i + PCI_VPD_LRDT_TAG_SIZE + rosize;
13009 i += PCI_VPD_LRDT_TAG_SIZE;
13010
13011 if (block_end > TG3_NVM_VPD_LEN)
13012 goto out_not_found;
13013
Matt Carlson184b8902010-04-05 10:19:25 +000013014 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13015 PCI_VPD_RO_KEYWORD_MFR_ID);
13016 if (j > 0) {
13017 len = pci_vpd_info_field_size(&vpd_data[j]);
13018
13019 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13020 if (j + len > block_end || len != 4 ||
13021 memcmp(&vpd_data[j], "1028", 4))
13022 goto partno;
13023
13024 j = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13025 PCI_VPD_RO_KEYWORD_VENDOR0);
13026 if (j < 0)
13027 goto partno;
13028
13029 len = pci_vpd_info_field_size(&vpd_data[j]);
13030
13031 j += PCI_VPD_INFO_FLD_HDR_SIZE;
13032 if (j + len > block_end)
13033 goto partno;
13034
13035 memcpy(tp->fw_ver, &vpd_data[j], len);
13036 strncat(tp->fw_ver, " bc ", TG3_NVM_VPD_LEN - len - 1);
13037 }
13038
13039partno:
Matt Carlson4181b2c2010-02-26 14:04:45 +000013040 i = pci_vpd_find_info_keyword(vpd_data, i, rosize,
13041 PCI_VPD_RO_KEYWORD_PARTNO);
13042 if (i < 0)
13043 goto out_not_found;
13044
13045 len = pci_vpd_info_field_size(&vpd_data[i]);
13046
13047 i += PCI_VPD_INFO_FLD_HDR_SIZE;
13048 if (len > TG3_BPN_SIZE ||
13049 (len + i) > TG3_NVM_VPD_LEN)
13050 goto out_not_found;
13051
13052 memcpy(tp->board_part_number, &vpd_data[i], len);
13053
Linus Torvalds1da177e2005-04-16 15:20:36 -070013054out_not_found:
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013055 kfree(vpd_data);
Matt Carlson37a949c2010-09-30 10:34:33 +000013056 if (tp->board_part_number[0])
Matt Carlsona4a8bb12010-09-15 09:00:00 +000013057 return;
13058
13059out_no_vpd:
Matt Carlson37a949c2010-09-30 10:34:33 +000013060 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717) {
13061 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717)
13062 strcpy(tp->board_part_number, "BCM5717");
13063 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718)
13064 strcpy(tp->board_part_number, "BCM5718");
13065 else
13066 goto nomatch;
13067 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780) {
13068 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57780)
13069 strcpy(tp->board_part_number, "BCM57780");
13070 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57760)
13071 strcpy(tp->board_part_number, "BCM57760");
13072 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790)
13073 strcpy(tp->board_part_number, "BCM57790");
13074 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57788)
13075 strcpy(tp->board_part_number, "BCM57788");
13076 else
13077 goto nomatch;
13078 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765) {
13079 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761)
13080 strcpy(tp->board_part_number, "BCM57761");
13081 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765)
13082 strcpy(tp->board_part_number, "BCM57765");
13083 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781)
13084 strcpy(tp->board_part_number, "BCM57781");
13085 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785)
13086 strcpy(tp->board_part_number, "BCM57785");
13087 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791)
13088 strcpy(tp->board_part_number, "BCM57791");
13089 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13090 strcpy(tp->board_part_number, "BCM57795");
13091 else
13092 goto nomatch;
13093 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanb5d37722006-09-27 16:06:21 -070013094 strcpy(tp->board_part_number, "BCM95906");
Matt Carlson37a949c2010-09-30 10:34:33 +000013095 } else {
13096nomatch:
Michael Chanb5d37722006-09-27 16:06:21 -070013097 strcpy(tp->board_part_number, "none");
Matt Carlson37a949c2010-09-30 10:34:33 +000013098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013099}
13100
Matt Carlson9c8a6202007-10-21 16:16:08 -070013101static int __devinit tg3_fw_img_is_valid(struct tg3 *tp, u32 offset)
13102{
13103 u32 val;
13104
Matt Carlsone4f34112009-02-25 14:25:00 +000013105 if (tg3_nvram_read(tp, offset, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013106 (val & 0xfc000000) != 0x0c000000 ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013107 tg3_nvram_read(tp, offset + 4, &val) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013108 val != 0)
13109 return 0;
13110
13111 return 1;
13112}
13113
Matt Carlsonacd9c112009-02-25 14:26:33 +000013114static void __devinit tg3_read_bc_ver(struct tg3 *tp)
13115{
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013116 u32 val, offset, start, ver_offset;
Matt Carlson75f99362010-04-05 10:19:24 +000013117 int i, dst_off;
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013118 bool newver = false;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013119
13120 if (tg3_nvram_read(tp, 0xc, &offset) ||
13121 tg3_nvram_read(tp, 0x4, &start))
13122 return;
13123
13124 offset = tg3_nvram_logical_addr(tp, offset);
13125
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013126 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013127 return;
13128
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013129 if ((val & 0xfc000000) == 0x0c000000) {
13130 if (tg3_nvram_read(tp, offset + 4, &val))
Matt Carlsonacd9c112009-02-25 14:26:33 +000013131 return;
13132
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013133 if (val == 0)
13134 newver = true;
13135 }
13136
Matt Carlson75f99362010-04-05 10:19:24 +000013137 dst_off = strlen(tp->fw_ver);
13138
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013139 if (newver) {
Matt Carlson75f99362010-04-05 10:19:24 +000013140 if (TG3_VER_SIZE - dst_off < 16 ||
13141 tg3_nvram_read(tp, offset + 8, &ver_offset))
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013142 return;
13143
13144 offset = offset + ver_offset - start;
13145 for (i = 0; i < 16; i += 4) {
13146 __be32 v;
13147 if (tg3_nvram_read_be32(tp, offset + i, &v))
13148 return;
13149
Matt Carlson75f99362010-04-05 10:19:24 +000013150 memcpy(tp->fw_ver + dst_off + i, &v, sizeof(v));
Matt Carlsonff3a7cb2009-02-25 14:26:58 +000013151 }
13152 } else {
13153 u32 major, minor;
13154
13155 if (tg3_nvram_read(tp, TG3_NVM_PTREV_BCVER, &ver_offset))
13156 return;
13157
13158 major = (ver_offset & TG3_NVM_BCVER_MAJMSK) >>
13159 TG3_NVM_BCVER_MAJSFT;
13160 minor = ver_offset & TG3_NVM_BCVER_MINMSK;
Matt Carlson75f99362010-04-05 10:19:24 +000013161 snprintf(&tp->fw_ver[dst_off], TG3_VER_SIZE - dst_off,
13162 "v%d.%02d", major, minor);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013163 }
13164}
13165
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013166static void __devinit tg3_read_hwsb_ver(struct tg3 *tp)
13167{
13168 u32 val, major, minor;
13169
13170 /* Use native endian representation */
13171 if (tg3_nvram_read(tp, TG3_NVM_HWSB_CFG1, &val))
13172 return;
13173
13174 major = (val & TG3_NVM_HWSB_CFG1_MAJMSK) >>
13175 TG3_NVM_HWSB_CFG1_MAJSFT;
13176 minor = (val & TG3_NVM_HWSB_CFG1_MINMSK) >>
13177 TG3_NVM_HWSB_CFG1_MINSFT;
13178
13179 snprintf(&tp->fw_ver[0], 32, "sb v%d.%02d", major, minor);
13180}
13181
Matt Carlsondfe00d72008-11-21 17:19:41 -080013182static void __devinit tg3_read_sb_ver(struct tg3 *tp, u32 val)
13183{
13184 u32 offset, major, minor, build;
13185
Matt Carlson75f99362010-04-05 10:19:24 +000013186 strncat(tp->fw_ver, "sb", TG3_VER_SIZE - strlen(tp->fw_ver) - 1);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013187
13188 if ((val & TG3_EEPROM_SB_FORMAT_MASK) != TG3_EEPROM_SB_FORMAT_1)
13189 return;
13190
13191 switch (val & TG3_EEPROM_SB_REVISION_MASK) {
13192 case TG3_EEPROM_SB_REVISION_0:
13193 offset = TG3_EEPROM_SB_F1R0_EDH_OFF;
13194 break;
13195 case TG3_EEPROM_SB_REVISION_2:
13196 offset = TG3_EEPROM_SB_F1R2_EDH_OFF;
13197 break;
13198 case TG3_EEPROM_SB_REVISION_3:
13199 offset = TG3_EEPROM_SB_F1R3_EDH_OFF;
13200 break;
Matt Carlsona4153d42010-02-17 15:16:56 +000013201 case TG3_EEPROM_SB_REVISION_4:
13202 offset = TG3_EEPROM_SB_F1R4_EDH_OFF;
13203 break;
13204 case TG3_EEPROM_SB_REVISION_5:
13205 offset = TG3_EEPROM_SB_F1R5_EDH_OFF;
13206 break;
Matt Carlsonbba226a2010-10-14 10:37:38 +000013207 case TG3_EEPROM_SB_REVISION_6:
13208 offset = TG3_EEPROM_SB_F1R6_EDH_OFF;
13209 break;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013210 default:
13211 return;
13212 }
13213
Matt Carlsone4f34112009-02-25 14:25:00 +000013214 if (tg3_nvram_read(tp, offset, &val))
Matt Carlsondfe00d72008-11-21 17:19:41 -080013215 return;
13216
13217 build = (val & TG3_EEPROM_SB_EDH_BLD_MASK) >>
13218 TG3_EEPROM_SB_EDH_BLD_SHFT;
13219 major = (val & TG3_EEPROM_SB_EDH_MAJ_MASK) >>
13220 TG3_EEPROM_SB_EDH_MAJ_SHFT;
13221 minor = val & TG3_EEPROM_SB_EDH_MIN_MASK;
13222
13223 if (minor > 99 || build > 26)
13224 return;
13225
Matt Carlson75f99362010-04-05 10:19:24 +000013226 offset = strlen(tp->fw_ver);
13227 snprintf(&tp->fw_ver[offset], TG3_VER_SIZE - offset,
13228 " v%d.%02d", major, minor);
Matt Carlsondfe00d72008-11-21 17:19:41 -080013229
13230 if (build > 0) {
Matt Carlson75f99362010-04-05 10:19:24 +000013231 offset = strlen(tp->fw_ver);
13232 if (offset < TG3_VER_SIZE - 1)
13233 tp->fw_ver[offset] = 'a' + build - 1;
Matt Carlsondfe00d72008-11-21 17:19:41 -080013234 }
13235}
13236
Matt Carlsonacd9c112009-02-25 14:26:33 +000013237static void __devinit tg3_read_mgmtfw_ver(struct tg3 *tp)
Michael Chanc4e65752006-03-20 22:29:32 -080013238{
13239 u32 val, offset, start;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013240 int i, vlen;
Matt Carlson9c8a6202007-10-21 16:16:08 -070013241
13242 for (offset = TG3_NVM_DIR_START;
13243 offset < TG3_NVM_DIR_END;
13244 offset += TG3_NVM_DIRENT_SIZE) {
Matt Carlsone4f34112009-02-25 14:25:00 +000013245 if (tg3_nvram_read(tp, offset, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013246 return;
13247
13248 if ((val >> TG3_NVM_DIRTYPE_SHIFT) == TG3_NVM_DIRTYPE_ASFINI)
13249 break;
13250 }
13251
13252 if (offset == TG3_NVM_DIR_END)
13253 return;
13254
13255 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS))
13256 start = 0x08000000;
Matt Carlsone4f34112009-02-25 14:25:00 +000013257 else if (tg3_nvram_read(tp, offset - 4, &start))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013258 return;
13259
Matt Carlsone4f34112009-02-25 14:25:00 +000013260 if (tg3_nvram_read(tp, offset + 4, &offset) ||
Matt Carlson9c8a6202007-10-21 16:16:08 -070013261 !tg3_fw_img_is_valid(tp, offset) ||
Matt Carlsone4f34112009-02-25 14:25:00 +000013262 tg3_nvram_read(tp, offset + 8, &val))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013263 return;
13264
13265 offset += val - start;
13266
Matt Carlsonacd9c112009-02-25 14:26:33 +000013267 vlen = strlen(tp->fw_ver);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013268
Matt Carlsonacd9c112009-02-25 14:26:33 +000013269 tp->fw_ver[vlen++] = ',';
13270 tp->fw_ver[vlen++] = ' ';
Matt Carlson9c8a6202007-10-21 16:16:08 -070013271
13272 for (i = 0; i < 4; i++) {
Matt Carlsona9dc5292009-02-25 14:25:30 +000013273 __be32 v;
13274 if (tg3_nvram_read_be32(tp, offset, &v))
Matt Carlson9c8a6202007-10-21 16:16:08 -070013275 return;
13276
Al Virob9fc7dc2007-12-17 22:59:57 -080013277 offset += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013278
Matt Carlsonacd9c112009-02-25 14:26:33 +000013279 if (vlen > TG3_VER_SIZE - sizeof(v)) {
13280 memcpy(&tp->fw_ver[vlen], &v, TG3_VER_SIZE - vlen);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013281 break;
13282 }
13283
Matt Carlsonacd9c112009-02-25 14:26:33 +000013284 memcpy(&tp->fw_ver[vlen], &v, sizeof(v));
13285 vlen += sizeof(v);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013286 }
Matt Carlsonacd9c112009-02-25 14:26:33 +000013287}
13288
Matt Carlson7fd76442009-02-25 14:27:20 +000013289static void __devinit tg3_read_dash_ver(struct tg3 *tp)
13290{
13291 int vlen;
13292 u32 apedata;
Matt Carlsonecc79642010-08-02 11:26:01 +000013293 char *fwtype;
Matt Carlson7fd76442009-02-25 14:27:20 +000013294
13295 if (!(tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) ||
13296 !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF))
13297 return;
13298
13299 apedata = tg3_ape_read32(tp, TG3_APE_SEG_SIG);
13300 if (apedata != APE_SEG_SIG_MAGIC)
13301 return;
13302
13303 apedata = tg3_ape_read32(tp, TG3_APE_FW_STATUS);
13304 if (!(apedata & APE_FW_STATUS_READY))
13305 return;
13306
13307 apedata = tg3_ape_read32(tp, TG3_APE_FW_VERSION);
13308
Matt Carlsondc6d0742010-09-15 08:59:55 +000013309 if (tg3_ape_read32(tp, TG3_APE_FW_FEATURES) & TG3_APE_FW_FEATURE_NCSI) {
13310 tp->tg3_flags3 |= TG3_FLG3_APE_HAS_NCSI;
Matt Carlsonecc79642010-08-02 11:26:01 +000013311 fwtype = "NCSI";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013312 } else {
Matt Carlsonecc79642010-08-02 11:26:01 +000013313 fwtype = "DASH";
Matt Carlsondc6d0742010-09-15 08:59:55 +000013314 }
Matt Carlsonecc79642010-08-02 11:26:01 +000013315
Matt Carlson7fd76442009-02-25 14:27:20 +000013316 vlen = strlen(tp->fw_ver);
13317
Matt Carlsonecc79642010-08-02 11:26:01 +000013318 snprintf(&tp->fw_ver[vlen], TG3_VER_SIZE - vlen, " %s v%d.%d.%d.%d",
13319 fwtype,
Matt Carlson7fd76442009-02-25 14:27:20 +000013320 (apedata & APE_FW_VERSION_MAJMSK) >> APE_FW_VERSION_MAJSFT,
13321 (apedata & APE_FW_VERSION_MINMSK) >> APE_FW_VERSION_MINSFT,
13322 (apedata & APE_FW_VERSION_REVMSK) >> APE_FW_VERSION_REVSFT,
13323 (apedata & APE_FW_VERSION_BLDMSK));
13324}
13325
Matt Carlsonacd9c112009-02-25 14:26:33 +000013326static void __devinit tg3_read_fw_ver(struct tg3 *tp)
13327{
13328 u32 val;
Matt Carlson75f99362010-04-05 10:19:24 +000013329 bool vpd_vers = false;
13330
13331 if (tp->fw_ver[0] != 0)
13332 vpd_vers = true;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013333
Matt Carlsondf259d82009-04-20 06:57:14 +000013334 if (tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) {
Matt Carlson75f99362010-04-05 10:19:24 +000013335 strcat(tp->fw_ver, "sb");
Matt Carlsondf259d82009-04-20 06:57:14 +000013336 return;
13337 }
13338
Matt Carlsonacd9c112009-02-25 14:26:33 +000013339 if (tg3_nvram_read(tp, 0, &val))
13340 return;
13341
13342 if (val == TG3_EEPROM_MAGIC)
13343 tg3_read_bc_ver(tp);
13344 else if ((val & TG3_EEPROM_MAGIC_FW_MSK) == TG3_EEPROM_MAGIC_FW)
13345 tg3_read_sb_ver(tp, val);
Matt Carlsona6f6cb12009-02-25 14:27:43 +000013346 else if ((val & TG3_EEPROM_MAGIC_HW_MSK) == TG3_EEPROM_MAGIC_HW)
13347 tg3_read_hwsb_ver(tp);
Matt Carlsonacd9c112009-02-25 14:26:33 +000013348 else
13349 return;
13350
13351 if (!(tp->tg3_flags & TG3_FLAG_ENABLE_ASF) ||
Matt Carlson75f99362010-04-05 10:19:24 +000013352 (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) || vpd_vers)
13353 goto done;
Matt Carlsonacd9c112009-02-25 14:26:33 +000013354
13355 tg3_read_mgmtfw_ver(tp);
Matt Carlson9c8a6202007-10-21 16:16:08 -070013356
Matt Carlson75f99362010-04-05 10:19:24 +000013357done:
Matt Carlson9c8a6202007-10-21 16:16:08 -070013358 tp->fw_ver[TG3_VER_SIZE - 1] = 0;
Michael Chanc4e65752006-03-20 22:29:32 -080013359}
13360
Michael Chan7544b092007-05-05 13:08:32 -070013361static struct pci_dev * __devinit tg3_find_peer(struct tg3 *);
13362
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013363static inline u32 tg3_rx_ret_ring_size(struct tg3 *tp)
13364{
Matt Carlsonde9f5232011-04-05 14:22:43 +000013365 if (tp->tg3_flags3 & TG3_FLG3_LRG_PROD_RING_CAP)
13366 return TG3_RX_RET_MAX_SIZE_5717;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013367 else if ((tp->tg3_flags & TG3_FLAG_JUMBO_CAPABLE) &&
13368 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Matt Carlsonde9f5232011-04-05 14:22:43 +000013369 return TG3_RX_RET_MAX_SIZE_5700;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013370 else
Matt Carlsonde9f5232011-04-05 14:22:43 +000013371 return TG3_RX_RET_MAX_SIZE_5705;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000013372}
13373
Matt Carlson41434702011-03-09 16:58:22 +000013374static DEFINE_PCI_DEVICE_TABLE(tg3_write_reorder_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080013375 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_FE_GATE_700C) },
13376 { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_8131_BRIDGE) },
13377 { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8385_0) },
13378 { },
13379};
13380
Linus Torvalds1da177e2005-04-16 15:20:36 -070013381static int __devinit tg3_get_invariants(struct tg3 *tp)
13382{
Linus Torvalds1da177e2005-04-16 15:20:36 -070013383 u32 misc_ctrl_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013384 u32 pci_state_reg, grc_misc_cfg;
13385 u32 val;
13386 u16 pci_cmd;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013387 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013388
Linus Torvalds1da177e2005-04-16 15:20:36 -070013389 /* Force memory write invalidate off. If we leave it on,
13390 * then on 5700_BX chips we have to enable a workaround.
13391 * The workaround is to set the TG3PCI_DMA_RW_CTRL boundary
13392 * to match the cacheline size. The Broadcom driver have this
13393 * workaround but turns MWI off all the times so never uses
13394 * it. This seems to suggest that the workaround is insufficient.
13395 */
13396 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13397 pci_cmd &= ~PCI_COMMAND_INVALIDATE;
13398 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13399
13400 /* It is absolutely critical that TG3PCI_MISC_HOST_CTRL
13401 * has the register indirect write enable bit set before
13402 * we try to access any of the MMIO registers. It is also
13403 * critical that the PCI-X hw workaround situation is decided
13404 * before that as well.
13405 */
13406 pci_read_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13407 &misc_ctrl_reg);
13408
13409 tp->pci_chip_rev_id = (misc_ctrl_reg >>
13410 MISC_HOST_CTRL_CHIPREV_SHIFT);
Matt Carlson795d01c2007-10-07 23:28:17 -070013411 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_USE_PROD_ID_REG) {
13412 u32 prod_id_asic_rev;
13413
Matt Carlson5001e2f2009-11-13 13:03:51 +000013414 if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_5717 ||
13415 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5718 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013416 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5719 ||
13417 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5720)
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013418 pci_read_config_dword(tp->pdev,
13419 TG3PCI_GEN2_PRODID_ASICREV,
13420 &prod_id_asic_rev);
Matt Carlsonb703df62009-12-03 08:36:21 +000013421 else if (tp->pdev->device == TG3PCI_DEVICE_TIGON3_57781 ||
13422 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57785 ||
13423 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57761 ||
13424 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57765 ||
13425 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
13426 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795)
13427 pci_read_config_dword(tp->pdev,
13428 TG3PCI_GEN15_PRODID_ASICREV,
13429 &prod_id_asic_rev);
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013430 else
13431 pci_read_config_dword(tp->pdev, TG3PCI_PRODID_ASICREV,
13432 &prod_id_asic_rev);
13433
Matt Carlson321d32a2008-11-21 17:22:19 -080013434 tp->pci_chip_rev_id = prod_id_asic_rev;
Matt Carlson795d01c2007-10-07 23:28:17 -070013435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013436
Michael Chanff645be2005-04-21 17:09:53 -070013437 /* Wrong chip ID in 5752 A0. This code can be removed later
13438 * as A0 is not in production.
13439 */
13440 if (tp->pci_chip_rev_id == CHIPREV_ID_5752_A0_HW)
13441 tp->pci_chip_rev_id = CHIPREV_ID_5752_A0;
13442
Michael Chan68929142005-08-09 20:17:14 -070013443 /* If we have 5702/03 A1 or A2 on certain ICH chipsets,
13444 * we need to disable memory and use config. cycles
13445 * only to access all registers. The 5702/03 chips
13446 * can mistakenly decode the special cycles from the
13447 * ICH chipsets as memory write cycles, causing corruption
13448 * of register and memory space. Only certain ICH bridges
13449 * will drive special cycles with non-zero data during the
13450 * address phase which can fall within the 5703's address
13451 * range. This is not an ICH bug as the PCI spec allows
13452 * non-zero address during special cycles. However, only
13453 * these ICH bridges are known to drive non-zero addresses
13454 * during special cycles.
13455 *
13456 * Since special cycles do not cross PCI bridges, we only
13457 * enable this workaround if the 5703 is on the secondary
13458 * bus of these ICH bridges.
13459 */
13460 if ((tp->pci_chip_rev_id == CHIPREV_ID_5703_A1) ||
13461 (tp->pci_chip_rev_id == CHIPREV_ID_5703_A2)) {
13462 static struct tg3_dev_id {
13463 u32 vendor;
13464 u32 device;
13465 u32 rev;
13466 } ich_chipsets[] = {
13467 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_8,
13468 PCI_ANY_ID },
13469 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_8,
13470 PCI_ANY_ID },
13471 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_11,
13472 0xa },
13473 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_6,
13474 PCI_ANY_ID },
13475 { },
13476 };
13477 struct tg3_dev_id *pci_id = &ich_chipsets[0];
13478 struct pci_dev *bridge = NULL;
13479
13480 while (pci_id->vendor != 0) {
13481 bridge = pci_get_device(pci_id->vendor, pci_id->device,
13482 bridge);
13483 if (!bridge) {
13484 pci_id++;
13485 continue;
13486 }
13487 if (pci_id->rev != PCI_ANY_ID) {
Auke Kok44c10132007-06-08 15:46:36 -070013488 if (bridge->revision > pci_id->rev)
Michael Chan68929142005-08-09 20:17:14 -070013489 continue;
13490 }
13491 if (bridge->subordinate &&
13492 (bridge->subordinate->number ==
13493 tp->pdev->bus->number)) {
13494
13495 tp->tg3_flags2 |= TG3_FLG2_ICH_WORKAROUND;
13496 pci_dev_put(bridge);
13497 break;
13498 }
13499 }
13500 }
13501
Matt Carlson41588ba2008-04-19 18:12:33 -070013502 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)) {
13503 static struct tg3_dev_id {
13504 u32 vendor;
13505 u32 device;
13506 } bridge_chipsets[] = {
13507 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_0 },
13508 { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_PXH_1 },
13509 { },
13510 };
13511 struct tg3_dev_id *pci_id = &bridge_chipsets[0];
13512 struct pci_dev *bridge = NULL;
13513
13514 while (pci_id->vendor != 0) {
13515 bridge = pci_get_device(pci_id->vendor,
13516 pci_id->device,
13517 bridge);
13518 if (!bridge) {
13519 pci_id++;
13520 continue;
13521 }
13522 if (bridge->subordinate &&
13523 (bridge->subordinate->number <=
13524 tp->pdev->bus->number) &&
13525 (bridge->subordinate->subordinate >=
13526 tp->pdev->bus->number)) {
13527 tp->tg3_flags3 |= TG3_FLG3_5701_DMA_BUG;
13528 pci_dev_put(bridge);
13529 break;
13530 }
13531 }
13532 }
13533
Michael Chan4a29cc22006-03-19 13:21:12 -080013534 /* The EPB bridge inside 5714, 5715, and 5780 cannot support
13535 * DMA addresses > 40-bit. This bridge may have other additional
13536 * 57xx devices behind it in some 4-port NIC designs for example.
13537 * Any tg3 device found behind the bridge will also need the 40-bit
13538 * DMA workaround.
13539 */
Michael Chana4e2b342005-10-26 15:46:52 -070013540 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780 ||
13541 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
13542 tp->tg3_flags2 |= TG3_FLG2_5780_CLASS;
Michael Chan4a29cc22006-03-19 13:21:12 -080013543 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
Michael Chan4cf78e42005-07-25 12:29:19 -070013544 tp->msi_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_MSI);
Matt Carlson859a5882010-04-05 10:19:28 +000013545 } else {
Michael Chan4a29cc22006-03-19 13:21:12 -080013546 struct pci_dev *bridge = NULL;
13547
13548 do {
13549 bridge = pci_get_device(PCI_VENDOR_ID_SERVERWORKS,
13550 PCI_DEVICE_ID_SERVERWORKS_EPB,
13551 bridge);
13552 if (bridge && bridge->subordinate &&
13553 (bridge->subordinate->number <=
13554 tp->pdev->bus->number) &&
13555 (bridge->subordinate->subordinate >=
13556 tp->pdev->bus->number)) {
13557 tp->tg3_flags |= TG3_FLAG_40BIT_DMA_BUG;
13558 pci_dev_put(bridge);
13559 break;
13560 }
13561 } while (bridge);
13562 }
Michael Chan4cf78e42005-07-25 12:29:19 -070013563
Linus Torvalds1da177e2005-04-16 15:20:36 -070013564 /* Initialize misc host control in PCI block. */
13565 tp->misc_host_ctrl |= (misc_ctrl_reg &
13566 MISC_HOST_CTRL_CHIPREV);
13567 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
13568 tp->misc_host_ctrl);
13569
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013570 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704 ||
13571 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013572 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13573 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Michael Chan7544b092007-05-05 13:08:32 -070013574 tp->pdev_peer = tg3_find_peer(tp);
13575
Matt Carlsonc885e822010-08-02 11:25:57 +000013576 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsond78b59f2011-04-05 14:22:46 +000013577 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13578 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlson0a58d662011-04-05 14:22:45 +000013579 tp->tg3_flags3 |= TG3_FLG3_5717_PLUS;
13580
13581 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765 ||
13582 (tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Matt Carlson1407deb2011-04-05 14:22:44 +000013583 tp->tg3_flags3 |= TG3_FLG3_57765_PLUS;
Matt Carlsonc885e822010-08-02 11:25:57 +000013584
Matt Carlson321d32a2008-11-21 17:22:19 -080013585 /* Intentionally exclude ASIC_REV_5906 */
13586 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Michael Chand9ab5ad2006-03-20 22:27:35 -080013587 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013588 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013589 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013590 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013591 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +000013592 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS))
Matt Carlson321d32a2008-11-21 17:22:19 -080013593 tp->tg3_flags3 |= TG3_FLG3_5755_PLUS;
13594
13595 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
13596 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
Michael Chanb5d37722006-09-27 16:06:21 -070013597 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013598 (tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chana4e2b342005-10-26 15:46:52 -070013599 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
John W. Linville6708e5c2005-04-21 17:00:52 -070013600 tp->tg3_flags2 |= TG3_FLG2_5750_PLUS;
13601
John W. Linville1b440c562005-04-21 17:03:18 -070013602 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) ||
13603 (tp->tg3_flags2 & TG3_FLG2_5750_PLUS))
13604 tp->tg3_flags2 |= TG3_FLG2_5705_PLUS;
13605
Matt Carlson027455a2008-12-21 20:19:30 -080013606 /* 5700 B0 chips do not support checksumming correctly due
13607 * to hardware bugs.
13608 */
Michał Mirosławdc668912011-04-07 03:35:07 +000013609 if (tp->pci_chip_rev_id != CHIPREV_ID_5700_B0) {
13610 u32 features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013611
Matt Carlson027455a2008-12-21 20:19:30 -080013612 if (tp->tg3_flags3 & TG3_FLG3_5755_PLUS)
Eric Dumazet7fe876a2010-07-08 06:14:55 +000013613 features |= NETIF_F_IPV6_CSUM;
13614 tp->dev->features |= features;
Michał Mirosławdc668912011-04-07 03:35:07 +000013615 tp->dev->hw_features |= features;
13616 tp->dev->vlan_features |= features;
Matt Carlson027455a2008-12-21 20:19:30 -080013617 }
13618
Matt Carlson507399f2009-11-13 13:03:37 +000013619 /* Determine TSO capabilities */
Matt Carlson2866d952011-02-10 20:06:46 -080013620 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719)
Matt Carlson4d163b72011-01-25 15:58:48 +000013621 ; /* Do nothing. HW bug. */
Matt Carlson1407deb2011-04-05 14:22:44 +000013622 else if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
Matt Carlsone849cdc2009-11-13 13:03:38 +000013623 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_3;
13624 else if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
13625 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlson507399f2009-11-13 13:03:37 +000013626 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_2;
13627 else if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
13628 tp->tg3_flags2 |= TG3_FLG2_HW_TSO_1 | TG3_FLG2_TSO_BUG;
13629 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 &&
13630 tp->pci_chip_rev_id >= CHIPREV_ID_5750_C2)
13631 tp->tg3_flags2 &= ~TG3_FLG2_TSO_BUG;
13632 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
13633 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
13634 tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) {
13635 tp->tg3_flags2 |= TG3_FLG2_TSO_BUG;
13636 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705)
13637 tp->fw_needed = FIRMWARE_TG3TSO5;
13638 else
13639 tp->fw_needed = FIRMWARE_TG3TSO;
13640 }
13641
13642 tp->irq_max = 1;
13643
Michael Chan5a6f3072006-03-20 22:28:05 -080013644 if (tp->tg3_flags2 & TG3_FLG2_5750_PLUS) {
Michael Chan7544b092007-05-05 13:08:32 -070013645 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSI;
13646 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_AX ||
13647 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5750_BX ||
13648 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714 &&
13649 tp->pci_chip_rev_id <= CHIPREV_ID_5714_A2 &&
13650 tp->pdev_peer == tp->pdev))
13651 tp->tg3_flags &= ~TG3_FLAG_SUPPORT_MSI;
13652
Matt Carlson321d32a2008-11-21 17:22:19 -080013653 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) ||
Michael Chanb5d37722006-09-27 16:06:21 -070013654 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
Michael Chanfcfa0a32006-03-20 22:28:41 -080013655 tp->tg3_flags2 |= TG3_FLG2_1SHOT_MSI;
Michael Chan52c0fd82006-06-29 20:15:54 -070013656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013657
Matt Carlson1407deb2011-04-05 14:22:44 +000013658 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson507399f2009-11-13 13:03:37 +000013659 tp->tg3_flags |= TG3_FLAG_SUPPORT_MSIX;
13660 tp->irq_max = TG3_IRQ_MAX_VECS;
13661 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013662 }
Matt Carlson0e1406d2009-11-02 12:33:33 +000013663
Matt Carlson615774f2009-11-13 13:03:39 +000013664 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
Matt Carlsona50d0792010-06-05 17:24:37 +000013665 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
Matt Carlson615774f2009-11-13 13:03:39 +000013666 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
13667 tp->tg3_flags3 |= TG3_FLG3_SHORT_DMA_BUG;
13668 else if (!(tp->tg3_flags3 & TG3_FLG3_5755_PLUS)) {
13669 tp->tg3_flags3 |= TG3_FLG3_4G_DMA_BNDRY_BUG;
13670 tp->tg3_flags3 |= TG3_FLG3_40BIT_DMA_LIMIT_BUG;
Matt Carlson0e1406d2009-11-02 12:33:33 +000013671 }
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013672
Matt Carlson0a58d662011-04-05 14:22:45 +000013673 if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS)
Matt Carlsonde9f5232011-04-05 14:22:43 +000013674 tp->tg3_flags3 |= TG3_FLG3_LRG_PROD_RING_CAP;
13675
Matt Carlson1407deb2011-04-05 14:22:44 +000013676 if ((tp->tg3_flags3 & TG3_FLG3_57765_PLUS) &&
Matt Carlson2866d952011-02-10 20:06:46 -080013677 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5719)
Matt Carlsonb703df62009-12-03 08:36:21 +000013678 tp->tg3_flags3 |= TG3_FLG3_USE_JUMBO_BDFLAG;
13679
Matt Carlsonf51f3562008-05-25 23:45:08 -070013680 if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
Matt Carlsonc6cdf432010-04-05 10:19:26 +000013681 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS) ||
13682 (tp->tg3_flags3 & TG3_FLG3_USE_JUMBO_BDFLAG))
Matt Carlson8f666b02009-08-28 13:58:24 +000013683 tp->tg3_flags |= TG3_FLAG_JUMBO_CAPABLE;
Michael Chan0f893dc2005-07-25 12:30:38 -070013684
Matt Carlson52f44902008-11-21 17:17:04 -080013685 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
13686 &pci_state_reg);
13687
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013688 tp->pcie_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_EXP);
13689 if (tp->pcie_cap != 0) {
13690 u16 lnkctl;
13691
Linus Torvalds1da177e2005-04-16 15:20:36 -070013692 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013693
Matt Carlsoncf790032010-11-24 08:31:48 +000013694 tp->pcie_readrq = 4096;
Matt Carlsond78b59f2011-04-05 14:22:46 +000013695 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5719 ||
13696 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
Matt Carlsonb4495ed2011-01-25 15:58:47 +000013697 tp->pcie_readrq = 2048;
Matt Carlsoncf790032010-11-24 08:31:48 +000013698
13699 pcie_set_readrq(tp->pdev, tp->pcie_readrq);
Matt Carlson5f5c51e2007-11-12 21:19:37 -080013700
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013701 pci_read_config_word(tp->pdev,
13702 tp->pcie_cap + PCI_EXP_LNKCTL,
13703 &lnkctl);
13704 if (lnkctl & PCI_EXP_LNKCTL_CLKREQ_EN) {
13705 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanc7835a72006-11-15 21:14:42 -080013706 tp->tg3_flags2 &= ~TG3_FLG2_HW_TSO_2;
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013707 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013708 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson9cf74eb2009-04-20 06:58:27 +000013709 tp->pci_chip_rev_id == CHIPREV_ID_57780_A0 ||
13710 tp->pci_chip_rev_id == CHIPREV_ID_57780_A1)
Matt Carlson5e7dfd02008-11-21 17:18:16 -080013711 tp->tg3_flags3 |= TG3_FLG3_CLKREQ_BUG;
Matt Carlson614b05902010-01-20 16:58:02 +000013712 } else if (tp->pci_chip_rev_id == CHIPREV_ID_5717_A0) {
13713 tp->tg3_flags3 |= TG3_FLG3_L1PLLPD_EN;
Michael Chanc7835a72006-11-15 21:14:42 -080013714 }
Matt Carlson52f44902008-11-21 17:17:04 -080013715 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785) {
Matt Carlsonfcb389d2008-11-03 16:55:44 -080013716 tp->tg3_flags2 |= TG3_FLG2_PCI_EXPRESS;
Matt Carlson52f44902008-11-21 17:17:04 -080013717 } else if (!(tp->tg3_flags2 & TG3_FLG2_5705_PLUS) ||
13718 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
13719 tp->pcix_cap = pci_find_capability(tp->pdev, PCI_CAP_ID_PCIX);
13720 if (!tp->pcix_cap) {
Matt Carlson2445e462010-04-05 10:19:21 +000013721 dev_err(&tp->pdev->dev,
13722 "Cannot find PCI-X capability, aborting\n");
Matt Carlson52f44902008-11-21 17:17:04 -080013723 return -EIO;
13724 }
13725
13726 if (!(pci_state_reg & PCISTATE_CONV_PCI_MODE))
13727 tp->tg3_flags |= TG3_FLAG_PCIX_MODE;
13728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013729
Michael Chan399de502005-10-03 14:02:39 -070013730 /* If we have an AMD 762 or VIA K8T800 chipset, write
13731 * reordering to the mailbox registers done by the host
13732 * controller can cause major troubles. We read back from
13733 * every mailbox register write to force the writes to be
13734 * posted to the chip in order.
13735 */
Matt Carlson41434702011-03-09 16:58:22 +000013736 if (pci_dev_present(tg3_write_reorder_chipsets) &&
Michael Chan399de502005-10-03 14:02:39 -070013737 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
13738 tp->tg3_flags |= TG3_FLAG_MBOX_WRITE_REORDER;
13739
Matt Carlson69fc4052008-12-21 20:19:57 -080013740 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE,
13741 &tp->pci_cacheline_sz);
13742 pci_read_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13743 &tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013744 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
13745 tp->pci_lat_timer < 64) {
13746 tp->pci_lat_timer = 64;
Matt Carlson69fc4052008-12-21 20:19:57 -080013747 pci_write_config_byte(tp->pdev, PCI_LATENCY_TIMER,
13748 tp->pci_lat_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013749 }
13750
Matt Carlson52f44902008-11-21 17:17:04 -080013751 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5700_BX) {
13752 /* 5700 BX chips need to have their TX producer index
13753 * mailboxes written twice to workaround a bug.
13754 */
13755 tp->tg3_flags |= TG3_FLAG_TXD_MBOX_HWBUG;
Matt Carlson9974a352007-10-07 23:27:28 -070013756
Matt Carlson52f44902008-11-21 17:17:04 -080013757 /* If we are in PCI-X mode, enable register write workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013758 *
13759 * The workaround is to use indirect register accesses
13760 * for all chip writes not to mailbox registers.
13761 */
Matt Carlson52f44902008-11-21 17:17:04 -080013762 if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070013763 u32 pm_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013764
13765 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
13766
13767 /* The chip can have it's power management PCI config
13768 * space registers clobbered due to this bug.
13769 * So explicitly force the chip into D0 here.
13770 */
Matt Carlson9974a352007-10-07 23:27:28 -070013771 pci_read_config_dword(tp->pdev,
13772 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013773 &pm_reg);
13774 pm_reg &= ~PCI_PM_CTRL_STATE_MASK;
13775 pm_reg |= PCI_PM_CTRL_PME_ENABLE | 0 /* D0 */;
Matt Carlson9974a352007-10-07 23:27:28 -070013776 pci_write_config_dword(tp->pdev,
13777 tp->pm_cap + PCI_PM_CTRL,
Linus Torvalds1da177e2005-04-16 15:20:36 -070013778 pm_reg);
13779
13780 /* Also, force SERR#/PERR# in PCI command. */
13781 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13782 pci_cmd |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR;
13783 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13784 }
13785 }
13786
Linus Torvalds1da177e2005-04-16 15:20:36 -070013787 if ((pci_state_reg & PCISTATE_BUS_SPEED_HIGH) != 0)
13788 tp->tg3_flags |= TG3_FLAG_PCI_HIGH_SPEED;
13789 if ((pci_state_reg & PCISTATE_BUS_32BIT) != 0)
13790 tp->tg3_flags |= TG3_FLAG_PCI_32BIT;
13791
13792 /* Chip-specific fixup from Broadcom driver */
13793 if ((tp->pci_chip_rev_id == CHIPREV_ID_5704_A0) &&
13794 (!(pci_state_reg & PCISTATE_RETRY_SAME_DMA))) {
13795 pci_state_reg |= PCISTATE_RETRY_SAME_DMA;
13796 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE, pci_state_reg);
13797 }
13798
Michael Chan1ee582d2005-08-09 20:16:46 -070013799 /* Default fast path register access methods */
Michael Chan20094932005-08-09 20:16:32 -070013800 tp->read32 = tg3_read32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013801 tp->write32 = tg3_write32;
Michael Chan09ee9292005-08-09 20:17:00 -070013802 tp->read32_mbox = tg3_read32;
Michael Chan20094932005-08-09 20:16:32 -070013803 tp->write32_mbox = tg3_write32;
Michael Chan1ee582d2005-08-09 20:16:46 -070013804 tp->write32_tx_mbox = tg3_write32;
13805 tp->write32_rx_mbox = tg3_write32;
13806
13807 /* Various workaround register access methods */
13808 if (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG)
13809 tp->write32 = tg3_write_indirect_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013810 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 ||
13811 ((tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) &&
13812 tp->pci_chip_rev_id == CHIPREV_ID_5750_A0)) {
13813 /*
13814 * Back to back register writes can cause problems on these
13815 * chips, the workaround is to read back all reg writes
13816 * except those to mailbox regs.
13817 *
13818 * See tg3_write_indirect_reg32().
13819 */
Michael Chan1ee582d2005-08-09 20:16:46 -070013820 tp->write32 = tg3_write_flush_reg32;
Matt Carlson98efd8a2007-05-05 12:47:25 -070013821 }
13822
Michael Chan1ee582d2005-08-09 20:16:46 -070013823 if ((tp->tg3_flags & TG3_FLAG_TXD_MBOX_HWBUG) ||
13824 (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)) {
13825 tp->write32_tx_mbox = tg3_write32_tx_mbox;
13826 if (tp->tg3_flags & TG3_FLAG_MBOX_WRITE_REORDER)
13827 tp->write32_rx_mbox = tg3_write_flush_reg32;
13828 }
Michael Chan20094932005-08-09 20:16:32 -070013829
Michael Chan68929142005-08-09 20:17:14 -070013830 if (tp->tg3_flags2 & TG3_FLG2_ICH_WORKAROUND) {
13831 tp->read32 = tg3_read_indirect_reg32;
13832 tp->write32 = tg3_write_indirect_reg32;
13833 tp->read32_mbox = tg3_read_indirect_mbox;
13834 tp->write32_mbox = tg3_write_indirect_mbox;
13835 tp->write32_tx_mbox = tg3_write_indirect_mbox;
13836 tp->write32_rx_mbox = tg3_write_indirect_mbox;
13837
13838 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070013839 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070013840
13841 pci_read_config_word(tp->pdev, PCI_COMMAND, &pci_cmd);
13842 pci_cmd &= ~PCI_COMMAND_MEMORY;
13843 pci_write_config_word(tp->pdev, PCI_COMMAND, pci_cmd);
13844 }
Michael Chanb5d37722006-09-27 16:06:21 -070013845 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
13846 tp->read32_mbox = tg3_read32_mbox_5906;
13847 tp->write32_mbox = tg3_write32_mbox_5906;
13848 tp->write32_tx_mbox = tg3_write32_mbox_5906;
13849 tp->write32_rx_mbox = tg3_write32_mbox_5906;
13850 }
Michael Chan68929142005-08-09 20:17:14 -070013851
Michael Chanbbadf502006-04-06 21:46:34 -070013852 if (tp->write32 == tg3_write_indirect_reg32 ||
13853 ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
13854 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
David S. Millerf49639e2006-06-09 11:58:36 -070013855 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701)))
Michael Chanbbadf502006-04-06 21:46:34 -070013856 tp->tg3_flags |= TG3_FLAG_SRAM_USE_CONFIG;
13857
Michael Chan7d0c41e2005-04-21 17:06:20 -070013858 /* Get eeprom hw config before calling tg3_set_power_state().
Michael Chan9d26e212006-12-07 00:21:14 -080013859 * In particular, the TG3_FLG2_IS_NIC flag must be
Michael Chan7d0c41e2005-04-21 17:06:20 -070013860 * determined before calling tg3_set_power_state() so that
13861 * we know whether or not to switch out of Vaux power.
13862 * When the flag is set, it means that GPIO1 is used for eeprom
13863 * write protect and also implies that it is a LOM where GPIOs
13864 * are not used to switch power.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013865 */
Michael Chan7d0c41e2005-04-21 17:06:20 -070013866 tg3_get_eeprom_hw_cfg(tp);
13867
Matt Carlson0d3031d2007-10-10 18:02:43 -070013868 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
13869 /* Allow reads and writes to the
13870 * APE register and memory space.
13871 */
13872 pci_state_reg |= PCISTATE_ALLOW_APE_CTLSPC_WR |
Matt Carlsonf92d9dc2010-06-05 17:24:30 +000013873 PCISTATE_ALLOW_APE_SHMEM_WR |
13874 PCISTATE_ALLOW_APE_PSPACE_WR;
Matt Carlson0d3031d2007-10-10 18:02:43 -070013875 pci_write_config_dword(tp->pdev, TG3PCI_PCISTATE,
13876 pci_state_reg);
13877 }
13878
Matt Carlson9936bcf2007-10-10 18:03:07 -070013879 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
Matt Carlson57e69832008-05-25 23:48:31 -070013880 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson321d32a2008-11-21 17:22:19 -080013881 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013882 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
Matt Carlson1407deb2011-04-05 14:22:44 +000013883 (tp->tg3_flags3 & TG3_FLG3_57765_PLUS))
Matt Carlsond30cdd22007-10-07 23:28:35 -070013884 tp->tg3_flags |= TG3_FLAG_CPMU_PRESENT;
13885
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013886 /* Set up tp->grc_local_ctrl before calling tg_power_up().
Michael Chan314fba32005-04-21 17:07:04 -070013887 * GPIO1 driven high will bring 5700's external PHY out of reset.
13888 * It is also used as eeprom write protect on LOMs.
13889 */
13890 tp->grc_local_ctrl = GRC_LCLCTRL_INT_ON_ATTN | GRC_LCLCTRL_AUTO_SEEPROM;
13891 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13892 (tp->tg3_flags & TG3_FLAG_EEPROM_WRITE_PROT))
13893 tp->grc_local_ctrl |= (GRC_LCLCTRL_GPIO_OE1 |
13894 GRC_LCLCTRL_GPIO_OUTPUT1);
Michael Chan3e7d83b2005-04-21 17:10:36 -070013895 /* Unused GPIO3 must be driven as output on 5752 because there
13896 * are no pull-up resistors on unused GPIO pins.
13897 */
13898 else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752)
13899 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE3;
Michael Chan314fba32005-04-21 17:07:04 -070013900
Matt Carlson321d32a2008-11-21 17:22:19 -080013901 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsoncb4ed1f2010-01-20 16:58:09 +000013902 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780 ||
13903 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57765)
Michael Chanaf36e6b2006-03-23 01:28:06 -080013904 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13905
Matt Carlson8d519ab2009-04-20 06:58:01 +000013906 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5761 ||
13907 tp->pdev->device == TG3PCI_DEVICE_TIGON3_5761S) {
Matt Carlson5f0c4a32008-06-09 15:41:12 -070013908 /* Turn off the debug UART. */
13909 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_UART_SEL;
13910 if (tp->tg3_flags2 & TG3_FLG2_IS_NIC)
13911 /* Keep VMain power. */
13912 tp->grc_local_ctrl |= GRC_LCLCTRL_GPIO_OE0 |
13913 GRC_LCLCTRL_GPIO_OUTPUT0;
13914 }
13915
Linus Torvalds1da177e2005-04-16 15:20:36 -070013916 /* Force the chip into D0. */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000013917 err = tg3_power_up(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070013918 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000013919 dev_err(&tp->pdev->dev, "Transition to D0 failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070013920 return err;
13921 }
13922
Linus Torvalds1da177e2005-04-16 15:20:36 -070013923 /* Derive initial jumbo mode from MTU assigned in
13924 * ether_setup() via the alloc_etherdev() call
13925 */
Michael Chan0f893dc2005-07-25 12:30:38 -070013926 if (tp->dev->mtu > ETH_DATA_LEN &&
Michael Chana4e2b342005-10-26 15:46:52 -070013927 !(tp->tg3_flags2 & TG3_FLG2_5780_CLASS))
Michael Chan0f893dc2005-07-25 12:30:38 -070013928 tp->tg3_flags |= TG3_FLAG_JUMBO_RING_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013929
13930 /* Determine WakeOnLan speed to use. */
13931 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
13932 tp->pci_chip_rev_id == CHIPREV_ID_5701_A0 ||
13933 tp->pci_chip_rev_id == CHIPREV_ID_5701_B0 ||
13934 tp->pci_chip_rev_id == CHIPREV_ID_5701_B2) {
13935 tp->tg3_flags &= ~(TG3_FLAG_WOL_SPEED_100MB);
13936 } else {
13937 tp->tg3_flags |= TG3_FLAG_WOL_SPEED_100MB;
13938 }
13939
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013940 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013941 tp->phy_flags |= TG3_PHYFLG_IS_FET;
Matt Carlson7f97a4b2009-08-25 10:10:03 +000013942
Linus Torvalds1da177e2005-04-16 15:20:36 -070013943 /* A few boards don't want Ethernet@WireSpeed phy feature */
13944 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700) ||
13945 ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705) &&
13946 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A0) &&
Michael Chan747e8f82005-07-25 12:33:22 -070013947 (tp->pci_chip_rev_id != CHIPREV_ID_5705_A1)) ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013948 (tp->phy_flags & TG3_PHYFLG_IS_FET) ||
13949 (tp->phy_flags & TG3_PHYFLG_ANY_SERDES))
13950 tp->phy_flags |= TG3_PHYFLG_NO_ETH_WIRE_SPEED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013951
13952 if (GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5703_AX ||
13953 GET_CHIP_REV(tp->pci_chip_rev_id) == CHIPREV_5704_AX)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013954 tp->phy_flags |= TG3_PHYFLG_ADC_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013955 if (tp->pci_chip_rev_id == CHIPREV_ID_5704_A0)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013956 tp->phy_flags |= TG3_PHYFLG_5704_A0_BUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013957
Matt Carlson321d32a2008-11-21 17:22:19 -080013958 if ((tp->tg3_flags2 & TG3_FLG2_5705_PLUS) &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013959 !(tp->phy_flags & TG3_PHYFLG_IS_FET) &&
Matt Carlson321d32a2008-11-21 17:22:19 -080013960 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5785 &&
Matt Carlsonf6eb9b12009-09-01 13:19:53 +000013961 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_57780 &&
Matt Carlson1407deb2011-04-05 14:22:44 +000013962 !(tp->tg3_flags3 & TG3_FLG3_57765_PLUS)) {
Michael Chanc424cb22006-04-29 18:56:34 -070013963 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755 ||
Matt Carlsond30cdd22007-10-07 23:28:35 -070013964 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5787 ||
Matt Carlson9936bcf2007-10-10 18:03:07 -070013965 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 ||
13966 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761) {
Michael Chand4011ad2007-02-13 12:17:25 -080013967 if (tp->pdev->device != PCI_DEVICE_ID_TIGON3_5756 &&
13968 tp->pdev->device != PCI_DEVICE_ID_TIGON3_5722)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013969 tp->phy_flags |= TG3_PHYFLG_JITTER_BUG;
Michael Chanc1d2a192007-01-08 19:57:20 -080013970 if (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5755M)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013971 tp->phy_flags |= TG3_PHYFLG_ADJUST_TRIM;
Matt Carlson321d32a2008-11-21 17:22:19 -080013972 } else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000013973 tp->phy_flags |= TG3_PHYFLG_BER_BUG;
Michael Chanc424cb22006-04-29 18:56:34 -070013974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070013975
Matt Carlsonb2a5c192008-04-03 21:44:44 -070013976 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
13977 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) {
13978 tp->phy_otp = tg3_read_otp_phycfg(tp);
13979 if (tp->phy_otp == 0)
13980 tp->phy_otp = TG3_OTP_DEFAULT;
13981 }
13982
Matt Carlsonf51f3562008-05-25 23:45:08 -070013983 if (tp->tg3_flags & TG3_FLAG_CPMU_PRESENT)
Matt Carlson8ef21422008-05-02 16:47:53 -070013984 tp->mi_mode = MAC_MI_MODE_500KHZ_CONST;
13985 else
13986 tp->mi_mode = MAC_MI_MODE_BASE;
13987
Linus Torvalds1da177e2005-04-16 15:20:36 -070013988 tp->coalesce_mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013989 if (GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_AX &&
13990 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5700_BX)
13991 tp->coalesce_mode |= HOSTCC_MODE_32BYTE;
13992
Matt Carlson4d958472011-04-20 07:57:35 +000013993 /* Set these bits to enable statistics workaround. */
13994 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5717 ||
13995 tp->pci_chip_rev_id == CHIPREV_ID_5719_A0 ||
13996 tp->pci_chip_rev_id == CHIPREV_ID_5720_A0) {
13997 tp->coalesce_mode |= HOSTCC_MODE_ATTN;
13998 tp->grc_mode |= GRC_MODE_IRQ_ON_FLOW_ATTN;
13999 }
14000
Matt Carlson321d32a2008-11-21 17:22:19 -080014001 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
14002 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
Matt Carlson57e69832008-05-25 23:48:31 -070014003 tp->tg3_flags3 |= TG3_FLG3_USE_PHYLIB;
14004
Matt Carlson158d7ab2008-05-29 01:37:54 -070014005 err = tg3_mdio_init(tp);
14006 if (err)
14007 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014008
14009 /* Initialize data/descriptor byte/word swapping. */
14010 val = tr32(GRC_MODE);
Matt Carlsonf2096f92011-04-05 14:22:48 +000014011 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5720)
14012 val &= (GRC_MODE_BYTE_SWAP_B2HRX_DATA |
14013 GRC_MODE_WORD_SWAP_B2HRX_DATA |
14014 GRC_MODE_B2HRX_ENABLE |
14015 GRC_MODE_HTX2B_ENABLE |
14016 GRC_MODE_HOST_STACKUP);
14017 else
14018 val &= GRC_MODE_HOST_STACKUP;
14019
Linus Torvalds1da177e2005-04-16 15:20:36 -070014020 tw32(GRC_MODE, val | tp->grc_mode);
14021
14022 tg3_switch_clocks(tp);
14023
14024 /* Clear this out for sanity. */
14025 tw32(TG3PCI_MEM_WIN_BASE_ADDR, 0);
14026
14027 pci_read_config_dword(tp->pdev, TG3PCI_PCISTATE,
14028 &pci_state_reg);
14029 if ((pci_state_reg & PCISTATE_CONV_PCI_MODE) == 0 &&
14030 (tp->tg3_flags & TG3_FLAG_PCIX_TARGET_HWBUG) == 0) {
14031 u32 chiprevid = GET_CHIP_REV_ID(tp->misc_host_ctrl);
14032
14033 if (chiprevid == CHIPREV_ID_5701_A0 ||
14034 chiprevid == CHIPREV_ID_5701_B0 ||
14035 chiprevid == CHIPREV_ID_5701_B2 ||
14036 chiprevid == CHIPREV_ID_5701_B5) {
14037 void __iomem *sram_base;
14038
14039 /* Write some dummy words into the SRAM status block
14040 * area, see if it reads back correctly. If the return
14041 * value is bad, force enable the PCIX workaround.
14042 */
14043 sram_base = tp->regs + NIC_SRAM_WIN_BASE + NIC_SRAM_STATS_BLK;
14044
14045 writel(0x00000000, sram_base);
14046 writel(0x00000000, sram_base + 4);
14047 writel(0xffffffff, sram_base + 4);
14048 if (readl(sram_base) != 0x00000000)
14049 tp->tg3_flags |= TG3_FLAG_PCIX_TARGET_HWBUG;
14050 }
14051 }
14052
14053 udelay(50);
14054 tg3_nvram_init(tp);
14055
14056 grc_misc_cfg = tr32(GRC_MISC_CFG);
14057 grc_misc_cfg &= GRC_MISC_CFG_BOARD_ID_MASK;
14058
Linus Torvalds1da177e2005-04-16 15:20:36 -070014059 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14060 (grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788 ||
14061 grc_misc_cfg == GRC_MISC_CFG_BOARD_ID_5788M))
14062 tp->tg3_flags2 |= TG3_FLG2_IS_5788;
14063
David S. Millerfac9b832005-05-18 22:46:34 -070014064 if (!(tp->tg3_flags2 & TG3_FLG2_IS_5788) &&
14065 (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700))
14066 tp->tg3_flags |= TG3_FLAG_TAGGED_STATUS;
14067 if (tp->tg3_flags & TG3_FLAG_TAGGED_STATUS) {
14068 tp->coalesce_mode |= (HOSTCC_MODE_CLRTICK_RXBD |
14069 HOSTCC_MODE_CLRTICK_TXBD);
14070
14071 tp->misc_host_ctrl |= MISC_HOST_CTRL_TAGGED_STATUS;
14072 pci_write_config_dword(tp->pdev, TG3PCI_MISC_HOST_CTRL,
14073 tp->misc_host_ctrl);
14074 }
14075
Matt Carlson3bda1252008-08-15 14:08:22 -070014076 /* Preserve the APE MAC_MODE bits */
14077 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE)
Matt Carlsond2394e6b2010-11-24 08:31:47 +000014078 tp->mac_mode = MAC_MODE_APE_TX_EN | MAC_MODE_APE_RX_EN;
Matt Carlson3bda1252008-08-15 14:08:22 -070014079 else
14080 tp->mac_mode = TG3_DEF_MAC_MODE;
14081
Linus Torvalds1da177e2005-04-16 15:20:36 -070014082 /* these are limited to 10/100 only */
14083 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 &&
14084 (grc_misc_cfg == 0x8000 || grc_misc_cfg == 0x4000)) ||
14085 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 &&
14086 tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14087 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901 ||
14088 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5901_2 ||
14089 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5705F)) ||
14090 (tp->pdev->vendor == PCI_VENDOR_ID_BROADCOM &&
14091 (tp->pdev->device == PCI_DEVICE_ID_TIGON3_5751F ||
Michael Chan676917d2006-12-07 00:20:22 -080014092 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5753F ||
14093 tp->pdev->device == PCI_DEVICE_ID_TIGON3_5787F)) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080014094 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57790 ||
Matt Carlsond1101142010-02-17 15:16:55 +000014095 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57791 ||
14096 tp->pdev->device == TG3PCI_DEVICE_TIGON3_57795 ||
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014097 (tp->phy_flags & TG3_PHYFLG_IS_FET))
14098 tp->phy_flags |= TG3_PHYFLG_10_100_ONLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014099
14100 err = tg3_phy_probe(tp);
14101 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014102 dev_err(&tp->pdev->dev, "phy probe failed, err %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014103 /* ... but do not return immediately ... */
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070014104 tg3_mdio_fini(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014105 }
14106
Matt Carlson184b8902010-04-05 10:19:25 +000014107 tg3_read_vpd(tp);
Michael Chanc4e65752006-03-20 22:29:32 -080014108 tg3_read_fw_ver(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014109
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014110 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES) {
14111 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014112 } else {
14113 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014114 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014115 else
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014116 tp->phy_flags &= ~TG3_PHYFLG_USE_MI_INTERRUPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014117 }
14118
14119 /* 5700 {AX,BX} chips have a broken status block link
14120 * change bit implementation, so we must use the
14121 * status register in those cases.
14122 */
14123 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700)
14124 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
14125 else
14126 tp->tg3_flags &= ~TG3_FLAG_USE_LINKCHG_REG;
14127
14128 /* The led_ctrl is set during tg3_phy_probe, here we might
14129 * have to force the link status polling mechanism based
14130 * upon subsystem IDs.
14131 */
14132 if (tp->pdev->subsystem_vendor == PCI_VENDOR_ID_DELL &&
Michael Chan007a880d2007-05-31 14:49:51 -070014133 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014134 !(tp->phy_flags & TG3_PHYFLG_PHY_SERDES)) {
14135 tp->phy_flags |= TG3_PHYFLG_USE_MI_INTERRUPT;
14136 tp->tg3_flags |= TG3_FLAG_USE_LINKCHG_REG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014137 }
14138
14139 /* For all SERDES we poll the MAC status register. */
Matt Carlsonf07e9af2010-08-02 11:26:07 +000014140 if (tp->phy_flags & TG3_PHYFLG_PHY_SERDES)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014141 tp->tg3_flags |= TG3_FLAG_POLL_SERDES;
14142 else
14143 tp->tg3_flags &= ~TG3_FLAG_POLL_SERDES;
14144
Matt Carlsonbf933c82011-01-25 15:58:49 +000014145 tp->rx_offset = NET_IP_ALIGN;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014146 tp->rx_copy_thresh = TG3_RX_COPY_THRESHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014147 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701 &&
Matt Carlsond2757fc2010-04-12 06:58:27 +000014148 (tp->tg3_flags & TG3_FLAG_PCIX_MODE) != 0) {
Matt Carlsonbf933c82011-01-25 15:58:49 +000014149 tp->rx_offset = 0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014150#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
Matt Carlson9dc7a112010-04-12 06:58:28 +000014151 tp->rx_copy_thresh = ~(u16)0;
Matt Carlsond2757fc2010-04-12 06:58:27 +000014152#endif
14153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014154
Matt Carlson2c49a442010-09-30 10:34:35 +000014155 tp->rx_std_ring_mask = TG3_RX_STD_RING_SIZE(tp) - 1;
14156 tp->rx_jmb_ring_mask = TG3_RX_JMB_RING_SIZE(tp) - 1;
Matt Carlson7cb32cf2010-09-30 10:34:36 +000014157 tp->rx_ret_ring_mask = tg3_rx_ret_ring_size(tp) - 1;
14158
Matt Carlson2c49a442010-09-30 10:34:35 +000014159 tp->rx_std_max_post = tp->rx_std_ring_mask + 1;
Michael Chanf92905d2006-06-29 20:14:29 -070014160
14161 /* Increment the rx prod index on the rx std ring by at most
14162 * 8 for these chips to workaround hw errata.
14163 */
14164 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750 ||
14165 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5752 ||
14166 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5755)
14167 tp->rx_std_max_post = 8;
14168
Matt Carlson8ed5d972007-05-07 00:25:49 -070014169 if (tp->tg3_flags & TG3_FLAG_ASPM_WORKAROUND)
14170 tp->pwrmgmt_thresh = tr32(PCIE_PWR_MGMT_THRESH) &
14171 PCIE_PWR_MGMT_L1_THRESH_MSK;
14172
Linus Torvalds1da177e2005-04-16 15:20:36 -070014173 return err;
14174}
14175
David S. Miller49b6e95f2007-03-29 01:38:42 -070014176#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014177static int __devinit tg3_get_macaddr_sparc(struct tg3 *tp)
14178{
14179 struct net_device *dev = tp->dev;
14180 struct pci_dev *pdev = tp->pdev;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014181 struct device_node *dp = pci_device_to_OF_node(pdev);
David S. Miller374d4ca2007-03-29 01:57:57 -070014182 const unsigned char *addr;
David S. Miller49b6e95f2007-03-29 01:38:42 -070014183 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014184
David S. Miller49b6e95f2007-03-29 01:38:42 -070014185 addr = of_get_property(dp, "local-mac-address", &len);
14186 if (addr && len == 6) {
14187 memcpy(dev->dev_addr, addr, 6);
14188 memcpy(dev->perm_addr, dev->dev_addr, 6);
14189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014190 }
14191 return -ENODEV;
14192}
14193
14194static int __devinit tg3_get_default_macaddr_sparc(struct tg3 *tp)
14195{
14196 struct net_device *dev = tp->dev;
14197
14198 memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
John W. Linville2ff43692005-09-12 14:44:20 -070014199 memcpy(dev->perm_addr, idprom->id_ethaddr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014200 return 0;
14201}
14202#endif
14203
14204static int __devinit tg3_get_device_address(struct tg3 *tp)
14205{
14206 struct net_device *dev = tp->dev;
14207 u32 hi, lo, mac_offset;
Michael Chan008652b2006-03-27 23:14:53 -080014208 int addr_ok = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014209
David S. Miller49b6e95f2007-03-29 01:38:42 -070014210#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014211 if (!tg3_get_macaddr_sparc(tp))
14212 return 0;
14213#endif
14214
14215 mac_offset = 0x7c;
David S. Millerf49639e2006-06-09 11:58:36 -070014216 if ((GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) ||
Michael Chana4e2b342005-10-26 15:46:52 -070014217 (tp->tg3_flags2 & TG3_FLG2_5780_CLASS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070014218 if (tr32(TG3PCI_DUAL_MAC_CTRL) & DUAL_MAC_CTRL_ID)
14219 mac_offset = 0xcc;
14220 if (tg3_nvram_lock(tp))
14221 tw32_f(NVRAM_CMD, NVRAM_CMD_RESET);
14222 else
14223 tg3_nvram_unlock(tp);
Matt Carlson0a58d662011-04-05 14:22:45 +000014224 } else if (tp->tg3_flags3 & TG3_FLG3_5717_PLUS) {
Matt Carlsona50d0792010-06-05 17:24:37 +000014225 if (PCI_FUNC(tp->pdev->devfn) & 1)
Matt Carlsona1b950d2009-09-01 13:20:17 +000014226 mac_offset = 0xcc;
Matt Carlsona50d0792010-06-05 17:24:37 +000014227 if (PCI_FUNC(tp->pdev->devfn) > 1)
14228 mac_offset += 0x18c;
Matt Carlsona1b950d2009-09-01 13:20:17 +000014229 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906)
Michael Chanb5d37722006-09-27 16:06:21 -070014230 mac_offset = 0x10;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014231
14232 /* First try to get it from MAC address mailbox. */
14233 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_HIGH_MBOX, &hi);
14234 if ((hi >> 16) == 0x484b) {
14235 dev->dev_addr[0] = (hi >> 8) & 0xff;
14236 dev->dev_addr[1] = (hi >> 0) & 0xff;
14237
14238 tg3_read_mem(tp, NIC_SRAM_MAC_ADDR_LOW_MBOX, &lo);
14239 dev->dev_addr[2] = (lo >> 24) & 0xff;
14240 dev->dev_addr[3] = (lo >> 16) & 0xff;
14241 dev->dev_addr[4] = (lo >> 8) & 0xff;
14242 dev->dev_addr[5] = (lo >> 0) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014243
Michael Chan008652b2006-03-27 23:14:53 -080014244 /* Some old bootcode may report a 0 MAC address in SRAM */
14245 addr_ok = is_valid_ether_addr(&dev->dev_addr[0]);
14246 }
14247 if (!addr_ok) {
14248 /* Next, try NVRAM. */
Matt Carlsondf259d82009-04-20 06:57:14 +000014249 if (!(tp->tg3_flags3 & TG3_FLG3_NO_NVRAM) &&
14250 !tg3_nvram_read_be32(tp, mac_offset + 0, &hi) &&
Matt Carlson6d348f22009-02-25 14:25:52 +000014251 !tg3_nvram_read_be32(tp, mac_offset + 4, &lo)) {
Matt Carlson62cedd12009-04-20 14:52:29 -070014252 memcpy(&dev->dev_addr[0], ((char *)&hi) + 2, 2);
14253 memcpy(&dev->dev_addr[2], (char *)&lo, sizeof(lo));
Michael Chan008652b2006-03-27 23:14:53 -080014254 }
14255 /* Finally just fetch it out of the MAC control regs. */
14256 else {
14257 hi = tr32(MAC_ADDR_0_HIGH);
14258 lo = tr32(MAC_ADDR_0_LOW);
14259
14260 dev->dev_addr[5] = lo & 0xff;
14261 dev->dev_addr[4] = (lo >> 8) & 0xff;
14262 dev->dev_addr[3] = (lo >> 16) & 0xff;
14263 dev->dev_addr[2] = (lo >> 24) & 0xff;
14264 dev->dev_addr[1] = hi & 0xff;
14265 dev->dev_addr[0] = (hi >> 8) & 0xff;
14266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014267 }
14268
14269 if (!is_valid_ether_addr(&dev->dev_addr[0])) {
David S. Miller7582a332008-03-20 15:53:15 -070014270#ifdef CONFIG_SPARC
Linus Torvalds1da177e2005-04-16 15:20:36 -070014271 if (!tg3_get_default_macaddr_sparc(tp))
14272 return 0;
14273#endif
14274 return -EINVAL;
14275 }
John W. Linville2ff43692005-09-12 14:44:20 -070014276 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014277 return 0;
14278}
14279
David S. Miller59e6b432005-05-18 22:50:10 -070014280#define BOUNDARY_SINGLE_CACHELINE 1
14281#define BOUNDARY_MULTI_CACHELINE 2
14282
14283static u32 __devinit tg3_calc_dma_bndry(struct tg3 *tp, u32 val)
14284{
14285 int cacheline_size;
14286 u8 byte;
14287 int goal;
14288
14289 pci_read_config_byte(tp->pdev, PCI_CACHE_LINE_SIZE, &byte);
14290 if (byte == 0)
14291 cacheline_size = 1024;
14292 else
14293 cacheline_size = (int) byte * 4;
14294
14295 /* On 5703 and later chips, the boundary bits have no
14296 * effect.
14297 */
14298 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14299 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701 &&
14300 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS))
14301 goto out;
14302
14303#if defined(CONFIG_PPC64) || defined(CONFIG_IA64) || defined(CONFIG_PARISC)
14304 goal = BOUNDARY_MULTI_CACHELINE;
14305#else
14306#if defined(CONFIG_SPARC64) || defined(CONFIG_ALPHA)
14307 goal = BOUNDARY_SINGLE_CACHELINE;
14308#else
14309 goal = 0;
14310#endif
14311#endif
14312
Matt Carlson1407deb2011-04-05 14:22:44 +000014313 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014314 val = goal ? 0 : DMA_RWCTRL_DIS_CACHE_ALIGNMENT;
14315 goto out;
14316 }
14317
David S. Miller59e6b432005-05-18 22:50:10 -070014318 if (!goal)
14319 goto out;
14320
14321 /* PCI controllers on most RISC systems tend to disconnect
14322 * when a device tries to burst across a cache-line boundary.
14323 * Therefore, letting tg3 do so just wastes PCI bandwidth.
14324 *
14325 * Unfortunately, for PCI-E there are only limited
14326 * write-side controls for this, and thus for reads
14327 * we will still get the disconnects. We'll also waste
14328 * these PCI cycles for both read and write for chips
14329 * other than 5700 and 5701 which do not implement the
14330 * boundary bits.
14331 */
14332 if ((tp->tg3_flags & TG3_FLAG_PCIX_MODE) &&
14333 !(tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS)) {
14334 switch (cacheline_size) {
14335 case 16:
14336 case 32:
14337 case 64:
14338 case 128:
14339 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14340 val |= (DMA_RWCTRL_READ_BNDRY_128_PCIX |
14341 DMA_RWCTRL_WRITE_BNDRY_128_PCIX);
14342 } else {
14343 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14344 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14345 }
14346 break;
14347
14348 case 256:
14349 val |= (DMA_RWCTRL_READ_BNDRY_256_PCIX |
14350 DMA_RWCTRL_WRITE_BNDRY_256_PCIX);
14351 break;
14352
14353 default:
14354 val |= (DMA_RWCTRL_READ_BNDRY_384_PCIX |
14355 DMA_RWCTRL_WRITE_BNDRY_384_PCIX);
14356 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014357 }
David S. Miller59e6b432005-05-18 22:50:10 -070014358 } else if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14359 switch (cacheline_size) {
14360 case 16:
14361 case 32:
14362 case 64:
14363 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14364 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14365 val |= DMA_RWCTRL_WRITE_BNDRY_64_PCIE;
14366 break;
14367 }
14368 /* fallthrough */
14369 case 128:
14370 default:
14371 val &= ~DMA_RWCTRL_WRITE_BNDRY_DISAB_PCIE;
14372 val |= DMA_RWCTRL_WRITE_BNDRY_128_PCIE;
14373 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014374 }
David S. Miller59e6b432005-05-18 22:50:10 -070014375 } else {
14376 switch (cacheline_size) {
14377 case 16:
14378 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14379 val |= (DMA_RWCTRL_READ_BNDRY_16 |
14380 DMA_RWCTRL_WRITE_BNDRY_16);
14381 break;
14382 }
14383 /* fallthrough */
14384 case 32:
14385 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14386 val |= (DMA_RWCTRL_READ_BNDRY_32 |
14387 DMA_RWCTRL_WRITE_BNDRY_32);
14388 break;
14389 }
14390 /* fallthrough */
14391 case 64:
14392 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14393 val |= (DMA_RWCTRL_READ_BNDRY_64 |
14394 DMA_RWCTRL_WRITE_BNDRY_64);
14395 break;
14396 }
14397 /* fallthrough */
14398 case 128:
14399 if (goal == BOUNDARY_SINGLE_CACHELINE) {
14400 val |= (DMA_RWCTRL_READ_BNDRY_128 |
14401 DMA_RWCTRL_WRITE_BNDRY_128);
14402 break;
14403 }
14404 /* fallthrough */
14405 case 256:
14406 val |= (DMA_RWCTRL_READ_BNDRY_256 |
14407 DMA_RWCTRL_WRITE_BNDRY_256);
14408 break;
14409 case 512:
14410 val |= (DMA_RWCTRL_READ_BNDRY_512 |
14411 DMA_RWCTRL_WRITE_BNDRY_512);
14412 break;
14413 case 1024:
14414 default:
14415 val |= (DMA_RWCTRL_READ_BNDRY_1024 |
14416 DMA_RWCTRL_WRITE_BNDRY_1024);
14417 break;
Stephen Hemminger855e1112008-04-16 16:37:28 -070014418 }
David S. Miller59e6b432005-05-18 22:50:10 -070014419 }
14420
14421out:
14422 return val;
14423}
14424
Linus Torvalds1da177e2005-04-16 15:20:36 -070014425static int __devinit tg3_do_test_dma(struct tg3 *tp, u32 *buf, dma_addr_t buf_dma, int size, int to_device)
14426{
14427 struct tg3_internal_buffer_desc test_desc;
14428 u32 sram_dma_descs;
14429 int i, ret;
14430
14431 sram_dma_descs = NIC_SRAM_DMA_DESC_POOL_BASE;
14432
14433 tw32(FTQ_RCVBD_COMP_FIFO_ENQDEQ, 0);
14434 tw32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ, 0);
14435 tw32(RDMAC_STATUS, 0);
14436 tw32(WDMAC_STATUS, 0);
14437
14438 tw32(BUFMGR_MODE, 0);
14439 tw32(FTQ_RESET, 0);
14440
14441 test_desc.addr_hi = ((u64) buf_dma) >> 32;
14442 test_desc.addr_lo = buf_dma & 0xffffffff;
14443 test_desc.nic_mbuf = 0x00002100;
14444 test_desc.len = size;
14445
14446 /*
14447 * HP ZX1 was seeing test failures for 5701 cards running at 33Mhz
14448 * the *second* time the tg3 driver was getting loaded after an
14449 * initial scan.
14450 *
14451 * Broadcom tells me:
14452 * ...the DMA engine is connected to the GRC block and a DMA
14453 * reset may affect the GRC block in some unpredictable way...
14454 * The behavior of resets to individual blocks has not been tested.
14455 *
14456 * Broadcom noted the GRC reset will also reset all sub-components.
14457 */
14458 if (to_device) {
14459 test_desc.cqid_sqid = (13 << 8) | 2;
14460
14461 tw32_f(RDMAC_MODE, RDMAC_MODE_ENABLE);
14462 udelay(40);
14463 } else {
14464 test_desc.cqid_sqid = (16 << 8) | 7;
14465
14466 tw32_f(WDMAC_MODE, WDMAC_MODE_ENABLE);
14467 udelay(40);
14468 }
14469 test_desc.flags = 0x00000005;
14470
14471 for (i = 0; i < (sizeof(test_desc) / sizeof(u32)); i++) {
14472 u32 val;
14473
14474 val = *(((u32 *)&test_desc) + i);
14475 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR,
14476 sram_dma_descs + (i * sizeof(u32)));
14477 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_DATA, val);
14478 }
14479 pci_write_config_dword(tp->pdev, TG3PCI_MEM_WIN_BASE_ADDR, 0);
14480
Matt Carlson859a5882010-04-05 10:19:28 +000014481 if (to_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014482 tw32(FTQ_DMA_HIGH_READ_FIFO_ENQDEQ, sram_dma_descs);
Matt Carlson859a5882010-04-05 10:19:28 +000014483 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070014484 tw32(FTQ_DMA_HIGH_WRITE_FIFO_ENQDEQ, sram_dma_descs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014485
14486 ret = -ENODEV;
14487 for (i = 0; i < 40; i++) {
14488 u32 val;
14489
14490 if (to_device)
14491 val = tr32(FTQ_RCVBD_COMP_FIFO_ENQDEQ);
14492 else
14493 val = tr32(FTQ_RCVDATA_COMP_FIFO_ENQDEQ);
14494 if ((val & 0xffff) == sram_dma_descs) {
14495 ret = 0;
14496 break;
14497 }
14498
14499 udelay(100);
14500 }
14501
14502 return ret;
14503}
14504
David S. Millerded73402005-05-23 13:59:47 -070014505#define TEST_BUFFER_SIZE 0x2000
Linus Torvalds1da177e2005-04-16 15:20:36 -070014506
Matt Carlson41434702011-03-09 16:58:22 +000014507static DEFINE_PCI_DEVICE_TABLE(tg3_dma_wait_state_chipsets) = {
Joe Perches895950c2010-12-21 02:16:08 -080014508 { PCI_DEVICE(PCI_VENDOR_ID_APPLE, PCI_DEVICE_ID_APPLE_UNI_N_PCI15) },
14509 { },
14510};
14511
Linus Torvalds1da177e2005-04-16 15:20:36 -070014512static int __devinit tg3_test_dma(struct tg3 *tp)
14513{
14514 dma_addr_t buf_dma;
David S. Miller59e6b432005-05-18 22:50:10 -070014515 u32 *buf, saved_dma_rwctrl;
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014516 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014517
Matt Carlson4bae65c2010-11-24 08:31:52 +000014518 buf = dma_alloc_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE,
14519 &buf_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014520 if (!buf) {
14521 ret = -ENOMEM;
14522 goto out_nofree;
14523 }
14524
14525 tp->dma_rwctrl = ((0x7 << DMA_RWCTRL_PCI_WRITE_CMD_SHIFT) |
14526 (0x6 << DMA_RWCTRL_PCI_READ_CMD_SHIFT));
14527
David S. Miller59e6b432005-05-18 22:50:10 -070014528 tp->dma_rwctrl = tg3_calc_dma_bndry(tp, tp->dma_rwctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014529
Matt Carlson1407deb2011-04-05 14:22:44 +000014530 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS)
Matt Carlsoncbf9ca62009-11-13 13:03:40 +000014531 goto out;
14532
Linus Torvalds1da177e2005-04-16 15:20:36 -070014533 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14534 /* DMA read watermark not used on PCIE */
14535 tp->dma_rwctrl |= 0x00180000;
14536 } else if (!(tp->tg3_flags & TG3_FLAG_PCIX_MODE)) {
Michael Chan85e94ce2005-04-21 17:05:28 -070014537 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5705 ||
14538 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5750)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014539 tp->dma_rwctrl |= 0x003f0000;
14540 else
14541 tp->dma_rwctrl |= 0x003f000f;
14542 } else {
14543 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14544 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704) {
14545 u32 ccval = (tr32(TG3PCI_CLOCK_CTRL) & 0x1f);
Michael Chan49afdeb2007-02-13 12:17:03 -080014546 u32 read_water = 0x7;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014547
Michael Chan4a29cc22006-03-19 13:21:12 -080014548 /* If the 5704 is behind the EPB bridge, we can
14549 * do the less restrictive ONE_DMA workaround for
14550 * better performance.
14551 */
14552 if ((tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) &&
14553 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14554 tp->dma_rwctrl |= 0x8000;
14555 else if (ccval == 0x6 || ccval == 0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014556 tp->dma_rwctrl |= DMA_RWCTRL_ONE_DMA;
14557
Michael Chan49afdeb2007-02-13 12:17:03 -080014558 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703)
14559 read_water = 4;
David S. Miller59e6b432005-05-18 22:50:10 -070014560 /* Set bit 23 to enable PCIX hw bug fix */
Michael Chan49afdeb2007-02-13 12:17:03 -080014561 tp->dma_rwctrl |=
14562 (read_water << DMA_RWCTRL_READ_WATER_SHIFT) |
14563 (0x3 << DMA_RWCTRL_WRITE_WATER_SHIFT) |
14564 (1 << 23);
Michael Chan4cf78e42005-07-25 12:29:19 -070014565 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5780) {
14566 /* 5780 always in PCIX mode */
14567 tp->dma_rwctrl |= 0x00144000;
Michael Chana4e2b342005-10-26 15:46:52 -070014568 } else if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5714) {
14569 /* 5714 always in PCIX mode */
14570 tp->dma_rwctrl |= 0x00148000;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014571 } else {
14572 tp->dma_rwctrl |= 0x001b000f;
14573 }
14574 }
14575
14576 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5703 ||
14577 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5704)
14578 tp->dma_rwctrl &= 0xfffffff0;
14579
14580 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5700 ||
14581 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5701) {
14582 /* Remove this if it causes problems for some boards. */
14583 tp->dma_rwctrl |= DMA_RWCTRL_USE_MEM_READ_MULT;
14584
14585 /* On 5700/5701 chips, we need to set this bit.
14586 * Otherwise the chip will issue cacheline transactions
14587 * to streamable DMA memory with not all the byte
14588 * enables turned on. This is an error on several
14589 * RISC PCI controllers, in particular sparc64.
14590 *
14591 * On 5703/5704 chips, this bit has been reassigned
14592 * a different meaning. In particular, it is used
14593 * on those chips to enable a PCI-X workaround.
14594 */
14595 tp->dma_rwctrl |= DMA_RWCTRL_ASSERT_ALL_BE;
14596 }
14597
14598 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14599
14600#if 0
14601 /* Unneeded, already done by tg3_get_invariants. */
14602 tg3_switch_clocks(tp);
14603#endif
14604
Linus Torvalds1da177e2005-04-16 15:20:36 -070014605 if (GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5700 &&
14606 GET_ASIC_REV(tp->pci_chip_rev_id) != ASIC_REV_5701)
14607 goto out;
14608
David S. Miller59e6b432005-05-18 22:50:10 -070014609 /* It is best to perform DMA test with maximum write burst size
14610 * to expose the 5700/5701 write DMA bug.
14611 */
14612 saved_dma_rwctrl = tp->dma_rwctrl;
14613 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14614 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14615
Linus Torvalds1da177e2005-04-16 15:20:36 -070014616 while (1) {
14617 u32 *p = buf, i;
14618
14619 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++)
14620 p[i] = i;
14621
14622 /* Send the buffer to the chip. */
14623 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 1);
14624 if (ret) {
Matt Carlson2445e462010-04-05 10:19:21 +000014625 dev_err(&tp->pdev->dev,
14626 "%s: Buffer write failed. err = %d\n",
14627 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014628 break;
14629 }
14630
14631#if 0
14632 /* validate data reached card RAM correctly. */
14633 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14634 u32 val;
14635 tg3_read_mem(tp, 0x2100 + (i*4), &val);
14636 if (le32_to_cpu(val) != p[i]) {
Matt Carlson2445e462010-04-05 10:19:21 +000014637 dev_err(&tp->pdev->dev,
14638 "%s: Buffer corrupted on device! "
14639 "(%d != %d)\n", __func__, val, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014640 /* ret = -ENODEV here? */
14641 }
14642 p[i] = 0;
14643 }
14644#endif
14645 /* Now read it back. */
14646 ret = tg3_do_test_dma(tp, buf, buf_dma, TEST_BUFFER_SIZE, 0);
14647 if (ret) {
Matt Carlson5129c3a2010-04-05 10:19:23 +000014648 dev_err(&tp->pdev->dev, "%s: Buffer read failed. "
14649 "err = %d\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014650 break;
14651 }
14652
14653 /* Verify it. */
14654 for (i = 0; i < TEST_BUFFER_SIZE / sizeof(u32); i++) {
14655 if (p[i] == i)
14656 continue;
14657
David S. Miller59e6b432005-05-18 22:50:10 -070014658 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14659 DMA_RWCTRL_WRITE_BNDRY_16) {
14660 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014661 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
14662 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14663 break;
14664 } else {
Matt Carlson2445e462010-04-05 10:19:21 +000014665 dev_err(&tp->pdev->dev,
14666 "%s: Buffer corrupted on read back! "
14667 "(%d != %d)\n", __func__, p[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014668 ret = -ENODEV;
14669 goto out;
14670 }
14671 }
14672
14673 if (i == (TEST_BUFFER_SIZE / sizeof(u32))) {
14674 /* Success. */
14675 ret = 0;
14676 break;
14677 }
14678 }
David S. Miller59e6b432005-05-18 22:50:10 -070014679 if ((tp->dma_rwctrl & DMA_RWCTRL_WRITE_BNDRY_MASK) !=
14680 DMA_RWCTRL_WRITE_BNDRY_16) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014681
David S. Miller59e6b432005-05-18 22:50:10 -070014682 /* DMA test passed without adjusting DMA boundary,
Michael Chan6d1cfba2005-06-08 14:13:14 -070014683 * now look for chipsets that are known to expose the
14684 * DMA bug without failing the test.
David S. Miller59e6b432005-05-18 22:50:10 -070014685 */
Matt Carlson41434702011-03-09 16:58:22 +000014686 if (pci_dev_present(tg3_dma_wait_state_chipsets)) {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014687 tp->dma_rwctrl &= ~DMA_RWCTRL_WRITE_BNDRY_MASK;
14688 tp->dma_rwctrl |= DMA_RWCTRL_WRITE_BNDRY_16;
Matt Carlson859a5882010-04-05 10:19:28 +000014689 } else {
Michael Chan6d1cfba2005-06-08 14:13:14 -070014690 /* Safe to use the calculated DMA boundary. */
14691 tp->dma_rwctrl = saved_dma_rwctrl;
Matt Carlson859a5882010-04-05 10:19:28 +000014692 }
Michael Chan6d1cfba2005-06-08 14:13:14 -070014693
David S. Miller59e6b432005-05-18 22:50:10 -070014694 tw32(TG3PCI_DMA_RW_CTRL, tp->dma_rwctrl);
14695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014696
14697out:
Matt Carlson4bae65c2010-11-24 08:31:52 +000014698 dma_free_coherent(&tp->pdev->dev, TEST_BUFFER_SIZE, buf, buf_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014699out_nofree:
14700 return ret;
14701}
14702
Linus Torvalds1da177e2005-04-16 15:20:36 -070014703static void __devinit tg3_init_bufmgr_config(struct tg3 *tp)
14704{
Matt Carlson1407deb2011-04-05 14:22:44 +000014705 if (tp->tg3_flags3 & TG3_FLG3_57765_PLUS) {
Matt Carlson666bc832010-01-20 16:58:03 +000014706 tp->bufmgr_config.mbuf_read_dma_low_water =
14707 DEFAULT_MB_RDMA_LOW_WATER_5705;
14708 tp->bufmgr_config.mbuf_mac_rx_low_water =
14709 DEFAULT_MB_MACRX_LOW_WATER_57765;
14710 tp->bufmgr_config.mbuf_high_water =
14711 DEFAULT_MB_HIGH_WATER_57765;
14712
14713 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14714 DEFAULT_MB_RDMA_LOW_WATER_5705;
14715 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14716 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_57765;
14717 tp->bufmgr_config.mbuf_high_water_jumbo =
14718 DEFAULT_MB_HIGH_WATER_JUMBO_57765;
14719 } else if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
Michael Chanfdfec172005-07-25 12:31:48 -070014720 tp->bufmgr_config.mbuf_read_dma_low_water =
14721 DEFAULT_MB_RDMA_LOW_WATER_5705;
14722 tp->bufmgr_config.mbuf_mac_rx_low_water =
14723 DEFAULT_MB_MACRX_LOW_WATER_5705;
14724 tp->bufmgr_config.mbuf_high_water =
14725 DEFAULT_MB_HIGH_WATER_5705;
Michael Chanb5d37722006-09-27 16:06:21 -070014726 if (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5906) {
14727 tp->bufmgr_config.mbuf_mac_rx_low_water =
14728 DEFAULT_MB_MACRX_LOW_WATER_5906;
14729 tp->bufmgr_config.mbuf_high_water =
14730 DEFAULT_MB_HIGH_WATER_5906;
14731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014732
Michael Chanfdfec172005-07-25 12:31:48 -070014733 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14734 DEFAULT_MB_RDMA_LOW_WATER_JUMBO_5780;
14735 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14736 DEFAULT_MB_MACRX_LOW_WATER_JUMBO_5780;
14737 tp->bufmgr_config.mbuf_high_water_jumbo =
14738 DEFAULT_MB_HIGH_WATER_JUMBO_5780;
14739 } else {
14740 tp->bufmgr_config.mbuf_read_dma_low_water =
14741 DEFAULT_MB_RDMA_LOW_WATER;
14742 tp->bufmgr_config.mbuf_mac_rx_low_water =
14743 DEFAULT_MB_MACRX_LOW_WATER;
14744 tp->bufmgr_config.mbuf_high_water =
14745 DEFAULT_MB_HIGH_WATER;
14746
14747 tp->bufmgr_config.mbuf_read_dma_low_water_jumbo =
14748 DEFAULT_MB_RDMA_LOW_WATER_JUMBO;
14749 tp->bufmgr_config.mbuf_mac_rx_low_water_jumbo =
14750 DEFAULT_MB_MACRX_LOW_WATER_JUMBO;
14751 tp->bufmgr_config.mbuf_high_water_jumbo =
14752 DEFAULT_MB_HIGH_WATER_JUMBO;
14753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014754
14755 tp->bufmgr_config.dma_low_water = DEFAULT_DMA_LOW_WATER;
14756 tp->bufmgr_config.dma_high_water = DEFAULT_DMA_HIGH_WATER;
14757}
14758
14759static char * __devinit tg3_phy_string(struct tg3 *tp)
14760{
Matt Carlson79eb6902010-02-17 15:17:03 +000014761 switch (tp->phy_id & TG3_PHY_ID_MASK) {
14762 case TG3_PHY_ID_BCM5400: return "5400";
14763 case TG3_PHY_ID_BCM5401: return "5401";
14764 case TG3_PHY_ID_BCM5411: return "5411";
14765 case TG3_PHY_ID_BCM5701: return "5701";
14766 case TG3_PHY_ID_BCM5703: return "5703";
14767 case TG3_PHY_ID_BCM5704: return "5704";
14768 case TG3_PHY_ID_BCM5705: return "5705";
14769 case TG3_PHY_ID_BCM5750: return "5750";
14770 case TG3_PHY_ID_BCM5752: return "5752";
14771 case TG3_PHY_ID_BCM5714: return "5714";
14772 case TG3_PHY_ID_BCM5780: return "5780";
14773 case TG3_PHY_ID_BCM5755: return "5755";
14774 case TG3_PHY_ID_BCM5787: return "5787";
14775 case TG3_PHY_ID_BCM5784: return "5784";
14776 case TG3_PHY_ID_BCM5756: return "5722/5756";
14777 case TG3_PHY_ID_BCM5906: return "5906";
14778 case TG3_PHY_ID_BCM5761: return "5761";
14779 case TG3_PHY_ID_BCM5718C: return "5718C";
14780 case TG3_PHY_ID_BCM5718S: return "5718S";
14781 case TG3_PHY_ID_BCM57765: return "57765";
Matt Carlson302b5002010-06-05 17:24:38 +000014782 case TG3_PHY_ID_BCM5719C: return "5719C";
Matt Carlson6418f2c2011-04-05 14:22:49 +000014783 case TG3_PHY_ID_BCM5720C: return "5720C";
Matt Carlson79eb6902010-02-17 15:17:03 +000014784 case TG3_PHY_ID_BCM8002: return "8002/serdes";
Linus Torvalds1da177e2005-04-16 15:20:36 -070014785 case 0: return "serdes";
14786 default: return "unknown";
Stephen Hemminger855e1112008-04-16 16:37:28 -070014787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014788}
14789
Michael Chanf9804dd2005-09-27 12:13:10 -070014790static char * __devinit tg3_bus_string(struct tg3 *tp, char *str)
14791{
14792 if (tp->tg3_flags2 & TG3_FLG2_PCI_EXPRESS) {
14793 strcpy(str, "PCI Express");
14794 return str;
14795 } else if (tp->tg3_flags & TG3_FLAG_PCIX_MODE) {
14796 u32 clock_ctrl = tr32(TG3PCI_CLOCK_CTRL) & 0x1f;
14797
14798 strcpy(str, "PCIX:");
14799
14800 if ((clock_ctrl == 7) ||
14801 ((tr32(GRC_MISC_CFG) & GRC_MISC_CFG_BOARD_ID_MASK) ==
14802 GRC_MISC_CFG_BOARD_ID_5704CIOBE))
14803 strcat(str, "133MHz");
14804 else if (clock_ctrl == 0)
14805 strcat(str, "33MHz");
14806 else if (clock_ctrl == 2)
14807 strcat(str, "50MHz");
14808 else if (clock_ctrl == 4)
14809 strcat(str, "66MHz");
14810 else if (clock_ctrl == 6)
14811 strcat(str, "100MHz");
Michael Chanf9804dd2005-09-27 12:13:10 -070014812 } else {
14813 strcpy(str, "PCI:");
14814 if (tp->tg3_flags & TG3_FLAG_PCI_HIGH_SPEED)
14815 strcat(str, "66MHz");
14816 else
14817 strcat(str, "33MHz");
14818 }
14819 if (tp->tg3_flags & TG3_FLAG_PCI_32BIT)
14820 strcat(str, ":32-bit");
14821 else
14822 strcat(str, ":64-bit");
14823 return str;
14824}
14825
Michael Chan8c2dc7e2005-12-19 16:26:02 -080014826static struct pci_dev * __devinit tg3_find_peer(struct tg3 *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014827{
14828 struct pci_dev *peer;
14829 unsigned int func, devnr = tp->pdev->devfn & ~7;
14830
14831 for (func = 0; func < 8; func++) {
14832 peer = pci_get_slot(tp->pdev->bus, devnr | func);
14833 if (peer && peer != tp->pdev)
14834 break;
14835 pci_dev_put(peer);
14836 }
Michael Chan16fe9d72005-12-13 21:09:54 -080014837 /* 5704 can be configured in single-port mode, set peer to
14838 * tp->pdev in that case.
14839 */
14840 if (!peer) {
14841 peer = tp->pdev;
14842 return peer;
14843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070014844
14845 /*
14846 * We don't need to keep the refcount elevated; there's no way
14847 * to remove one half of this device without removing the other
14848 */
14849 pci_dev_put(peer);
14850
14851 return peer;
14852}
14853
David S. Miller15f98502005-05-18 22:49:26 -070014854static void __devinit tg3_init_coal(struct tg3 *tp)
14855{
14856 struct ethtool_coalesce *ec = &tp->coal;
14857
14858 memset(ec, 0, sizeof(*ec));
14859 ec->cmd = ETHTOOL_GCOALESCE;
14860 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS;
14861 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS;
14862 ec->rx_max_coalesced_frames = LOW_RXMAX_FRAMES;
14863 ec->tx_max_coalesced_frames = LOW_TXMAX_FRAMES;
14864 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT;
14865 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT;
14866 ec->rx_max_coalesced_frames_irq = DEFAULT_RXCOAL_MAXF_INT;
14867 ec->tx_max_coalesced_frames_irq = DEFAULT_TXCOAL_MAXF_INT;
14868 ec->stats_block_coalesce_usecs = DEFAULT_STAT_COAL_TICKS;
14869
14870 if (tp->coalesce_mode & (HOSTCC_MODE_CLRTICK_RXBD |
14871 HOSTCC_MODE_CLRTICK_TXBD)) {
14872 ec->rx_coalesce_usecs = LOW_RXCOL_TICKS_CLRTCKS;
14873 ec->rx_coalesce_usecs_irq = DEFAULT_RXCOAL_TICK_INT_CLRTCKS;
14874 ec->tx_coalesce_usecs = LOW_TXCOL_TICKS_CLRTCKS;
14875 ec->tx_coalesce_usecs_irq = DEFAULT_TXCOAL_TICK_INT_CLRTCKS;
14876 }
Michael Chand244c892005-07-05 14:42:33 -070014877
14878 if (tp->tg3_flags2 & TG3_FLG2_5705_PLUS) {
14879 ec->rx_coalesce_usecs_irq = 0;
14880 ec->tx_coalesce_usecs_irq = 0;
14881 ec->stats_block_coalesce_usecs = 0;
14882 }
David S. Miller15f98502005-05-18 22:49:26 -070014883}
14884
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014885static const struct net_device_ops tg3_netdev_ops = {
14886 .ndo_open = tg3_open,
14887 .ndo_stop = tg3_close,
Stephen Hemminger00829822008-11-20 20:14:53 -080014888 .ndo_start_xmit = tg3_start_xmit,
Eric Dumazet511d2222010-07-07 20:44:24 +000014889 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger00829822008-11-20 20:14:53 -080014890 .ndo_validate_addr = eth_validate_addr,
14891 .ndo_set_multicast_list = tg3_set_rx_mode,
14892 .ndo_set_mac_address = tg3_set_mac_addr,
14893 .ndo_do_ioctl = tg3_ioctl,
14894 .ndo_tx_timeout = tg3_tx_timeout,
14895 .ndo_change_mtu = tg3_change_mtu,
Michał Mirosławdc668912011-04-07 03:35:07 +000014896 .ndo_fix_features = tg3_fix_features,
Stephen Hemminger00829822008-11-20 20:14:53 -080014897#ifdef CONFIG_NET_POLL_CONTROLLER
14898 .ndo_poll_controller = tg3_poll_controller,
14899#endif
14900};
14901
14902static const struct net_device_ops tg3_netdev_ops_dma_bug = {
14903 .ndo_open = tg3_open,
14904 .ndo_stop = tg3_close,
14905 .ndo_start_xmit = tg3_start_xmit_dma_bug,
Eric Dumazet511d2222010-07-07 20:44:24 +000014906 .ndo_get_stats64 = tg3_get_stats64,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014907 .ndo_validate_addr = eth_validate_addr,
14908 .ndo_set_multicast_list = tg3_set_rx_mode,
14909 .ndo_set_mac_address = tg3_set_mac_addr,
14910 .ndo_do_ioctl = tg3_ioctl,
14911 .ndo_tx_timeout = tg3_tx_timeout,
14912 .ndo_change_mtu = tg3_change_mtu,
Stephen Hemminger7c7d64b2008-11-19 22:25:36 -080014913#ifdef CONFIG_NET_POLL_CONTROLLER
14914 .ndo_poll_controller = tg3_poll_controller,
14915#endif
14916};
14917
Linus Torvalds1da177e2005-04-16 15:20:36 -070014918static int __devinit tg3_init_one(struct pci_dev *pdev,
14919 const struct pci_device_id *ent)
14920{
Linus Torvalds1da177e2005-04-16 15:20:36 -070014921 struct net_device *dev;
14922 struct tg3 *tp;
Matt Carlson646c9ed2009-09-01 12:58:41 +000014923 int i, err, pm_cap;
14924 u32 sndmbx, rcvmbx, intmbx;
Michael Chanf9804dd2005-09-27 12:13:10 -070014925 char str[40];
Michael Chan72f2afb2006-03-06 19:28:35 -080014926 u64 dma_mask, persist_dma_mask;
Michał Mirosławdc668912011-04-07 03:35:07 +000014927 u32 hw_features = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014928
Joe Perches05dbe002010-02-17 19:44:19 +000014929 printk_once(KERN_INFO "%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014930
14931 err = pci_enable_device(pdev);
14932 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014933 dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014934 return err;
14935 }
14936
Linus Torvalds1da177e2005-04-16 15:20:36 -070014937 err = pci_request_regions(pdev, DRV_MODULE_NAME);
14938 if (err) {
Matt Carlson2445e462010-04-05 10:19:21 +000014939 dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014940 goto err_out_disable_pdev;
14941 }
14942
14943 pci_set_master(pdev);
14944
14945 /* Find power-management capability. */
14946 pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
14947 if (pm_cap == 0) {
Matt Carlson2445e462010-04-05 10:19:21 +000014948 dev_err(&pdev->dev,
14949 "Cannot find Power Management capability, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014950 err = -EIO;
14951 goto err_out_free_res;
14952 }
14953
Matt Carlsonfe5f5782009-09-01 13:09:39 +000014954 dev = alloc_etherdev_mq(sizeof(*tp), TG3_IRQ_MAX_VECS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014955 if (!dev) {
Matt Carlson2445e462010-04-05 10:19:21 +000014956 dev_err(&pdev->dev, "Etherdev alloc failed, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070014957 err = -ENOMEM;
14958 goto err_out_free_res;
14959 }
14960
Linus Torvalds1da177e2005-04-16 15:20:36 -070014961 SET_NETDEV_DEV(dev, &pdev->dev);
14962
Linus Torvalds1da177e2005-04-16 15:20:36 -070014963 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014964
14965 tp = netdev_priv(dev);
14966 tp->pdev = pdev;
14967 tp->dev = dev;
14968 tp->pm_cap = pm_cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070014969 tp->rx_mode = TG3_DEF_RX_MODE;
14970 tp->tx_mode = TG3_DEF_TX_MODE;
Matt Carlson8ef21422008-05-02 16:47:53 -070014971
Linus Torvalds1da177e2005-04-16 15:20:36 -070014972 if (tg3_debug > 0)
14973 tp->msg_enable = tg3_debug;
14974 else
14975 tp->msg_enable = TG3_DEF_MSG_ENABLE;
14976
14977 /* The word/byte swap controls here control register access byte
14978 * swapping. DMA data byte swapping is controlled in the GRC_MODE
14979 * setting below.
14980 */
14981 tp->misc_host_ctrl =
14982 MISC_HOST_CTRL_MASK_PCI_INT |
14983 MISC_HOST_CTRL_WORD_SWAP |
14984 MISC_HOST_CTRL_INDIR_ACCESS |
14985 MISC_HOST_CTRL_PCISTATE_RW;
14986
14987 /* The NONFRM (non-frame) byte/word swap controls take effect
14988 * on descriptor entries, anything which isn't packet data.
14989 *
14990 * The StrongARM chips on the board (one for tx, one for rx)
14991 * are running in big-endian mode.
14992 */
14993 tp->grc_mode = (GRC_MODE_WSWAP_DATA | GRC_MODE_BSWAP_DATA |
14994 GRC_MODE_WSWAP_NONFRM_DATA);
14995#ifdef __BIG_ENDIAN
14996 tp->grc_mode |= GRC_MODE_BSWAP_NONFRM_DATA;
14997#endif
14998 spin_lock_init(&tp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014999 spin_lock_init(&tp->indirect_lock);
David Howellsc4028952006-11-22 14:57:56 +000015000 INIT_WORK(&tp->reset_task, tg3_reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015001
Matt Carlsond5fe4882008-11-21 17:20:32 -080015002 tp->regs = pci_ioremap_bar(pdev, BAR_0);
Andy Gospodarekab0049b2007-09-06 20:42:14 +010015003 if (!tp->regs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015004 dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015005 err = -ENOMEM;
15006 goto err_out_free_dev;
15007 }
15008
Linus Torvalds1da177e2005-04-16 15:20:36 -070015009 tp->rx_pending = TG3_DEF_RX_RING_PENDING;
15010 tp->rx_jumbo_pending = TG3_DEF_RX_JUMBO_RING_PENDING;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015011
Linus Torvalds1da177e2005-04-16 15:20:36 -070015012 dev->ethtool_ops = &tg3_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015013 dev->watchdog_timeo = TG3_TX_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015014 dev->irq = pdev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015015
15016 err = tg3_get_invariants(tp);
15017 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015018 dev_err(&pdev->dev,
15019 "Problem fetching invariants of chip, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015020 goto err_out_iounmap;
15021 }
15022
Matt Carlson615774f2009-11-13 13:03:39 +000015023 if ((tp->tg3_flags3 & TG3_FLG3_5755_PLUS) &&
Matt Carlson0a58d662011-04-05 14:22:45 +000015024 !(tp->tg3_flags3 & TG3_FLG3_5717_PLUS))
Stephen Hemminger00829822008-11-20 20:14:53 -080015025 dev->netdev_ops = &tg3_netdev_ops;
15026 else
15027 dev->netdev_ops = &tg3_netdev_ops_dma_bug;
15028
15029
Michael Chan4a29cc22006-03-19 13:21:12 -080015030 /* The EPB bridge inside 5714, 5715, and 5780 and any
15031 * device behind the EPB cannot support DMA addresses > 40-bit.
Michael Chan72f2afb2006-03-06 19:28:35 -080015032 * On 64-bit systems with IOMMU, use 40-bit dma_mask.
15033 * On 64-bit systems without IOMMU, use 64-bit dma_mask and
15034 * do DMA address check in tg3_start_xmit().
15035 */
Michael Chan4a29cc22006-03-19 13:21:12 -080015036 if (tp->tg3_flags2 & TG3_FLG2_IS_5788)
Yang Hongyang284901a2009-04-06 19:01:15 -070015037 persist_dma_mask = dma_mask = DMA_BIT_MASK(32);
Michael Chan4a29cc22006-03-19 13:21:12 -080015038 else if (tp->tg3_flags & TG3_FLAG_40BIT_DMA_BUG) {
Yang Hongyang50cf1562009-04-06 19:01:14 -070015039 persist_dma_mask = dma_mask = DMA_BIT_MASK(40);
Michael Chan72f2afb2006-03-06 19:28:35 -080015040#ifdef CONFIG_HIGHMEM
Yang Hongyang6a355282009-04-06 19:01:13 -070015041 dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015042#endif
Michael Chan4a29cc22006-03-19 13:21:12 -080015043 } else
Yang Hongyang6a355282009-04-06 19:01:13 -070015044 persist_dma_mask = dma_mask = DMA_BIT_MASK(64);
Michael Chan72f2afb2006-03-06 19:28:35 -080015045
15046 /* Configure DMA attributes. */
Yang Hongyang284901a2009-04-06 19:01:15 -070015047 if (dma_mask > DMA_BIT_MASK(32)) {
Michael Chan72f2afb2006-03-06 19:28:35 -080015048 err = pci_set_dma_mask(pdev, dma_mask);
15049 if (!err) {
15050 dev->features |= NETIF_F_HIGHDMA;
15051 err = pci_set_consistent_dma_mask(pdev,
15052 persist_dma_mask);
15053 if (err < 0) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015054 dev_err(&pdev->dev, "Unable to obtain 64 bit "
15055 "DMA for consistent allocations\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015056 goto err_out_iounmap;
15057 }
15058 }
15059 }
Yang Hongyang284901a2009-04-06 19:01:15 -070015060 if (err || dma_mask == DMA_BIT_MASK(32)) {
15061 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Michael Chan72f2afb2006-03-06 19:28:35 -080015062 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015063 dev_err(&pdev->dev,
15064 "No usable DMA configuration, aborting\n");
Michael Chan72f2afb2006-03-06 19:28:35 -080015065 goto err_out_iounmap;
15066 }
15067 }
15068
Michael Chanfdfec172005-07-25 12:31:48 -070015069 tg3_init_bufmgr_config(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015070
Matt Carlson507399f2009-11-13 13:03:37 +000015071 /* Selectively allow TSO based on operating conditions */
15072 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) ||
15073 (tp->fw_needed && !(tp->tg3_flags & TG3_FLAG_ENABLE_ASF)))
15074 tp->tg3_flags2 |= TG3_FLG2_TSO_CAPABLE;
15075 else {
15076 tp->tg3_flags2 &= ~(TG3_FLG2_TSO_CAPABLE | TG3_FLG2_TSO_BUG);
15077 tp->fw_needed = NULL;
15078 }
15079
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015080 if (tp->pci_chip_rev_id == CHIPREV_ID_5701_A0)
Matt Carlson9e9fd122009-01-19 16:57:45 -080015081 tp->fw_needed = FIRMWARE_TG3;
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015082
Michael Chan4e3a7aa2006-03-20 17:47:44 -080015083 /* TSO is on by default on chips that support hardware TSO.
15084 * Firmware TSO on older chips gives lower performance, so it
15085 * is off by default, but can be enabled using ethtool.
15086 */
Matt Carlsone849cdc2009-11-13 13:03:38 +000015087 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO) &&
Michał Mirosławdc668912011-04-07 03:35:07 +000015088 (dev->features & NETIF_F_IP_CSUM))
15089 hw_features |= NETIF_F_TSO;
Matt Carlsone849cdc2009-11-13 13:03:38 +000015090 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_2) ||
15091 (tp->tg3_flags2 & TG3_FLG2_HW_TSO_3)) {
Michał Mirosławdc668912011-04-07 03:35:07 +000015092 if (dev->features & NETIF_F_IPV6_CSUM)
15093 hw_features |= NETIF_F_TSO6;
Matt Carlsone849cdc2009-11-13 13:03:38 +000015094 if ((tp->tg3_flags2 & TG3_FLG2_HW_TSO_3) ||
15095 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5761 ||
Matt Carlson57e69832008-05-25 23:48:31 -070015096 (GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5784 &&
15097 GET_CHIP_REV(tp->pci_chip_rev_id) != CHIPREV_5784_AX) ||
Matt Carlson321d32a2008-11-21 17:22:19 -080015098 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_5785 ||
Michał Mirosławdc668912011-04-07 03:35:07 +000015099 GET_ASIC_REV(tp->pci_chip_rev_id) == ASIC_REV_57780)
15100 hw_features |= NETIF_F_TSO_ECN;
Michael Chanb0026622006-07-03 19:42:14 -070015101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015102
Michał Mirosławdc668912011-04-07 03:35:07 +000015103 dev->hw_features |= hw_features;
15104 dev->features |= hw_features;
15105 dev->vlan_features |= hw_features;
15106
Linus Torvalds1da177e2005-04-16 15:20:36 -070015107 if (tp->pci_chip_rev_id == CHIPREV_ID_5705_A1 &&
15108 !(tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) &&
15109 !(tr32(TG3PCI_PCISTATE) & PCISTATE_BUS_SPEED_HIGH)) {
15110 tp->tg3_flags2 |= TG3_FLG2_MAX_RXPEND_64;
15111 tp->rx_pending = 63;
15112 }
15113
Linus Torvalds1da177e2005-04-16 15:20:36 -070015114 err = tg3_get_device_address(tp);
15115 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015116 dev_err(&pdev->dev,
15117 "Could not obtain valid ethernet address, aborting\n");
Matt Carlson026a6c22009-12-03 08:36:24 +000015118 goto err_out_iounmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015119 }
15120
Matt Carlson0d3031d2007-10-10 18:02:43 -070015121 if (tp->tg3_flags3 & TG3_FLG3_ENABLE_APE) {
Matt Carlson63532392008-11-03 16:49:57 -080015122 tp->aperegs = pci_ioremap_bar(pdev, BAR_2);
Al Viro79ea13c2008-01-24 02:06:46 -080015123 if (!tp->aperegs) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015124 dev_err(&pdev->dev,
15125 "Cannot map APE registers, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015126 err = -ENOMEM;
Matt Carlson026a6c22009-12-03 08:36:24 +000015127 goto err_out_iounmap;
Matt Carlson0d3031d2007-10-10 18:02:43 -070015128 }
15129
15130 tg3_ape_lock_init(tp);
Matt Carlson7fd76442009-02-25 14:27:20 +000015131
15132 if (tp->tg3_flags & TG3_FLAG_ENABLE_ASF)
15133 tg3_read_dash_ver(tp);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015134 }
15135
Matt Carlsonc88864d2007-11-12 21:07:01 -080015136 /*
15137 * Reset chip in case UNDI or EFI driver did not shutdown
15138 * DMA self test will enable WDMAC and we'll see (spurious)
15139 * pending DMA on the PCI bus at that point.
15140 */
15141 if ((tr32(HOSTCC_MODE) & HOSTCC_MODE_ENABLE) ||
15142 (tr32(WDMAC_MODE) & WDMAC_MODE_ENABLE)) {
15143 tw32(MEMARB_MODE, MEMARB_MODE_ENABLE);
15144 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
15145 }
15146
15147 err = tg3_test_dma(tp);
15148 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015149 dev_err(&pdev->dev, "DMA engine test failed, aborting\n");
Matt Carlsonc88864d2007-11-12 21:07:01 -080015150 goto err_out_apeunmap;
15151 }
15152
Matt Carlson78f90dc2009-11-13 13:03:42 +000015153 intmbx = MAILBOX_INTERRUPT_0 + TG3_64BIT_REG_LOW;
15154 rcvmbx = MAILBOX_RCVRET_CON_IDX_0 + TG3_64BIT_REG_LOW;
15155 sndmbx = MAILBOX_SNDHOST_PROD_IDX_0 + TG3_64BIT_REG_LOW;
Matt Carlson6fd45cb2010-09-15 08:59:57 +000015156 for (i = 0; i < tp->irq_max; i++) {
Matt Carlson78f90dc2009-11-13 13:03:42 +000015157 struct tg3_napi *tnapi = &tp->napi[i];
15158
15159 tnapi->tp = tp;
15160 tnapi->tx_pending = TG3_DEF_TX_RING_PENDING;
15161
15162 tnapi->int_mbox = intmbx;
15163 if (i < 4)
15164 intmbx += 0x8;
15165 else
15166 intmbx += 0x4;
15167
15168 tnapi->consmbox = rcvmbx;
15169 tnapi->prodmbox = sndmbx;
15170
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015171 if (i)
Matt Carlson78f90dc2009-11-13 13:03:42 +000015172 tnapi->coal_now = HOSTCC_MODE_COAL_VEC1_NOW << (i - 1);
Matt Carlson66cfd1b2010-09-30 10:34:30 +000015173 else
Matt Carlson78f90dc2009-11-13 13:03:42 +000015174 tnapi->coal_now = HOSTCC_MODE_NOW;
Matt Carlson78f90dc2009-11-13 13:03:42 +000015175
15176 if (!(tp->tg3_flags & TG3_FLAG_SUPPORT_MSIX))
15177 break;
15178
15179 /*
15180 * If we support MSIX, we'll be using RSS. If we're using
15181 * RSS, the first vector only handles link interrupts and the
15182 * remaining vectors handle rx and tx interrupts. Reuse the
15183 * mailbox values for the next iteration. The values we setup
15184 * above are still useful for the single vectored mode.
15185 */
15186 if (!i)
15187 continue;
15188
15189 rcvmbx += 0x8;
15190
15191 if (sndmbx & 0x4)
15192 sndmbx -= 0x4;
15193 else
15194 sndmbx += 0xc;
15195 }
15196
Matt Carlsonc88864d2007-11-12 21:07:01 -080015197 tg3_init_coal(tp);
15198
Michael Chanc49a1562006-12-17 17:07:29 -080015199 pci_set_drvdata(pdev, dev);
15200
Linus Torvalds1da177e2005-04-16 15:20:36 -070015201 err = register_netdev(dev);
15202 if (err) {
Matt Carlsonab96b242010-04-05 10:19:22 +000015203 dev_err(&pdev->dev, "Cannot register net device, aborting\n");
Matt Carlson0d3031d2007-10-10 18:02:43 -070015204 goto err_out_apeunmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015205 }
15206
Joe Perches05dbe002010-02-17 19:44:19 +000015207 netdev_info(dev, "Tigon3 [partno(%s) rev %04x] (%s) MAC address %pM\n",
15208 tp->board_part_number,
15209 tp->pci_chip_rev_id,
15210 tg3_bus_string(tp, str),
15211 dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015212
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015213 if (tp->phy_flags & TG3_PHYFLG_IS_CONNECTED) {
Matt Carlson3f0e3ad2009-11-02 14:24:36 +000015214 struct phy_device *phydev;
15215 phydev = tp->mdio_bus->phy_map[TG3_PHY_MII_ADDR];
Matt Carlson5129c3a2010-04-05 10:19:23 +000015216 netdev_info(dev,
15217 "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
Joe Perches05dbe002010-02-17 19:44:19 +000015218 phydev->drv->name, dev_name(&phydev->dev));
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015219 } else {
15220 char *ethtype;
15221
15222 if (tp->phy_flags & TG3_PHYFLG_10_100_ONLY)
15223 ethtype = "10/100Base-TX";
15224 else if (tp->phy_flags & TG3_PHYFLG_ANY_SERDES)
15225 ethtype = "1000Base-SX";
15226 else
15227 ethtype = "10/100/1000Base-T";
15228
Matt Carlson5129c3a2010-04-05 10:19:23 +000015229 netdev_info(dev, "attached PHY is %s (%s Ethernet) "
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015230 "(WireSpeed[%d])\n", tg3_phy_string(tp), ethtype,
15231 (tp->phy_flags & TG3_PHYFLG_NO_ETH_WIRE_SPEED) == 0);
15232 }
Matt Carlsondf59c942008-11-03 16:52:56 -080015233
Joe Perches05dbe002010-02-17 19:44:19 +000015234 netdev_info(dev, "RXcsums[%d] LinkChgREG[%d] MIirq[%d] ASF[%d] TSOcap[%d]\n",
Michał Mirosławdc668912011-04-07 03:35:07 +000015235 (dev->features & NETIF_F_RXCSUM) != 0,
Joe Perches05dbe002010-02-17 19:44:19 +000015236 (tp->tg3_flags & TG3_FLAG_USE_LINKCHG_REG) != 0,
Matt Carlsonf07e9af2010-08-02 11:26:07 +000015237 (tp->phy_flags & TG3_PHYFLG_USE_MI_INTERRUPT) != 0,
Joe Perches05dbe002010-02-17 19:44:19 +000015238 (tp->tg3_flags & TG3_FLAG_ENABLE_ASF) != 0,
15239 (tp->tg3_flags2 & TG3_FLG2_TSO_CAPABLE) != 0);
15240 netdev_info(dev, "dma_rwctrl[%08x] dma_mask[%d-bit]\n",
15241 tp->dma_rwctrl,
15242 pdev->dma_mask == DMA_BIT_MASK(32) ? 32 :
15243 ((u64)pdev->dma_mask) == DMA_BIT_MASK(40) ? 40 : 64);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015244
15245 return 0;
15246
Matt Carlson0d3031d2007-10-10 18:02:43 -070015247err_out_apeunmap:
15248 if (tp->aperegs) {
15249 iounmap(tp->aperegs);
15250 tp->aperegs = NULL;
15251 }
15252
Linus Torvalds1da177e2005-04-16 15:20:36 -070015253err_out_iounmap:
Michael Chan68929142005-08-09 20:17:14 -070015254 if (tp->regs) {
15255 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015256 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015258
15259err_out_free_dev:
15260 free_netdev(dev);
15261
15262err_out_free_res:
15263 pci_release_regions(pdev);
15264
15265err_out_disable_pdev:
15266 pci_disable_device(pdev);
15267 pci_set_drvdata(pdev, NULL);
15268 return err;
15269}
15270
15271static void __devexit tg3_remove_one(struct pci_dev *pdev)
15272{
15273 struct net_device *dev = pci_get_drvdata(pdev);
15274
15275 if (dev) {
15276 struct tg3 *tp = netdev_priv(dev);
15277
Jaswinder Singh Rajput077f8492009-01-04 16:11:25 -080015278 if (tp->fw)
15279 release_firmware(tp->fw);
15280
Tejun Heo23f333a2010-12-12 16:45:14 +010015281 cancel_work_sync(&tp->reset_task);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015282
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015283 if (tp->tg3_flags3 & TG3_FLG3_USE_PHYLIB) {
15284 tg3_phy_fini(tp);
Matt Carlson158d7ab2008-05-29 01:37:54 -070015285 tg3_mdio_fini(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015286 }
Matt Carlson158d7ab2008-05-29 01:37:54 -070015287
Linus Torvalds1da177e2005-04-16 15:20:36 -070015288 unregister_netdev(dev);
Matt Carlson0d3031d2007-10-10 18:02:43 -070015289 if (tp->aperegs) {
15290 iounmap(tp->aperegs);
15291 tp->aperegs = NULL;
15292 }
Michael Chan68929142005-08-09 20:17:14 -070015293 if (tp->regs) {
15294 iounmap(tp->regs);
Peter Hagervall22abe312005-09-16 17:01:03 -070015295 tp->regs = NULL;
Michael Chan68929142005-08-09 20:17:14 -070015296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070015297 free_netdev(dev);
15298 pci_release_regions(pdev);
15299 pci_disable_device(pdev);
15300 pci_set_drvdata(pdev, NULL);
15301 }
15302}
15303
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015304#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015305static int tg3_suspend(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015306{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015307 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015308 struct net_device *dev = pci_get_drvdata(pdev);
15309 struct tg3 *tp = netdev_priv(dev);
15310 int err;
15311
15312 if (!netif_running(dev))
15313 return 0;
15314
Tejun Heo23f333a2010-12-12 16:45:14 +010015315 flush_work_sync(&tp->reset_task);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015316 tg3_phy_stop(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015317 tg3_netif_stop(tp);
15318
15319 del_timer_sync(&tp->timer);
15320
David S. Millerf47c11e2005-06-24 20:18:35 -070015321 tg3_full_lock(tp, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015322 tg3_disable_ints(tp);
David S. Millerf47c11e2005-06-24 20:18:35 -070015323 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015324
15325 netif_device_detach(dev);
15326
David S. Millerf47c11e2005-06-24 20:18:35 -070015327 tg3_full_lock(tp, 0);
Michael Chan944d9802005-05-29 14:57:48 -070015328 tg3_halt(tp, RESET_KIND_SHUTDOWN, 1);
Michael Chan6a9eba12005-12-13 21:08:58 -080015329 tp->tg3_flags &= ~TG3_FLAG_INIT_COMPLETE;
David S. Millerf47c11e2005-06-24 20:18:35 -070015330 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015331
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015332 err = tg3_power_down_prepare(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015333 if (err) {
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015334 int err2;
15335
David S. Millerf47c11e2005-06-24 20:18:35 -070015336 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015337
Michael Chan6a9eba12005-12-13 21:08:58 -080015338 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015339 err2 = tg3_restart_hw(tp, 1);
15340 if (err2)
Michael Chanb9ec6c12006-07-25 16:37:27 -070015341 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015342
15343 tp->timer.expires = jiffies + tp->timer_offset;
15344 add_timer(&tp->timer);
15345
15346 netif_device_attach(dev);
15347 tg3_netif_start(tp);
15348
Michael Chanb9ec6c12006-07-25 16:37:27 -070015349out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015350 tg3_full_unlock(tp);
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015351
15352 if (!err2)
15353 tg3_phy_start(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015354 }
15355
15356 return err;
15357}
15358
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015359static int tg3_resume(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070015360{
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015361 struct pci_dev *pdev = to_pci_dev(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015362 struct net_device *dev = pci_get_drvdata(pdev);
15363 struct tg3 *tp = netdev_priv(dev);
15364 int err;
15365
15366 if (!netif_running(dev))
15367 return 0;
15368
Linus Torvalds1da177e2005-04-16 15:20:36 -070015369 netif_device_attach(dev);
15370
David S. Millerf47c11e2005-06-24 20:18:35 -070015371 tg3_full_lock(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015372
Michael Chan6a9eba12005-12-13 21:08:58 -080015373 tp->tg3_flags |= TG3_FLAG_INIT_COMPLETE;
Michael Chanb9ec6c12006-07-25 16:37:27 -070015374 err = tg3_restart_hw(tp, 1);
15375 if (err)
15376 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015377
15378 tp->timer.expires = jiffies + tp->timer_offset;
15379 add_timer(&tp->timer);
15380
Linus Torvalds1da177e2005-04-16 15:20:36 -070015381 tg3_netif_start(tp);
15382
Michael Chanb9ec6c12006-07-25 16:37:27 -070015383out:
David S. Millerf47c11e2005-06-24 20:18:35 -070015384 tg3_full_unlock(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015385
Matt Carlsonb02fd9e2008-05-25 23:47:41 -070015386 if (!err)
15387 tg3_phy_start(tp);
15388
Michael Chanb9ec6c12006-07-25 16:37:27 -070015389 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015390}
15391
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015392static SIMPLE_DEV_PM_OPS(tg3_pm_ops, tg3_suspend, tg3_resume);
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015393#define TG3_PM_OPS (&tg3_pm_ops)
15394
15395#else
15396
15397#define TG3_PM_OPS NULL
15398
15399#endif /* CONFIG_PM_SLEEP */
Rafael J. Wysockic866b7e2010-12-25 12:56:23 +000015400
Linus Torvalds1da177e2005-04-16 15:20:36 -070015401static struct pci_driver tg3_driver = {
15402 .name = DRV_MODULE_NAME,
15403 .id_table = tg3_pci_tbl,
15404 .probe = tg3_init_one,
15405 .remove = __devexit_p(tg3_remove_one),
Eric Dumazetaa6027c2011-01-01 05:22:46 +000015406 .driver.pm = TG3_PM_OPS,
Linus Torvalds1da177e2005-04-16 15:20:36 -070015407};
15408
15409static int __init tg3_init(void)
15410{
Jeff Garzik29917622006-08-19 17:48:59 -040015411 return pci_register_driver(&tg3_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015412}
15413
15414static void __exit tg3_cleanup(void)
15415{
15416 pci_unregister_driver(&tg3_driver);
15417}
15418
15419module_init(tg3_init);
15420module_exit(tg3_cleanup);